$(function(){			   
	//Focus for form inputs
	
	focusfix("input:text, textarea, select, input:password","focused");

	// Tab functionality
	//setTabs();
	
	var tab, openTab;
	if (parseParm("tab")){
		openTab = '#'+ parseParm("tab");
		$(openTab).trigger('click');
	}

	// Table striping
	var dataTable = $("table.data tbody");
	dataTable.find("tr:odd").addClass("odd");
	//dataTableRow.find("td:first, th:first").addClass("first").addClass("hpadding6");
	//dataTableRow.find("td:last, th:last").addClass("last");

	$(".bulletlist, .tabcontentlist").find("li:odd").addClass("odd");

	$(".box").each(function(){
		$(this).prepend("<div style='float:left;position:relative;width:0;height:0;'><div class='box-overlay' style='height:" + $(this).height() + "px; width:" + ($(this).width() - 14) + "px;'></div></div><div class='clear'></div>");
	});

	$("a.pop-up").click(function(){
		var po = window.open($(this).attr("href"),'','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=678,height=500');
		return false;
	});
	
	//Site-wide banner details div
	$("#promo-banner img").toggle(
		function(){
			$(this).attr("src", $(this).attr("src").match(/[^\.]+/) + "-close.gif");
			$("#promo-details").show();
		},
		function(){
			$(this).attr("src", $(this).attr("src").replace("-close", ""));
			$("#promo-details").hide();
	});
	
	$("#email-deals-signup").submit(function(){
		var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
		
		if (!emailfilter.test(this.email.value)) {
			alert("Please enter a valid email address.");
			return false;
		} else if ((this.firstname.value == "") || (this.firstname.value == "First Name")) {
			alert("Please enter your first name.");
			return false;
		}
		else if ((this.zipcode.value == "ZIP code") || (this.zipcode.value.length < 5)) {
			alert("Please enter a valid 5 digit U.S. ZIP code or a Canadian postal code.");
			return false;
		}
	});
	
});

var oldIE = false;
	
	if($.browser.msie && $.browser.version < 7){
		oldIE = true;
	}

// Navigation drop downs
function setDropDown(){
	$("#main-nav").children().hover(
		function(){
			$(this).addClass("active").siblings().removeClass("active");
			if(oldIE){
				$(this).append('<iframe src="javascript:;" frameborder="0" id="ie-menu-fix"></iframe>');
			}
		},
		function(){
			$(this).removeClass("active");
			if(oldIE){
				$(this).find("#ie-menu-fix").remove();
			}
	});
}

function searchClear(){
	$('#search .text-input').focus(function() {
		$(this).addClass('focused');
		if($(this).val() == "Keyword(s) or Item #"){
			$(this).val('');
		}
	});
	$('#search .text-input').blur(function() {
		$(this).removeClass('focused');
		if($(this).val() == ""){
			$(this).val("Keyword(s) or Item #");
		}
	});
}

// Nav Search Checks: @RON1
// No special-char conversion/encoding necessary w/ form POST
function SearchCheck(searchword) {
	var sw = searchword.value;
	// Check for blank field
	if (sw == "" || sw == "Keyword(s) or Item #") {
		alert("Please enter a value in the search box.");
		return false;
	}
	return true;
}

// Text input field focus
function focusfix(selector, className) {
	$(selector).focus(function() {
		$(this).addClass(className);
	});
	$(selector).blur(function() {
		$(this).removeClass(className);
	});
}

// Email deals clear
function emailDealsClear(){
	$('#email-deals .text-input')
	.focus(function() {
		if($(this).attr("name") == "email" && $(this).val() == "Email Address"){
			$(this).val("");
		}
		if($(this).attr("name") == "firstname" && $(this).val() == "First Name"){
			$(this).val("");
		}
		if($(this).attr("name") == "zipcode" && $(this).val() == "ZIP code"){
			$(this).val("");
		}
	})
	.blur(function() {
		if($(this).attr("name") == "email" && $(this).val() == ""){
			$(this).val("Email Address");
		}
		if($(this).attr("name") == "firstname" && $(this).val() == ""){
			$(this).val("First Name");
		}
		if($(this).attr("name") == "zipcode" && $(this).val() == ""){
			$(this).val("ZIP code");
		}
	});
}

// Cookie functions: RONK02
/*function getCookie(Name) {
  var search = Name + "=";
  if (document.cookie.length > 0) { // if there are any cookies
   offset = document.cookie.indexOf(search);
   if (offset != -1) { // if cookie exists
    offset += search.length;
    // set index of beginning of value
    end = document.cookie.indexOf(";", offset);
    // set index of end of cookie value
    if (end == -1) { end = document.cookie.length; }
    return unescape(document.cookie.substring(offset, end));
} } }*/

/* New Get Cookie Function - http://www.java2s.com/Code/JavaScript/Development/Cookiesetdeletegetvalueandcreate.htm */

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;
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}

/* New Get Cookie Function Ends */

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 : "");
}

// Mac date fix
function FixCookieDate(date) {
	var base=new Date(0);
	var sku=base.getTime();
	if (sku > 0) { date.setTime(date.getTime() - sku); }
}

function deletesignOutCookie(){
	var expdate = new Date("dec 25,2000 00:00:00");
	FixCookieDate(expdate);  // For Mac bug
	SetCookie("sign_out", "", expdate, "/"); // remove sign out link from header: eckman
}

// Header quick cart
function itemInCart(sku) {
	var skusearch = sku + '#';
	var cartItems = getCookie("CartItems");
	if (cartItems != null && cartItems != "") {
		var index = cartItems.indexOf(skusearch);
		return index != -1;
	}
	return false;
}

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];
}

/*
// Email Deals sign up form
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;
	} else if ((form.zipcode.value != "") && (form.zipcode.value != "ZipCode") && (form.zipcode.value.length < 5))
		{
		alert("Please enter a valid 5 digit US ZipCode \n or a valid Canadian ZipCode \n or leave blank");
		return false;
		}
		else if ((form.zipcode.value != "") && (form.zipcode.value != "ZipCode") && (form.zipcode.value.length == 5) && (isNaN(stringTrim(form.zipcode.value))))
		{
		alert("Please enter a valid 5 digit US ZipCode\n or a valid Canadian ZipCode\n or leave blank");
		return false;
		}
	
	//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.northerntool.com/subscription.jsp?eMail=' + emailName + '&fName=' + fName + '&zip=' + zCode + '';
	return false;
	
}*/

/* String Trim Right function removes any whitespace (spaces, tabs, and line breaks) using Reg Expression from the
ending of a String and replaces them with nothing */
function stringTrimR(strToTrim) {
	return(strToTrim.replace(/\s+$/g, ''));
}

/* String Trim Left function removes any whitespace (spaces, tabs, and line breaks) using Reg Expression from the
beginning of a String and replaces them with nothing */
function stringTrimL(strToTrim) {
	return(strToTrim.replace(/^\s+/g, ''));
}

/* String Trim removes any whitespace (spaces, tabs, and line breaks) using Reg Expression from the
beginning and ending of a String and replaces them with nothing */
function stringTrim(strToTrim) {
	return(strToTrim.replace(/^\s+|\s+$/g, ''));
}

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 setTabs2() {
	$("#tabs-wrapper li a").click(function(){
		var index = $("#tabs-wrapper li a").index(this);
		$("#tabs-wrapper li").removeClass("active");
		$(this).parent().addClass("active");
		$(".tab-content").removeClass("open");
		$(".tab-content").eq(index).addClass("open");

		return false;
	})
	.hover(
		function(){
			$(this).parent().addClass("hover");			
		},
		function(){
			$(this).parent().removeClass("hover");	
		});
}

// 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);
	}
}


/*
var prefix = window.parent.document.location.protocol + "//";
var site = "www.northerntool.com";
var fullUrl = prefix + site;

function include(filename){
	document.write('<script type="text/javascript" src="'
	+ fullUrl + filename + '"></scr' + 'ipt>'); 
}

include("/storestatic/en_US/html/js/search/jquery.jsonSuggest.js");
include("/storestatic/en_US/html/js/search/json2.js");
*/
