
var product = {
	properties: {},
	propIdx: 1,
	
	setSku: function(sku, init) {
		var init = init || false;
		for ( var i = 1; i <= 7; ++i ) {
			if ( this.properties['ProductSKU' + i] == sku ) {
				this.setIdx(i, init);
				return i;
			}
		}
		return 0;
	},
	
	setIdx: function(idx, init) {
		var init = init || false;
		var _this = this;
		
		if ( !init && this.propIdx == idx ) return;
		
		this.propIdx = idx;
		$('.sku span').html(this.properties['ProductSKU' + idx]);
		
		$('a.mainView').data('viewImg', this.properties['MainImage' + idx]);
		$('a.mainView').css('display', ($('a.mainView').data('viewImg') ? '' : 'none'));
		
		$('a.sideView').data('viewImg', this.properties['SideImage' + idx]);
		$('a.sideView').css('display', ($('a.sideView').data('viewImg') ? '' : 'none'));
		
		$('a.bottomView').data('viewImg', this.properties['BottomImage' + idx]);
		$('a.bottomView').css('display', ($('a.bottomView').data('viewImg') ? '' : 'none'));
		
		$('.productViews a').removeClass('active');
		$('.productViews a.mainView').addClass('active');
		
		var sizes = '';
		if ( this.properties['BoysLowSize' + idx] && this.properties['BoysHighSize' + idx] ) {
			sizes += '<p>Boy\'s sizes: ' + this.properties['BoysLowSize' + idx] + ' - ' + this.properties['BoysHighSize' + idx] + '</p>';
		}
		if ( this.properties['GirlsLowSize' + idx] && this.properties['GirlsHighSize' + idx] ) {
			sizes += '<p>Girl\'s sizes ' + this.properties['GirlsLowSize' + idx] + ' - ' + this.properties['GirlsHighSize' + idx] + '</p>';
		}
		$('.sizes').html(sizes);
		
		if ( init ) {
			$('.productShot').html($('a.mainView').data('viewImg'));
		}
		else {
			$('.productShot').fadeTo('fast', 0.01, function() {
				$(this).html($('a.mainView').data('viewImg'));
				$(this).fadeTo('fast', 1);
			});
		}
		
		// extra GA tracking
		if ( !init ) {
			try {
				var path = window.location.href;
				path = path.substr(path.indexOf(window.location.hostname)+window.location.hostname.length)
					.replace(/sku=\d*/, 'sku=' + this.properties['ProductSKU' + this.propIdx]);
				pageTracker._trackPageview(escape(path));
			}
			catch (err) {}
		}
	},
	
	init: function() {
		var _this = this;
		
		var p = query(window.location.search);
		if ( !p.sku || (p.sku && !(this.setSku(p.sku, true))) ) {
			this.setIdx(1, true);
		}
		
		// product views links
		$('.productViews a').attr('href', 'javascript:void(0);');
		$('.productViews a').click(function() {
			var _this = this;
			$('.productShot').fadeTo('fast', 0.01, function() {
				$(this).html($(_this).data('viewImg'));
				$(this).fadeTo('fast', 1);
			});
			$('.productViews a').removeClass('active');
			$(this).addClass('active');
			return false;
		});
		
		// build the color pallette
		var chips = [];
		for ( var i = 1; i <= 7; ++i ) {
			if ( this.properties['ProductSKU' + i] ) {
				var chip = this.properties['PrimaryChip' + i].split(': ');
				chips.push({name: chip[0], hex: chip[1].toLowerCase(), sku: _this.properties['ProductSKU' + i]});
			}
		}
		if ( chips ) {
			var cols = 5;
			var extra = cols - (chips.length % cols)
			var pallette = '<div class="palletteHeading">See colors</div><table class="colorPallette" cellpadding="0" cellspacing="0" border="0"><tr>';
			for ( var i in chips ) {
				if ( (i > 0) && (i % cols == 0) && (i <= chips.length) ) {
					pallette += '</tr><tr>';
				}
				pallette += '<td><a href="javascript:void(0);" onclick="product.setIdx(' + (Number(i)+1) + ');" style="background-color:' + chips[i].hex + '; border:1px solid '
					+ (chips[i].hex == '#000000' ? '#ffffff' : chips[i].hex) + ';" title="' + chips[i].name + '"><img src="/Images/btn.gif" width="14" height="14" alt="' + chips[i].name + '"/></a></td>';
			}
			if ( extra != cols ) {
				for ( var i = 0; i < extra; ++i ) {
					pallette += '<td></td>';
				}
			}
			pallette += '</tr></table>';
			$('.colorPallette').html(pallette);
		}
		
		// next link
		$('a.next').click(function() {
			if ( window.parent != window ) { // probaly not necessary, but a sanity check anyways
				var catalog = window.parent.catalog;
				// we care about the position of the product in the list
				// as well as the key to access it in the catalog, so we
				// have to collect the keys before we start
				var keys = [];
				for ( var i in catalog.products ) {
					keys.push(i);
				}
				for ( var i = 0; i < keys.length; ++i ) {
					var key = keys[i];
					if ( catalog.products[key].ProductSKU == _this.properties['ProductSKU' + _this.propIdx] ) {
						var current = catalog.products[key];
						var nextKeys = keys.slice(i+1).concat(keys.slice(0, i));
						for ( var j = 0; j < nextKeys.length; ++j ) {
							var nextKey = nextKeys[j];
							if ( catalog.products[nextKey].visible ) {
								var next = catalog.products[nextKey];
								if ( current.ModelID == next.ModelID ) {
									_this.setSku(next.ProductSKU);
								}
								else {
									window.location.href = $('a', next.$node).attr('href');
								}
								return;
							}
						}
					}
				}
			}
		});
	}
};

$(document).ready(function(){
	$('.closeShadowbox').click(function(){
		window.parent.Shadowbox.close();
	});
	
	product.init();
	
	/**
	 * Extra GA tracking
	 */
	// ShareThis link click
	// We have to repeatedly poll for the link until we find it because
	// it's added to the page by the ShareThis script
	setTimeout(function() {
		var $link = $('.btnShare a.stbutton');
		if ( $link.length > 0 ) {
			$link.click(function() {
				try {
					pageTracker._trackPageview('/outbound/sharethis/'
						+ product.properties['Title']
						+ '/' + product.properties['ProductSKU' + product.propIdx]);
				}
				catch (err) {}
			});
		}
		else {
			setTimeout(this, 250);
		}
	}, 250);
});
