$(document).ready(function(){
	layout();
	if($("#landing").html() != undefined){
		landing();
	}
	if($("#media_list").html() != undefined){
		//apply media hover
		media_hover();
	}
	if($("#product_list").html() != undefined){
		hover($("#product_list .product"), $("#product_popup"));
	}
	if($("#product_page").html() != undefined){
		product_page();
		hover($('.hover'), $("#care_popup"));
	}
	show_weather();
	
	if($("#cartinfo").html() != undefined){
		cart_info($('#cartinfo'));
	}
});
function layout(){
	//fix ie6 don't know min-height
	ie6_fix();
	//always align menu and content.
	match_height();
	
	fix_last_li_border();
	
}
//fix ie6 not knowing min-height
function ie6_fix(){
	if($.browser.msie && $.browser.version < 7.0){
		if($("#menu ul").height()<323)
		{
			$("#menu ul").height(323);
		}
		if($(".inside").height()<323)
		{
			$(".inside").height(323);
		}
	}
}
function match_height(){
	var menu_height = $("#menu ul").height();
	var inside_height = $(".inside").height();
	if(menu_height > inside_height){
		$(".inside").height(menu_height);	
	}else if(menu_height < inside_height){
		$("#menu ul").height(inside_height);
	}	
}
function fix_last_li_border(){
	var height = 0;
	$("#menu ul li").each(function(){
		height += $(this).height();
	});
	if(height>323){
		$("#menu ul li:last").css('borderBottom', '1px solid white');
	}
}
function landing(){
	var left = $("#body").position().left;
	var top = $("#body").position().top;
	var landing = $("#landing");
	landing.hide();
	landing.css('left', left+12);
	landing.css('top', top);
	landing.fadeIn('fast');
	setTimeout('slide_landing()', 1500);
	//$("div", landing).animate({width: ""}, 2000, function(){landing.hide()});//slide to right and hide
}
function slide_landing(){
	$("div", $("#landing")).animate(
			{width: "0px"}, 
			1500, 
			function(){
				$("#landing").hide()
				$.ajax({
					type:"POST",
					url:self.location.href,
					data:'finish_sliding=true',
					success:function(){
						
					}
				});
			}
	);
}
function product_page(){
	thumb_enlarge();
	//apply_thumb_view();
	apply_swatch_view();
	apply_related_hover();
}

function media_hover(){
	$("#media_list .media").bind("mouseenter", function(e){
		show_div($("#media_popup"), $(this).parent().next(), e);
	});
	$("#media_list .media").mousemove(function(e){
		move_div($("#media_popup"), e);
	});
	$("#media_list .media").mouseout(function(e){
		hide_div($("#media_popup"));
	});
}
function thumb_enlarge(){
	$(".product_hover a").bind("click", function(){
		//$(this).parent().hide();
		close_thumb();
		return false;
	});
	$("#product_page #product_current_thumb").parent('a').bind("click", function(e){
		//show_div($("#popup"), $(this).parent().next(), e);
		open_thumb($(this).parent().next());
		return false;
	});
	$("#product_page .product_related .product_thumb").children('a').bind("click", function(e){
		open_thumb($(this).parent().next());
		return false;
	});
	$("#product_page #zoom").parent('a').bind("click", function(e){
		//show_div($("#popup"), $(this).parent().next(), e);
		//$("#product_page .product_hover").show();
		open_thumb($("#product_page #product_current_thumb").parent('a').parent().next());
		return false;
	});
}
function close_thumb(){
	$(this).parent().animate({width: '0', height:'0'}, 1000, function(){$(this).hide();});
	$('#thumb_overlay').remove();
	$('#thumb_wrapper').remove();
	
}
function open_thumb(odiv){
	var div = odiv;//$(".product_hover");
	div.css({width:0, height:0});
	ndiv = div.clone(true);
	var dheight = $(document).height();
	var wheight = $(window).height();
	var wwidth = $(window).width();
	var overlay = $('<div id="thumb_overlay"></div>');
	overlay.css({width: '100%', height:dheight, position: 'absolute', left: 0, top: 0, zIndex: 99});

	if($.browser.mozilla && $.browser.version=='1.9'){//don't know what happens on firefox3, have to use image to fix the background
		overlay.css({background:'url(/images/bg.png) repeat'});
	}else{
		overlay.css({backgroundColor:'#000', opacity: '0.75'});		
	}
	var wrapper = $('<div id="thumb_wrapper"></div>');
	wrapper.css({width: '100%', height:wheight, background:'none', position: 'absolute', left: 0, top: 0, zIndex: 100});
	wrapper.append(ndiv);
	ndiv.css({top:wheight/2-190, left:wwidth/2 - 190});
	$("#page_container").before(overlay).before(wrapper);

	ndiv.animate({width: '380', height:'400', opacity:'1.0'}, 500, function(){
					$(this).children('.image_container').show();
					$(this).children('a').show();
				});
}
/*function apply_thumb_view(){
	$("#product_page .product_thumb").click(function(){
		$("#product_page #product_current_thumb").attr('src', $(this).attr('src').replace(/30\/30/, "173/146" ));
		$(".product_hover .image_container").children('img').attr('src', $(this).attr('src').replace(/30\/30/, "380/380" ));
		return false;
	});
}*/
function hover(item, popup){
	item.bind("mouseenter", function(e){
		show_div(popup, $(this).next(), e);
	});
	item.mousemove(function(e){
		move_div(popup, e);
	});
	item.mouseout(function(e){
		hide_div(popup);
	});
}
function show_div(popup, obj, pos){
	if(obj.html()!=undefined)
		popup.children('.content').html(obj.html());
	popup.css('left', pos.pageX+10+'px');
	popup.css('top', pos.pageY+10+'px');
}
function move_div(popup, pos){
	popup.css('left', pos.pageX + 10+'px');
	popup.css('top', pos.pageY + 10 +'px');
}
function hide_div(popup){
	popup.css('left', '-1000px');
}

function apply_swatch_view(){
	$("#product_page .swatch").click(function(){
		var url = $(this).attr("href");
		$.ajax({
			type:"POST",
			url:url,
			data:'get_swatch=true&rnd='+ (new Date().getTime()),
			success:function(html){
				$("#body").html(html);
				//reapply javascript
				layout();
				product_page();
			}
		})
		
		return false;
	});
}
function apply_related_hover(){
	$("#product_page #product_related .related").bind("mouseenter", function(e){
		show_ajax_div($("#popup_related"), $(this).parent().attr('href'), e);
	});
	$("#product_page #product_related .related").mousemove(function(e){
		move_ajax_div($("#popup_related"), e);
	});
	$("#product_page #product_related .related").mouseout(function(e){
		hide_div($("#popup_related"));
	});
}
function show_ajax_div(popup, link, pos){
	popup.html('<img src="/images/loading.gif" width="" height="" />');
	popup.css('left', pos.pageX-460+'px');
	popup.css('top', pos.pageY-180+'px');
	$.ajax({
		type:'POST',
		url: link,
		data:'related=true&rnd='+(new Date().getTime()),
		success:function(html){
			popup.html(html);
		}
	});
}
function move_ajax_div(popup, pos){
	popup.css('left', pos.pageX-460+'px');
	popup.css('top', pos.pageY-180+'px');
}
function show_weather(){
	$.ajax({
		type:'POST',
		url:$('#sunny_url').val()+'validate/get_weather\/',
		date:'rnd='+(new Date().getTime()),
		success:function(html){
			$('#sunny .weather').html(html);
		}
	});
}
function cart_info(placeHolder){
	placeHolder.load($('#sunny_url').val()+'shop/cartinfo/'+new Date().getTime());
}
function update_cart(link, qty, callback){
	$.post(link, {'qty':qty}, callback);
}
/*function update_cart(link, qty){
	$.post(link, {'qty':qty}, function(){
		$('#head').append('<div id="cartinfo"></div>');
		cart_info($('#cartinfo'));
		$('#in_cart').html('ITEM ADDED TO CART');
	});
}*/
function get_total(callback){
	$.post($('#sunny_url').val()+'shop/carttotal/'+new Date().getTime(), {}, function(res){
		if(res.status=='0'){
			self.location.href = res.url;
		}else{
			$('#cart_total_amount strong').html(res.total);
			if(callback != null) {
				callback();
			}
		}
	}, "json");
	
}

