var t;

$(function(){
	$("#free-shipping-banner").click(function(){
		if($("#free-shipping-details").is(":visible")){
			$("#free-shipping-details").fadeOut("fast");
		} else {
			$("#free-shipping-details").fadeIn("fast");
		}
	});
	$("#free-shipping-details .popup-close-button").click(function(){
		$("#free-shipping-details").fadeOut("fast");
	});
	
	/*var oldIE = false;
	if($.browser.msie && $.browser.version < 7){
		oldIE = true;
	}*/
	
	$("#navigation td").hover(function(){
		$(this).addClass('hover');
		/*if(oldIE){
			$(this).append('<iframe src="javascript:;" frameborder="0" id="ie-menu-fix"></iframe>');
		}*/
	},
	function(){
		$(this).removeClass('hover');
		/*if(oldIE){
			$(this).find("#ie-menu-fix").remove();
		}*/
	});
	

	$(".prod-record").not(".tooltip").not(".tooltip-inner").hover(
		function(e){
			t = setTimeout("showToolTip('" + $(this).attr("id") + "')",1500);
		},
		function(e){
			clearTimeout(t);
			$(this).find(".tooltip").css("display","none");
		}
	);
	
	$(".prod-record, .cat-record").click(function(){
		document.location = $(this).find("a:first").attr("href");
	});
	
	$(".cat-record").not(".tooltip").not(".tooltip-inner").hover(
		function(e){
			t = setTimeout("showCatTip('" + $(this).attr("id") + "')",200);
		},
		function(e){
			clearTimeout(t);
			$(this).find(".tooltip").css("display","none");
		});
	
	$(".cat-record, #feature-item").click(function(){
		document.location = $(this).find("a:first").attr("href");
	});
	
    $(".defaultText").focus(function(){
        if ($(this).val() == $(this)[0].title){
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    
    $(".page_button").not(".page_button-disabled").hover(function(){
		$(this).addClass("page_button-over");
	},
	function(){
		$(this).removeClass("page_button-over");
	})
	$(".page_button").not(".page_button-disabled").not(".page_button-active").click(function(){
		document.location = $(this).find("a").attr("href");
	});
	
	$(".close-btn").click(function(){
		$(".richmedia-container").css("display","none");
		$(".close-btn").css("display","none");
		$(".richmedia-btn").css("display","block");
	});

	$(".thumbnail-item").click(function(){
		var newimage = $(this).find("img").eq(0).attr("src").replace("_med.gif","_lg.jpg");
		$("#main-image img").attr("src", newimage);
		$(".thumbnail-active").removeClass("thumbnail-active");
		$(this).addClass("thumbnail-active");
		$(".close-btn").trigger('click');
	});

	if ( $(".thumbnail-item").length > 3 ) {
		$(".gallery-button").css("display","block");
		$(".gallery-button").click(function(){
			var leftpos = parseFloat($("#thumbnail-gallery").css("left"));
			if ( $(this).attr("id") == "left" && leftpos < 0 ) {
				$("#thumbnail-gallery").css("left", ( leftpos + 119 ) + "px");
			}
			if ( $(this).attr("id") == "right" && leftpos > - ( ( $(".thumbnail-item").length - 3 ) * 119 )) {
				$("#thumbnail-gallery").css("left", ( leftpos - 119 ) + "px");
			}
		});
	}
	    
    $(".defaultText").blur(function(){
        if ($(this).val() == ""){
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
	
	$("#glp, #glp-details").click(function(){
		if($("#glp-details").is(":visible")){
			$("#glp-details").fadeOut("fast");
		} else {
			$("#glp-details").fadeIn("fast");
		}
	});
    
    $(".defaultText").blur();

});

function searchClear(){
	$("#search-input")
		.focus(function(){
	        if ($(this).val() == $(this)[0].title){
	            $(this).removeClass("defaultTextActive");
	            $(this).val("");
	        }
	    })
		.blur(function(){
		    if ($(this).val() == ""){
		        $(this).addClass("defaultTextActive");
		        $(this).val($(this)[0].title);
		    }
		});
}

function parseit(optStr){ //Ron
	var oarray = new Array();
	var urlArg = new Array();
	oarray = optStr.split("&");
	for (var loop = 0; loop < oarray.length; loop++) {
		var broken_info = oarray[loop].split("=");
		var the_property = broken_info[0];
		var the_value = broken_info[1];
		if (the_property) urlArg[the_property] = the_value;
	}
	return urlArg;
}
	
function parseParm(name) {
	var regexS = "[\\?&]"+name+"=([^&#]*)",
		regex = new RegExp( regexS ),
		tmpURL = window.location.href,
		results = regex.exec( tmpURL );
  if( results == null ) return "";
  else return results[1];
}

function setTabs() {
	// Tab functionality
	$(".tab-container").each(function(){
		var tabs = this;
		var tabcontent = $(".tab-content-container").eq($(".tab-container").index(this));
		$(this).find(".tab")
			.click(function(e){
				$(tabs).find(".tab").removeClass("open");
				$(this).removeClass("hover").addClass("open");
				$(".tab-content").eq($(".tab").index(this)).parent().find(".tab-content").removeClass("open");
				$(".tab-content").eq($(".tab").index(this)).addClass("open");
				e.preventDefault();
			})
			.hover(function(){
				$(this).addClass("hover");
			},
			function(){
				$(this).removeClass("hover");
			});
		$(this).find(".tab").removeClass("open");
		$(tabcontent).find(".tab-content").removeClass("open");
		$(this).find(".tab").eq(0).addClass("open");
		$(tabcontent).find(".tab-content").eq(0).addClass("open");
	});
}

function checkAndSubmit() {
	var sw = document.CatalogSearchForm.searchTerm.value;
	var sw = sw.replace(/^\s+|\s+$/g, ''); //trim spaces before/after keyword
	var sindex = sw.indexOf("-");
	if (sw == "" || sw == "Just 1 click from cool stuff!") { //check for blank field
		alert("Please enter a value in the search box.");
		return false;
	} else if (sindex != -1) { // There is a dash in the word
		alert("Only include the digits before the dash. For example, if you are searching for item# 1701-1504 enter only 1701 into the search field.");
		return false;
	} else if (sw.indexOf("%") != -1) {
		alert("Please remove the percent character from the search box.");
		return false;
	} else { return true; }
}

//begin email deals form //
////////////Betsy Bury 02/11/09: Enhanced edits within function
function subscribePg(form){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	if (!emailfilter.test(form.email.value)){
		alert("Please enter a valid Email Address");
		return false;
	}
	
	if (!(isNaN(form.firstname.value)) || (form.firstname.value) == "First Name"){
		alert("Please enter your First Name");
		return false;
	}
	
	if ((form.zipcode.value == "") || (form.zipcode.value == "ZIP code") || (form.zipcode.value.length < 5)){
		alert("Please enter a valid 5 digit U.S. ZIP code or Canadian postal code");
		return false;
	}
	/* else if ((form.zipcode.value != "") && (form.zipcode.value != "ZIP code") && (form.zipcode.value.length == 5) && (isNaN(stringTrim(form.zipcode.value)))){
		alert("Please enter a valid 5 digit U.S. ZIP code or a valid Canadian postal code \n or leave blank");
		return false;
	} */
	
	// Override title defaults if still present
	/*
	if ((form.firstname.value == "First Name") || (form.firstname.value == "")){
		form.firstname.value = ".";
	}
	if ((form.zipcode.value == "ZIP code") || (form.zipcode.value == "")){
		form.zipcode.value = ".";
	}
	*/
	//Get datestamp: Ron
	datetoday = new Date();
	var datestring;
	var month = datetoday.getMonth()+1;
	if (month < 10) { month = '0' + month; }
	var day = datetoday.getDate();
	if (day < 10) { day = '0' + day; }
	var year = datetoday.getFullYear();
	datestring = month + '/' + day + '/' + year;
	
	//Load variables with form field values
	var emailName = stringTrimL(form.email.value);
	var fName = stringTrimL(form.firstname.value);
	var zCode = stringTrimL(form.zipcode.value);
	/*
	location.href = 'http://newsletter.kotulas.com/emailsignup.jsp?email=' + emailName + '&firstname=' + fName + '&zipcode=' + zCode + '&url=http://<%=request.getServerName()%><%=request.getContextPath()%>/EmailConfirmView%3flangId=${langId}%26storeId=${storeId}%26catalogId=${catalogId}';
	*/
	
	location.href = 'http://newsletter.kotulas.com/emailsignup.jsp?email=' + emailName + '&firstname=' + fName + '&zipcode=' + zCode + '&url=http://www.kotulas.com/deals/EmailConfirmView?langId=-1%26storeId=10152%26catalogId=10602';
	
	return false;
}

function stringTrimR(strToTrim) {
	return(strToTrim.replace(/\s+$/g, ''));
}

function stringTrimL(strToTrim) {
	return(strToTrim.replace(/^\s+/g, ''));
}

function stringTrim(strToTrim) {
	return(strToTrim.replace(/^\s+|\s+$/g, ''));
}


function showToolTip (name) {
	obj = $("#" + name);
	$(obj).find(".tooltip").css("display","block");
	var pos = $(obj).position();
	$(obj).find(".tooltip").css("top",pos.top - 150);
	$(obj).find(".arrow").css("display","block");
	if($(window).width()<(pos.left+650) && pos.left > 460) {
		$(obj).find(".tooltip").css("left",pos.left - 470);
		$(obj).find(".arrow").addClass("arrow-left");
	} else {
		$(obj).find(".tooltip").css("left",pos.left + 190);
	}
	if(($(obj).find(".tooltip").eq(0).position().left + 460)>$(window).width()){
		$(obj).find(".tooltip").css("left",$(window).width() - 460);
		$(obj).find(".arrow").removeClass("arrow-left");
		$(obj).find(".arrow").css("display","none");
	}
}

function showCatTip (name) {
	obj = $("#" + name);
	$(obj).find(".tooltip").css("display","block");
	var pos = $(obj).position();
	$(obj).find(".tooltip").css("top",pos.top - 150);
	$(obj).find(".arrow").css("display","block");
	if($(window).width()<(pos.left+650) && pos.left > 460) {
		$(obj).find(".tooltip").css("left",pos.left - 470);
		$(obj).find(".arrow").addClass("arrow-left");
	} else {
		$(obj).find(".tooltip").css("left",pos.left + 190);
	}
	if(($(obj).find(".tooltip").eq(0).position().left + 460)>$(window).width()){
		$(obj).find(".tooltip").css("left",$(window).width() - 460);
		$(obj).find(".arrow").removeClass("arrow-left");
		$(obj).find(".arrow").css("display","none");
	}
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
    }
  return null;
}

//Cookie parsing plugin
jQuery.returnCookie = function(cookie) {
	var string = getCookie(cookie);
	var array = new Array();
	//alert(string);
	if(string) {
		$(string.split("|")).each(function(){
			var child = new Array();
			if(this.length > 0) {
				$(this.split("~")).each(function(){
					if(this != "") {
						child.push(this);
					}
				});
				array.push(child);
			} 
		});
	}
	if(array == "") {
		return false;
	} else {
		return $(array);
	}
}

function SetCookie(name, value, expires, path) {
  document.cookie=name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "");
}

function SetSessionCookie(name, value, path) {
  document.cookie=name + "=" + escape(value) +
    ((path) ? "; path=" + path : "");
}
