<?php
/* to override the default configurable options template  */

//echo ;

include_once 'attributeSwatches.php';
//$_d = $this->getJsonConfig();
$_json_config = addAttributeSwatches($this->getJsonConfig());
$_product = $this->getProduct();
?>
<?php if ($_product->isSaleable() && count($_json_config["attributes"])): ?>
    <dl>
        <?php foreach ($_json_config["attributes"] as $_attribute): ?>

            <dt id="label-attribute-<?php echo $_attribute["id"] ?>"><label class="required"><em>*</em><?php echo $_attribute["label"] ?></label><span class="selected-label">&nbsp;</span>   </dt>
            <dd<?php if ($_attribute->decoratedIsLast) { ?> class="last"<?php } ?>>
                <?php
                /* WILL DISPLAY THE COLOR SWATCHES AND ALL THE CUSTOM OPTION SELECTORS HERE */
                if (in_array($_attribute["swatch_type"], array("image", "child", "label"))) {
                    ?>
                    <div id="attribute-<?php echo $_attribute["id"] ?>-container" class="product-swatches-container">
                        <ul id="swatches-options-<?php echo $_attribute["id"] ?>" class="has-swatches">
                                <?php
                                /* preload swatches from product gallery if necessary */
                                $_gallery_images = array();
                                $_load_swatch_from_gallery = Mage::getStoreConfig("attributeswatches/settings/images") == "gallery";
                                if ($_attribute["swatch_type"] == "child" && $_load_swatch_from_gallery) {
                                    $_gallery = $_product->getSwatchesGalleryImages($_attribute["id"]);
                                    foreach ($_gallery as $_image) {
                                        $_gallery_images[$_image->getAttributeValue()] = $_image->getFile();
                                    }
                                }
                                foreach ($_attribute["options"] as $_option) {
                                    ?>
                                <li>
                                    <?php
                                    switch ($_attribute["swatch_type"]) {
                                        case "child":
                                            $_image = "";
                                            if ($_load_swatch_from_gallery) {
                                                if(isset($_gallery_images[$_option["id"]])){
                                                $_image = $this->helper('catalog/image')->init($_product, 'image', $_gallery_images[$_option["id"]]);
                                                }else{
                                                    $_image = $this->helper('catalog/image')->init($_product, 'image');
                                                }
                                            } else {
                                                $_image_product = Mage::getModel('catalog/product')->load($_option["products"][0]);
                                                $_image = $this->helper('catalog/image')->init($_image_product, 'image');
                                            }
                                            ?>
                                            <a class="" title="<?php echo $_option["label"] ?>" rel="<?php echo $_option["id"] ?>" id="swatches_option_value_<?php echo $_option["id"] ?>" href="<?php echo $_image; ?>" rev="<?php echo $_image->resize(256); ?>">
                                                <?php
                                                echo "<img src='" . $_image->resize(30) . "' title='" . $_option["label"] . "' alt='" . $_option["label"] . "'/>";
                                                ?>
                                            </a>
                                            <span class="tooltip-container"><span class="tooltip"><span><?php echo $_option["label"] ?></span></span></span>
                                            <?php
                                            break;
                                        case 'image':
                                            ?>
                                            <a class="" style="<?php echo $_option["swatch_value"] ?>" title="<?php echo $_option["label"] ?>" rel="<?php echo $_option["id"] ?>" id="swatches_option_value_<?php echo $_option["id"] ?>" href="#" rev="">
                                                <span><?php echo $_option["label"] ?></span>
                                            </a>
                                            <span class="tooltip-container"><span class="tooltip"><span><?php echo $_option["label"] ?></span></span></span>
                                            <?php
                                            break;
                                        case 'label':
                                            ?>
                                            <a class="" title="<?php echo $_option["label"] ?>"  rel="<?php echo $_option["id"] ?>" id="swatches_option_value_<?php echo $_option["id"] ?>" href="#">
                                                <?php echo $_option["label"] ?>
                                            </a>
                                            <?php
                                            break;
                                    }
                                    ?>
                                </li>
                                <?php
                            }
                            ?>
                        </ul>
                    </div>
                    <?php
                }
                /* Eof MAIN SWATCHES CODE */
                ?>
                <div class="input-box">
                    <select name="super_attribute[<?php echo $_attribute["id"] ?>]" id="attribute<?php echo $_attribute["id"] ?>" class="configurable-option-select required-entry super-attribute-select  swatches-type-<?php echo $_attribute["swatch_type"]; ?> <?php echo (($_attribute["hideselect"]) ? "hideselect" : ""); ?> <?php echo (($_attribute["switchgallery"]) ? "switch-gallery" : ""); ?>">
                        <option value=""><?php echo $this->__('Choose an Option...') ?></option>
                    </select>
                </div>
                <div class="clearer">&nbsp;</div>
            </dd>
        <?php endforeach; ?>
    </dl>
    <script type="text/javascript">
        var mng_Config = <?php echo Mage::helper('core')->jsonEncode($_json_config); ?>;
        var spConfig = new Product.Config(mng_Config);
        function out_of_stock_string(){
            return "<?php echo $this->__(" (Out of Stock)"); ?>";
        }
if(typeof(Product)!=="undefined" && typeof(Product.Config)!=="undefined"){
    

    /* override default configure element function */
    Product.Config.addMethods({
        configureElement : function(element){
            /*from original code*/
            this.reloadOptionLabels(element);
            if(element.value){
            
                //console.debug(element);
                _disable_cart_button = false;
                //console.debug(element.config.options);
                //console.debug(element.config.options.length);
                for(var k=0;k<element.config.options.length;k++){
                    if( element.config.options[k].id==element.value && !element.config.options[k].optionSaleable ){
                        //console.debug("will disable button");
                        // _disable_cart_button = true;   
                  }
                }
                 /* disable the add to cart button when item is not saleable */
                        if(_disable_cart_button){
                        jQuery("#product_addtocart_form .button.btn-cart").attr("disabled","disabled").addClass("disabled");
                    }else{
                        jQuery("#product_addtocart_form .button.btn-cart").removeAttr("disabled").removeClass("disabled");
                    }
                /* eof disable the add to cart button when item is not saleable */
            
            
                this.state[element.config.id] = element.value;
                if(element.nextSetting){
                    element.nextSetting.disabled = false;
                    this.fillSelect(element.nextSetting);
                    this.resetChildren(element.nextSetting);
                }
            }
            else {
                this.resetChildren(element);
            }
            this.reloadPrice();
            /*from orignal code*/
            /* swatches extra functions */
            window.enableSwatchesOptions ( element.config.id);
            window.resetLabels(element.config.id);
            window.switchGallery(element.config.id);
        /*eof swatches extra functions */
            
        }
    });

    Product.Config.addMethods({
        getOptionLabel: function(option, price){
            var price = parseFloat(price);
            if (this.taxConfig.includeTax) {
                var tax = price / (100 + this.taxConfig.defaultTax) * this.taxConfig.defaultTax;
                var excl = price - tax;
                var incl = excl*(1+(this.taxConfig.currentTax/100));
            } else {
                var tax = price * (this.taxConfig.currentTax / 100);
                var excl = price;
                var incl = excl + tax;
            }

            if (this.taxConfig.showIncludeTax || this.taxConfig.showBothPrices) {
                price = incl;
            } else {
                price = excl;
            }

            var str = option.label;
            /* added out of stock label */
            if( typeof option.optionSaleable !== "undefined" && !option.optionSaleable ) str+= window.out_of_stock_string();
            if(price){
                if (this.taxConfig.showBothPrices) {
                    str+= ' ' + this.formatPrice(excl, true) + ' (' + this.formatPrice(price, true) + ' ' + this.taxConfig.inclTaxTitle + ')';
                } else {
                    str+= ' ' + this.formatPrice(price, true);
                }
            }
            return str;
        }
    
    
    });

    Product.Config.addMethods({
        fillSelect: function(element){
            var attributeId = element.id.replace(/[a-z]*/, '');
            var options = this.getAttributeOptions(attributeId);
            this.clearSelect(element);
            element.options[0] = new Option(this.config.chooseText, '');

            var prevConfig = false;
            if(element.prevSetting){
                prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
            }

            //_all_options_not_saleable = true;
            if(options) {
                var index = 1;
                for(var i=0;i<options.length;i++){
                    var allowedProducts = [];
                    if(prevConfig) {
                        for(var j=0;j<options[i].products.length;j++){
                            if(prevConfig.config.allowedProducts
                                && prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){
                                allowedProducts.push(options[i].products[j]);
                            }
                        }
                    } else {
                        allowedProducts = options[i].products.clone();
                    }
                    //console.debug(allowedProducts);
                    /*To add an out of stock label*/
                    _option_saleable = false;
                    if(allowedProducts.size()>0 && typeof(this.config.saleableProducts)!=="undefined" ){
                        for(var k=0;k<allowedProducts.length;k++){
                            if(  this.config.saleableProducts[allowedProducts[k]] ==true ){
                                _option_saleable = true;
                                break;
                            }
                        }
                    }
                    options[i].optionSaleable = _option_saleable;
                    //if(_option_saleable) _all_options_not_saleable = false;
                    /*eof To add an out of stock label*/
                
                
                    if(allowedProducts.size()>0){
                        options[i].allowedProducts = allowedProducts;
                        element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                        if (typeof options[i].price != 'undefined') {
                            element.options[index].setAttribute('price', options[i].price);
                        }
                        element.options[index].config = options[i];
                        index++;
                    }
                }
            
            }
        } 
    });
}


jQuery.noConflict();
var _content_is_hidden = false;   
var _configureElement = true;
jQuery(document).ready(function(){
  

    if (typeof mng_Config!="undefined"){
        jQuery.each(mng_Config.attributes, function( att_id ){
            _container = jQuery("#attribute-" + this.id + "-container.product-swatches-container #swatches-options-" + this.id);
            /*jQuery.each(this.options, function(){
            _container.append('<a href="#" id="swatches_option_value_' + this.id + '" val="' + this.id + '" class="">' +  this.label  + '</a>');
        });*/
            /*if(mng_Config.attributes[att_id].swatches_type=="image" || mng_Config.attributes[att_id].swatches_type=="label"){
                jQuery("select#attribute" + att_id).css("display","none");
            }*/
            _container.find("a").on("click", function(){
                if(jQuery(this).hasClass("active") && !jQuery(this).hasClass("selected") ){
                    //jQuery("#product-options-wrapper dl dd select#attribute" + att_id).val( jQuery(this).attr("rel") );
                    jQuery(this).closest("ul").find("a").removeClass("selected");
                    jQuery(this).addClass("selected");
                    jQuery("dt#label-attribute-" + att_id + " label span" ).hide();
                    jQuery("dt#label-attribute-" + att_id + " span.selected-label" ).text( " : " + jQuery(this).attr("title"));
                    
                    
                    jQuery("#product-options-wrapper dl dd select#attribute" + att_id).val( jQuery(this).attr("rel") );//.change();
                    //console.debug(mng_Config.attributes[att_id].has_swatches);
                    //if(mng_Config.attributes[att_id].swatches_type=="image"  && jQuery(this).attr("href")!="#" ){
                    //jQuery(".product-img-box .product-image img").attr("src", jQuery(this).attr("href"));
                        
                    //}
                    /* when event is triggered from select with function .click()  */
                    if(window._configureElement){
                        window.spConfig.configureElement($('attribute' + att_id));
                    }
                    /*reset value to true*/
                    window._configureElement = true;
                    _value = jQuery(this).attr("rel");
                    
                    
                }
                return false;
            }).on("mouseenter",function(){
                if(jQuery(this).closest("ul").hasClass("has-swatches") ){
                    jQuery(this).siblings("span.tooltip-container").addClass("on");
                }
            }).on("mouseleave",function(){
                if(jQuery(this).closest("ul").hasClass("has-swatches") ){
                    jQuery(this).siblings("span.tooltip-container").removeClass("on");
                }
            });
        });
    }
  

    /* select click to activate swatches actions */
    jQuery("#product-options-wrapper dd select.configurable-option-select").bind("change", function(){
        /* find swatch/label to click */
        _swatch = jQuery(".product-swatches-container ul li a#swatches_option_value_" + jQuery("option:selected",this).val() );
        if(_swatch.length > 0){
            window._configureElement = false;/* do not trigger the configureElement function again */
            _swatch.click();
        }
    });

    enableSwatchesOptions ( "first" ); // 'attribute' + att_id  );
    /* gallery functions */
    jQuery("div.more-views ul li a").on("click",function(event){
        event.preventDefault();
        jQuery("img#image").attr("src",jQuery(this).attr("href"));
        jQuery("a#main-image-link").attr("href",jQuery(this).attr("rel"));
        if (jQuery.fn.CloudZoom!== undefined) {
            jQuery('#main-image-link').CloudZoom();
        }
    });

    /* get default values from window hash */
    if(window.location.hash){
        var _hash=location.hash.slice(1);
        
        jQuery.each(_hash.split('&'),function(c,q){
            var i = q.split('='); /*retrieve the att-option pair, the parameters must be arranged as the options*/
            //queries[] = ;
            jQuery("#attribute-" + i[0].toString() + "-container.product-swatches-container #swatches-options-" + i[0].toString() + " a.active#swatches_option_value_" + i[1].toString() ).click();
        });
    }
    /*for selecting default values on the product details page , in case there are default values and no hash is set*/
    else if (typeof mng_Config!="undefined" && typeof mng_Config.defaultValues!="undefined"){
        jQuery.each(mng_Config.defaultValues, function( att_id ){
            /* if the select has a selected value and the value is active */
            jQuery("#attribute-" + att_id+ "-container.product-swatches-container #swatches-options-" + att_id + " a.active#swatches_option_value_" + mng_Config.defaultValues[att_id] ).click();
        });
    }
    
    /*jQuery("#product-options-wrapper dl dd select.super-attribute-select").each(function(){
        if(jQuery(this).val()){
            val = jQuery(this).val();
            jQuery(this).closest("dd").find("a.active#swatches_option_value_" + val ).click();
        }
    })*/
    
    /* product list functions */
    
    jQuery("ul.attribute-swatches.product-list li a").on("mouseenter",function(){
        _item = jQuery(this).closest('li.item');
        _item.find('.product-image > img').attr("src",jQuery(this).attr("rel"));
        _item.find('.product-image, .product-name a').attr("href",jQuery(this).attr("href"));
        //jQuery(this).closest('li.item').find('button.button.btn-cart > img').attr("src",jQuery(this).attr("alt"));
        /*var a=jQuery(this).attr("href");
        jQuery(this).closest('li.item').find('button.button.btn-cart').click(function(){
            setLocation(a);
            return false;
        });*/
        jQuery(this).closest('ul.attribute-swatches li').find('span').addClass("on");
    }).on("mouseleave",function(){
        jQuery(this).closest('ul.attribute-swatches li').find('span').removeClass("on");
    });
    
/* if layered navigation is enabled, an option was selected and the configuration value was set to yes: */
/*if (typeof window._FILTER_SELECTED_OPTION !="undefined"){
        jQuery("ul.attribute-swatches.product-list li a." + _FILTER_SELECTED_OPTION).each(function(){
            jQuery(this).closest('li.item').find('.product-image > img').attr("src",jQuery(this).attr("rel"));
        })
    }*/
    
});


function enableSwatchesOptions ( select_id ){
    _enable_refresh = false;
    _disable_elements = false;
    //console.debug(select_id);
    if(select_id =="first")  {
        _disable_elements = true;
        _enable_refresh = true;
    }
    jQuery("#product-options-wrapper dl dd select.super-attribute-select").each(function(){
        if( _disable_elements ){
            jQuery(this).closest("dd").find(".product-swatches-container ul li a").removeClass("selected").removeClass("active");
        }
            
        _select = jQuery(this);
        if( _enable_refresh){
            if(!jQuery(this).prop("disabled") ){
                jQuery("option" , this).each(function(){
                    jQuery("a#swatches_option_value_" + jQuery(this).val()).addClass("active");
                });
            }
            _enable_refresh = false;
            
        }
        if(jQuery(this).attr("id")== "attribute"+select_id) {
            _disable_elements = true;
            _enable_refresh = true;
        }
    });
}

function resetLabels(select_id){
    _reset = false;
    jQuery("#product-options-wrapper dl dt").each(function(){
        if(_reset==true){
            jQuery( "label span", this ).show();
            jQuery("span.selected-label",this ).text( "");
        }
        //if(att_id == select_id ) _reset = true;
        if(jQuery(this).attr("id") == "label-attribute-" + select_id) _reset = true;
    });
}

function switchGallery(select_id){
    //console.debug(jQuery(this).attr("id"));
    /* switch only if select can switch the gallery */
    if(jQuery("#product-options-wrapper dd select.configurable-option-select.switch-gallery#attribute"+select_id).length > 0){
        _classes = new Array();
        jQuery("#product-options-wrapper dd select.configurable-option-select.switch-gallery").each(function(){
            if(jQuery("option:selected",this).val()!=""){
                //console.debug(jQuery(this).attr("id") + "-"+jQuery("option:selected",this).val() );
                _classes.push(jQuery(this).attr("id") + "-"+jQuery("option:selected",this).val() );
            }
            
        });
        _class = _classes.join(".");
        //console.debug(_class);
        jQuery("div.more-views ul li").hide();
        if(_class!=""){
            jQuery("div.more-views ul li." + _class).show();
            jQuery("div.more-views ul li." + _class + " a").first().click();
        }
        
    }

}


    </script>
<?php endif; ?>
