/**
* @author shengliang.yaosl
* @version 2008-9-10
*/
(function(){
var imgSpeend = 30; 		//设定小图滚动的时间
var img = $('#srcImg');		//获取图片对象
var zimg = $('#zoomImg') 	//获取图片放大的对象
var zoomRate;				//图片缩放比例
var oPos;
var isOut = false;
$(document).ready(function(){
var oScroll = $('#more_main_all li');
var imgUl = $('#more_main');
var imgUl1 = $('#more_main_all');
zimg.attr('src',img.attr('src'));
if(oScroll.length && oScroll.length>4){
imgUl1.html(imgUl1.html()+imgUl1.html()); //复制滚动层的代码
var mq = setInterval(marqueeImg,imgSpeend);//开始滚动
//鼠标上移时停止滚动
imgUl.bind('mouseover',function(){
clearInterval(mq);
});
//鼠标移开时重新滚动
imgUl.bind('mouseout',function(){
mq = setInterval(marqueeImg,imgSpeend);
});
}
function marqueeImg(){
if( 1800 - imgUl.attr('scrollLeft')<=0){
imgUl.attr('scrollLeft',0) ;
}else{
imgUl.attr('scrollLeft',imgUl.attr('scrollLeft')+1)
}
}
//调整图片大小
if(document.getElementById('srcImg').readyState){
if(document.getElementById('srcImg').readyState == 'complete'){
fitSize(img,490,270);
}else{
img.load(function(){
fitSize(img,490,270);
});
}
}else{
fitSize(img,350,250);
}
//切换图片
$('#pic_m li').bind('click',function(){
$('#pic_m li').removeClass('now');
$(this).addClass('now');
var s = $(this).attr('limg');
img.hide();
img.attr('src',s);
zimg.attr('src',img.attr('src'));
if (img.attr('readyState') && img.attr('readyState') == 'complete') {
fitSize(img,350,350);
}else{
img.load(function(){
fitSize(img,350,350);
});
}
});
//自动播放
$('#switch').bind('click',function(event){
if($(this).hasClass('autoplay')){
$(this).removeClass('autoplay');
$(this).addClass('stopPlay');
}else{
$(this).removeClass('stopPlay');
$(this).addClass('autoplay');
}
});
});
function fitSize(oImg,maxWidth,maxHeight){
//先清除上一次对图片设定的宽和高
oImg.width('auto');
oImg.height('auto');
oW = oImg.width(); 	//获取图片的原始宽度
oH = oImg.height();	//获取图片的原始高度
bl = oW / oH;		//获取图片的宽高比
if( bl>=1 ){
if (oW > maxWidth){
zoomRate = oW/350;
oImg.width(maxWidth);
oImg.height(maxWidth/bl);
isOut = false;
}else{
isOut = true;
}
if(oImg.height()<maxHeight){
oImg.css('marginTop',(maxHeight-oImg.height())/2); //调整margin让图片垂直居中显示
}else{
oImg.css('marginTop',0);
}
}else{
if(oH >=maxHeight){
zoomRate = oH/350;
oImg.width(maxHeight*bl);
oImg.height(maxHeight);
isOut = false;
}else{
isOut = true;
}
if(oImg.height()<maxHeight){
oImg.css('marginTop',(maxHeight-oImg.height())/2); //调整margin让图片垂直居中显示
}else{
oImg.css('marginTop',0);
}
}
if(isOut){
$('#guid').remove();
$('#guider').remove();
oImg.show('slow');
}else{
oImg.show('slow',function(){
checkImgSize(oImg);
});
};
}
function checkImgSize(oImg){
$('#guid').remove();
$('#guider').remove();
if(!isOut){
oPos = oImg.offset();
var guidContainer = $('<div id="guid"></div>');
var guidObject = $('<img src="http://img.china.alibaba.com/images/detailpic/guid.gif" id="guider" />');
guidContainer.css('opacity',0.5);
$('#pic').append(guidContainer);
//$('#pic').append(guidObject);
guidObject.bind('mouseover',function(){			//		鼠标上移变色
guidContainer.css('backgroundImage','url(http://img.china.alibaba.com/images/detailpic/guid_big_h.gif)')
guidContainer.css('opacity',0.3);
});
guidObject.bind('mouseout',function(){
guidContainer.css('backgroundImage','url(http://img.china.alibaba.com/images/detailpic/guid_big.gif)');
guidContainer.css('opacity',0.5);
});
img.unbind('mousemove');
img.css('cursor','pointer');
guidObject.bind('click',function(){
$('#guid').remove();
$('#guider').remove();
//获取原始图片的缩放比例
var	w = 350/zoomRate/2;
var	h = 350/zoomRate/2;
//触发放大镜
img.bind('mousemove',function(event){
if(isOut){
img.css('cursor','pointer');
return;
}else if(!oPos){
return;
}
$('#memo').hide();
$('#pic_zoom').show();
img.css('cursor','url(http://img.china.alibaba.com/images/detailpic/pointer.ico), auto');
var x = event.pageX - oPos.left;
x=x<(img.width()-w)?x<w?w:x:img.width()-w;
zimg.css('marginLeft',(w-x)*zoomRate);
var y = event.pageY - oPos.top;
y=y<(img.height()-h)?y<h?h:y:img.height()-h;
zimg.css('marginTop',(h-y)*zoomRate);
});
//图片放大镜隐藏
img.bind('mouseout',function(){
$('#pic_zoom').hide();
$('#memo').show();
});
});
}else{
return;
}
}
})();
