$(document).ready(function(){
	// 图片滚动
	var liW = $('#pSmallImageDiv li').outerWidth(true);
	$('#pSmallImageDiv ul').width(liW * $('#pSmallImageDiv li').length);
	$('#pSmallImageDiv').html('<div class="smlimgs">'+ $('#pSmallImageDiv').html() +'</div>');
	$('#pSmallImageDiv').prepend('<div class="scrlpre" />');
	$('#pSmallImageDiv').append('<div class="scrlnext" />');
	$('#pSmallImageDiv .scrlpre').click(function(){chhk(this);});
	$('#pSmallImageDiv .scrlnext').click(function(){chhk(this);});
	function chhk(o) {
		var oUL = $('#pSmallImageDiv ul');
		var ulL = parseInt(oUL.css('margin-left'), 10);
		if (o)
		if ($(o).attr('class').indexOf('scrlpre') != -1) {
			if (ulL < 0) ulL += liW;
		}
		else if ($(o).attr('class').indexOf('scrlnext') != -1) {
			if (oUL.outerWidth(true) - oUL.parent().width() > 8) ulL -= liW;
		}
		oUL.css('margin-left', ulL+'px');
		$('#pSmallImageDiv .scrlpre').removeClass('hover1');
		$('#pSmallImageDiv .scrlnext').removeClass('hover2');
		if (ulL < 0) $('#pSmallImageDiv .scrlpre').addClass('hover1');
		if (oUL.outerWidth(true) - oUL.parent().width() > 8) $('#pSmallImageDiv .scrlnext').addClass('hover2');
	}
	chhk();

	// 图片变化
	function switchImg(obj) {
		var mdlImgSrc = obj.src;		//此处路径需修改
		$(obj).parent().siblings().removeClass('hover');
		$(obj).parent().addClass('hover');
		$('#pMiddleImageDiv img').remove();
		$('#pMiddleImageDiv').html('<img src="'+ mdlImgSrc +'" />');
		var mdlImg = $('#pMiddleImageDiv img');
		if (mdlImg.height() > mdlImg.width()) {
			mdlImg.css('height', '210px');
		}
		else {
			mdlImg.css('width', '210px');
			var mTop = (mdlImg.parent().height() - mdlImg.height()) / 2;
			mdlImg.css('margin-top', mTop+'px');
		}
	}
	$('#pSmallImageDiv li img').mouseover(function(){switchImg(this)});
	switchImg($('#pSmallImageDiv ul li img').get(0))

	// 放大图显示
	$('#pMiddleImageDiv').parent().prepend('<div id="pBigImageDiv" class="picZoomIn"><img /></div>');
	$('#pMiddleImageDiv').hover(function() {
		$('#pMiddleImageDiv').prepend('<div />');
		var mdlImg = $(this).children('img');
		var mdlDiv = $(this).children('div');
		var bigImg = $('#pBigImageDiv img');
		//
		var bigImgSrc = mdlImg.attr('src');		//此处路径需修改
		bigImg.attr('src', bigImgSrc);
		$('#pBigImageDiv').show();
		//
		var mLeft = ($(this).width() - $(this).children('img').width()) / 2;
		var mTop = ($(this).height() - $(this).children('img').height()) / 2;
		$(this).children('img').css('margin-top', mTop+'px');
		//
		var zoneWidth = $('#pBigImageDiv').width() / bigImg.width() * mdlImg.width();
		var zoneHeight = zoneWidth * $('#pBigImageDiv').height() / $('#pBigImageDiv').width();
		if (zoneWidth > mdlImg.width() ) zoneWidth = mdlImg.width();
		if (zoneHeight > mdlImg.height() ) zoneHeight = mdlImg.height();
		mdlDiv.css('width', zoneWidth + 'px').css('height', zoneHeight + 'px').end();
		mdlDiv.css('visibility', 'visible');
		//
		var imageLeft = mdlImg.offset().left;
		var imageTop = mdlImg.offset().top;
		var imageWidth = mdlImg.get(0).offsetWidth;
		var imageHeight = mdlImg.get(0).offsetHeight;

		$(document.body).mousemove(function(e) {
			var mouse = new function() {
				this.x = e.pageX;
				this.y = e.pageY;
			};
			var xpos = mouse.x - zoneWidth / 2 - imageLeft;
			var ypos = mouse.y - zoneHeight / 2 - imageTop;
			if (xpos < 0) xpos = 0;
			if (ypos < 0) ypos = 0;
			if (xpos + zoneWidth > imageWidth) xpos = imageWidth - zoneWidth;
			if (ypos + zoneHeight > imageHeight) ypos = imageHeight - zoneHeight;

			$('#pMiddleImageDiv div').css('margin-left', (mLeft + xpos) + 'px').css('margin-top', (mTop + ypos) + 'px').end();
			var imageRate = $('#pBigImageDiv img').width() / imageWidth;
			$('#pBigImageDiv img').css('margin-left', (0 - xpos * imageRate) + 'px').css('margin-top', (0 - ypos * imageRate) + 'px').end();
		});
	},function() {
		$('#pBigImageDiv').hide();
		$(this).children('div').css('visibility', 'hidden');
	});

	// 比较价格 商品参数 发表评论
	$('.cardGroupB .cardHead li').click(function(){
		var cheads = $('.cardGroupB .cardHead li');
		var cbodys = $('.cardGroupB .cardBody .cardContent');
		cheads.removeClass('hover');
		cbodys.hide();
		for (var i=0; i<cheads.length; i++)
		{
			if (cheads[i] == this) break;
		}
		$(cheads[i]).addClass('hover');
		$(cbodys[i]).show();
	});
	$('.cardGroupB .cardHead li:first-child').click();
	// 查看评论 发表评论

	//获得淘宝客数据
	if(AJAX_EXACT_RATE_100)
	{
		getTaobaokeItems(RETURN_ID_100,PRODUCT_ID_YICIKE,SEARCH_KEYWORD_100,'100',LIMIT_100);
	}
});

function getTaobaokeItems(return_id,product_id_yicike,keyword,exact_rate,limit)
{
	$.ajax({
		type:'POST',
		url:script_root+'/ajaxData/taobaokeItems.php',  //ajax获得淘宝客内容
		data:'keyword='+keyword+'&product_id_yicike='+product_id_yicike+'&exact_rate='+exact_rate+'&limit='+limit,
		success:function(msg){
			$('#'+return_id).html(msg); 
		}
	});
}

//复制网址
copyToClipboard = function(txt) {
	if(window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
		alert('已复制到粘贴板，在 MSN 或 QQ 中粘贴，即可发送给您的好友。');
	}
	else if(navigator.userAgent.indexOf("Opera") != -1) {
		window.location = txt;
	}
	else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch (e) {
			alert("您的firefox安全限制限制您进行剪贴板操作，请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试");
			return false;
		}
	}
}

 //举报商品
 
//创建XMLHttpRequest对象。

function createXMLHttpRequest() {
     if (window.ActiveXObject) {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
     } 
     else if (window.XMLHttpRequest) {
         xmlHttp = new XMLHttpRequest();
     }
}
    
function startRequest(value) {
	createXMLHttpRequest();
	
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", "/jubao.php?id="+value, true);//
	xmlHttp.send(null);//发送该请求
}

 //处理请求
function handleStateChange() {
	//XMLHttpRequest对象的ReadyState属性值4   描述一种"已加载"状态；此时，响应已经被完全接收。
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			//responseText属性
			//alert("fes");
		　　//这个responseText属性包含客户端接收到的HTTP响应的文本内容
			alert(xmlHttp.responseText);//
		}
	}
}


