$(function() {
	initProduct();
	tee.init();
});

/**
 * 产品展示
 */
var tee = {
	id : null,			//产品id
	name : null,		//产品名称
	author : null,		//作者名称
	description : null,	//产品描述
	price : 0,			//价格
	style : null,		//款式
	subject : null,		//主题
	color : null,		//颜色
	images : null,		//图片
	baseurl : null,
	imagePath : null,
	/**
	 * 初始化
	 */
	init : function() {
	    baseurl = "/product/";
	    imagePath = "/images/product/";
	    
		//去除相同设计产品功能
		$("#samedesign").hide();
		
	    //绑定点击产品弹出预览窗口
		$("dl","#content").click(tee.start);
		
		//绑定关闭产品预览窗口
		$("#closewindow").click(tee.hide);
		
		//绑定大图预览
		$("img","#product").click(tee.moveBigImage,function(){$(document.body).unbind("mousemove");});
		
		//绑定上一个产品
		$("#left").click(tee.prev);
		
		//绑定下一个产品
		$("#right").click(tee.next);
		
		//绑定分页导航
		$("#pagination a").click(tee.pagination);
		
		//绑定放入购物车
		$("#addcart").click(tee.addCart);
		
		//绑定放入收藏夹
		$("#addfavor").click(tee.addFavor);	
		
		//尺码说明
		$(".viewsize").click(function(){
			$("body").append('<div id=sizeform_bg></div><div id=sizeform><div class=content><img src=/images/icon/sizeform.png /></div><div class=footer><a href=javascript:void(0); class=back><span>返回</span></a></div></div>');
			$("#sizeform_bg").show();
			$("#sizeform").show();
			
			$(".back","#sizeform").click(function(){
				$("#sizeform_bg").remove();
				$("#sizeform").remove();
				return false;
			});
			return false;
		});
	},
	/**
	 * T恤预览
	 */
	start : function() {
		//遮罩层
		addOverlay();
		
		//显示加载动画
		$("#load").show();
		
		//调整弹出窗口位置
		setWindowPosition();
		
		//弹出窗口显示
		$("#window").show();
		
		//清除缩略图内容
		$("#pshow").html('');
		
		//清除尺码
		$("#size").html('');
		
		//取得产品id
		//不用var声明变量，ie下引起错误
		var pid = $(this).attr("id");
		
		//ajax取得产品数据
		$.ajax({
			type: "POST",
			url: baseurl+'index/index/product_id/'+pid,
			data: "product_id="+pid,
			dataType: "json",
			timeout: 50000,
			success: function(data){
				tee.update(data);
			},
			error: function() {
				alert("加载产品信息错误");
			}
		});
		return false;
	},
	/**
	 * 上一个产品
	 */
	prev : function() {
		var id = $("#pid").val();
		var prev_id = tee.getPrev(id);
		
		$("#load").show();
		$(".zoom > img ").attr("src","/images/icon/blank.gif");
		$("#pshow").html('');
		$("#pimg").html('');
		$("#size").html('');
		$("#product").removeClass("salemark");
		
		$.ajax({
			type: "POST",
			url: baseurl,
			data: "product_id="+prev_id,
			dataType: "json",
			timeout: 20000,
			success: function(data){
				tee.update(data);
			},
			error: function() {
				alert("something wrong");
			}
		});
		
	},
	next : function() {
		var id = $("#pid").val();
		var next_id = tee.getNext(id);
		
		$("#load").show();
		$(".zoom > img ").attr("src","/images/icon/blank.gif");
		$("#pshow").html('');
		$("#pimg").html('');
		$("#size").html('');
		$("#product").removeClass("salemark");
		$.ajax({
			type: "POST",
			url: baseurl,
			data: "product_id="+next_id,
			dataType: "json",
			timeout: 20000,
			success: function(data){
				tee.update(data);
			},
			error: function() {
				alert("something wrong");
			}
		});
	},
	getNext : function(id) {
		$.each($("dl","#content"),function(){
			if($(this).attr("id") == id) {
				var next_dl = $(this).next();
				temp =  next_dl.attr("id");
			}
		});
		if(!temp){
			temp = $("dl:first","#content").attr("id");
		}
		return temp;
	},
	getPrev : function(id) {
		$.each($("dl","#content"),function(){
			if($(this).attr("id") == id) {
				var prev_dl = $(this).prev();
				temp =  prev_dl.attr("id");
			}
		});
		if(!temp){
			temp = $("dl:last","#content").attr("id");
		}
		return temp;
	},
	/**
	 * 更新产品信息
	 */
	update : function(data) {
		
		//产品名称
		$("#name").text(data.PRODUCT_TITLE);
		
		//产品作者
		$("#author").html('作者： <a href="/user/index/index/uid/'+data.USER_ID+'">'+data.USER_NICKNAME+'</a>');
		
		//产品描述
		$("#desc").text("说明： "+data.PRODUCT_DESCRIPTION);
		
		//产品价格
		if( data.PRODUCT_SALES_PRICE != 0 ) {
			$("#price").text("¥"+data.PRODUCT_SALES_PRICE);
		}
		else {
			$("#price").text("¥"+data.PRODUCT_PRICE);
		}
		
		//产品款式
		$("#style").text(data.PRODUCT_STYLE);
		
		//主题
		$("#subject").text(data.SUBJECT_NAME);
		
		//颜色
		$("#color").text(data.COLOR_NAME);
		
		//产品id
		$("#pid").val(data.PRODUCT_ID);
		
		/**
		 * 输出产品图片
		 */
		$.each(data.PRODUCT_IMAGES,function(i,n){
			image_name = n.IMAGE_URL;
			$("#pshow").append('<li><a rel="'+image_name+'" href="javascript:void(0);" hidefocus="true"><img src="'+image_name+'_56x56.png" alt="'+n.IMAGE_TYPE+'" /></a></li>' );
			if(n.IMAGE_TYPE == 'front') {
				$("#pimg").append('<a  rel="'+image_name+'" href="javascript:void(0);" hidefocus="true"><img src="'+image_name+'_530x530.png" alt="正面大图" /></a>' );
			}
		});
		
		//暂时去除的功能，以后添加
		//相同图案不同底板的成品
		/*
		if( data.OTHER_PRODUCT != '' ) {
			$.each(data.OTHER_PRODUCT,function(i,n) {
				$("#pcolor").append('<li>一个</li>');
			});
			$("#samedesign").show();
		}
		else {
			$("#samedesign").hide();
		}
		*/
		
		//产品图片绑定动作
		$("#pshow > li > a").click(tee.changeView);
		
		//产品预览图绑定动作
		$("#pimg > a").click(tee.loadBigImage);
		
		//特价标志
		if( data.PRODUCT_SALES_PRICE != 0 ) { $("#product").addClass("salemark");}
				
		//输出产品尺码选择列表
		var product_size = data.PRODUCT_SIZE.split(";");
		$.each(product_size,function(i,n){
			$("#size").html($("#size").html()+"<option value="+n+">"+n+"</option");
		});
		
		$("#load").hide();

	},
	/**
	 * 更换预览图片
	 */
	changeView : function() {
		
		$("#load").show();
		
		//清除大图内容
		$(".zoom > img ").attr("src","/images/icon/blank.gif");
        
		//预览图显示
		$("a","#pimg").show();
        
		//取得图片地址
		image_name = $(this).attr("rel");

		//图片加载完成后 加载动画隐藏
		$("#pimg > a > img").load(function() {
			$("#load").hide();
		});
		
		$("#pimg > a > img").attr("src",image_name+'_530x530.png');
		$("#pimg > a").attr("rel",image_name);
	},
	/**
	 * 加载大图
	 */
	loadBigImage : function() {
		$("img",".zoom").load(function() {
			$("#load").hide();
		});
		$("#load").show();
		
		$("a","#pimg").hide();
		
		var image_name = $(this).attr("rel");
		
		
		$("img",".zoom").attr("src",image_name+"_1174x1174.png");
		$("img",".zoom").attr("rel",image_name);
		
		//$("img","#product").click(this.moveBigImage,function(){$(document.body).unbind("mousemove");});
		
		tee.moveBigImage();
		//初始化大图位置
		$("div.zoom").css({ top: -252,left: -197 });
		return false;
	},
	/**
	 * 大图跟随鼠标移动
	 */
	moveBigImage : function() {
		$("#window").mousemove(function(e){	
    		mouse = new MouseEvent(e);
			
    		//var bigwidth = $(".zoom").get(0).offsetWidth;
    		//var bigheight = $(".zoom").get(0).offsetHeight;
    		//alert(bigwidth);
    		var bigwidth = 1174;
    		var bigheight = 1174;
    
    		xpos = mouse.x-219 - bigwidth/2;
    		ypos = mouse.y-30 - bigheight/2;
    		$("div.zoom").css({ top: ypos,left: xpos });
			
		});
		$(document.body).unbind("mousemove");
		$("#window").click(function(){	
			tee.closeBigImage();
		});
		$("#window").css("cursor","url(/images/icon/zoomout.cur), default");
		
	},
	closeBigImage : function() {
		//取得图片地址
		var image_name = $("img",".zoom").attr("rel");

		//图片加载完成后 加载动画隐藏
		$("#pimg > a > img").load(function() {
			$("#load").hide();
		});
		
		if( image_name != '') {
			$("#load").show();
	
			//清除大图内容
			$(".zoom > img ").attr("src","/images/icon/blank.gif");
			$(".zoom > img ").attr("rel"," ");
			
			//预览图显示
			$("a","#pimg").show();
							
			$("#pimg > a > img").attr("src",image_name+'_530x530.png');
			$("#pimg > a").attr("rel",image_name);
			
			$("#window").unbind("click");
			$("#window").css("cursor"," ");
		}
		
	},
	view : function() {
		$("#pimg").hover(function(){
			$("body").append("<div id='ptool'><span>放大</span><span>缩小</span></div>");
			
		},function(){
			$("#ptool").remove();
		});
		
	},
	hide : function() {
		tee.closeBigImage();
	    $("#pimg").html('');
	    $("#pshow").html('');
	    $("img",".zoom").attr("src","/images/icon/blank.gif");
	    $("#product").removeClass("salemark");
		$("#pid").val('');
		//$("#load").hide();
	    hideWindow();  
	},
	pagination : function() {
		$("#load").show();
		var href = $(this).attr("href");
		
		$.ajax({
			type: "POST",
			url: href,
			dataType: "html",
			success: function(data){
				$("#content").replaceWith(data); 
				$("#load").hide();
				initProduct();	//init ths product view
				tee.init();
				$('html').animate({scrollTop:0}, 'slow'); 
			},
			error: function() {
				alert("something wrong");
			}
		});
		return false;
	},
	addCart : function() {
		var pid = $("#pid").val();
		$("span",".button").text('查看购物车');
		$(".button").attr("href","/cart");
		var pnum = $("#num option:selected").val();
		var psize = $("#size option:selected").val();
		$(".back","#message").click(function() {
			$("#message").hide();
			$("#message_bg").hide();
			$("#message > .content").text('');
		});
		$.ajax({
			type: "POST",
			url: '/cart/index/add/',
			data : 'id='+pid+'&num='+pnum+'&size='+psize,
			dataType: "html",
			success: function(data){
				
				$("#message > .content").text(data);
				$("#message_bg").show();
				$("#message").show();
				
				//$("#message_bg").animate({ opacity: 'hide'},8000,function(){$("#message > .content").text('');});
				//$("#message").animate({ opacity: 'hide'},8000,function(){$("#message > .content").text('');});
				
			},
			error: function() {
				alert("something wrong");
			}
		});
		return false;
	},
	addFavor : function() {
		var pid = $("#pid").val();
		$("span",".button").text('查看收藏夹');
		$(".button").attr("href","/account/favorite");
		$(".back","#message").click(function() {
			$("#message").hide();
			$("#message_bg").hide();
			$("#message > .content").text('');
		});
		
		$.ajax({
			type: "POST",
			url: '/account/favorite/add/',
			data : 'id='+pid,
			dataType: "html",
			success: function(data){
				$("#message > .content").text(data);
				$("#message_bg").show();
				$("#message").show();
				//$("#message_bg").animate({ opacity: 'hide'},8000,function(){$("#message > .content").text('');});
				//$("#message").animate({ opacity: 'hide'},8000,function(){$("#message > .content").text('');});
			},
			error: function() {
				alert("something wrong");
			}
		});
		return false;
	},
	addViewTool : function() {
	    $("body").append('<div id="viewTool"><a href="javascript:void(0);" class="zoomin">放大</a> <a href="javascript:void(0);" class="back">还原</a></div>');
        
	    return false;
	}
}

/**
 * 鼠标经过产品效果
 */
function initProduct() {
	//mouseover effect
	$("dl > dd.img").mouseover(function(){
		$(this).addClass("onmouseover").mouseout(function(){
			$(this).removeClass("onmouseover");
		});
		
	});
}
