// ----------------------------------------------------------------
// DOM ready
// ----------------------------------------------------------------
$(document).ready(function(){
	$('#st-product-right div.right h2:contains("Skipjack Polo")').css({
		textIndent: "-9999px",
		width: "239px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/skipjackPolo.gif) no-repeat"
	});
	
	$('#st-product-right div.right h2:contains("Skipjack Hat")').css({
		textIndent: "-9999px",
		width: "218px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/skipjackHat.gif) no-repeat"
	});
	
	$('#st-product-right div.right h2:contains("Skipjack Women")').css({
		textIndent: "-9999px",
		width: "250px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/womensPolo.gif) no-repeat"
	});
	
	$('#st-product-right div.right h2:contains("Gift Certificate")').css({
		textIndent: "-9999px",
		width: "306px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/giftCertificates.gif) no-repeat"
	});
	
	$('#st-product-right div.right h2:contains("Skipjack Neck Ties")').css({
		textIndent: "-9999px",
		width: "325px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/skipjackTies.gif) no-repeat"
	});
	
	$('#st-product-right div.right h2:contains("Skipjack Shorts")').css({
		textIndent: "-9999px",
		width: "377px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/shorts-heading.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Original Skipjack Tee")').css({
		textIndent: "-9999px",
		width: "377px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/classicTee.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Channel Markers Tee")').css({
		textIndent: "-9999px",
		width: "377px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/channelMarkerTee.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Khaki Pants")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/khakiHeading.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Boxers")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/boxersHeading.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("long sleeve")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/longsleeveTee.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Flags")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/flagsHeading.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Pattern sportshirt")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/patternsportshirt.gif) no-repeat"
	});
	$('#st-product-right div.right h2:contains("Pinpoint oxford")').css({
		textIndent: "-9999px",
		width: "390px",
		height: "29px",
		background: "transparent url(http://store.southerntide.com/media/layout/solidpinpointsportshirt.gif) no-repeat"
	});
	
	// cycle testimonials on the the product pages
	$('.customer-comment-rotate').cycle({timeout:10000});
	$('.fancy-iframe').fancybox({
		'frameWidth': 800,
		'frameHeight': 600,
		'autoScale': false,
		'type': 'iframe'
	});
	
	
	//check if we need to add the handler for the add to cart ajax form
	if ($('#orderForm').length > 0){
		addToCartForm();
	}
	if ($('#orderFormProductId').length > 0){
		$.getJSON("/scripts/options-json.cfm?productID=" + $('#orderFormProductId').html(),
			function(thejson){
				var uniqueSizes = new Array();
				var uniqueColors = new Array();
				var colorSwatches = new Array();
				var temp = new Array();
				var sizeHtml = '';
				var colorHtml = '';
				var swatchHtml = '<ul>';
				var priceHtml = '';
				var theone = 0;
				var theelementtoset = -1;
				
				
				for (var i = 0;i<thejson.recordcount;i++){
					temp = thejson.data.item[i];
					priceHtml = priceHtml +'<option value="' + temp +'">' + temp + '</option>';
					$('#product_option_1').val(thejson.data.optionitemid[0]);
					//console.log(priceHtml);
				}
				$("#price_option").change(function(){
					for (var i = 0; i < thejson.recordcount; i++) {
						if ($('#price_option').val() == thejson.data.item[i]){
							$('#product_option_1').val(thejson.data.optionitemid[i]);
						}
					}
				});
				//console.log($('#price_option').html(priceHtml));
				$('#price_option').html(priceHtml);
				
				//build #size_option #color_option #swatches
				//loop through json and get unique sizes
				if ($('#orderFormProductId').html() != 1060 && $('#orderFormProductId').html() != 1035){sizeHtml = sizeHtml + '<option value ="" selected = selected>Select Size</option>';}
				if ($('#orderFormProductId').html() == 1008){sizeHtml = sizeHtml + '<option value ="" selected = selected>Select Style</option>';}
				colorHtml = colorHtml + '<option value ="" selected = selected>Select Color</option>';
				for (var i = 0; i < thejson.recordcount; i++) {
					temp = thejson.data.item[i].split(':');
					if (jQuery.inArray(temp[0], uniqueSizes) == -1){
						uniqueSizes[uniqueSizes.length] = temp[0];
						sizeHtml = sizeHtml + '<option value="' + temp[0] + '"';
						sizeHtml = sizeHtml + '>' + temp[0] + '</option>';
					}
					//now that we have a selected option, grab any colors that match
					if (temp[0] == uniqueSizes[0]){
						if (colorHtml == '<option value ="" selected = selected>Select Color</option>'){
							//set the main image to the first one
							$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[0]);			
							$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
							$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
							$('#mainProductImageLink').attr('class','group');
							$('#enlargeLink').click(function(){return false;});
							$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[0]);
							var extraimages = '';
							if (thejson.data.additionalimage[i] > 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								for(var k = 2; k <= thejson.data.additionalimage[i];k++){
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
								}
								extraimages = extraimages + '</ul>';
								$('#extraimages').html(extraimages);
							}
							else if(thejson.data.additionalimage[i] == 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								tempimage = thejson.data.smallimage[i].split('.');
								extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
								extraimages = extraimages + '</ul>';
								$('#extraimages').html(extraimages);
							}
							
							$('#colorName').html(temp[1]);
							theelementtoset = i;
							theone = 1;
						}
							colorHtml = colorHtml + '<option value="' + temp[1] + '"';
						if (theone){
							colorHtml = colorHtml + ' ';
							theone = 0;
						}
						
							colorHtml = colorHtml + '>' + temp[1] + '</option>';
						
						//grab the images for this color
						swatchHtml = swatchHtml + '<li><a href=""><img class="swatchimages" src="http://store.southerntide.com/media/options/swatches/thumbs/' + thejson.data.smallimage[i] + '" alt="' + temp[1] + '" /></a></li>';
					}
				}
				$('#size_option').html(sizeHtml);
				$('#color_option').html(colorHtml);
				$('#swatches').html(swatchHtml + '</ul><div id="swatchpreview"></div><div id="extraimages"></div>');
				$('#extraimages').html(extraimages);
				$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
				if (theelementtoset >= 0){
					$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[theelementtoset] + '" />');
				}
					
				//update the currently selected option - is there a better way than looping through the json?
				for (var i = 0; i < thejson.recordcount; i++) {
					if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
						$('#product_option_1').val(thejson.data.optionitemid[i]);
					}
				}
				
				//add the event handlers
				//size change - grab colors for that size
				$("#size_option").change(function(){
				//if size is Select Size build the colors again
					if($("#size_option").val() == ''){
						var uniqueSizes = new Array();
						var uniqueColors = new Array();
						var colorSwatches = new Array();
						var temp = new Array();
						var sizeHtml = '';
						var colorHtml = '';
						var swatchHtml = '<ul>';
						var theone = 0;
						var theelementtoset = -1;
						
						//build #size_option #color_option #swatches
						//loop through json and get unique sizes
						
						colorHtml = colorHtml + '<option value ="" selected = selected>Select Color</option>';
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							if (jQuery.inArray(temp[0], uniqueSizes) == -1){
								uniqueSizes[uniqueSizes.length] = temp[0];
							}
							//now that we have a selected option, grab any colors that match
							if (temp[0] == uniqueSizes[0]){
								
								if (colorHtml == '<option value ="" selected = selected>Select Color</option>'){
									//set the main image to the first one
									$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[0]);
									$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
									$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
									$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
									$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[0]);
									var extraimages = '';
									if (thejson.data.additionalimage[i] > 1){
										extraimages = '<h3>Additional Photos</h3><ul>';
										for(var k = 2; k <= thejson.data.additionalimage[i];k++){
											tempimage = thejson.data.smallimage[i].split('.');
											extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
										}
										extraimages = extraimages + '</ul>';						
										$('#extraimages').html(extraimages);
									}
									else if(thejson.data.additionalimage[i] == 1){
										extraimages = '<h3>Additional Photos</h3><ul>';
										tempimage = thejson.data.smallimage[i].split('.');
										extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
										extraimages = extraimages + '</ul>';
										$('#extraimages').html(extraimages);
									}
									
									$('#colorName').html(temp[1]);
									theelementtoset = i;
									theone = 1;
								}
								
									colorHtml = colorHtml + '<option value="' + temp[1] + '"';
								if (theone){
									colorHtml = colorHtml + ' ';
									theone = 0;
								}
								
									colorHtml = colorHtml + '>' + temp[1] + '</option>';
								
								//grab the images for this color
								swatchHtml = swatchHtml + '<li><a href=""><img class="swatchimages" src="http://store.southerntide.com/media/options/swatches/thumbs/' + thejson.data.smallimage[i] + '" alt="' + temp[1] + '" /></a></li>';
							}
						}
						$('#color_option').html(colorHtml);
						$('#swatches').html(swatchHtml + '</ul><div id="swatchpreview"></div><div id="extraimages"></div>');
						$('#extraimages').html(extraimages);
						$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
						if (theelementtoset >= 0){
							$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[theelementtoset] + '" />');
						}
					}
					//end of building colors
					else{
						colorHtml = '';
						swatchHtml = '<ul>';
						theone = 0;
						var theelementtoset = -1;
						if ($('#color_option').val() == ''){
									colorHtml = colorHtml + '<option value="" selected = "selected">Select Color</option>';
								}
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							if (temp[0] == $("#size_option").val()){
								if (temp [1] == $('#color_option').val()){
									//set the main image to the selected one
									$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);	
									$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
									$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
									$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
									$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
									var extraimages = '';
									if (thejson.data.additionalimage[i] > 1){
										extraimages = '<h3>Additional Photos</h3><ul>';
										for(var k = 2; k <= thejson.data.additionalimage[i];k++){
											tempimage = thejson.data.smallimage[i].split('.');
											extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
										}
										extraimages = extraimages + '</ul>';						
										$('#extraimages').html(extraimages);
									}
									else if(thejson.data.additionalimage[i] == 1){
										extraimages = '<h3>Additional Photos</h3><ul>';
										tempimage = thejson.data.smallimage[i].split('.');
										extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
										extraimages = extraimages + '</ul>';
										$('#extraimages').html(extraimages);
									}
									
									$('#colorName').html(temp[1]);
									theelementtoset = i;
									theone = 1;
								}
									colorHtml = colorHtml + '<option value="' + temp[1] + '"';
								if (theone){
									colorHtml = colorHtml + ' selected = "selected" ';
									theone = 0;
								}
								//if statement here out of stock 
								if(thejson.data.onhand[i] <= 0){
									colorHtml = colorHtml + '>' + temp[1] + ' - Sold Out' + '</option>';
								}
								else{
									colorHtml = colorHtml + '>' + temp[1] + '</option>';
								}
								//grab the image for this color
								swatchHtml = swatchHtml + '<li><a href=""><img class="swatchimages" src="http://store.southerntide.com/media/options/swatches/thumbs/' + thejson.data.smallimage[i] + '" alt="' + temp[1] + '" /></a></li>';
							}
						}
						
						$('#color_option').html(colorHtml);
						$('#swatches').html(swatchHtml + '</ul><div id="swatchpreview"></div><div id="extraimages"></div>');
						$('#extraimages').html(extraimages);
						$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
						if (theelementtoset >= 0){
							$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[theelementtoset] + '" />');
						}
						
						//update the currently selected option - is there a better way than looping through the json?
						for (var i = 0; i < thejson.recordcount; i++) {
							if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
								$('#product_option_1').val(thejson.data.optionitemid[i]);
							}
						}
					}
					//swatch mouseover/out
					$(".swatchimages").mouseover(function(){
						//change model image and link
						var currentImage = $(this).attr('src').replace('http://store.southerntide.com/media/options/swatches/thumbs/','');
						//IE has the whole path in there
						currentImage = currentImage.replace('','');
						var currentElement = 0;
						$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + currentImage + '" />');
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							if (currentImage == thejson.data.smallimage[i]){
								$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);	
								$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
								$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
								$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
								$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
								var extraimages = '';
								if (thejson.data.additionalimage[i] > 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									for(var k = 2; k <= thejson.data.additionalimage[i];k++){
										tempimage = thejson.data.smallimage[i].split('.');
										extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
									}
									extraimages = extraimages + '</ul>';						
									$('#extraimages').html(extraimages);
								}
								else if(thejson.data.additionalimage[i] == 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
									extraimages = extraimages + '</ul>';
									$('#extraimages').html(extraimages);
								}
								$('#extraimages').html(extraimages);
								$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
								$('#colorName').html(temp[1]);
							}
						}
					}).mouseout(function(){
						//set back to selected color
						//set back to selected color
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
								$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);		
								$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
								$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
								$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
								$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
								$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[i] + '" />');
								var extraimages = '';
								if (thejson.data.additionalimage[i] > 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									for(var k = 2; k <= thejson.data.additionalimage[i];k++){
										tempimage = thejson.data.smallimage[i].split('.');
										extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
									}
									extraimages = extraimages + '</ul>';						
									$('#extraimages').html(extraimages);
								}
								else if(thejson.data.additionalimage[i] == 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
									extraimages = extraimages + '</ul>';
									$('#extraimages').html(extraimages);
								}
								$('#extraimages').html(extraimages);
								$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
								$('#colorName').html(temp[1]);
							}
						}
					});
					
					//swatch click
					$(".swatchimages").click(function(){
						//set the select box with the correct color
						var currentImage = $(this).attr('src').replace('http://store.southerntide.com/media/options/swatches/thumbs/','');
						//IE has the whole path in there
						currentImage = currentImage.replace('','');
						var currentElement = 0;
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							if (currentImage == thejson.data.smallimage[i]){
								$('#color_option').val(temp[1]);
								$('#colorName').html(temp[1]);
							}
						}
						//update the currently selected option - is there a better way than looping through the json?
						for (var i = 0; i < thejson.recordcount; i++) {
							if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
								$('#product_option_1').val(thejson.data.optionitemid[i]);
							}
						}
						return false;
					});
				});//size change stop
				
				//color change
				$("#color_option").change(function(){
					if ($("#color_option").val() != ''){
					if($("#size_option").val() == ''){
						sizeHtml =  '<option value=""';
						//if ($('#size_option').val() == ''){
						sizeHtml = sizeHtml + ' selected = "selected" ';
						//}
						
						if ($('#orderFormProductId').html() == 1008){sizeHtml = sizeHtml + '>Select Style</option>';}else{sizeHtml = sizeHtml + '>Select Size</option>';}
						for (var i = 0; i < thejson.recordcount; i++) {
							temp = thejson.data.item[i].split(':');
							
							//building the sizes for that color if I can
							if (temp[1] == $("#color_option").val()){
								//console.log($('#size_option').val());
								//console.log(temp [0]);
								if (temp [0] == $('#size_option').val()){
									theone = 1;
								}
									
									sizeHtml = sizeHtml + '<option value="' + temp[0] + '"';
								if (theone){
									sizeHtml = sizeHtml + ' selected = "selected" ';
									theone = 0;
								}
								//if statement here out of stock 
								if(thejson.data.onhand[i] <= 0){
									sizeHtml = sizeHtml + '>' + temp[0] + ' - Sold Out' + '</option>';
								}
								else{
									sizeHtml = sizeHtml + '>' + temp[0] + '</option>';
								}
							}
							//ending building the sizes
							
							
							
							$('#size_option').html(sizeHtml);
							
							if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
								$('#product_option_1').val(thejson.data.optionitemid[i]);
								$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);		
								$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
								$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
								$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
								$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
								$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[i] + '" />');
								var extraimages = '';
								if (thejson.data.additionalimage[i] > 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									for(var k = 2; k <= thejson.data.additionalimage[i];k++){
										tempimage = thejson.data.smallimage[i].split('.');
										extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
									}
									extraimages = extraimages + '</ul>';						
									$('#extraimages').html(extraimages);
								}
								else if(thejson.data.additionalimage[i] == 1){
									extraimages = '<h3>Additional Photos</h3><ul>';
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
									extraimages = extraimages + '</ul>';
									$('#extraimages').html(extraimages);
								}
								$('#extraimages').html(extraimages);
								$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
								$('#colorName').html(temp[1]);
							}
						}

					}
					else{
						sizeHtml =  '<option value="' + $('#size_option').val() + '"';
						//if ($('#size_option').val() == $('#size_option').val()){
							sizeHtml = sizeHtml + ' selected = "selected" ';
						//}
						sizeHtml = sizeHtml + '>' + $('#size_option').val() + '</option>';
					for (var i = 0; i < thejson.recordcount; i++) {
						temp = thejson.data.item[i].split(':');
						//building the sizes for that color if I can
						if(temp[0] != $('#size_option').val()){
							if (temp[1] == $("#color_option").val()){
								if (temp [0] == $('#size_option').val()){
									theone = 1;
								}
								sizeHtml = sizeHtml + '<option value="' + temp[0] + '"';
								if (theone){
									sizeHtml = sizeHtml + ' selected = "selected" ';
									theone = 0;
								}
									//if statement here out of stock 
								if(thejson.data.onhand[i] <= 0){
									sizeHtml = sizeHtml + '>' + temp[0] + ' - Sold Out' + '</option>';
								}
								else{
									sizeHtml = sizeHtml + '>' + temp[0] + '</option>';
								}
						}
					}
						//ending building the sizes
					
					$('#size_option').html(sizeHtml);
					
					if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
						$('#product_option_1').val(thejson.data.optionitemid[i]);
						$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);
						$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
						$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
						$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
						$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
						$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[i] + '" />');
						var extraimages = '';
						if (thejson.data.additionalimage[i] > 1){
							extraimages = '<h3>Additional Photos</h3><ul>';
							for(var k = 2; k <= thejson.data.additionalimage[i];k++){
								tempimage = thejson.data.smallimage[i].split('.');
								extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
							}
							extraimages = extraimages + '</ul>';						
							$('#extraimages').html(extraimages);
						}
						else if(thejson.data.additionalimage[i] == 1){
							extraimages = '<h3>Additional Photos</h3><ul>';
							tempimage = thejson.data.smallimage[i].split('.');
							extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
							extraimages = extraimages + '</ul>';
							$('#extraimages').html(extraimages);
						}
						$('#extraimages').html(extraimages);
						$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
						$('#colorName').html(temp[1]);
						
					}
				}

				}
					}//update the currently selected option - is there a better way than looping through the json?
				});
				
				//swatch mouseover/out
				$(".swatchimages").mouseover(function(){
					//change model image and link
					var currentImage = $(this).attr('src').replace('http://store.southerntide.com/media/options/swatches/thumbs/','');
					//IE has the whole path in there
					currentImage = currentImage.replace('','');
					var currentElement = 0;
					$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + currentImage + '" />');
					//console.log(currentImage);
					for (var i = 0; i < thejson.recordcount; i++) {
						temp = thejson.data.item[i].split(':');
						if (currentImage == thejson.data.smallimage[i]){
							$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);				
							$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
							$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
							$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
							$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
							var extraimages = '';
							if (thejson.data.additionalimage[i] > 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								for(var k = 2; k <= thejson.data.additionalimage[i];k++){
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
								}
								extraimages = extraimages + '</ul>';						
								$('#extraimages').html(extraimages);
							}
							else if(thejson.data.additionalimage[i] == 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								tempimage = thejson.data.smallimage[i].split('.');
								extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
								extraimages = extraimages + '</ul>';
								$('#extraimages').html(extraimages);
							}
							$('#extraimages').html(extraimages);
							$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
							$('#colorName').html(temp[1]);
						}
					}
				}).mouseout(function(){
					//set back to selected color
					for (var i = 0; i < thejson.recordcount; i++) {
						temp = thejson.data.item[i].split(':');
						if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i] || $('#color_option').val() == temp[1]){
							$('#mainProductImageLink, #enlargeLink').attr('href','/media/options/' + thejson.data.largeimage[i]);		
							$('#mainProductImageLink, #enlargeLink').attr('rel',temp[1]);
							$('#mainProductImageLink, #enlargeLink').attr('title',temp[1]);
							$('#mainProductImageLink').attr('class','group');$('#enlargeLink').click(function(){return false;});
							$('#mainProductImage').attr('src','http://store.southerntide.com/media/options/models/thumbs/' + thejson.data.largeimage[i]);
							$('#swatchpreview').html('<img src="http://store.southerntide.com/media/options/swatches/' + thejson.data.smallimage[i] + '" />');
							var extraimages = '';
							if (thejson.data.additionalimage[i] > 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								for(var k = 2; k <= thejson.data.additionalimage[i];k++){
									tempimage = thejson.data.smallimage[i].split('.');
									extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + k + '.' + tempimage[1] + '" /></a></li>';
								}
								extraimages = extraimages + '</ul>';						
								$('#extraimages').html(extraimages);
							}
							else if(thejson.data.additionalimage[i] == 1){
								extraimages = '<h3>Additional Photos</h3><ul>';
								tempimage = thejson.data.smallimage[i].split('.');
								extraimages = extraimages + '<li><a class="group" rel="'+temp[1]+'" title="'+temp[1]+'" href="http://store.southerntide.com/media/options/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '"><img src="http://store.southerntide.com/media/options/thumbs/' + tempimage[0] + '_' + 2 + '.' + tempimage[1] + '" /></a></li>';
								extraimages = extraimages + '</ul>';
								$('#extraimages').html(extraimages);
							}
							$('#extraimages').html(extraimages);
							$("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
							$('#colorName').html(temp[1]);
						}
					}
				});
				
				//swatch click
				$(".swatchimages").click(function(){
					//set the select box with the correct color
					var currentImage = $(this).attr('src').replace('http://store.southerntide.com/media/options/swatches/thumbs/','');
					//IE has the whole path in there
					currentImage = currentImage.replace('','');
					var currentElement = 0;
					for (var i = 0; i < thejson.recordcount; i++) {
						temp = thejson.data.item[i].split(':');
						if (currentImage == thejson.data.smallimage[i]){
							$('#color_option').val(temp[1]);
							$('#colorName').html(temp[1]);
						}
					}
					//update the currently selected option - is there a better way than looping through the json?
					for (var i = 0; i < thejson.recordcount; i++) {
						if ($('#size_option').val() + ':' +  $('#color_option').val() == thejson.data.item[i]){
							$('#product_option_1').val(thejson.data.optionitemid[i]);
						}
					}
					return false;
				});
				//console.log(thejson);
        	});
     }
     
     // NOTE: the shorts are unique and the images are static. Lame, I know
	 // saved shorts info - for locking the image
     var savedShortsImage = 'shorts-front-shot.jpg';
     var savedShortsImageTitle = 'Front View';
     $('a.fancy-single').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
     $('a.fancy-pics-group').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
    //shorts mouseover/out
	$(".short-thumb").mouseover(function(){
		//change model image and link
		var currentImage = $(this).attr('src').replace('/media/options/models/thumbs','');
		//IE has the whole path in there
		currentImage = currentImage.replace('','');
		var currentElement = 0;
		$('#mainProductImage-shorts').attr({src: '/media/options/models/' + currentImage});
		$('#colorName-shorts').html($(this).attr('alt'));
	}).mouseout(function(){
		$('#mainProductImage-shorts').attr({src: '/media/options/models/' + savedShortsImage});
		$('#colorName-shorts').html(savedShortsImageTitle);
	});
	
	// NOTE: the coolers are unique and the images are static. Lame, I know
	 // saved coolers info - for locking the image
     var savedCoolerImage = '/media/options/models/thumbs/cooler-15.jpg';
     var savedCoolerLarge = '/media/options/cooler-15.jpg';
     var savedCoolerCapacity = '/media/options/swatches/cooler-15.jpg';
     $('a.fancy-single').fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });
    //coolers mouseover/out
	$("#cooler-swatches ul li a").mouseover(function(){
		//change model image and link
		var currentImage = $(this).attr('href');
		var currentLargeImage = $(this).attr('name');
		var currentSwatch = $(this).attr('target'); 
		$('#coolers-large-image a img').attr({src: currentImage});
		$('#coolers-large-image a').attr({href: currentLargeImage});
		$('#cooler-capacity img').attr({src: currentSwatch});
	}).mouseout(function(){
		$('#coolers-large-image a img').attr({src: savedCoolerImage});
		$('#cooler-capacity img').attr({src: savedCoolerCapacity});
		$('#coolers-large-image a').attr({href: savedCoolerLarge});
	}).click(function(){
		savedCoolerImage = $(this).attr('href');
     	savedCoolerCapacity = $(this).attr('target');
     	savedCoolerLarge = $(this).attr('name');
     	return false;
	});

     
});

function addToCartForm(){
	var options = {
		url:		   '/cart/cart.cfm?mode=add&ajaxsubmit=1',
        beforeSubmit:  beforeAddToCartForm, 
        success:       afterAddToCartForm
    }; 
 
    $('#orderForm').ajaxForm(options);
}

function beforeAddToCartForm(formData, jqForm, options) { 
    // some error checking
 
 	// return false if there are errors
    return true; 
}

function afterAddToCartForm(formData, jqForm, options) { 
    // update the cart totals
    $('#orderFormCartContents').fadeIn().html(jQuery.trim(formData));
	if($('#orderFormNumberInCart').html() > 0){
    	$('#viewOrderButton').show();
	}
}
