                                                        // Blake Standard 2008 //


function imgResize(src){
	if(src.indexOf("x0.") < 1){

		var s = src.split("_resize");		
		var t = s[1].split("."); 
		var size = t[0].split("x");
		var width = size[0];
		var height = size[1];
		if(width > 0){		
			var i = new Image();	
			i.onload = function(){
			var ratio = i.width / i.height;
	
	
			if (ratio < 1)
			{
				width = 220;
				height = parseInt(220 / ratio); 
			}
			else
			{
				width = parseInt(220 * ratio); 
				height = 223; 
			}
			
			var newImgx = s[0] + "_resize"+width + "x" + height + "." + t[1];		
			$('.productImage img').attr('src', newImgx);
			};
			i.src = s[0] + "." + t[1]; 		
		}
	}else{
		newImgx = src.replace('resize34x0', 'resize220x0');
		$('.productImage img').attr('src', newImgx);
	}
}


// End of resize function


$(document).ready(function(){
// Stores
try{
	var storesHeader = $('.storesContent h3').text();

	$('.storesListing li a:contains('+ storesHeader +')').addClass('selected');

}catch(err){}
// Shop Blake

	// Margins & Clearing

	//$("li:eq(3), li:eq(7), li:eq(12), li:eq(16)",".productsList").css({marginRight:"0px"});
	//$("li:eq(4), li:eq(9), li:eq(14), li:eq(19)",".productsList").css({clear: "left"});
	$("li:eq(3), li:eq(7), li:eq(11), li:eq(15)",".productThumbnails").css({marginRight:"0px"});
	$("li:eq(4), li:eq(8), li:eq(12), li:eq(16)",".productThumbnails").css({clear: "left"});
	
	// Hover
	
	$("dl.product .prodLink").hover(function() {
		$(this).parent().siblings("dd").children("a").addClass("hoverState");
	}, function() {
		$(this).parent().siblings("dd").children("a").removeClass("hoverState");
	});
	
	// Aligment
		
	//$('.prodLink img').each
	//(function(i){
		//var ah = $('.prodLink').eq(i).height();
		//var ph = $('.prodLink').eq(i).parent().height();
		//var mh = (ph - ah) / 2;
		//var nw = (mh + 1);
		//$('.prodLink img').eq(i).css('marginTop', nw);
	//});

	// Add To Bag Summary Box
	
	$('ul.summaryList li:last').css({marginBottom:"0px", paddingTop:"10px"});	

	// Default values for selects
	
	
	var sizeDefault = $('.choicesList .skufield:eq(1) option:selected').text();
	var colorDefault = $('.choicesList .skufield:eq(0) option:selected').text();



// Select Style/Color
	
	$('#productColor').text(colorDefault);
	
	$('.choicesList .skufield:eq(0)').change(function() {
													
		var str = "";
		
		$('option:selected', this).each(function() {
			str = $(this).text();
		});
		$('#productColor').html('' + str + '');
		// Change main image source on change

var img = "";

$('.productThumbnails img').each(function(){

var src = $(this).attr('src');
var text = $('option[value='+$('.choicesList .skufield:eq(0)').val()+']').text();

src = src.toLowerCase();
text = text.toLowerCase();



if (src.indexOf(text) > -1 && !img)
{

	
	imgResize(src);	
	
	return false;
}

});

//$('.productImage img').attr('src', img);

		
		/*$('.productImage img').each(function() {
			var piSrc, h2a, h2b, h2c;
			
			piSrc = $(this).attr("src");
			piSrca = piSrc.slice(0, piSrc.lastIndexOf('/')+1);
			piSrcb = piSrc.slice(piSrc.indexOf('_'));
			$(this).attr({src:piSrca+str+piSrcb});
		});*/
		
		$('#productColor').html('' + str + '');
	});
			
	// Select Size
			
	$('#productSize').text(sizeDefault);
	
	$('.choicesList .skufield:eq(1)').change(function() {
		var str = "";
		
		$('option:selected', this).each(function() {
			str = $(this).text();
		});
		
		$('#productSize').html('' + str + '');
	});
	
	// Thumbnail Container & Image Switching
	
	if($('.productThumbnails').html() != null && $('.productThumbnails').html().length <= 30) {
		$('.moreImages').hide();
	}
	
	// resize if have height dimensions

try{
	
	$('.productThumbnails a img').click(function(){
	if($(this).attr('src').indexOf("x0.") < 1){

		var s = $(this).attr('src').split("_resize");		
		var t = s[1].split("."); 
		var size = t[0].split("x");
		var width = size[0];
		var height = size[1];
		if(width > 0){		
		var i = new Image();

		i.onload = function(){
		var ratio = i.width / i.height;


		if (ratio < 1)
		{
			width = 220;
			height = parseInt(220 / ratio); 
		}
		else
		{
			width = parseInt(220 * ratio); 
			height = 223; 
		}
		
		var newImg = s[0] + "_resize"+width + "x" + height + "." + t[1];		
		$('.productImage img').attr('src', newImg);
		};
		i.src = s[0] + "." + t[1]; 
		
		
}
	}else{

		var newImg = $(this).attr('src');
		
		$(this).parent().addClass('selected');
		if(newImg.indexOf("34x0") > 1) {
			var oImg = newImg.replace("34x0", "220x0");
		        var nImg = oImg.replace("thumbnail", "full image")
			$('.productImage img').attr('src', oImg);
			}
			else {}
		return false;
	}
		
});

	}catch(err){}

// Footer Margins

$("li:eq(1)",".footerList").css({marginRight:"20px"});
$("li:eq(2)",".footerList").css({marginRight:"10px"});
	
});

    

    

    
