<!-- START -- Scripts from /common/javascript/ -->

/* 
	Script: base.zlib.js

	@title: Zappos Javascript Library - Base
	@desc: Required setup for the Zappos Javascript Library
*/

zlib = new Object();

/*
Properties:
	window.ie - will be set to true if the current browser is internet explorer (any).
	window.ie6 - will be set to true if the current browser is internet explorer 6.
	window.ie7 - will be set to true if the current browser is internet explorer 7.
	window.khtml - will be set to true if the current browser is Safari/Konqueror.
	window.gecko - will be set to true if the current browser is Mozilla/Gecko.
*/
if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
else if (document.childNodes && !document.all && !navigator.taintEnabled) window.khtml = true;
else if (document.getBoxObjectFor != null) window.gecko = true;

//enables background image cache for internet explorer 6 (removes IE flicker bug)
if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch (e){};



/* 
	onDomReady.zlib.js

	@title: Zappos Javascript Library - onDomReady
	@desc: This is an alternative to window.onload, that does not wait for images to load.
*/

/*
	Override this function in your application instead of window.onload()
*/
zlib.onDomReady = function () {}

/* --- Browser OnDomReady Config --- */
zlib.init = function () {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    if (_timer) clearInterval(_timer);

		zlib.onDomReady(); //define this function in your application instead of window.onload()
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", zlib.init, false);
}

/* for Internet Explorer */



/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            zlib.init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = zlib.init;
/* --- End Browser OnReady Config --- */



/* 
	dom.zlib.js

	@title: Zappos Javascript Library - DOM
	@desc: These are commonly used Javascript functions relating to the DOM.
*/

/*
	@type: zlib.Function
	@use: getElementsByClassName(document, '*', 'do-something') => returns a list of all elements with the class of 'do-something'
*/
zlib.getElementsByClassName = function (oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements);
}

/*
	@type: function
	@use: $('header') => the element with the id of 'header'
*/
var $ = $ || function (el) {
	return document.getElementById(el);
}

/*
	@type: function
	@use: Short hand of zlib.getElementsByClassName(document, '*', <el>)
*/
var $c = $c || function (el) {
	return zlib.getElementsByClassName(document, '*', el);
}

/*
	@type: method (Array)
	@use: Loop through all elements of an Array, calling a function on each item.
*/
Array.prototype.each = Array.prototype.each || function(fn, bind){
	for (var i = 0; i < this.length; i++) fn.call(bind, this[i], i, this);
};



/* 
	Script: common.zlib.js

	@title: Zappos Javascript Library - Common
	@desc: Holds functions that are common to many applications.
*/

/*
	@type: zlib.Function
	@use: Creates the Overnight Shipping popup with the given url, or the default Zappos one.
*/
zlib.overNightShipping = function (uri) {
	var uri = (uri == null) ? "http://zappos.com/overnightpop.html" : uri;
	
	zlib.popup(uri, 400, 640, 'overnight_ship');
	return false;
}

/*
	@type: zlib.Function
	@use: Creates a popup and focuses on it.
*/
zlib.popup = function (uri, width, height, name) {
	var name = (name == null) ? "zappos_window" : name;
	
	var newWindow = window.open(uri, name, "height=" + height + ",width=" + width + ",status=no,toolbar=no,menubar=no,scrollbars=yes,location=no")
	newWindow.focus();
	return newWindow;
}

/*
	@type: zlib.Function
	@use: Reads the cookie with the given name.
*/
zlib.readCookies = function (name) {
	var cname = name + "=";
	var ca = document.cookie.split(';');
	
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(cname) == 0) return c.substring(cname.length, c.length);
	}
}

/*
	@type: zlib.Function
	@use: Returns the number of items that are in the user's shopping cart.
*/
zlib.getItemsInCart = function () {
	var items = zlib.readCookies('scitems');
	if (items) return items;
	return 0;
}

/*
	@type: zlib.Function
	@use: Appends an element with the cart count to all cart-with-count classed elements.
	@dependencies: Dom.zlib.js
*/
zlib.addCartCounts = function (options) {
	var options = (options == null) ? {} : options;
	options['className'] = options['className'] || 'cart-with-count';
	options['element'] = options['element'] || 'span';
	options['beforeCount'] = options['beforeCount'] || ' (';
	options['afterCount'] = options['afterCount'] || ')';
	options['withItems'] = options['withItems'] || false;
	
	var carts = $c(options['className']);
	var items = zlib.getItemsInCart();
	var text = "";
	if (options['withItems']) {
		var ti = (items == 1) ? ' item' : ' items';
		text = options['beforeCount'] + items + ti + options['afterCount'];
	} else {
		text = options['beforeCount'] + items + options['afterCount'];
	}

	for (var i = 0; i < carts.length; i++) {
		var cartSpan = document.createElement(options['element']);
		cartSpan.appendChild(document.createTextNode(text));
		carts[i].appendChild(cartSpan);
	}
}


  zlib.onDomReady = function () {
    $c('overnship').each(function (el) { 
      el.onclick = function () { return zlib.overNightShipping(); }
    });
  }



<!-- END -- Scripts from /common/javascript/ -->




<!-- START -- Category Page Gender Tab script -->

/*  
    Highlights active tab by changing css class to 'tab selected' and shows active area by making css class visible

    tab ids format = name+2digitnumber
    area ids format = name+2digitnumber 

    (tab and area group numbers should increment by 1)

    tab = the id of the tab to highlight
    area = the id of the area to show
    begincls = beginning range of id's to unhighlight 
    endcls = end of range of id's to unhighlight 
*/

/* hide old tab script

function showtab(tab,area,begincls,endcls){ 


	//Tabs
	//Extract number and name from id 

	var tabnumber = tab.substr(tab.length -2);
	var tabname = tab.substr(0, tab.length-2);	

	for(x = begincls; x <= endcls; x++){
		var el = document.getElementById(tabname+x);
		  if(tabnumber == x){
		  	el.className = "tab selected"
		  }else{
		  	el.className = "tab"
		  }
	}


	//Area
	//Extract number and name from id 
	
	var areanumber = area.substr(area.length -2);
	var areaname = area.substr(0, area.length-2);	

	for(x = begincls; x <= endcls; x++){
	  if(areanumber == x){
		eval("document.getElementById('"+areaname+x+"').style.display='block'");
	  }else{
	   eval("document.getElementById('"+areaname+x+"').style.display='none'");
	  }
	}
} 

*/

 //New Tab Script

window.onDomReady(function () {
  $$('.category-content').each(function (el) {
    if (el.id) tabMe(parseInt(el.id.split('-')[1]));
  });
  
  $$('.tabs').getElements('li a').each(function (el) {
    el.addEvent('mouseenter', function () { this.addClass('hovering'); });
    el.addEvent('mouseleave', function () { this.removeClass('hovering'); });
  });
});

TAB_NAMES = {
  'mens': "Men's",
  'womens': "Women's",
  'boys': "Boys'",
  'girls': "Girls'",
  'shades': "Shades",
  'goggles': "Goggles"
}

function tabMe (id) {
  var dds = $('category-' + id).getElements('dd');
  var tab_keys = [];
  var tabs = [];
  
  dds.each(function (dd) { tab_keys = tab_keys.include(dd.className); });
  
  var ul_element = new Element('ul', {
    'id': ("tabs-" + id),
    'class': 'tabs'
  });
    
  tab_keys.each(function (tab_key) {
    var li = new Element('li');
//    var a = new Element('a').setText(TAB_NAMES[tab_key]);
    var a = document.createElement('a');
    var text = document.createTextNode(TAB_NAMES[tab_key]);
    a.appendChild(text);
    a.onclick = function () { tabClicked(id, tab_key); }
    
    li.adopt(a);
    ul_element.adopt(li);
  });
  ul_element.getFirst().addClass("active");
  ul_element.getLast().addClass("last");
  
  var keep_this = $('category-' + id).clone();
  var parent = $('category-' + id).getParent();
  parent.empty();
  parent.adopt(ul_element);
  parent.adopt(keep_this);
  
  keep_this.getChildren().each(function (el) { el.style.display = 'none'; });
  keep_this.getFirst().getNext().style.display = 'block';
}

function tabClicked (id, key) {
  var main = $('category-' + id);
  main.getChildren().each(function (el) { el.style.display = 'none'; });
  main.getElement('.' + key).style.display = 'block';
  
  var tabs = $('tabs-' + id);
  tabs.getElements('li').each(function (el) { el.removeClass('active'); });
  tabs.getElements('li a').each(function (el) {
    if (el.firstChild.nodeValue == TAB_NAMES[key])
      el.parentNode.addClass('active');
  });
  return false;
}


<!-- END -- Category Page Gender Tab script -->


<!-- START -- Definitions -->

    self.name = 'mainwindow';

    function PopWindowDefinitions() {
    window.open('http://zappos.com/definitions.html','popup','width=500,height=600,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes,top=100,left=0');
    }


<!-- END -- Definitions -->


<!-- START -- Old Scripts -->

/* ** IE Flicker Bug Fix ** */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
/* **** */

function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
		application_init();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

/* --- End Browser OnReady Config --- */


function application_init () {
	var oships = getElementsByClassName(document, "*", 'overnship');
	for (var i = 0; i < oships.length; i++) {
		oships[i].onclick = function () {
			overnship();
			return false;
		}
	}
	
	var carts = getElementsByClassName(document, "*", 'cart-with-count');
	var items = getItems();
	for (var i = 0; i < carts.length; i++) {
		var cartSpan = document.createElement('span');
		cartSpan.appendChild(document.createTextNode(" (" + items + ")"));
		carts[i].appendChild(cartSpan);
	}
}


function overnship() {
	var overnight = window.open("http://zappos.com/overnightpop.html",'overnight_ship',"height=640,width=400,status=no,toolbar=no,menubar=no,scrollbars=yes,location=no, top=150, left=0");       
	overnight.focus();      
}

/* For Shopping Cart */


function getItems() {
	var items = readCookies('scitems');
	if (items) {
		return items;
	}
	return 0;
}

function readCookies(name) {
    var cname = name + "=";
    var ca = document.cookie.split(';');

    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(cname) == 0) return c.substring(cname.length,c.length) ;
    }
}

/* Application Functions */

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


<!-- END -- Old Scripts -->
