//Array of sku and upc pairs
var skuupc= new Array(
	'2', '381519035340',
	'4', '381519022067',
	'4G', '381519038419',
	'4R', '381519035333',
	'5', '381519022043',
	'5A', '381519022036',
	'5G', '381519022050',
	'5R', '381519038396',
	'6.5A', '381519035357',
	'6', '381519022029',
	'6A', '381519022012',
	'6G', '381519022005',
	'6R', '381519022074',
	'6WN', '381519038426',
	'7.5A', '381519021961',
	'7', '381519021992',
	'8.5A', '381519030673',
	'8', '381519021978',
	'8G', '381519021985',
	'8RB', '381519035326',
	'9', '381519035319',
	'9A', '381519021954',
	'9G', '381519038433',
	'10', '381519021947'
);


//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;
		}
		
		if ($("BVCustomerRatings")) {
			$("BVCustomerRatings").addEvent('mouseenter', function() {
				prepareReviews();
			});
		}
		
		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');
			//ratings
			if ($("BVCustomerRatings")) {
				$('BVSVPLinkContainer').getFirst("a").href=bvRootURL+sku2upc[this.options.sku]+"/reviews.htm";
				$('BVFrame').src = bvRootURL+sku2upc[this.options.sku]+'/reviews.htm?format=embedded';
				$("BVCustomerRatings").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");
			});	
			el.addEvent('mouseleave', function() {
				if (lastHref) {
					sp.toggleInfo(lastHref);
				}
			});	
		}, this);
	},
	
	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.options.href = $(this.options.color).getFirst("div").getFirst("div").getFirst("a").href;

		this.prepareTabs();
		sp.toggleInfo(this.options.href);
	},

	togglePreview: function(href) {
		if (this.options.sku) {
			//info
			$("Info_"+this.options.sku).addClass('hidden');
		}
		if ($("BVCustomerRatings")) $("BVCustomerRatings").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 ($("BVCustomerRatings")) $("BVCustomerRatings").addClass('hidden');
		
		if (href) {
			this.options.sku = this.getQueryString(href).sku;
			$('BVSVPLinkContainer').getFirst("a").href=bvRootURL+sku2upc[this.options.sku]+"/reviews.htm";
			$('BVFrame').src = bvRootURL+sku2upc[this.options.sku]+'/reviews.htm?format=embedded';

			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];

				//Rewrite submission url to include this color and upc
				var loc=window.location;
				returnQuery='color='+this.options.color+'&upc='+sku2upc[this.options.sku];
				returnURL=loc.protocol+'//'+loc.host+loc.pathname+"?"+returnQuery;
			}
			prepareReviews();

			//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');
			//ratings
			$("BVCustomerRatings").removeClass('hidden');
		}
	}
});

function prepareReviews() {
	if ($("BVSummaryReadReviewsLink")) {
		$("BVSummaryReadReviewsLink").removeEvents();
		$("BVSummaryReadReviewsLink").addEvent('click', function(el) {
			new Event(el).stop();
			$("CReviewsContainer").setStyle('display', 'block');
		});
	}
	if ($("BVSubmissionURL")) {
		$("BVSubmissionURL").href = $("BVSubmissionURL").href.replace(/([\?&]return=)[^&]*/, "$1"+escape(returnURL))+"&"+returnQuery;
	}
}

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();
});

