//Array of sku and upc pairs
var skuupc= new Array(
	'35', '381519032356',
	'36', '381519003134',
	'38', '381519003141',
	'10', '381519002915',
	'02', '381519003035',
	'04', '381519003158',
	'05', '381519038211',
	'06', '381519003165',
	'07', '381519040610',
	'08', '381519038235',
	'09', '381519038242',
	'7C', '381519027994',
	'7R', '381519003172',
	'8C', '381519027987',
	'9C', '381519038204',
	'9G', '381519032325',
	'9N', '381519003202',
	'12', '381519002939',
	'13', '381519002953',
	'14', '381519002984',
	'16', '381519003004',
	'18', '381519003028',
	'20', '381519003042',
	'22', '381519003066',
	'24', '381519003073',
	'26', '381519003080',
	'28', '381519003097',
	'30', '381519003110',
	'32', '381519003127',
	'11G', '381519002922',
	'12A', '381519002946',
	'13B', '381519002977',
	'16RG', '381519003011',
	'18RG', '381519032332',
	'20B', '381519003059',
	'20RB', '381519034886',
	'27A', '381519034893',
	'28B', '381519003103',
	'28G', '381519032349',
	'5C', '381519028014',
	'6.5C', '381519038198',
	'6C', '381519028007',
	'15RG', '381519032448',
	'20R', '381519034879'
);


//Creat objects to convert sku to upc and upc 2 sku 
var sku2upc= new Object();
var upc2sku= new Object();
for (i=0; i<=skuupc.length; i=i+2) {
	sku2upc[skuupc[i]]=skuupc[i+1];
	upc2sku[skuupc[i+1]]=skuupc[i];
};
var lastHref;
var lastThumb;
var returnQuery;
var returnURL;

var ShadePalette = new Class({
	/**
	 * implements
	 */
	Implements: [Options],
	
	defaultOptions: {
		color: 'blonde'
	},
		
	initialize: function(options) {
		this.setOptions(this.defaultOptions, options, this.getQueryString(location.href));
		
		//this.options.href = $(this.options.color).getFirst("div").getFirst("div").getFirst("a").href;

		if (this.getQueryString(location.href).upc) {
			this.options.sku = upc2sku[this.getQueryString(location.href).upc];
		} else if (this.getQueryString(location.href).bvproductid) {
			this.options.sku = upc2sku[this.getQueryString(location.href).bvproductid];
		} else if (this.getQueryString(location.href).sku) {
			this.options.sku = this.getQueryString(location.href).sku;
		}
		
		this.prepareTabs();				
		this.prepareThumbs();
		
		//show default color
		$(this.options.color).removeClass('hidden');

		if (this.options.sku || this.options.upc) {
			//large preview swatch
			$("Info_"+this.options.sku).getFirst("img").addClass('hidden');
			//box
			$("Info_"+this.options.sku).getFirst("img").getNext("img").removeClass('hidden');
			//swatch and buy now
			$("Info_"+this.options.sku).getFirst("div").removeClass('hidden');
			//info
			$("Info_"+this.options.sku).removeClass('hidden');
			
			//Highlight thumb
			$$(".thumb").each(function(el) {
				if (this.getQueryString(el.href).sku == this.options.sku) {
					el.getFirst("img").addClass('on');
					lastHref = el.href;
					lastThumb = el.getFirst("img");
				}
			}, this);
		}
	},
	
	getQueryString: function(uri) {
		var o = {};
		
		$A(uri.replace(/(^.*\?)|(#.*$)/g,'').split('&')).each(function(p){
			p = p.split("=");
			o[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
		});
		
		return o;	
	},
	
	prepareTabs: function() {
		$$("ul#tabs li.tab a").each(function(el) {
			el.removeEvents();
			if (this.getQueryString(el.href).color == this.options.color) {
				el.getParent('li').addClass('on');
				this.prepareThumbs();
			} else {
				el.getParent('li').removeClass('on');
				el.addEvent('click', function() {
					if (lastThumb) {
						lastThumb.removeClass('on');
					}
					lastHref = "";
					sp.toggleTabs(this.href);
				});
			}
		}, this);
	},
	
	 prepareThumbs: function() {
		$$(".thumb").each(function(el) {
			el.removeEvents();
			el.addEvent('mouseenter', function() {
				sp.togglePreview(this.href);
			});
			el.addEvent('click', function() {
				if (lastThumb) {
					lastThumb.removeClass('on');
				}
				this.getFirst("img").addClass('on');
				$$('div#cii-store').addClass('hide');
				sp.toggleInfo(this.href);
				lastHref = this.href;
				lastThumb = this.getFirst("img");
				window.location.href="#top";
			});	
			el.addEvent('mouseleave', function() {
				if (lastHref) {
					sp.toggleInfo(lastHref);
				}
			});	
		});
	},
	
	toggleTabs: function(href) {
		$(this.options.color).addClass('hidden');
		this.options.lastColor = this.options.color;
		this.options.color = this.getQueryString(href).color;
		$(this.options.color).removeClass('hidden');
		this.prepareTabs();
		sp.toggleInfo(this.options.href);
	},

	togglePreview: function(href) {
		if (this.options.sku) {
			//info
			$("Info_"+this.options.sku).addClass('hidden');
		}
		this.options.sku = this.getQueryString(href).sku;
		//large preview swatch
		$("Info_"+this.options.sku).getFirst("img").removeClass('hidden');
		//box
		$("Info_"+this.options.sku).getFirst("img").getNext("img").addClass('hidden');
		//swatch and buy now
		$("Info_"+this.options.sku).getFirst("div").addClass('hidden');
		//info
		$("Info_"+this.options.sku).removeClass('hidden');
	},
	
	toggleInfo: function(href) {
		if ($("Info_"+this.options.sku)) $("Info_"+this.options.sku).addClass('hidden');
		
		if (href) {
			this.options.sku = this.getQueryString(href).sku;

			if (!lastHref || lastHref!=href) {
				$('cii-iframe').set('src', '/buy_it_now/product_links_bv.jsp?upc='+sku2upc[this.options.sku]);
				$('upc').value = sku2upc[this.options.sku];
			}

			//large preview swatch
			$("Info_"+this.options.sku).getFirst("img").addClass('hidden');
			//box
			$("Info_"+this.options.sku).getFirst("img").getNext("img").removeClass('hidden');
			//swatch and buy now
			$("Info_"+this.options.sku).getFirst("div").removeClass('hidden');
			//info
			$("Info_"+this.options.sku).removeClass('hidden');
		}
	}
});

function showBuyNow(el) {
	$$('div#cii-store').removeClass('hide');
	var zip = document.getElementById('zip');
	if(zip.value == "" || zip.value == undefined || zip.value == " ") {
		zip.value = "Enter Zip Code";
	}
	//urchinTracker(this.href);
	return true;
}

function checkZipField() {
	var el = document.getElementById('zip');
	el.onfocus = function() { if(this.value == "Enter Zip Code"){this.value = "";}}
}

function validateZipField() {
	var el = document.getElementById('zipcodeform');
	el.onsubmit = function() 
	{ 
		var el = document.getElementById('zip');
		var num = Number(el.value);
		if(el.value == "Enter Zip Code" || isNaN(num))
		{
			el.value = "Enter Zip Code";
			return false;
		} else {
			return true;
		}
	}
}

/*
Clear default value of text input
*/
function clearField(thefield) {
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}


var sp;
window.addEvent('domready', function() {
	sp = new ShadePalette();
});

