//Detect Internet Explorer and/or Firefox
var isMac = false;
var isFirefox = false;
var isIE6 = false;
if (navigator.platform.indexOf("Mac")>-1) isMac = true;
if (navigator.userAgent.indexOf("Firefox")>-1) isFirefox = true;

var hasScrollers = false;
var isEmailOptInVisible = false;

// On DOM Ready :: START //
$(function() {
	isIE6 = detectIE(6);
	if (isMac & isFirefox) hasScrollers = checkForScrollers();

	$("#emailOptInDropDown .content").hide();
	$("#emailOptInDropDownTrigger").click(emailOptInShowOrHide);
	$("#emailOptInClose").click(emailOptInShowOrHide);

	$("input.emailOptIn").focus(function() {
		var node = $(this);
		if(!node.attr("cleared")) {
			node.get(0).value = "";
			node.attr("cleared","cleared");
		}
	});

	$('#menu .dropList .subNavContainer').hide();	
	$("#selectHider").hide();

	$("#menu .subNavContainer").hover(
		function(){ 
			$(this).addClass('active');
				if (isIE6){
					$(".selectHider", this).css('top', 25);
					$(".selectHider", this).css('width', $(".subNavContainer", this).css('width'));
					$(".selectHider", this).css('height', $(".subNavContainer", this).height());
					$(".selectHider", this).fadeIn("fast");
				}					 
		},
		function(event){ 
			$(this).removeClass('active');
		}
	);

    $("#menu .dropList li").hover(		   
        function(){ 
			$(this).addClass('active');
			if (isMac & isFirefox) $(".subNavContainer", this).slideDown("fast");
			else {
				$(".subNavContainer", this).fadeIn("fast");
				if (isIE6){
					$(".selectHider", this).css('top', 25);
					$(".selectHider", this).css('width', $(".subNavContainer", this).css('width'));
					$(".selectHider", this).css('height', $(".subNavContainer", this).height());
					$(".selectHider", this).fadeIn("fast");
				}					 
			}
		}, 
        function(){ 
			$(this).removeClass('active');
			$(".subNavContainer", this).slideUp("fast");
		}
	);
		
    $("#menu .dropList").mouseout(		   
        function(){ 
			$(this).removeClass('active');
			$(".subNavContainer", this).slideUp("fast");
			if( isIE6 ) $(".selectHider").slideUp("fast");
		}
	);

	$('#cartContainer').hide();	
	$("#miniCartTrigger").click(miniCartShow);
	$("#miniCartIconTrigger").click(miniCartShow);
	$("#closeTheCart").click(miniCartHide);


	$("#dropdownRegion .content").hide();
	$("#dropdownRegion").click(toggleDropdownRegion);

	$("#dropdownExperience .content").hide();
	$("#dropdownExperience").click(toggleDropdownExperience);

	$('#menu .size2go').click(function(){
			if($(this).attr('value') == "Enter Keyword or item#")	{
				$(this).attr('value', '');
			}
	})
	$('#menu .emailAddress input').click(function(){
			if($(this).attr('value') == "Enter e-mail address:")	{
				$(this).attr('value', '');
			}
	})	
	
	/* Forgot Password Hide */
	$('#forgot_password').hide();
	
	/* hide all form element tables*/
	$('#edit_billing').hide();
	$('#edit_shipping').hide();
	$('#edit_personal').hide();
	$('#edit_password').hide();
	
	/* MA1 create toggle effect for edit links arrows*/
	$('#middle .toggle_arrow_link').each(function(){							
		
		//$(this).click(function(){
			
			$(this).toggle(function(){
  				$(this).children('.arrow_toggle_right').addClass("arrow_toggle_down");
			},function(){
  				$(this).children('.arrow_toggle_right').removeClass("arrow_toggle_down");
			})
		//})	
	});

});
// On DOM Ready :: END //

function toggleDropdownRegion() {
	if($("#dropdownRegion").hasClass("active"))
		hideDropdownRegion();
	else
		showDropdownRegion();
}
function hideDropdownRegion() {
	if (isIE6 && isEmailOptInVisible==false) $(".pagination select").css("visibility", "visible");
	$("#dropdownRegion").removeClass("active");	
	if (isMac & isFirefox) $("#dropdownRegion .content").slideUp("fast");
	else $("#dropdownRegion .content").fadeOut("fast");
}
function showDropdownRegion() {
	if (isIE6) $(".pagination select").css("visibility", "hidden");
	$("#dropdownRegion").addClass("active");
	if (isMac & isFirefox) $("#dropdownRegion .content").slideDown("fast");
	else $("#dropdownRegion .content").fadeIn("fast");

	hideDropdownExperience();
}
function toggleDropdownExperience() {
	if($("#dropdownExperience").hasClass("active"))
		hideDropdownExperience();
	else
		showDropdownExperience();
}
function hideDropdownExperience() {
	$("#dropdownExperience").removeClass("active");
	if (isMac & isFirefox) $("#dropdownExperience .content").slideUp("fast");
	else $("#dropdownExperience .content").fadeOut("fast");
	if (isIE6 && isEmailOptInVisible==false) $(".pagination select").css("visibility", "visible");
	if (isIE6 && isEmailOptInVisible==false) $("#divFilterSelects select").css("visibility", "visible");
}
function showDropdownExperience() {
	if (isIE6) $(".pagination select").css("visibility", "hidden");
	if (isIE6) $("#divFilterSelects select").css("visibility", "hidden");
	$("#dropdownExperience").addClass("active");
	if (isMac & isFirefox) $("#dropdownExperience .content").slideDown("fast");
	else $("#dropdownExperience .content").fadeIn("fast");

	// Shift the Site Selector Menu to the left if we're on Mac Firefox
	if (isMac & isFirefox & hasScrollers) $("#dropdownExperience .content").css("right", "18px");

	hideDropdownRegion();
}


function emailOptInShowOrHide() {
	if(isEmailOptInVisible) {
		if (isIE6) $(".pagination select").css("visibility", "visible");
		if (isIE6) $("#divFilterSelects select").css("visibility", "visible");
		$("#emailOptInDropDown").removeClass("active");
		if (isMac & isFirefox) $("#emailOptInDropDown .content").slideUp("fast");
		else $("#emailOptInDropDown .content").fadeOut("fast");
		isEmailOptInVisible = false;
	}
	else {
		if (isIE6) $(".pagination select").css("visibility", "hidden");
		if (isIE6) $("#divFilterSelects select").css("visibility", "hidden");
		$("#emailOptInDropDown").addClass("active");
		if (isMac & isFirefox) $("#emailOptInDropDown .content").slideDown("fast");
		else $("#emailOptInDropDown .content").fadeIn("fast");
		isEmailOptInVisible = true;
	}
	return false;
}

function miniCartShow() {
	$("#cartContainer").slideDown("fast").fadeIn();
	$("#cartContainerIFrame").slideDown("fast").fadeIn();

	return false;
}

function miniCartHide() {
	if (isMac & isFirefox){
		$("#cartContainer").slideUp("fast");
		$("#cartContainerIFrame").slideUp("fast");
	} else {
		$("#cartContainer").slideUp("fast").fadeOut("fast");
		$("#cartContainerIFrame").slideUp("fast");
	}
}

/* Forgot Password Toggle */
function togglePasswordField() {
	$('#forgot_password').toggle();
	$('#silo_btn_signin').toggle(); /* CHANGED: Added toggle to the sign-in button so it hides when the forgot password block shows */
	toggleLoginFields();
}
/* CHANGED: Moved the login table opacity switch to a new method so it can be called elsewhere. This is to cleanup an inconsistency between clicking the checkbox and clicking the "Forgot Password" text */
function toggleLoginFields() {
	var table = document.getElementById("login_table");
	if (table.className=="registerTable2") {
		table.className = "registerTable2 inactive"
		table.getElementsByTagName("input")[0].disabled = true;
		table.getElementsByTagName("input")[1].disabled = true;
		$("#sign_in_btn").attr('disabled','disabled');
	} else {
		table.className = "registerTable2";
		table.getElementsByTagName("input")[0].disabled = false;
		table.getElementsByTagName("input")[1].disabled = false;
		$("#sign_in_btn").removeAttr('disabled');
	}
}
/* CHANGED: Added method for setting the Forgot Password checkbox when the label is clicked */
function togglePasswordCheckbox() {
	var inp = document.getElementById("forgotPasswordCheckbox");
	inp.checked = (inp.checked) ? false : true;
	togglePasswordField();
}

/* info edit forms toggle MA1 */
/*
$("p").toggle(function(){
  $(this).addClass("selected");
},function(){
  $(this).removeClass("selected");
});
*/

function toggleEditTable(tableId){
	var table = '#' + tableId;
	$(table).toggle();
	return false;
}

function toggleLockerTagEdit() {
	$("#locker_tag_display").toggle();
	$("#locker_tag_edit").toggle();
}

detectIE = function(version) {
	var isie = false;
	var istargetversion = false;
	if (navigator.appName.indexOf("Explorer")>-1 & navigator.userAgent.indexOf('Opera')==-1) isie = true;
	if (isie) {
		var min = version;
		var max = version + 1;
		var tmp=navigator.appVersion.split("MSIE")
		var browserVersion=parseFloat(tmp[1])
		if (browserVersion>=min & browserVersion < max) {
			istargetversion = true;
		}
	}
	return istargetversion;
};

// Runs only for Firefox Mac - checks for DIVs (and only DIVs) that have an overflow: auto or : scroll property.
function checkForScrollers() {
	try {
		var scrollers = false
		var startElem = document.getElementById("middle");
		var divs = startElem.getElementsByTagName("div");
		for (var i=0; i<divs.length; i++) {
			var div = divs[i];
			var tmp = getStyle(div, "overflow");
			if (tmp=="auto"|tmp=="scroll") {
				if (div.id!="cartScrollerBox") {
					scrollers = true;
					break;
				}
			}
		}
		return scrollers;
	} catch(e) {
	}
}

// Get the computed css property
// From Elektronaut (Inge Jørgensen)
// http://www.elektronaut.no/articles/2006/06/07/computed-styles
function getStyle( element, cssRule )
{

	/*
  alert(document.defaultView);	
	
  if( document.defaultView && document.defaultView.getComputedStyle )
  {
    var value = document.defaultView.getComputedStyle( element, '' ).getPropertyValue( 
      cssRule.replace( /[A-Z]/g, function( match, char ) 
      { 
        return "-" + char.toLowerCase(); 
      } ) 
    );
  }
  else if ( element.currentStyle ) var value = element.currentStyle[ cssRule ];
  else                             var value = false;
  return value;
  */
  return false;
}


function clearField(obj, defaultVal) {
	if (obj.value==defaultVal) obj.value = "";
}
