﻿// JScript 文件

var layerHMargin=20;//弹出层与事件源的水平间距
var layerVMargin=15;//弹出层与事件源的垂直间距
var layerVJiaoMargin=10;
createLayer=function(o)
{
    var layerObj=getObj('divLayer');
    if(!layerObj) return;
    var senderObj=getElementAbsolutePosition(o);
    var objTop=document.documentElement.clientHeight+document.documentElement.scrollTop;
    var objLeft=document.documentElement.clientWidth+document.documentElement.scrollLeft;
    var scrollLeft=document.documentElement.scrollLeft;
    var sanJiaoLeftSrc='url(/images/common/sanJiao_Left.jpg)';
    var sanJiaoRightSrc='url(/images/common/sanJiao_Right.jpg)';
    with(layerObj.style)
    {
        var sanJiao=getObj('sanJiao');//三角
        if(objTop<senderObj.top+posHeight)
        {
            top=objTop-(posHeight+parseInt(borderWidth)*2);
            if(sanJiao)
            {
                if(objTop-senderObj.top<layerVJiaoMargin)
                {
                   sanJiao.style.top=senderObj.top-sanJiao.style.posHeight;
                }else
                {
                    if(objTop-senderObj.top<layerVJiaoMargin+sanJiao.style.posHeight)
                    {
                        sanJiao.style.top=objTop-sanJiao.style.posHeight;
                    }else
                    {
                        sanJiao.style.top=senderObj.top+layerVJiaoMargin;
                    }
                }
                sanJiao.style.display='block';
            }
        }else
        {
            top=senderObj.top-layerVMargin;
            if(sanJiao)
            {
               sanJiao.style.top=senderObj.top+layerVJiaoMargin;
               sanJiao.style.display='block';
            }
        }
        var center=scrollLeft+document.documentElement.clientWidth/2;//中心位置
        if(senderObj.left+senderObj.width<center)
        {
            //右侧显示
            left=senderObj.left+(senderObj.width+layerHMargin);
            sanJiao.style.left=posLeft-sanJiao.offsetWidth+parseInt(borderWidth);
            sanJiao.style.backgroundImage=sanJiaoLeftSrc;
        }else
        {
          //左侧显示
            left=senderObj.left-(posWidth+layerHMargin);
            sanJiao.style.left=posLeft+posWidth+parseInt(borderWidth);
            sanJiao.style.backgroundImage=sanJiaoRightSrc;
        }
        layerObj.style.display='block';
    }
    //数据写入
    var layerImg=getObj('layerImg');
    if(layerImg)
    {
          var re=/[_]\d+[.]/g;
          layerImg.src=o.src.replace(o.src.match(re),'.');   
    }
    var productObj;
    if(o.configData!='')
    {
        productObj=eval(o.configData);
        o.config=productObj;
        o.configData='';
    }else
    {
        productObj=o.config;
    }
    if(productObj){
        var divProductInfo=document.getElementById('divProductInfo');
        if(!divProductInfo) return;
        var strHtml='';
        strHtml+=' <div style="margin:6px 0px;font-size:14px;">';
        strHtml+='<span  style="color:#333335;font-weight:bold;font-size:14px;">'+productObj.formatShortName+'</span>';
        strHtml+='</div>';
        strHtml+='规格：'+productObj.formatName+'<br/>';
        if(productObj.pageCount!=null){
            strHtml+='页数：'+productObj.pageCount+'<br/>';
        }
        if(productObj.flag!='undefined' && productObj.flag.toLowerCase()=='true'){
             strHtml+='原价：<span style="text-decoration:line-through;">￥'+formatToDecimal(productObj.oldPrice,digital)+'</span><br/>';
             strHtml+='优惠价：<span style="font-size:10px;color:red;font-family:Arial;">￥</span><span style="color:red;font-weight:bold;font-size:14px;">'+formatToDecimal(productObj.newPrice,digital)+'</span><br/>';
             strHtml+='为您节省：'+formatToDecimal((productObj.oldPrice-productObj.newPrice),digital)+'元';
        }else
        {
            strHtml+='原价：￥'+formatToDecimal(productObj.oldPrice,digital)+'<br/>';
        }
        divProductInfo.innerHTML=strHtml;    
    }else
    {
        var divProductInfo=document.getElementById('divProductInfo');
        if(!divProductInfo) return;
        divProductInfo.innerHTML='';
    }
}
var digital=2;//保留小数位数
hiddenLayer=function()
{
    var layerObj=getObj('divLayer');
    if(!layerObj) return;
    layerObj.style.display='none';
    
    var sanJiao=getObj('sanJiao');
    if(!sanJiao) return; 
    sanJiao.style.display='none';
}
pageLoad=function()
{
    var divList=document.getElementsByTagName('div');
    for(var i=0;i<divList.length;i++)
    {
        if(divList[i].className=='divNewProductBox')
        {
            var imgList=divList[i].getElementsByTagName('img');
            for(var j=0;j<imgList.length;j++)
            {
                imgList[j].onmouseover= function(){createLayer(this);};
                imgList[j].onmouseout=function(){hiddenLayer();};
                imgList[j].style.cursor='pointer';
            }
        }
    }
    var imgMainTopAd=document.getElementById('imgMainTopAd');
    if(imgMainTopAd)
    {
        setTimeout(function(){auto=window.setInterval(goUp,50);},2000);//图片定时上移
    }
}
getObj=function(id)
{
    return document.getElementById(id);
}
 goUp=function()
{
    var autoImg=document.getElementById('autoImg');
    if (!autoImg) return;
    var surplusHeight=0;
    if(increment<autoImg.style.posHeight-surplusHeight)
    {
        autoImg.style.posHeight-=increment;
        increment+=10;
    }else
    {
        window.clearInterval(auto);
        setTimeout(function(){auto=window.setInterval(goDown,20);},300);//图片定时下移
        increment=10;
        autoImg.style.posHeight=surplusHeight;
    }
}
var auto;
var increment=1;
goDown=function()
{
    var imgMainTopAd=document.getElementById('imgMainTopAd');
    if(imgMainTopAd){
        var maxHeight=40;
        if(imgMainTopAd.style.posHeight+increment<maxHeight && increment>0)
        {
            imgMainTopAd.style.posHeight+=increment;
            increment-=1;
        }else
        {
            imgMainTopAd.style.posHeight=maxHeight;
            window.clearInterval(auto);
        }
    }
}
ProductObj=function(formatShortName,formatName,pageCount,oldPrice,newPrice,flag)
{
    this.formatShortName=formatShortName;
    this.formatName=formatName;
    this.pageCount=pageCount;
    this.oldPrice=oldPrice;
    this.newPrice=newPrice;
    this.flag=flag;
}
/*****************切换广告图片********************/



if(window.attachEvent){
	window.attachEvent('onload',prossAdLink); 
}
else{
	window.addEventListener('load',prossAdLink,false);
}

function prossAdLink(){
	//alert('1');
	if (imgArray && imgArray.length == 1){
		stopAutoRun();
		return;
	}
		
	var divAlink = document.getElementById('divAlink');
	if (divAlink){
		var aList = divAlink.getElementsByTagName('a');
		if (aList){
			for(var i = 0; i < aList.length; i++){
				aList[i].onmouseover = new Function('mouseOver(\''+ aList[i].id +'\','+i+')');
				aList[i].onmouseout = new Function('mouseOut(\''+aList[i].id+'\','+i+')');
				aList[i].style.cursor = 'pointer';
			}
		}
	}
	
	var divObj = document.getElementById('divAdImg');
	divObj.onmouseover = new Function('stopAutoRun()');
	divObj.onmouseout = new Function('autoRun()');
	divObj.style.cursor= 'pointer';
	autoRun();
}

//鼠标过
function mouseOver(id,flag){
	stopAutoRun();
	ShowImage(id,flag);
}
//da
var currentImgIndex = 0;
var count = 4;
var autoObj = null;

//鼠标移开
function mouseOut(id,flag){
	autoRun();
}

//图片切换
function ShowImage(id,flag){
	var aObj = document.getElementById(id);
	var divObj = document.getElementById('divAdImg');
	if (aObj){
//		if (flag != currentImgIndex){
			aObj.className  = 'abtnCurrent';
			divObj.filters[0].apply();
			divObj.style.backgroundImage = 'url(\''+imgArray[flag]+'\')';
			divObj.filters[0].play();
			currentImgIndex = flag;
//		}
	}
	var divAlink = document.getElementById('divAlink');
	if (divAlink){
		var aList = divAlink.getElementsByTagName('a');
		if (aList){
			for(var i = 0; i < aList.length; i++){
				if (id != aList[i].id){
					aList[i].className  = 'abtn';
				}
			}
		}
	}
	
	//div加点击事件
	divObj.onclick = new Function('divImgClick(\'' +imgLinkArray[currentImgIndex] +'\')')
}
//div点击事件
function divImgClick(url){
	navigate(url);
}

//自动切换
function autoRun(){
	stopAutoRun();
	autoObj = setInterval(autoMain,5000);
}
//停止自动切换
function stopAutoRun(){
	clearInterval(autoObj);
}
function autoMain(){
	currentImgIndex++;
	//var divObj = document.getElementById('divAdImg');
	var divAlink = document.getElementById('divAlink');
	if (currentImgIndex > 3){
		currentImgIndex = 0;
	}
	if (divAlink){
		var aList = divAlink.getElementsByTagName('a');
		ShowImage(aList[currentImgIndex].id, currentImgIndex);
	}
}
//格式化数字
formatToDecimal=function(d,scale)
{
    if(isNaN(d) || isNaN(scale)) return d;
    var zeroList='000000000000000000000000000000';
    scale=parseInt(scale);
    if(scale>zeroList.length) scale=zeroList.length;
    var d=d.toString();
    if(d.indexOf('.')==-1) return d+'.'+zeroList.substr(0,scale);//整数
    var count=(d.length-1)-d.indexOf('.');//小数点后的位数
    if(count>scale){
         var mul=Math.pow(10,scale);
         d=d*mul;
         d=(Math.round(d)/mul).toString();
         if(d.indexOf('.')==-1) return d+'.'+zeroList.substr(0,scale);
         count=(d.length-1)-d.indexOf('.');
    }
    return d+zeroList.substr(0,scale-count);
}
//跳转到日历详情页
function openTLMsg(oldPhookId,newPhookId,nextYearPhookId,price)
{
	var url='/PhookProduct/CalendarDetail.aspx?price={0}&phookProductId={1}&newTl={2}&nextYearTl={3}';
    url=format(url,price,oldPhookId,newPhookId,nextYearPhookId);
	urlEncode(url);
}

function openMsg(phookId,price,pageNum)
{
    var url;
    if(pageNum!=undefined){
        url='/PhookProduct/HcDetail.aspx?pageNum={0}&price={1}&phookProductId={2}';
        url=format(url,pageNum,price,phookId);
    }else
    {
        url='/PhookProduct/CalendarDetail.aspx?price={0}&phookProductId={1}';
        url=format(url,price,phookId);
    }
    urlEncode(url);
}

format=function(strFormat)
{
    var re=/[{]\d+[}]/g;
    var indexList=strFormat.match(re);
    for(var i=0;i<indexList.length;i++){
        var index=parseInt(indexList[i].replace(/[\D]+/ig,''));
        if(index+2>arguments.length){
            throw new Error('索引出界!');
        }
        strFormat=strFormat.replace(indexList[i],arguments[index+1]);
    }
    return strFormat;
}