var ResultsPageSizeDimensionListener = {
	initialize : function() {
		if(window.location.pathname.match(/Results.aspx/i)) {
			//this.clearSizeWidthCookie();
			var sizeSelectorArray;
			var multiselect = false;
			var widthSelectorArray;			
			
			//alert($$('.size .sel a').size());
			if ($$('.size .sel a').size())
			{				
				// Here we know that we are in multiselect mode - lets see if there is more than one - if there is only one, we are going to try
				// to set the size to it.
				multiselect = true;
				sizeSelectorArray = $$('.size .sel a');									
			}
			else
			{
				// This is the single select control
				sizeSelectorArray = $$('#sortbar-size select');
			}
			
			if ($$('.widths .sel a').size())
			{
				// We are in multiselect mode
				multiselect = true;								
				widthSelectorArray = $$('.widths .sel a');
			}
			else
			{
				// This is the single select control
				widthSelectorArray = $$('#sortbar-width select');
			}

			//var multiSelectSizeArray = $$('.size .sel a');
			//var sizeSelectorArray = $$('#sortbar-size select');			
			//var widthSelectorArray = $$('#sortbar-width select');
			var colorSelectorArray = $$('#sortbar-color select');
			var departmentLabelArray = $$('div.nav-department div.nav-selected div');
            var categoryLabelArray = $$('div.nav-category div.nav-selected div');
            
            //debugger;
            this.sizeValue = null;
            this.widthValue = null;
            this.colorValue = null;
            this.departmentValue = null;
            this.catergoryValue = null;
            
            this.nSizeValue = null;
            this.nWidthValue = null;
            this.nColorValue = null;
            this.nDepartmentValue = null;
            this.nCategoryValue = null;
            
            this.loadGenderSizeCookie();
			// if this is the search results page and the size dropdown exists
			if(sizeSelectorArray.size()) {
				var sizeSelector = sizeSelectorArray.first();
				var widthSelector = widthSelectorArray.first();
				var colorSelector = colorSelectorArray.first();
				var departmentLabel = departmentLabelArray.first();
				var categoryLabel = categoryLabelArray.first();
				
				var department;
			    var departmentNValueURL;
			    if(departmentLabel) {
			        department = this.getText(departmentLabel);
			        department=department.replace(/^\s+|\s+$/g,'');
				    departmentNValueURL = department;
			    }
			    
			    var category;
			    var categoryNValueURL;
			    if(categoryLabel) {
			        category = this.getText(categoryLabel);
			        category=category.replace(/^\s+|\s+$/g,'');
                    category = category.split('\n')[0]; //.match(/^[A-Za-z]+$/g);                    				    
				    categoryNValueURL = category;
			    }
			    
			    var size;				
                var sizeNValueURL;
                if (!multiselect)
                {
                if((sizeSelector) && (!sizeSelector.childElements()[0].value.match(/^Choose one$/))) {
                    size = sizeSelector.options[sizeSelector.selectedIndex].text;
                    sizeNValueURL = sizeSelector.options[sizeSelector.selectedIndex].value;
			    }
			    }
			    else
			    {
					//alert(sizeSelector.innerHTML);
					if((sizeSelector) && sizeSelector.innerHTML.length != null)
					{
						size = sizeSelector.innerHTML;
						sizeNValueURL = sizeSelector.href;
					}
			    }
				
			    var width;
			    var widthNValueURL;
			    if (!multiselect)
			    {
                if((widthSelector) && (!widthSelector.childElements()[0].value.match(/^Choose one$/))) {
				    width = widthSelector.options[widthSelector.selectedIndex].text;
				    widthNValueURL = widthSelector.options[widthSelector.selectedIndex].value;
			    }
				}
				else
				{
					if ((widthSelector) && widthSelector.innerHTML.length)
					{
						width = widthSelector.innerHTML;
						widthNValueURL = widthSelector.href;
					}
				}
				
				var color;
			    var colorNValueURL;
                if((colorSelector) && (!colorSelector.childElements()[0].value.match(/^Choose one$/))) {
				    color = colorSelector.options[colorSelector.selectedIndex].text;
				    colorNValueURL = colorSelector.options[colorSelector.selectedIndex].value;
			    }
			    
				//debugger;
                //Populat ResultsTopNav with cookie values, if the user did not just 
                //choose values from the Choose You Shoes control.
				if (!multiselect)
				{
				if((sizeSelector.childElements()[0].value != "Choose One" && !sizeSelector.childElements()[0].value.match(/^Choose one$/) && !sizeSelector.childElements()[0].value == "")){
				    this.dropSizeWidthCookie(size, width, color, department, category);
				    this.dropSizeWidthNValueCookie(sizeNValueURL, widthNValueURL, colorNValueURL, departmentNValueURL, categoryNValueURL);
				}
			  }
				else
				{					
					if (sizeSelector)
					{
						this.dropSizeWidthCookie(size, width, color, department, category);
						this.dropSizeWidthNValueCookie(sizeNValueURL, widthNValueURL, colorNValueURL, departmentNValueURL, categoryNValueURL);					
					}				
				}
			  }
		}
	},
	
	 getText : function(el)
        {
        if (el.textContent) return el.textContent;
        if (el.innerText) return el.innerText;
        },

    //Added for the ResultsTopNav population.
    loadGenderSizeCookie : function() {
		var start = document.cookie.indexOf('nSizeWidth=');
		if(start != -1) {
			var end = document.cookie.indexOf(';', start);
			var cookiedValue = document.cookie.substring(start, end);
			var cookiedValues = cookiedValue.replace('nSizeWidth=','').split('|');

			this.nSizeValue = cookiedValues[0];
			this.nWidthValue = cookiedValues[1];
			this.nColorValue = cookiedValues[2];
			this.nDepartmentValue = cookiedValues[3];
			this.nCategoryValue = cookiedValues[4];
		}
		
		var start = document.cookie.indexOf('rysSizeWidth=');

		if(start != -1) {
			var end = document.cookie.indexOf(';', start);
			var cookiedValue = document.cookie.substring(start, end);
			var cookiedValues = cookiedValue.replace('rysSizeWidth=','').split('|');
            
            this.sizeValue = cookiedValues[0];
			this.widthValue = cookiedValues[1];
			this.colorValue = cookiedValues[2];
			this.departmentValue = cookiedValues[3];
			this.categoryValue = cookiedValues[4];
		}
		
	},
	
	/*setTopNavSize : function() {
		var detailsSize = $$('#sortbar-size select');
		if( detailsSize.size() ) {
			var sizeRegex = (this.sizeValue) ? new RegExp('\\b' + this.sizeValue + '\\b') : null;

			// set selected
			var findDesiredSizeHandler = this.findDesiredSize.bind(this, sizeRegex);

			var desiredSize = detailsSize.first().childElements().find(findDesiredSizeHandler);

			if(desiredSize) {
				detailsSize.first().selectedIndex = detailsSize.first().childElements().indexOf(desiredSize);
			}
		}
	},

	findDesiredSize : function(sizeRegex, elt) {
		var found = false;
		found = Boolean(elt.text.match(sizeRegex));
		return found;
	},*/ 
	
    setCookie: function(c_name,value,expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +value+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+ ";path=/";
    },
    //End Add
    
	clearSizeWidthCookie : function() {
		if(document.cookie.indexOf('rysSizeWidth=') != -1) {
			this.setCookie('rysSizeWidth', '', 30);
		}
	},

	dropSizeWidthCookie : function(size, width, color, department, category) {
		if(!size) return;
		if(size != undefined && size != null){
		    this.setCookie('rysSizeWidth', ((size) ? size : '') + '|' + ((width) ? width : '') + '|' + ((color) ? color : '') + '|' + ((department) ? department : '') + '|' + ((category) ? category : ''), 30);
		}
	},
	
	dropSizeWidthNValueCookie : function(sizeNValueURL, widthNValueURL, colorNValueURL, departmentNValueURL, categoryNValueURL) {
		if(!sizeNValueURL) return;
		if(sizeNValueURL != undefined && sizeNValueURL != null){
		    this.setCookie('nSizeWidth', ((sizeNValueURL) ? sizeNValueURL : '') + '|' + ((widthNValueURL) ? widthNValueURL : '') + '|' + ((colorNValueURL) ? colorNValueURL : '') + '|' + ((departmentNValueURL) ? departmentNValueURL : '') + '|' + ((categoryNValueURL) ? categoryNValueURL : ''), 30);
		}
	}
};

var ProductDetailsPageSizePreSelector = {
	initialize : function() {
		this.sizeValue = null;
		this.widthValue = null;
		this.departmentValue = null;
        this.colorValue = null;
		this.categoryValue = null;
			
		this.loadGenderSizeCookie();


        var currentURL = window.location.toString();     
        
        var departmentLabelArray = $$('div.rightCol-top div.style-price h1');
        var departmentLabel = departmentLabelArray.first();
        var department;
        if(departmentLabel) {
	        department = departmentLabel.innerHTML.replace(/^\s+|\s+$/g,'');
	        if(department.indexOf("Men") > 0){
	            department = "Mens";
	        }else if(department.indexOf("Women") > 0){
	            department = "Womens";
	        }else if(currentURL.toLowerCase().indexOf("naturalizer") != -1){
	            department = "Womens";
	        }else{
	            department = "Kids";
	        }
        }
		//if(window.location.pathname.match(/ProductDetails.aspx/i) && document.referrer.match(/Results.aspx/i) && this.sizeValue) {
		if(window.location.pathname.match(/ProductDetails.aspx/i) && this.sizeValue) {
		  if(this.departmentValue == department || ((this.departmentValue == "Boys" || this.departmentValue == "Girls") && department == "Kids")){
			    this.setProductDetailsSize();
		  }
		}
	},

	abbreviateWidth : function() {
		var pattern;

		// is ugly because our application is so poorly cobbled together
		if(this.departmentValue.match(/^Womens$/i)) {
			if(this.widthValue.match(/^Extra Narrow/i) || this.widthValue.match(/^X narrow/i)) {
				pattern = '\\b(4A|2S|Quad)\\b';
			}
			else if(this.widthValue.match(/^Narrow/i)) {
				pattern = '\\b(3A|2A|N|Slim)\\b';
			}
			else if(this.widthValue.match(/^Wide/i)) {
				pattern = '\\b(C|W)\\b';
			}
			else if(this.widthValue.match(/^Extra Wide/i) || this.widthValue.match(/^X wide/i)) {
				pattern = '\\b(D|2W|W)\\b';
			}
			else if(this.widthValue.match(/^Extra Extra Wide/i) || this.widthValue.match(/^XX wide/i)) {
				pattern = '\\b(E|2E|3E|4E|3W)\\b';
			}
			else { //if(this.widthValue.match(/^Medium/i)) {
				pattern = '\\b(B|M)\\b';
			}
		}
		else if(this.departmentValue.match(/^Mens$/i)) {
			if(this.widthValue.match(/^Extra Narrow/i) || this.widthValue.match(/^X narrow/i)) {
				pattern = '\\b(2A|A)\\b';
			}
			else if(this.widthValue.match(/^Narrow/i)) {
				pattern = '\\b(B|N)\\b';
			}
			else if(this.widthValue.match(/^Wide/i)) {
				pattern = '\\b(E|2E|W)\\b';
			}
			else if(this.widthValue.match(/^Extra Wide/i) || this.widthValue.match(/^X wide/i)) {
				pattern = '\\b(3E|4E|2W)\\b';
			}
			else if(this.widthValue.match(/^Extra Extra Wide/i) || this.widthValue.match(/^XX wide/i)) {
				pattern = '\\b(5E|6E|3W)\\b';
			}
			else { //if(this.widthValue.match(/^Medium/i)) {
				pattern = '\\b(D|M)\\b';
			}
		}
		else if(this.departmentValue.match(/^Boys$/i) || this.departmentValue.match(/^Girls$/i)) {
			if(this.widthValue.match(/^Extra Narrow/i) || this.widthValue.match(/^X narrow/i)) {
				pattern = '\\b()\\b';
			}
			else if(this.widthValue.match(/^Narrow/i)) {
				pattern = '\\b(N)\\b';
			}
			else if(this.widthValue.match(/^Wide/i)) {
				pattern = '\\b(D|E|W)\\b';
			}
			else if(this.widthValue.match(/^Extra Wide/i) || this.widthValue.match(/^X wide/i)) {
				pattern = '\\b()\\b';
			}
			else if(this.widthValue.match(/^Extra Extra Wide/i) || this.widthValue.match(/^XX wide/i)) {
				pattern = '\\b(3E|XXW|2W)\\b';
			}
			else { //if(this.widthValue.match(/^Medium/i)) {
				pattern = '\\b(M)\\b';
			}
		}
		return new RegExp(pattern);
	},

	loadGenderSizeCookie : function() {
		var start = document.cookie.indexOf('rysSizeWidth=');

		if(start != -1) {
			var end = document.cookie.indexOf(';', start);
			var cookiedValue = document.cookie.substring(start, end);
			var cookiedValues = cookiedValue.replace('rysSizeWidth=','').split('|');

			this.sizeValue = cookiedValues[0];
			this.widthValue = cookiedValues[1];
			this.colorValue = cookiedValues[2];
			this.departmentValue = cookiedValues[3];
			this.categoryValue = cookiedValues[4];
		}
	},

	setProductDetailsSize : function() {
		var detailsSize = $$('div.chooseBox select.SizeWidth');
		if( detailsSize.size() ) {
			var sizeRegex = (this.sizeValue) ? new RegExp('\\b' + this.sizeValue + '\\b') : null;
			var widthRegex = this.abbreviateWidth();
			// set selected
			var findDesiredSizeHandler = this.findDesiredSize.bind(this, sizeRegex, widthRegex);

			var desiredSize = detailsSize.first().childElements().find(findDesiredSizeHandler);

			if(desiredSize) {
				detailsSize.first().selectedIndex = detailsSize.first().childElements().indexOf(desiredSize);
			}
		}
	},

	findDesiredSize : function(sizeRegex, widthRegex, elt) {
		var found = false;
		if(this.widthValue) {
			found = Boolean(elt.text.match(sizeRegex));
			found = found && Boolean(elt.text.match(widthRegex));
		} else {
			found = Boolean(elt.text.match(sizeRegex));
		}

		return found;
	}
};

Event.observe(window, 'load', ResultsPageSizeDimensionListener.initialize.bind(ResultsPageSizeDimensionListener));
Event.observe(window, 'load', ProductDetailsPageSizePreSelector.initialize.bind(ProductDetailsPageSizePreSelector));

