/**********************************
	CLEAR INPUT SCRIPTS 
**********************************/
function clearValue(varObj,varValue) {
	if($(varObj).val() == varValue) {
		$(varObj).val("");
	}
}

function testValue(varObj,varValue) {
	if($(varObj).val() == "") {
		$(varObj).val(varValue);
	}
}


// InstantService
// IF AGENTS ARE AVAILABLE:
function agents_available() {
 
document.getElementById('smartbutton').innerHTML = 'Need Help? <a href="" onclick="window.open(\'http://www.sstack.com/index.cfm/fuseaction/customerService.instantService\',\'custclient\',\'width=500,height=320,scrollbars=0\');return false;" title="Chat live with a customer service representative.">Chat Live</a> or <span>Call 1-800-365-1311</span>';
 
return true;
}
 
// IF AGENTS ARE NOT AVAILABLE:
function agents_not_available() {
 
document.getElementById('smartbutton').innerHTML = 'Need Help? <a href="/customer-service_contact-us" title="Contact a customer service representative.">E-mail Us</a> or <span>Call 1-800-365-1311</span>';
 
return true;
}

//Expand Option Selects in IE
var expandOnce = true;
function initExpandSelects(showHide) {
	//The following script will resize a select on hover in IE if the contents are getting cut off.
	if(($.browser.msie && $.browser.version < 9)) {
		if(showHide && expandOnce) {
			var $selects = $('#fusebox-container table.product-accessories td.product-item-options select');
				expandOnce = false;
		} else {
			var $selects = $('#fusebox-container table.product-items td.product-item-options select, #fusebox-container table.v-cart-cartdetail-itemstable td.product select').not('table.product-accessories select');
		}

		var expand = function() {
				var $this = $(this);

				$this.css({ 
					width: 'auto', 
					position: 'absolute' 
				});

				// If options are narrower than the defined width, retain original width instead of collapsing
				if($this.width() <= $this.data('originalWidth')) {
					this.noHide = false;
					contract.call(this);
				}	
			},
			contract = function() {
				if (!this.noHide) {
					$(this).css({ 
						width: $(this).data('originalWidth'), 
						position: 'static'
					});
				}	
			},
			focus = function() {
				this.noHide = true;
			},
			blur = function() {
				this.noHide = false; 
				contract.call(this);
			};

		$selects
			.each(function() {
				var $select = $(this),
					$span = $('<span>')
						.addClass('select-dynamicwidth')
						.css({
							display: 'block',
							position: 'relative',
							height: $select.outerHeight()
						});

				$select
					.data('originalWidth', $select.width() + 4)
					.wrap($span);
			})
			.hover(expand, contract)
			.focus(focus)
			.click(focus)
			.blur(blur)
			.change(blur);
	}
}


$(document).ready(function() {

	// Empty search field on focus
	$(".vSearch-basicSearchForm-container .input-type-text").focus( function() {
		if ( $(this).attr("value") == 'enter your keyword / item #' )
			{
				$(this).addClass("user-input");
				$(this).attr("value", "");
			}
	});


	// Repopulate search field on blur
	$(".vSearch-basicSearchForm-container .input-type-text").blur( function() {
		if ( $(this).attr("value") == '' )
			{
				$(this).removeClass("user-input");
				$(this).attr("value", "enter your keyword / item #");
			}
	});


	// Show/hide Shopping Cart contents on mouseenter/mouseleave,
	// but only if they have items in their cart
	$("#header-container ul.bag li.cart a").mouseenter(function () {
		if ( $("#header-container ul.bag li.cart span.item-count span").html() != '0 items' )
			{
		        $("li.cart-message").show();
		        $("li.cart-prod").show();
		        $("li.actions-bottom").addClass("show");
		        $("li.cart-special").show();
				$("ul.bag").css("border", "1px solid #d9d7d4");
			}
	});
	
	$("#header-container ul.bag").mouseleave(function () {
        $("li.cart-message").hide();
        $("li.cart-prod").hide();
        $("li.actions-bottom").removeClass("show");
        $("li.cart-special").hide();
		$(this).css("border", "1px solid #fff");
	});
	

	// Show/hide top nav dropdown menus
	$("#primary-nav > ul > li").mouseenter(
      function () {
		var $this = $(this);
        $this.addClass("hover");
        $this.children("a").addClass("hover");
        $this.children("ul").show();
	});

	$("#primary-nav > ul > li").mouseleave(
      function () {
		var $this = $(this);
		$this.removeClass("hover");
		$this.children("a").removeClass("hover");
        $this.children("ul").hide();
	});

	//Initiate tabs for product browse content body
	$(function() {
		$("#co-tabs").tabs();
	});

	$("#content-container div.v-product-browsepagetemplate-productscontainer li.productview-grid a").click( function() {
		$(".v-product-browsepagetemplate-productscontainer").removeClass("list");
		$(".v-product-browsepagetemplate-productscontainer").addClass("grid");
		$("li.productview-list a").addClass("option");
		$("li.productview-list a").removeClass("current");
		$("li.productview-grid a").addClass("current");
		$("li.productview-grid a").removeClass("option");
		
		//Set all nav links to carry the selection through
		$(".v-product-browsepagedisplaypreferenceform-form input[name='productView']").val('grid');
		$('.v-product-browsepagenavigation-container a').queryString('productView=grid');
		
		return false;
	});

	$("#content-container div.v-product-browsepagetemplate-productscontainer li.productview-list a").click( function() {
		$(".v-product-browsepagetemplate-productscontainer").removeClass("grid");
		$(".v-product-browsepagetemplate-productscontainer").addClass("list");
		$("li.productview-grid a").addClass("option");
		$("li.productview-grid a").removeClass("current");
		$("li.productview-list a").addClass("current");
		$("li.productview-list a").removeClass("option");
		
		//Set all nav links to carry the selection through
		$(".v-product-browsepagedisplaypreferenceform-form input[name='productView']").val('list');
		$('.v-product-browsepagenavigation-container a').queryString('productView=list');
		
		return false;
	});

	// Show/hide more info containers in product descriptions
	$("a.view-more").click(function() {
		$(this).slideToggle("fast");
		$(this).closest("div").children("div.view-more-content").slideToggle("fast");
		
		$productName = $('.v-product-detailinfo-container h1');
		if($productName.length) {
			var gaProductName = $productName.text();
			//_gaq.push(['Expand Content','Prod_Page', gaProductName]);
		}
		
		$productName = $(this).closest("div.description").siblings('.name').children('a');
		if($productName.length) {
			var gaProductName = $productName.text();
			//_gaq.push(['Expand Content','Cat_Page', gaProductName]);
		}
		
		return false;
	});
	$("a.view-more-close").click(function() {
		$(this).closest("div.view-more").children("a.view-more").slideToggle("fast");
		$(this).closest("div.view-more-content").slideToggle("fast");
		return false;
	});

	// Checkout bonus buck help
	$("div.gift-certificate span.help a").click(function() {
		$(this).parent("span.help").siblings("div.help-content-wrap").addClass("help-content-wrap-click");
		return false;
	});
	$("div.gift-certificate div.help-content-wrap a.close").click(function() {
		$(this).closest(".help-content-wrap").removeClass("help-content-wrap-click");
		return false;
	});

	//Init select expander
	initExpandSelects();

	// Bind lightboxes to specified links
	$('a[rel*=facebox],a[class*=lightbox]').facebox();

});
