$(function(){
	jQuery.event.add(window, "load", resizeFrame);
	jQuery.event.add(window, "resize", resizeFrame);
	jQuery.event.add(window, "unload", resizeFrame);
	
	function resizeFrame() 
	{
		var centerpos = $(".floatLeftDetail").width();
		var www = $(".widthg").width();
		var w = $(".floatLeftDetailCrop img").attr("width");
		var centerpos = (centerpos/2)-(w/2);
		$(".floatLeftDetailCrop .widthg").css({
			left: (centerpos)+"px",
			width: w+'px'
		});
		
		var centerpos = $(".floatLeftDetailCrop img").attr("height");
		var hhh = centerpos;
		var centerpos = (centerpos/2)-(hhh/2);
		$(".floatLeftDetailCrop .heightg").css({
			top: centerpos+"px",
			height: hhh+"px"
		});
		
		$(".product .itemlist li:nth-child(6n+5)").css("margin-right", "0px");
	}
	
	$("#tableScale input[name='price']").click(function(){
		var height = $(this).attr('pheight');
		var width = $(this).attr('pwidth');
		var price = $(this).attr('pprice');
		
		_change_info(width, height, price);
		_change_photo(height-((height*55)/100), width-((width*55)/100));
		_change_label(width, height);
		
		_set_wrap();
	});
	
	function _change_photo(toheight, towidth)
	{
		$("div[id='photo']").animate({
			left: (285/2)-((towidth)/2),
			top: (258/2)-((toheight)-70)
			}, 320, function() {
			//animate complete
		});
		
		$("div[id='photo'] img").animate({
			width: towidth,
			height: toheight
			}, 320, function() {
			//animate complete
		});
	}
	
	$("select[name='cheight']").change(function(){
		$("select[name='cheight'] option:selected").each(function () {
        	var str = $(this).attr('rel');
			
			$("select[name='cwidth'] option[rel="+str+"]").attr('selected', 'selected');
			
			var height = $(this).attr('value');
			var width = $("select[name='cwidth'] option[rel="+str+"]").attr('value');
			var price = $("select[name='cwidth'] option[rel="+str+"]").attr('price');
			
			$("input[rel='custom']").attr('checked', 'checked');
			
			_change_info(width, height, price);
			_change_photo(height-((height*55)/100), width-((width*55)/100));
			_change_label(width, height);
			
			_set_wrap();
        });
	});
	
	$("select[name='cwidth']").change(function () {
		$("select[name='cwidth'] option:selected").each(function () {
        	var str = $(this).attr('rel');
			
			$("select[name='cheight'] option[rel="+str+"]").attr('selected', 'selected');
			
			var width = $(this).attr('value');
			var height = $("select[name='cheight'] option[rel="+str+"]").attr('value');
			var price = $("select[name='cheight'] option[rel="+str+"]").attr('price');
			
			$("input[rel='custom']").attr('checked', 'checked');
			
			_change_info(width, height, price);			
			_change_photo(height-((height*55)/100), width-((width*55)/100));
			_change_label(width, height);
			
			_set_wrap();
        });
	});
	
	$("input[rel='custom']").click(function(){
		$("select[name='cwidth'] option[rel=0]").attr('selected', 'selected');
		$("select[name='cheight'] option[rel=0]").attr('selected', 'selected');
		
		var width = $(this).attr('pwidth');
		var height = $(this).attr('pheight');
		_change_label(width, height);
		
		_set_wrap();
	});
	
	$(".inputlistsmall").change(function(){
		$(".inputlistsmall option:selected").each(function () {
        	var rel = $(this).attr("rel");
			var price = $(this).attr("price");
			
			$("span[id='option']").html(rel+' + $'+price);
			
			_set_wrap();
        });
	});
	
	function _change_label(width, height)
	{
		$(".widthg strong").html(width+' cm');
		$(".heightg strong").html(height+'<br>cm');
	}
	
	function _change_info(width, height, price)
	{
		$("span[id='height']").html(height);
		$("span[id='width']").html(width);
		$("span[id='price']").html(price);
		$("input[id=priceCheck6]").attr("value", price);
	}
	
	function _set_wrap()
	{
		$(".inputlistsmall option:selected").each(function () {
        	var wrap = $(this).attr('rel');
			var price = $(this).attr('price');
			var width = $("span[id=width]").html();
			var height = $("span[id=height]").html();
			
			var total = $("span[id=price]").html();
			var totall = parseFloat(total)+parseFloat(price);
			$("span[id=tprice]").html(totall);
			
			var win = width/2.54;
			var win = win.toFixed(0);
			var hin = height/2.54;
			var hin = hin.toFixed(0);
			
			var inside = ' - '+wrap+' Height '+height+' x Width '+width+' cm ('+hin+' x '+win+' in) |||'+price;
			$("input[name=option1cost]").attr('value', inside);
        });
	}
	
	//DEFINE CSS
	$(".product .itemlist li:nth-child(6n+5)").css("margin-right", "0px");
	$(".summary ul li:nth-child(2n+2)").css("background", "#fbffbf");
	$(".summary ul li a[rel=wrapdemo]").css({
		"cursor": "pointer"
	});
	
	$("a[rel=wrapdemo]").css({
		"color": "#754c24",
		"width": "194px"
	});
	$("a[rel=wrapdemo]").hover(function(){
		$(".wrapdemo").css("display", "block");
	}, function(){
		$(".wrapdemo").css("display", "none");		
	});
	//END DEFINE CSS
});
