var $accessory = null;
var is_chrome = null;

function activateAccessoryTab() {	
	is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if($accessory == null){
		$accessory = jQuery.noConflict();
		initAccessoryTab();
	}
}

function initAccessoryTab() {	
	$accessory('.feature_accessories_tooltip').hover(
	function () {
			if (!this.tip) {
				this.tip = this.title;
			}
			$accessory(this).append(
				'<div class="feature_accessories_tooltipWrapper">'
				+'<div class="feature_accessories_tooltipTop"></div>'
				+'<div class="feature_accessories_tooltipMid">'
				+'<div class="feature_accessories_tooltipText">'
				+this.tip
				+'</div>'
				+'</div>'
				+'<div class="feature_accessories_tooltipBtm"></div>'
				+'</div>'
			);
			this.title = "";
			this.width = $accessory(this).width();
			this.top = $accessory(this).find('.feature_accessories_tooltipWrapper').height() + 20;
			if (is_chrome){
				this.top = this.top + $accessory(this).find('.image_product_tip').height();
			}
			$accessory(this).find('.feature_accessories_tooltipWrapper').css({left:this.width-100})
			$accessory(this).find('.feature_accessories_tooltipWrapper').css({top:-this.top})
			//$accessory('.feature_accessories_tooltipWrapper').fadeIn(1000);
			//$accessory('.feature_accessories_tooltipWrapper').slideDown("slow");
			 $accessory('.feature_accessories_tooltipWrapper').animate({opacity: "show", top: - (this.top - 30) }, "slow");
		},
		function () {
			//$accessory('.feature_accessories_tooltipWrapper').fadeOut(1000);
			$accessory('.feature_accessories_tooltipWrapper').animate({opacity: "hide", top: - (this.top) }, "fast");
			//$accessory('.feature_accessories_tooltipWrapper').slideUp("slow");
			$accessory(this).children(".feature_accessories_tooltipWrapper").remove();
			this.title = this.tip;
		}
	); 
	// set the dividers
	handleDuplicateAccessoryProducts();
	$accessory(".accessoryTab .accessory_product_list:visible .accessoriesDivider:odd").removeClass( "accessoriesDivider" ).addClass("accessoriesDividerVisible"); 
	resetLastDiv();
	
	activateAccessoryTabLava();
}


function activateAccessoryTabLava() {
    try {
    	$accessory(document).ready(function(){
    	    $accessory(".lavaLampAccessoriesTitle").lavaLamp({
    	      fx: "backout",
    	      speed: 1000
    	    });	
    	 });
    } catch(err) {
        //do nothing
    }
}

function resetDivider(id) {
	$accessory(".accessoriesDividerVisible").removeClass("accessoriesDividerVisible").addClass("accessoriesDivider");
	$accessory(id+" .accessory_product_list:visible .accessoriesDivider:odd").removeClass("accessoriesDivider").addClass("accessoriesDividerVisible");
}

function resetLastDiv(){
	$accessory(".product_table_noborder").removeClass("product_table_noborder").addClass("product_table_nobar");
	var numberOfProducts= $accessory(".accessoryTab .accessory_product_list:visible .product_table_nobar").size( );
	
	if(numberOfProducts%2==1){
		$accessory("#accessory_product_list_last_div").show();
		$accessory(".accessoryTab .accessory_product_list:visible .product_table_nobar:last").removeClass("product_table_nobar").addClass("product_table_noborder");
	} else {
		$accessory("#accessory_product_list_last_div").hide();
		numberOfProducts = numberOfProducts - 3;
		if (numberOfProducts > 0) {
		 $accessory(".accessoryTab .accessory_product_list:visible .product_table_nobar:gt("+numberOfProducts+")").removeClass("product_table_nobar").addClass("product_table_noborder");
		} else {
		    $accessory(".accessoryTab .accessory_product_list:visible .product_table_nobar").removeClass("product_table_nobar").addClass("product_table_noborder");
		}
	}
}

 function showAndHideAllBasedOnStyle(hide,show) {
    $accessory(hide).hide();
  	$accessory(show).show();
  	$accessory(".accessoryTab .duplicate").show();
  	resetDivider(show);
  	resetLastDiv();
}

function showAllAccessories() {
    $accessory(".accessoryTab .duplicate").hide();
    $accessory(".accessoryTab").show();
	resetDivider(".accessoryTab");
	resetLastDiv();
}

function handleDuplicateAccessoryProducts(){
    var $products= $accessory(".accessoryTab .accessory_product_list");
    
    $products.each( function(item){
        var $previousProduct = this;
        var $previousProductCheckbox = $accessory(this).find("input:checkbox");
        var $previousProductSelect = $accessory(this).find("select");
        $products.each( function(i){
            if(i > item){
                var $thisProduct = this;
                var $thisProductCheckbox = $accessory(this).find("input:checkbox");
                var $thisProductSelect = $accessory(this).find("select");
                if($thisProductCheckbox.val() == $previousProductCheckbox.val()) {
                    var sku = $previousProductCheckbox.val();

                    $accessory($thisProduct).addClass("duplicate");
                    
                    $accessory($previousProductSelect).addClass("select"+sku);
                    $accessory($previousProductCheckbox).addClass("checkbox"+sku);
                    
                    $accessory($thisProductSelect).addClass("select"+sku);
                    $accessory($thisProductCheckbox).addClass("checkbox"+sku); 
                                       
                    $accessory($previousProductSelect).bind( "change",function(){
                        copyValueForDuplicateProductSelect($accessory(this).attr("name"),$accessory(this).val());
                    });
                    $previousProductCheckbox.bind( "change",function(){
                        copyValueForDuplicateProductCheckbox($accessory(this).val(),$accessory(this).is(':checked'));
                    });
                    $accessory($thisProductSelect).bind( "change",function(){
                        copyValueForDuplicateProductSelect($accessory(this).attr("name"),$accessory(this).val());
                    });
                    $accessory($thisProductCheckbox).bind( "change",function(){
                        copyValueForDuplicateProductCheckbox($accessory(this).val(),$accessory(this).is(':checked'));
                    });
                }
            }
        });
        // Now Hide the duplicates
        $accessory(".accessoryTab .duplicate").hide();
    });
}

function copyValueForDuplicateProductCheckbox(sku,statusvar){
    $accessory(".checkbox"+sku).attr("checked",statusvar);
}
function copyValueForDuplicateProductSelect(sku,value){
    $accessory(".select"+sku).val(value);
}


