$(function(){
	//某个页面需要单双行不同颜色的
	$('.product-body-right-body-table > tbody > tr:even').addClass('product-body-table-even');

	//为每一个商品的页面自动打开默认页面
	var product_li_current_num = $('.product-body-right-title li.current').attr('value');
	$('.product-body-right-body').hide();
	$('#product-body-right-body' + product_li_current_num).show();

	//每个订购页面默认选中第一只颜色
	$('.deluxe-body-right input[name=sourceProductId]:first').attr('checked', 'checked');

	//自动对应匹配颜色下拉框(上)
	$('select[name=product_id]').change(function(){
		var value = $(this).val();
		if(value == ''){return false;}
		$('select[name=sourceProductId] option').removeAttr('selected');
		$('select[name=sourceProductId] option[value='+value+']').attr('selected', 'selected');
	});

	//自动对应匹配颜色下拉框(下)
	$('select[name=sourceProductId]').change(function(){
		var value = $(this).val();
		if(value == ''){return false;}
		$('select[name=product_id] option').removeAttr('selected');
		$('select[name=product_id] option[value='+value+']').attr('selected', 'selected');
	});
});

isDOM=document.getElementById?true:false;
isOpera=isOpera5=window.opera && isDOM;
isOpera6=isOpera && window.print;
isOpera7=isOpera && document.readyState;
isMSIE=isIE=document.all && document.all.item && !isOpera;
isStrict=document.compatMode=='CSS1Compat';
isNN=isNC=navigator.appName=="Netscape";
isNN4=isNC4=isNN && !isDOM;
isMozilla=isNN6=isNN && isDOM;

function showdisblg(obj, ow)
{
	var marginT=50;
	if (isMSIE)
	{
		var selectoff=document.getElementsByTagName("select");
		for (var i=0; i<selectoff.length; i++)
		{
			if (selectoff[i].className.match("canhide")!==null)
			{
				selectoff[i].style.display="none";
			}
		}
	}
	var obj=document.getElementById(obj);
	obj.style.display="block";
	obj.style.zIndex="111100";
	if(ow !== undefined){obj.style.width = ow;}//设置宽度
	obj.style.left = (document.documentElement.clientWidth - obj.offsetWidth)/2 + "px";
	if (!isOpera)
	{
		if (obj.offsetHeight<document.documentElement.clientHeight)
		{
			obj.style.top=document.documentElement.scrollTop+(document.documentElement.clientHeight-obj.offsetHeight)/2+"px";
		}
		else
		{
			obj.style.top=(document.documentElement.scrollTop)+marginT+"px";
		}
	}
	else
	{
		if (obj.offsetHeight<window.innerHeight)
		{
			obj.style.top=document.documentElement.scrollTop+(window.innerHeight-obj.offsetHeight)/2+"px";
		}else{
			obj.style.top=(document.documentElement.scrollTop)+marginT+"px";
		}
	}
	if(document.getElementById('bgDiv'))
	{
		document.getElementById('bgDiv').style.display="block";
		document.getElementById('bgDiv').style.height=document.documentElement.scrollHeight>document.documentElement.clientHeight?document.documentElement.scrollHeight:document.documentElement.clientHeight+"px";
	}
	else
	{
		sWidth=document.documentElement.scrollWidth>document.documentElement.clientWidth?document.documentElement.scrollWidth:document.documentElement.clientWidth;
		sHeight=document.documentElement.scrollHeight>document.documentElement.clientHeight?document.documentElement.scrollHeight:document.documentElement.clientHeight;
		var bgObj=document.createElement("div");
		bgObj.setAttribute('id','bgDiv');
		bgObj.style.position="absolute";
		bgObj.style.top="0";
		bgObj.style.background="#999999";
		bgObj.style.filter="alpha(opacity=30);";
		bgObj.style.opacity="0.3";
		bgObj.style.left="0";
		bgObj.style.width=sWidth+ "px";
		bgObj.style.height=sHeight + "px";
		bgObj.style.zIndex = "10000";
		document.body.appendChild(bgObj);
	}
}

function hidedisblg(obj)
{
	document.getElementById('bgDiv').style.display="none";
	//obj.parentNode.style.display="none";
	$(obj).parents(".floatset").hide();
	var selecton=document.getElementsByTagName("select");

	if (isMSIE)
	{
		var selecton=document.getElementsByTagName("select");
		for (var i=0; i<selecton.length; i++)
		{
			if (selecton[i].className.match("canhide")!==null)
			{
				selecton[i].style.display="";
			}
		}
	}
	return false;
}

//首页的图片轮换
$.fn.extend({
	TrunAd2:function(options){
		var auto=null;
		var obj=$(this);
		count=$("a",obj).size();
		n=0;
		var settings={timer:5000,menu:"#play_text"};
		options = options || {};
		$.extend(settings, options);
		var ulcontent="<ul>";
		for(i=1;i<=count;i++){ulcontent=ulcontent+"<li>"+i+"</li>";}
		ulcontent=ulcontent+"</ul>";
		$(settings.menu).html(ulcontent);
		$("a:not(:first-child)",this).hide();
		$(settings.menu+" li").eq(0).css({"background":"#7f0019","color":"#fff","font-weight":"bolder"});
		$(settings.menu+" li").mouseover(function() {
			i = $(this).text()-1;
			n=i;
			if (n >= count) return;
			$("a",obj).filter(":visible").fadeOut(200,function(){$(this).parent().children().eq(n).fadeIn(300);});
			$(this).css({"background":"#7f0019","color":"#fff","font-weight":"bolder"}).siblings().css({"background":"#fff","color":"#7f0019","font-weight":"normal"});
		});
		auto = setInterval(showAuto, settings.timer);
		obj.hover(function(){clearInterval(auto)}, function(){auto = setInterval(showAuto, settings.timer);});
		function showAuto()
		{
			n = n >= (count - 1) ? 0 : ++n;
			$(settings.menu+" li").eq(n).trigger('mouseover');
		}
	}
});
$(function(){
	$('#play_list').TrunAd2();
});
/*************************************************** 会员内页部分 ***************************************************/
//弹出详细信息
function toggleDetail(name, id) {
	var name = name + id;
	$("#"+name).toggle();
	return false;
}

/*************************************************** 订单部分 ***************************************************/
//调整购买数量时候的JS
function numIncrease()
{
	var num = parseInt($('#amount').val());
	$("input[name=amount]").each(function(){
    if(num < 10 && num > 0)
    {
				$(this).val(parseInt($('#amount').val()) + 1);
				$("#theProductTotalPrice").html($("#theProductPrice").attr("perPrice")*(num+1));
    }
	});
}

function numDecrease()
{
	var num = parseInt($('#amount').val());
	if(num > 1 && num <11)
	{
		$("input[name=amount]").each(function(){
			$(this).val(num - 1);
		});
		$("#theProductTotalPrice").html($("#theProductPrice").attr("perPrice")*(num-1));
	}
}


//判断如果来时的页面是#comment
$(function(){
	if($(".product-body-right-title") != '')
	{
		//首先绑定click事件
		$('.product-body-right-title > .current,.list').bind('click', function(){
			if($(this).attr('class') == 'current')
			{
				//什么都不用做
				return false;
			}
			$('.product-body-right-title > .current').removeClass('current').addClass('list');
			$(this).removeClass('list').addClass('current');
			var id = $(this).attr('value');
			$('.product-body-right .product-body-right-body').hide();
			$('#product-body-right-body'+id).show();
		});
		//然后触发comment事件
		commentTrigger();
	}
});

//打开页面触发事件
function productTrigger(num)
{
	if($(".product-body-right-title li[value="+num+"]").html != '')//存在才执行
	{
		location.href = '#product-body-right';
		$(".product-body-right-title li[value="+num+"]").trigger("click");
	}
}
//虽然改名commentTrigger，但已经不只是用于comment了
function commentTrigger(){
	var p = /#.*/i;
	var url = window.location.href;
	qs = p.exec(url);
	if(qs == '#comment'){
		productTrigger(3);
	}else if(qs == '#order'){
		productTrigger(5);
	}else if(qs == '#detail'){
		productTrigger(2);
	}else if(qs == '#question'){
		productTrigger(4);
	}else if(qs == '#service'){
		productTrigger(8);
	}
}

$(function(){
	//如果是#order的link即弹到订购页面
	$("area[href$='#order']").bind('click', function(){
		productTrigger(5);
	});

	//绑定分类图片上面的弹出效果
	$(".pic").bind("mouseleave", function(){PushObj();}).bind("mouseenter", function(){SetObj(this);});

	//刷新验证码
    $("#regCode").click(function(){
      var i = Math.round(Math.random()*10000);
        $("#sCode").attr('src', 'index.php?id=356&r='+i);
    });
	
	//同步购买商品数量
	$("input[name=amount]").bind('change', function(){
		var order_amount = $(this).val();
		if(order_amount > 10)
		{
			order_amount = 10;
		}
		if(order_amount < 1)
		{
			order_amount = 1;
		}
		$("input[name=amount]").each(function(){
			$(this).val(order_amount);
		});
		$("#theProductTotalPrice").html($("#theProductPrice").attr("perPrice")*order_amount);
	});
		

	//下拉颜色的时候购物框的隐藏也要变
	$('select[name=product_id]').bind('change', function(){
		$('#product-body-right-body5').find('input[name=product_id]').val($(this).val());
	});

	//浮动弹出菜单
	$('.floor_nav li').hover(function(){
		var name = $(this).attr('class');
		var left = $(this).position().left;
		var top = $(this).position().top;
		left = left - 80;
		top = top + 28;

		$me = $('#popmenu_'+name);
		$me.css('left', left);
		$me.css('top', top);
		$me.css('z-index', '2');	//for the cover,created by joyce 2010.6.21
		$me.find('iframe').css({width: $me.width(), height: $me.height()});
		$me.show();
	},function(){
		var name = $(this).attr('class');
		$('#popmenu_'+name).hide();
	});
	$('.popmenu').hover(function(){
		var id = $(this).attr('id');

		$me = $('#'+id);
		$me.show();
	},function(){
		var id = $(this).attr('id');

		$me = $('#'+id);
		$me.hide();
	});
});

//写trace测试函数
function w(txt){
	$('.hd-right').html(txt);
}


/******************************** 页面左方产品分类列表 隐藏/显示 st *********************************/
$(document).ready(function(){
productLeftListShow();

$("#bd .title").click(function(){
	$(this).siblings("ul").toggle();														 
})

function productLeftListShow()
{
	var theUrl = location.href;
	theUrl = theUrl.match(/id=([0-9]+)/i);
	if(theUrl != null)
	{
		$(".theHideUl").hide();
		$("#productLeftList_ul"+theUrl[1]+" ul").show("");
	}
}

})		//the one of the "$(document).ready()" end

/******************************** 页面左方产品分类列表 隐藏/显示 end *********************************/



/***************************产品页面的2个购买表单的提交,任一或2个 st*************************************/
$(document).ready(function(){
	$("#formProductInfo2 input").removeAttr("checked");		//去除#formProductInfo2 input按钮被选中的状态
	$("#theProductTotalPrice").html(1*$("#theProductPrice").attr("perPrice"));		//默认用户购买数量为1

$("#formProductInfo1 label span,#formProductInfo2 label input").click(function(){
	/**********核心功能,拿取产品的目标属性进行处理,通过用户对产品的选择;显示给用户关心的信息以及提交必要数据**********/

	var theProductIdColor = $(this).attr("thecolor");		//还原产品颜色
	var product_id = $(this).attr("value");		//还原产品唯一id
	$("#theProductIdColor").html(theProductIdColor);		//把产品颜色显示到目标位置
	$("#formProductInfo2 label input").removeAttr("checked");		//先去除"在线订购"中全部按钮选中的状态
	eval($("#formProductInfo2 label input[value='"+product_id+"']").attr("checked","checked"));		//再为"在线订购"中目标产品设选中状态
	$("#formProductInfo1 input[name='product_id']").val(product_id);		//把目标产品id添加到#formProductInfo1表单中隐藏文本框里,成为任一表单提交时所需的其中一个值(此处为product_id值)
	
	/**********样式视觉功能,并不必需但为满足用户视觉体验的样式设计(即:删除也不影响关键功能的实现)**********/
	$("#formProductInfo1 label span,#formProductInfo2 label span").css({border:"1px solid #DCDCDC",padding:"3px 29px 1px 10px"});
	//先重设待定目标元素样式
	eval($("#formProductInfo1 label span[value='"+product_id+"'],#formProductInfo2 label span[value='"+product_id+"']").css({border:"2px solid #336699",padding:"2px 28px 0px 9px"}));		//为目标元素添加指定样式
	
	
});		//the one of the "$(document).ready()" end


/**********异步,根据用户选择的颜色,载入基于指定属性的产品视图,放大镜/图片浏览;id=882为测试页面,请按需使用**********/
$(document).ready(function(){
    $("#formProductInfo1 label span,#formProductInfo2 label input").attr("beChoosed","no");
		
		$("#formProductInfo1 label span,#formProductInfo2 label input").click(function(){
        //alert($(this).attr("beChoosed"));
		if($(this).attr("beChoosed") == "yes")
		{
		
		}

		else if ($(this).attr("beChoosed") == "no")
		{
			loadZoom($(this).attr("value"));
			$("#formProductInfo1 label span").attr("beChoosed","no");
			 $("#formProductInfo2 label input").attr("beChoosed","no");
			$(this).attr("beChoosed","yes");
		}

});

function loadZoom(zoomproductid, id)
{
		if(id === undefined){id = 890;}
		
		$.ajax({
				type: "GET",
				//url: "http://xinw.net/index.php?id=890",
				url: "index.php?id="+id,
				data: "zoomproductid="+zoomproductid,
				beforeSend: function()
				{
					$("#loadTheZoom").html("<div style='width:359px; text-align:center; padding-top:165px;'>"+"<img src='http://xinw.net/assets/themes/v100409/images/loadingimg01.gif' width='100' height='100' />"+"</div>");
				},
				success: function(a)
				{           
					 $("#loadTheZoom").html(a);
					 bindRollPics();	//经过预览小图换大图
				}
		})
}

});		//the one of the "$(document).ready()" end





/**********核心功能,实现产品页面第1个表单的"小计";根据用户输入的购买数量,与产品单价相乘,于指定目标位置显示小计数目**********/
$("#theProductAmout,#amount").change(function(){
	if($(this).val() < 10 && $(this).val() > 1)
	{
		$("#theProductTotalPrice").html($("#theProductPrice").attr("perPrice")*$(this).val());
	}	
})


})
/***************************产品页面2个购买表单的提交,任一或2个 end*************************************/


/***************************#floatInTip被用户点击关闭后, 1小时内不再已存在的页面弹出 st****************************************/
$(document).ready(function(){
	if($("#floatInTip"))
	{
		$("#closeFloatInTip").click(function(){
			setCookie("closeFloatInTip", "yes", 1, "/")
		});
	}
	else
	{
		
	}
});		//$(document) end

function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure)
{
    var sCookie = sName + "=" + encodeURIComponent(sValue);

    if(oExpires){
        var date = new Date();
        date.setHours(date.getHours()+oExpires);
        sCookie += "; expires=" + date.toGMTString();
    }

    if(sPath){
        sCookie += "; path=" + sPath;
    }

    if(sDomain){
        sCookie += "; domain=" + sDomain;
    }

    if(bSecure){
        sCookie += "; secure";
    }
    //alert(sCookie);
    document.cookie = sCookie;
}

function getCookie(sName)
{
    var sRE = "(?:;)?" + sName +"=([^;]*);?";
    var oRE = new RegExp(sRE);

    if(oRE.test(document.cookie)){
        return decodeURIComponent(RegExp["$1"]);
    }
    else
    {
        return null;
    }
}
/***************************#floatInTip被用户点击关闭后, 1小时内不再已存在的页面弹出 end****************************************/
