// Functions specific to a site
/*Cufon.replace("h1,.sort-by,.block-title h2, h3", {
    fontFamily: 'BankGothic Lt BT'
});*/
Cufon.replace('.sort-by label', {
    fontFamily: 'BankGothic Lt BT'
});
Cufon.replace('.block-title h2', {
    fontFamily: 'BankGothic Lt BT'
});
Cufon.replace('.block-title strong span', {
    fontFamily: 'BankGothic Lt BT'
});
Cufon.replace('h1', {
    fontFamily: 'BankGothic Lt BT'
});
Cufon.replace('.widget h2');
Cufon.replace('.product-view h3');

Cufon.replace("button.button span", {
    color: '#FFF',
    hoverables: {
        a: true,
        span: true
    },
    hover: {
        color: '#FFF',
        textShadow: 'rgba(51, 51, 51, 0.3) 0 -1px'
    }
});
var materialOption = new Array();
var colorOption = new Array();
jQuery(document).ready(function() {
    jQuery('.sort-by label, .block-title h2, .block-title strong span, h1, .widget h2, .product-view h3').show();
    //console.log(jQuery('h3:not(.cms-page-view h3)'));
    if(jQuery('.swatches-wrapper').length != 0) {
        var linkLis = jQuery('.swatches-wrapper ul li');
        var links = linkLis.children();
        var ids = new Array();
        if(links.length != 0) {
            //materialOption = jQuery('#attribute127');
            //colorOption = jQuery('#attribute128');
            materialOption = jQuery('.conf-opts-wrapper .conf-opts-item:eq(0) dd select');
            colorOption = jQuery('.conf-opts-wrapper .conf-opts-item:eq(1) dd select');
            //console.log(materialOption);
            materialOption.change(function (e) {
                if(colorOption.attr('disabled')) {
                    linkLis.parent().addClass('no-display');
                }
                else {
                    linkLis.parent().removeClass('no-display');
                    //First remove any active class from swatches
                    links.removeClass('active');
                    //Hide the li items at first
                    linkLis.addClass('no-display');
                    colorOption.children().each(function (index, element) {
                        if(index != 0) {
                            jQuery('#'+jQuery(element).val()+'_swatch').parent().removeClass('no-display');
                        }
                    });
                }
            });
            var clickedSwatch = null;
            //Click event on swatches
            /*if(!linkLis.parent().hasClass('no-display')) {*/
            linkLis.parent().click(function (e) {
                clickedSwatch = jQuery(e.target).closest('a');
                if(clickedSwatch.length == 0) {
                    return false;
                }
                var swatchId = clickedSwatch.attr('id').substring(0,clickedSwatch.attr('id').indexOf('_'));
                colorOption.children().attr('selected','');
                colorOption.children().each(function (index, element) {
                    if(index != 0) {
                        if(jQuery(element).val() == swatchId) {
                            jQuery(element).attr('selected', 'selected');
                            $('attribute128').simulate('change');
                        //jQuery(element).val(swatchId);
                        }
                    }
                });
                //Make the swatch active
                links.removeClass('active');
                //Add the class to target swatch
                clickedSwatch.addClass('active');
                return false;
            });
        /*}*/
        }
    }
    /* Product detail tabs */
    jQuery('.tabs-wrapper ul.attribute-tabs').tabs('div.attribute-tab-panes > div');
    /* Fix template when Magento's onchange is fired for configurable product */
    materialOption = jQuery('.conf-opts-wrapper .conf-opts-item:eq(0) dd select');
    if(materialOption.length != 0) {    //DOM element exists
        materialOption.change(function(e) {
            var spanInFocus = jQuery('div.price-box span');
            
            //console.log(jQuery('div.price-box span'));
            
            //Split the value and re-format
            var spanValue = spanInFocus.html();
            //spanValue = '<span class="only-price">'+spanValue.substr(3, spanValue.length)+'</span> <span class="currency-symbol">('+spanValue.substr(0, 3)+')</span>';
            spanValue = '<span class="only-price">'+spanValue+'</span> <span class="currency-symbol">(USD)</span>';
            spanInFocus.html(spanValue);
            //spanChange.html(spanValue);
            return false;
        });
    }
    if(colorOption.length != 0) {    //DOM element exists
        colorOption.change(function(e) {
            var spanInFocus = jQuery('div.price-box span');
            
            //Split the value and re-format
            var spanValue = spanInFocus.html();
            //spanValue = '<span class="only-price">'+spanValue.substr(3, spanValue.length)+'</span> <span class="currency-symbol">('+spanValue.substr(0, 3)+')</span>';
            spanValue = '<span class="only-price">'+spanValue+'</span> <span class="currency-symbol">(USD)</span>';
            spanInFocus.html(spanValue);
            //spanChange.html(spanValue);
            return false;
        });
    }
    /* CMS Page title class */
    /*var cmsDiv = jQuery('div.std');
    var cmsTitle = null;
    if(cmsDiv.length != 0) {
        cmsTitle = jQuery('div.page-title');
        /*cmsTitle.addClass('cms-title');*/
    //jQuery('.cms-page-view .col-main').css('width', '700px');
    //}
    /* Home page slider */
    /*var bannerSlider = jQuery('#banner-wrapper');
    if(bannerSlider.length != 0) {  //DOM exists
        jQuery('#banner-wrapper .banner-container').cycle({
            'pager': '#banner-counter'
        });
    }*/

    /* Fix the product name and price */
    var productGrid = jQuery('ul.products-grid');
    var productNames = null;
    var pricebox = null;
    if(productGrid.length != 0) {   //DOM exists
        productNames = jQuery('li.item h2.product-name a');
        productNames.each(function(index, element){
            //Get the spans from the price-box
            pricebox = jQuery('div.price-box > span', element);
            //Delete the price-box div
            jQuery('div' , element).remove();
            jQuery(element).append(pricebox);
        });
    }

    /*Stupid IE 6*/
    if (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7) {
        DD_belatedPNG.fix('*');

        jQuery('.product-shop > .price-box').hide();
    }
});

