/*****************************************************************************************
* jquery: initialize dropdown menu
* makes SearchSpring act like there are quotes around the text being submitted
*****************************************************************************************/
$(document).ready(function() {
	$("ul#navwrapper li").hover(megaHoverOver,megaHoverOut);

	// uncomment to wrap submitted text in double-quotes
	// $('#txtSearch').parent().submit(function(){
	//	$('#txtSearch').val('"'+$('#txtSearch').val().replace('"', '')+'"');
	// });
});


/*****************************************************************************************
* from http://www.sohtanaka.com/web-design/mega-drop-downs-w-css-jquery/
*****************************************************************************************/
//On Hover Over
function megaHoverOver(){
    $(this).find(".dropdown").show(); //Find sub and fade it in
		var parentLiId = $(this).attr("id").split("nav")[1];
		$(this).addClass('navhover'+parentLiId);

    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery);

		$(this).calcSubWidth();  //Call function to calculate width of all ul's
		$(this).find(".dropdown").css({'width' : rowWidth}); //Set Width
}
//On Hover Out
function megaHoverOut(){
	var parentLiId = $(this).attr("id").split("nav")[1];
	$(this).removeClass('navhover'+parentLiId);
  $(this).find(".dropdown").hide();
}





/*****************************************************************************************
* change tabs on product details page
*****************************************************************************************/
function showTab(theTab) {
	// hide all
	if (document.getElementById('desc')) {
		document.getElementById('desc').style.display = 'none';
		document.getElementById('prodtabdesc').src = '/images/tabs/tabdesc_off.png';
	}
	if (document.getElementById('size')) {
		document.getElementById('size').style.display = 'none';
		document.getElementById('prodtabsize').src = '/images/tabs/tabsize_off.png';
	}
	if (document.getElementById('directions')) {
		document.getElementById('directions').style.display = 'none';
		document.getElementById('prodtabdirections').src = '/images/tabs/tabdirections_off.png';
	}
	if (document.getElementById('facts')) {
		document.getElementById('facts').style.display = 'none';
		document.getElementById('prodtabfacts').src = '/images/tabs/tabfacts_off.png';
	}
	if (document.getElementById('reviews')) {
		document.getElementById('reviews').style.display = 'none';
		document.getElementById('prodtabreviews').src = '/images/tabs/tabreviews_off.png';
	}

	// show only the one called
	document.getElementById(theTab).style.display = 'block';
	document.getElementById('prodtab'+theTab).src = '/images/tabs/tab' + theTab + '.png';
}





/*****************************************************************************************
* handles "same as shipping address" checkboxes
*****************************************************************************************/

function copyBillToOcst(theCheckbox) {
	if(!theCheckbox.checked) {
		return;
	}
	var theForm = document.getElementById('custform');
	theForm.BillFirstName.value =							theForm.ShipFirstName.value;
	theForm.BillLastName.value =							theForm.ShipLastName.value;
	theForm.BillEmail.value = 								theForm.ShipEmail.value;
	theForm.BillPhone.value = 								theForm.ShipPhone.value;
	theForm.BillCompany.value = 							theForm.ShipCompany.value;
	theForm.BillAddress1.value = 							theForm.ShipAddress1.value;
	theForm.BillCity.value = 									theForm.ShipCity.value;
	theForm.BillStateSelect.selectedIndex = 	theForm.ShipStateSelect.selectedIndex;
	theForm.BillZip.value = 									theForm.ShipZip.value;
}

function copyBillToAcnt(theCheckbox) {
	if(!theCheckbox.checked) {
		return;
	}
	var theForm = document.getElementById('custform');
	theForm.Customer_BillFirstName.value =				theForm.Customer_ShipFirstName.value;
	theForm.Customer_BillLastName.value =				theForm.Customer_ShipLastName.value;
	theForm.Customer_BillEmail.value = 					theForm.Customer_ShipEmail.value;
	theForm.Customer_BillPhone.value = 					theForm.Customer_ShipPhone.value;
	theForm.Customer_BillCompany.value = 				theForm.Customer_ShipCompany.value;
	theForm.Customer_BillAddress.value = 				theForm.Customer_ShipAddress.value;
	theForm.Customer_BillCity.value = 					theForm.Customer_ShipCity.value;
	theForm.Customer_BillStateSelect.selectedIndex =		theForm.Customer_ShipStateSelect.selectedIndex;
	theForm.Customer_BillZip.value = 						theForm.Customer_ShipZip.value;
}

/*****************************************************************************************
* handles tooltips
*****************************************************************************************/

$(document).ready(function() {
  $('.toolTip').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipWrapper">'
        +'<div class="toolTipTop"></div>'
        +'<div class="toolTipMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipWrapper').css({left:this.width-22})
    $('.toolTipWrapper').fadeIn(300);
  },
    function() {
      $('.toolTipWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );

  $('.toolTipUp').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipUpWrapper">'
        +'<div class="toolTipUpTop"></div>'
        +'<div class="toolTipUpMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipUpBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipUpWrapper').css({left:this.width-22})
    $('.toolTipUpWrapper').fadeIn(300);
  },
    function() {
      $('.toolTipUpWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );

  $('.toolTipLeft').hover(
    function() {
    this.tip = this.title;
    $(this).append(
     '<div class="toolTipLeftWrapper">'
        +'<div class="toolTipLeftTop"></div>'
        +'<div class="toolTipLeftMid">'
          +this.tip
        +'</div>'
        +'<div class="toolTipLeftBtm"></div>'
      +'</div>'
    );
    this.title = "";
    this.width = $(this).width();
    $(this).find('.toolTipLeftWrapper').css({left:this.width-310})
    $('.toolTipLeftWrapper').fadeIn(300);
  },
    function() {
      $('.toolTipLeftWrapper').fadeOut(100);
      $(this).children().remove();
        this.title = this.tip;
      }
  );});

/*****************************************************************************************
* functions for my favorites module
*****************************************************************************************/

function CheckAll(field_base, checked) {
	var i;
	var box;
	for ( i = 1; ( box = document.forms['favorites'].elements[ field_base +
		"[" + i.toString() + "]" ] ) != null; i++ )
	{
		if ( !box.disabled )
		{
			box.checked = checked;
		}
	}
}

function Check(field_base, checked, field) {
	var box;
	if ( ( box = document.forms['favorites'].elements[ field_base +
		"[" + field.toString() + "]" ] ) != null && !box.disabled) box.checked = checked;
}


/*****************************************************************************************
* cattree functions
*****************************************************************************************/
function cattreeShowHide(){
	$('.ct_show_more').siblings(".overage").slideToggle();
}
