/* ##### GLOBAL VARS ##### */

//browser check
var IE = (navigator.appName == "Microsoft Internet Explorer");
var IE6 = (navigator.appVersion.indexOf('MSIE 6.0')>0);
var CDN_Url; 
if (window.location.protocol == "https:") {
    CDN_Url = "https://a248.e.akamai.net/f/597/774/12h/www.landsend.com";
} else {
    CDN_Url = "http://aka.landsend.com";
}

function validateSearch(){
	var globalSearch = document.getElementById("globalNavSearchText");
	if ( (globalSearch.value.length == 0) || (globalSearch.value == " Enter keyword or item #")) {
		alert('Please enter a keyword or item # in the search box, then click "Search". ');
		globalSearch.focus();
		s_omtr = s_gi(s_account);
		s_omtr.pageName="Null search pop";
		s_omtr.tl(true,'o','link'); 
		return false;
	}
	return true;
} 

function initNav() {
	if (getCookie("signedIn") == "1" || getCookie("cid") != "") {
		document.getElementById('sign-in').innerHTML = "Sign Out";
		document.getElementById('sign-in-link').href='http://'+window.location.host+'/cgi-bin/SignOut.cgi?storeType=[SHOW storeTypeID]&resetSID=Y';
	} else {
		document.getElementById('sign-in-link').href='https://'+window.location.host+'/cgi-bin/Login.cgi?redirect=/cgi-bin/AccountPortal.cgi&type=GenLogin';
		setCookie("signedIn", "");
	}
	conditionallyDisplayCookieBanner();
	
	$(document).ready(function() {
		var promoCookie = getCookie("promoCookieKey");
		var location = "" + window.location;
		
		if (location.indexOf("OrderConfirm.cgi") != -1) {
	   		setCookie("promoCookieKey", "" , null);
		} else if (promoCookie != null && promoCookie != "") {
			var banner = new promoBanner(promoCookie);
		}    
    });
}

//current drop down number
var current_num;

//drop down expand
var this_max_h; //max height of current menu
var alpha_counter;
var m_timer; //setTimeOut
var m_counter; //set height 0 - max

//vars used to check for menu exit
var check_timer; //setTimeOut
var check_counter; //counts up to the max
var delay_check_for = 25; //the max 
if(IE) delay_check_for = 20;

/* ##### FUNCTIONS ##### */
function loadLink(url) {	
	window.location = url;
}

function clearRightMenu() {
	var ul_tags = document.getElementsByTagName("ul");
	for(var i = 0; i < ul_tags.length; i++) if(ul_tags[i].className == "gn_rightMenu_ul") ul_tags[i].style.display = "none";
}

function hideMenus() {	
	for(var i = 0; i < dd_menu.length; i++) if(document.getElementById("gn_iframe_" + i)) document.getElementById("gn_iframe_" + i).style.display = "none";
	for(var i = 0; i < dd_menu.length; i++) if(document.getElementById("m_dropdown_" + i)) document.getElementById("m_dropdown_" + i).style.display = "none";
	current_num = null;	
}

function clearRollovers() {			
	var li_tags = document.getElementsByTagName("li");	
	for(var i = 0; i < li_tags.length; i++) if(li_tags[i].className == "gn_menu_li_o") li_tags[i].className = "gn_menu_li";
}

function showMenu(obj, num) {
	if(typeof(dd_menu) == "undefined" || dd_menu.length <= 0) {
		return;
	}
	if(typeof(navABGroup) != "undefined" && navABGroup == "A") {
		return;
	}
	if(dd_menu[num]) {
		if(!obj.over) obj.onmouseout = function() { this.over = false; }	
		obj.over = true;
		checkOver = function() {
			if(obj.over) showMenuDelayed(obj, num);
		}
		setTimeout("checkOver()", 200);
	}
}

function showMenuDelayed(obj, num) {
	//clear old menus
	hideMenus();
	clearRightMenu();
	
	//set current num
	current_num = num;
	
	//javascript data for this menu
	var menu_data = dd_menu[num];
	
	//menu divs
	var m_dropdown = document.getElementById("m_dropdown_" + num);
	var m_header = document.getElementById("m_header_" + current_num);
	var m_links = document.getElementById("m_links_" + current_num);	
	var m_win = document.getElementById("m_win_" + num);
	var m_hide = document.getElementById("m_hide_" + num);
	var m_iframe = document.getElementById("gn_iframe_" + num);
	
	//popuplate menu if empty
	if(m_hide.innerHTML == "") {
		
		var menu_ul = document.createElement('ul');
		menu_ul.className = "gn_menu_ul";
		
		for(var i = 0; i < menu_data.length; i++) {
			
			//set menu li properties
			var menu_li = document.createElement('li');
			menu_li.id = "link_row_" + num + "_" + i;
			menu_li.className = "gn_menu_li";
			menu_li.index = i;
			menu_li.innerHTML = "<a href='" + menu_data[i][0][1] + "'>" + menu_data[i][0][0] + "</a>";
			menu_li.onclick = function() { loadLink(menu_data[this.index][0][1]); };
			menu_li.over = false;
			
			//build right menu
			var right_menu = dd_menu[current_num][i];
			if(right_menu.length <= 1) {
				
				menu_li.onmouseover = function() { 
					clearRollovers();
					this.over = true;
					this.className = "gn_menu_li_o";
				}
				
				menu_li.onmouseout = function() { this.over = false; }
			
			}else { //has enough elements for a right menu
				 
				var rm_ul = document.createElement('ul');
				
				menu_li.onmouseover = function() { //show right menu, set offset top			
					clearRollovers();
					this.over = true;
					this.className = "gn_menu_li_o";
					
					//adjust UL top based on item
					var li_iframe = document.getElementById("rm_iframe_" + this.index);
				//	li_iframe.src = "/images/clear.gif";
					
					var ul_tags = this.getElementsByTagName('ul');
					var li_tags = this.getElementsByTagName('li');
					var li_height = li_tags[0].offsetHeight;	
					
					if(this.index > 0 && li_tags.length != 2) {
						ul_tags[0].style.top = (-1 * li_height) + "px";
						li_iframe.style.top = (-1 * li_height) + "px";
					}else if(this.index > 0 && li_tags.length == 4) {
						ul_tags[0].style.top = (-1 * li_height) + "px";
						li_iframe.style.top = (-1 * li_height) + "px";
					}else if(this.index > 0 && li_tags.length > 4) {
						ul_tags[0].style.top = (-2 * li_height) + "px";
						li_iframe.style.top = (-2 * li_height) + "px";
					}
					
					li_iframe.style.height = (ul_tags[0].offsetHeight - 1) + "px";
					li_iframe.style.display = "block";
					
				}
				
				menu_li.onmouseout = function() { 
					this.over = false; 
					document.getElementById("rm_iframe_" + this.index).style.display = "none";
				}
	
				for(var j = 1; j < right_menu.length; j++) {
					
					var rm_li = document.createElement('li');
					rm_li.className = "gn_rightMenu_li";
					//rm_li.innerHTML = right_menu[j][0];
					rm_li.innerHTML = "<a href='" + right_menu[j][1]+ "'>" + right_menu[j][0] + "</a>";
					rm_li.over = false;
					rm_li.index = j;
					rm_li.id = "rm_link_" + i;	
					rm_li.onmouseover = function() { 
						this.over = true;
						this.className = "gn_rightMenu_li_o";
					}
					rm_li.onmouseout = function() { 
						this.over = false;
						this.className = "gn_rightMenu_li";
					}
					/*rm_li.onclick = function() { 
						loadLink(right_menu[this.index][1]); 

					}*/
					rm_ul.appendChild(rm_li);
				}
				menu_li.appendChild(rm_ul);
				
				//create iframe back-mask
				var rm_iframe = document.createElement('iframe');
				rm_iframe.className = "gn_rm_iframe";
				rm_iframe.id = "rm_iframe_" + i;
				// we need to set the source for this iframe so we don't get a secure/unsecure 
				// warning on https in IE6.
				if(IE) {
					rm_iframe.src = CDN_Url + "/images/clear.gif";
			    }	
				rm_iframe.frameBorder = 0;
				menu_li.appendChild(rm_iframe);
				
			}
			menu_ul.appendChild(menu_li);
		}
		
		//append menu
		m_hide.appendChild(menu_ul);
		
		//link win
		m_links.over = false;
		m_links.onmouseover = function() { this.over = true; }
		m_links.onmouseout = function() { this.over = false; }
		
		//set header mouse over ...
		m_header.over = false;
		m_header.onmouseover = function() {
			clearRollovers();
			clearRightMenu();
			this.over = true; 
		}
		
		m_header.onmouseout = function() { this.over = false; }
		
	}
	
	//get max height after div is hidden
	m_dropdown.style.visibility = "hidden";
	m_dropdown.style.display = "block";
	m_hide.style.display = "block";	
	this_max_h = document.getElementById("m_hide_" + num).offsetHeight;
	
	//set height to zero and div to visable before expand
	m_win.style.height = "0px";
	m_dropdown.style.visibility = "visible";
	m_dropdown.style.display = "block";
	
	//iframe
	m_iframe.style.display = "block";
	
	//set link div alpha to 0 before expanding
	alpha_counter = 0;
	setAlphaObj(m_links, 0);
	
	//expand menu & fade in
	clearTimeout(m_timer);
	m_counter = 0;
	expandMenu();
	
	//check for mouse out ...
	clearTimeout(check_timer);
	check_counter = 0;
	checkMouseOut();
	
}

function setAlphaObj(obj, opacity) {
	if(IE) {
		obj.style.filter = 'alpha(opacity=' + opacity + ')';
	}else {
		obj.style.opacity = obj.style.MozOpacity = obj.style.KhtmlOpacity = opacity / 100;
	}
}

function expandMenu() {
	
	var m_dropdown = document.getElementById("m_dropdown_" + current_num);
	var m_win = document.getElementById("m_win_" + current_num);
	var m_links = document.getElementById("m_links_" + current_num);	
	var m_iframe = document.getElementById("gn_iframe_" + current_num);
	
	if(m_counter < this_max_h && m_win) {
		
		//fade in
		alpha_counter += 20;
		if(alpha_counter <= 100) setAlphaObj(m_links, alpha_counter);
		
		m_win.style.overflow = "hidden";
		
		m_counter += 70;
		if(m_counter > this_max_h) m_counter = this_max_h;
		m_win.style.height = m_counter + "px";
		m_iframe.style.height = (m_counter+1) + "px";
		
		m_timer = setTimeout("expandMenu()", 32); 
		
	}else if(m_win) {
		
		setAlphaObj(m_links, 100);
		m_links.style.filter = "none"; //take off filter in IE to fix bug...
		m_win.style.overflow = "visible";
		
	}	
}

/* ##### CHECK FOR MOUSE OFF FUNCTION CURRENT MENUS FUNCTION ##### */

function checkMouseOut() {
	
	if(check_counter < delay_check_for) { //delay hide
		
		check_counter++;
		check_timer = setTimeout("checkMouseOut()", 34);
		 
	}else { //run check for if mouse is over any menu item
		
		var menu_header = document.getElementById("m_header_" + current_num);
		if(menu_header) {
			
			var m_links = document.getElementById("m_links_" + current_num);
			var m_hide = document.getElementById("m_hide_" + current_num);
			
			//check if mouse over header
			var mouseover = menu_header.over;
			if(m_links.over == true) mouseover = true;
			
			//check if mouse over links
			var menu_links = m_hide.getElementsByTagName('div');		
			if(menu_links) {
				for(var i = 0; i < menu_links.length; i++) {
					if(menu_links[i].over == true) mouseover = true;
				}
			}
			
			//check if right menu and mouse is over
			if(mouseover == false) {
				var right_menu = document.getElementById("gn_menuRight");
				if(right_menu && right_menu.over == true) mouseover = true;
			}
			
			if(mouseover == true) {
				check_counter = 0;
				check_timer = setTimeout("checkMouseOut()", 34);
			}else {
				hideMenus();	
			}
		}
		
	}
}

function promoBanner(thisCookie) {
	var thisPromo = this;
	var promoCookie = thisCookie;
	var promoCookieArr = promoCookie.split("_");
	var promotionInfo;
	var promotionCopy;
    var promotionButton;
    var promotionDelay = 0;
    var promotionTimeout = 0;
    var promotionWidth = 600;
    var promotionTop = 28;
    var _promotionTop = 31;
    var promotionLeft = -275;
    var _promotionLeft = -500;
    var promotionGroup = "A";
    var promotionShown = "";
    var promoCode;
    var promoNum;
    
    this.load = function() {
        var requestURL = "/pp/PB_" + promoNum + "_" + promoCode + "_" + promotionGroup + ".html";

        if (isPromoCDNActive) {
        	requestURL = CDN_Url + requestURL;
        }
        
       	try {
       		var loader = new contentLoader(requestURL, thisPromo.display, thisPromo.loadPromoErrorHandler, "GET", null);
        } catch (err) {
        	window.onerror(err);            
        }	
    };
    
    this.hidePromo = function() {
		promotionInfo.style.display = "none";
		promotionButton.style.display = "none";
    };

    this.showPromo = function() {
        promotionButton.style.display = "inline";
    };    

    this.togglePromo = function() {
		$('#promotion-copy').animate({"height": "toggle", "opacity": "toggle"}, 1600);
		$('#promotion-button').toggleClass('active');

		// set omniture tags if active
		if ($('#promotion-button').hasClass('active')) {
			cmCreatePageviewTag("PROMOTIONAL OFFER DROPDOWN");
			s_omtr.pageName="PROMOTIONAL OFFER DROPDOWN";
			s_omtr.channel="PROMOTIONAL OFFER DROPDOWN";
			s_omtr.eVar44= promoCode + "-_-VALID";
			var s_code=s_omtr.t();if(s_code)document.write(s_code);
		}		
		
		return false;
    };

    this.closePromo = function() {
    	if ($('#promotion-button').hasClass('active')) {
    		thisPromo.togglePromo();
    	}
    };
    
    this.display = function(request) {
    	// load the copy
    	if (promotionInfo != null) {
    		promotionInfo.innerHTML = promotionInfo.innerHTML + request.responseText;
    	} else {
    		return;
    	}
    	$('.promotion-hide-link').click(thisPromo.togglePromo);
    	
    	promotionCopy = document.getElementById("promotion-copy");
    	promotionButton = document.getElementById("promotion-button");
    	
    	// jquery has a bug when overwriting onclick in IE
    	if (promotionButton != null) {
    		promotionButton.onclick = thisPromo.togglePromo;
    	}

    	if (promotionCopy != null) {
    		promotionCopy.style.width = promotionWidth + "px";
 
    		if (IE6) {
    			promotionCopy.style.top = _promotionTop + "px";
    			promotionCopy.style.left = _promotionLeft + "px";
    		} else {
    			promotionCopy.style.left = promotionLeft + "px";
    			promotionCopy.style.top = promotionTop + "px";
    			promotionCopy.style._top = _promotionTop + "px";
    			promotionCopy.style._left = _promotionLeft + "px";    			
    		}
    	} 
    	
    	// show the banner
		thisPromo.showPromo();
		
		// if we've gotten here, it's time to show the promo
		thisPromo.togglePromo();
 
		// we automatically opened the promo so we we better close it too
		if (promotionDelay != 0 && promotionTimeout != 0) {
			setTimeout(thisPromo.closePromo, promotionTimeout);
		}
    };
    
    this.setShownCookie = function() {
   		setCookie("promoCookieKey", promoCode + "_" + promoNum + "_shown" , null);
    };
    
    this.loadPromoErrorHandler = function(){
//    	alert("an error occured");
    };

    this.init = function() {
        promoCode = promoCookieArr[0];
        promoNum = promoCookieArr[1];	
        promotionShown = promoCookieArr[2];       
    	promotionInfo = document.getElementById("promotional_info");
    	var location = "" + window.location;
    	
        if (window.promotionGroup) {
        	promotionGroup = window.promotionGroup;
        }
    	
    	if (promotionInfo != null) {
    		promotionInfo.style.display = "block";
    	}
    	
    	if (window.promotionTimeout || window.promotionTimeout == 0) {
    		promotionTimeout = window.promotionTimeout;
    	}

    	if (window.promotionDelay || window.promotionDelay == 0) {
    		promotionDelay = window.promotionDelay;
    	}       	
    	
    	if (window.promotionWidth) {
    		promotionWidth = window.promotionWidth;
    	}

    	if (window.promotionLeft) {
    		promotionLeft = window.promotionLeft;
    	}      	    	
    	
    	if (window._promotionLeft) {
    		_promotionLeft = window._promotionLeft;
    	} 
    	
    	if (window.promotionTop) {
    		promotionTop = window.promotionTop;
    	}      	    	
    	
    	if (window._promotionTop) {
    		_promotionTop = window._promotionTop;
    	}    	
    	
	    // load the banner if we haven't shown it already, we're not on Apply Promo, and we're supposed to automatically load it
	    if (promotionShown == undefined && location.indexOf("ApplyPromo.cgi") == -1 && window.promotionDelay && window.promotionDelay != 0) {
	    	this.load();
	    } else if (promotionShown == undefined && location.indexOf("BillTo.cgi") != -1) {
	    	// always load the promotion if we're on BillTo and we haven't shown it before
	    	this.load();
	    } else {
	    	// otherwise, load it only onclick
	    	$('#promotion-button').click(this.load);
	    }
	    
	    // we've gone through all of the auto-show logic now turn this off
	    if (promotionShown == undefined && location.indexOf("ApplyPromo.cgi") == -1) {
    		this.setShownCookie();
	    }
    }
    
    this.init();
}





