var b,w,b2;
var adLayers=new Array();
function initLayer(){
    for(var i=0;i<3;i++){
        adLayer=document.getElementById("l"+(i+1));
        if(adLayer!=null) 
            adLayers[i]=adLayer;}
    
    b=document.getElementById('b');
    w=document.getElementById('cv');
    b2=document.getElementById('b2');
    if (b!=null&&w!=null){
        eval('setInterval("d()",'+'500)');
    }
}

function d(){
    if (b.style.color!='yellow'){
        if(b2!=null) b2.style.color='yellow';
        b.style.color='yellow';
        w.style.color='yellow';
    }else{
        if(b2!=null) b2.style.color='blue';
        b.style.color='blue';
        w.style.color='blue';
    }
}

adLayersDisplayTime=new Array(3);
adLayersDisplayTime[0]=10000;
adLayersDisplayTime[1]=7000;
adLayersDisplayTime[2]=5000;
var sL=0,iS=0,iP=0;
function play(){
    iP++;
    if(iS==0 && iP>1){
        var display = sL%adLayers.length;
        var displayTime = adLayersDisplayTime[display];
        for(var i=0;i<adLayers.length;i++){
            if(i==display){
                cs(adLayers[i],1);
                displayTime=adLayersDisplayTime[i];
            }else{
                cs(adLayers[i],0);
            }
        }
        setTimeout('play()',displayTime);
        sL++;
    }else{
        setTimeout('play()',2000);
    }
}

function cs(a,b){
    if(a==null)
        return;
    if(navigator.userAgent.indexOf("MSIE")!=-1){
        a.filters.revealTrans.apply();
        if(b==1) a.style.visibility="visible";else a.style.visibility="hidden";
        a.filters.revealTrans.play();
    }else{
        if(b==1) a.style.visibility="visible";else a.style.visibility="hidden";
    }
}

function s(v){iS=v}
var tryDrawCount=0;

function draw(){
    var aObj = document.getElementById("adFromInner");
    var adStrs = aObj.value;
    tryDrawCount++;
    if(adStrs.length > 20){
        var ads = adStrs.split("\";\"");
        
        adLinks = new Array();
        for(var i=0;i<ads.length;i++){
            var paras = ads[i].split("\",\"");
            adLinks[i]=new ad(paras[0],paras[1],paras[2],paras[3],paras[4]); 
        }
        var templateValue = document.getElementById("templateFromInner").value;

        var v = build(templateValue);
        
        document.getElementById("adShow").innerHTML=v;
        
        initLayer();
        play();
     }else{//bc==maxTryDrawCount
         if(tryDrawCount<bc)
            setTimeout("draw()",100);
         else{
            //defaultPage === r
            document.getElementById("adShow").innerHTML=r;    
        }
     }
}
function allnad(){
    document.allnad.action='http://www.narrowad.com/redirect/nadRedirect.jsp';
    document.allnad.submit();
}

function go2Url(url){
    document.allnad.action=url;
    document.allnad.submit();
}
function go(k,u){
    document.t.parameter.value=k;
    document.t.parameter1.value=u;
    document.t.submit();
}

function setStatus(str){
    window.status=str;
}
function rp(s,f){
    a=f.split("+");
    for(i=0;i<a.length;i++){ 
        s=s.replace(new RegExp(a[i],"g"),'<b><font color=red>'+a[i]+'</font></b>');
    }
    return s;
}

function ad(title,href,detail,keyword,clickdata){
    this.title=title
    this.href=href
    this.detail=detail
    this.keyword=keyword
    this.clickdata=clickdata
}   

function Template(size,row,col,ggbks,ggds,ggbtzs,ggmszs,descCount,isHeader,ggydtds,ggydtzs,headerAd,middleAD,footerAD){
    this.size=parseInt(size)
    this.row=parseInt(row)
    this.col=parseInt(col)
    this.ggbks=ggbks
    this.ggds=ggds
    this.ggbtzs=ggbtzs
    this.ggmszs=ggmszs
    this.ggydtds=ggydtds
    this.ggydtzs=ggydtzs
    this.descCount=parseInt(descCount)
    this.isHeader=parseInt(isHeader)
    this.headerAd=parseInt(headerAd)
    this.middleAD=parseInt(middleAD)
    this.footerAD=parseInt(footerAD)
}

function makeHeader(sty){
    var strTable = '';
    var tableWidth = parseInt(sty.size);

    strTable += '<table width=' + tableWidth + ' border=0 cellpadding=3 cellspacing=0>';
    strTable += '<tr style="'+titleBackStyle+'">'
    
    if(tixa_wid=='337'){
        bk="<b><a href="+clku+"=kouchou target=_blank id=b style='color:yellow'>消灭口臭★</a>";
        bk+="<a href="+clku+"=dahan target=_blank id=b2 style='color:yellow'>打鼾致猝死</a></b>";
    }
    
    if (sty.col >= 3) {//cc==title1
        strTable += '<td style="'+titleStyle+'" nowrap>'+cc+'</td>'
        if(sty.headerAd == 0){//bk==titleMiddle bx==header
            strTable += '<td style="'+titleStyle+'" align=right nowrap>'+bk+'</td>'
            strTable += '<td style="'+titleStyle+'" align=right nowrap>'+bx+'</td>'
        }//moreNAD==bm
        strTable += '<td style="'+titleStyle+'" align=right nowrap>'+bm+'</td>'
    } else{//bz==title2
        strTable += '<td style="'+titleStyle+'" nowrap>'+bz+'</td>'
        strTable += '<td style="'+titleStyle+'" nowrap><a id=w c=yellow></a></td>'
    }

    strTable += '</tr>'
    strTable += '</table>'
    return strTable;    
}

function makeTail(sty){
    var strTable = '';
    var tableWidth = parseInt(sty.size);
    strTable += '<table width=' + tableWidth + ' border=0 cellpadding=3 cellspacing=0>';
    strTable += '<tr style="'+titleBackStyle+'">';
    strTable += '<td style="'+titleStyle+'" align=right>'+tail+'</td>';
    strTable += '</tr>';
    strTable += '</table>';
    return strTable;    
}

function makeTableContent(sty){
    var strTable = ''
    var counter=0
    
    var tableWidth =parseInt(sty.size);
    
    //每个广告的宽度
    var singleAdWidth = Math.round((tableWidth - 6) / sty.col);
    //每个广告行的最大字数
    var strnsize = (singleAdWidth - (singleAdWidth % 12)) / 12 - 1;

    //1111111111111111111111111111111111111111111111111111111111111111111111
    //Build第一层
    strTable += '<div id=l1 onmouseover=s(1) onmouseout=s(0) style="'+divStyle+'">'
    strTable += '<table style=\'TABLE-LAYOUT:fixed;word-break:break-all\' width='+tableWidth+' border=0 cellpadding=0 cellspacing=1 bgcolor=#' + sty.ggbks + '>'
   
    for (var i = 0; i < sty.row; i++) {
        strTable += '<tr style="'+adBackStyle+'">';
        
        for (var j = 0; j < sty.col; j++) {

            var onClickStr = 'onClick="go(\''+adLinks[counter].clickdata+'\',\''+adLinks[counter].href+'\');"'            
            var setStatusStr = 'onFocus="setStatus(\''+adLinks[counter].href+'\')" onMouseOver="setStatus(\''+adLinks[counter].href+'\')"  onMouseOut="setStatus(\'\')"'

            strTable += '<td height='+20*(parseInt(sty.descCount)+1)+' width='+singleAdWidth+' style="cursor:pointer;cursor:hand" '
            if(adLinks[counter].keyword.length>1)
                strTable += ' title='+adLinks[counter].keyword + ' '
            strTable += onClickStr+' '+setStatusStr+' valign=top>'
            strTable += '<font style="'+adTitleStyle+'"><b>'
            strTable += writeText(adLinks[counter].title,strnsize,adLinks[counter].keyword);
            strTable += '</b></font><br>'
            strTable += '<font style="'+adDescStyle+'">'
            strTable += writeText(adLinks[counter].detail,strnsize * sty.descCount,adLinks[counter].keyword);
            strTable += '</td>'
            counter++;
        }
        strTable += '</tr>'
    }
    strTable += '</table>'
    strTable += '</div>'
    //22222222222
    //Build第二层
    strTable += '<div id=l2 onmouseover=s(1) onmouseout=s(0) style="'+divStyle+'visibility:hidden">'
    strTable += '<table style=\'TABLE-LAYOUT:fixed;word-break:break-all\' width=' + tableWidth+ ' border=0 cellpadding=0 cellspacing=1 bgcolor=#' + sty.ggbks + '>'
   
    for (var i = 0; i < sty.row; i++) {
        strTable += '<tr style="'+adBackStyle+'">';
        for (var j = 0; j < sty.col; j++) {
            strTable += '<td height=' + 20 * (parseInt(sty.descCount)+1) + ' width=' + singleAdWidth+ '>'
            for (var k = 0; k <= sty.descCount; k++) {
                if(adLinks[counter].clickdata.length>1){
                    var onClickStr = 'onClick="go(\''+adLinks[counter].clickdata+'\',\''+adLinks[counter].href+'\');"' 
                    strTable += '<a href=#><font style="'+adTitleStyle+'" '+onClickStr+'>'
                    strTable += strCut(adLinks[counter].title,strnsize)
                    strTable += '</font></a>'
                }else if(adLinks[counter].href.length>1){
                    strTable += '<a href="'+adLinks[counter].href+'" target=_blank><font style="'+adTitleStyle+'">'
                    strTable += adLinks[counter].title
                    strTable += '</font></a>'   
                }else{
                    strTable += '<font style="'+adDescStyle+'">'
                    strTable += adLinks[counter].title
                    strTable += '</font>'                    
                }

                counter++;
                
                if(k<sty.descCount)
                    strTable += '<br>';
            }
            strTable += '</td>';
        }
        strTable += '</tr>';
    }
    strTable += '</table>';
    strTable += '</div>';
    
  	var thirdLayerAD_r=getY(sty.descCount*sty.row);
	var thirdLayerADUrl_r=getQ();
	var thirdLayerAD_h=getX(sty.row);
	var thirdLayerADUrl_h=getP();
	
	var thirdLayerAD="";
	var thirdLayerADUrl="";
	if(sty.col>=3){
	    var ranClickIndex=Math.floor(Math.random()*1000)%thirdLayerAD_r.length;
	    thirdLayerAD = thirdLayerAD_r[ranClickIndex];
	    thirdLayerADUrl = thirdLayerADUrl_r[ranClickIndex];
	}else{
	    var ranClickIndex=Math.floor(Math.random()*1000)%thirdLayerADUrl_h.length;
	    thirdLayerAD = thirdLayerAD_h[ranClickIndex];
	    thirdLayerADUrl = thirdLayerADUrl_h[ranClickIndex];
	}
	var blockHeight=((20*(parseInt(sty.descCount)+1)+1)*sty.row-1);
	if(br==1){
	strTable+='<div id=l3 onmouseover=s(1) onmouseout=s(0) style="'+divStyle+'visibility:hidden">'
	strTable+='<table style="TABLE-LAYOUT:fixed;word-break:break-all" width='+sty.size+' border=0 cellpadding=0 cellspacing=1 bgcolor=#'+sty.ggbk+'>'
	strTable+='<tr style="'+adBackStyle+'">'
	strTable+='<td height='+blockHeight+' style="cursor:pointer;" onClick="go2Url(\''+thirdLayerADUrl+'\')">'
	strTable+='<div style="width:'+sty.size+'px;height:'+blockHeight+'px;overflow:hidden;">'+thirdLayerAD+'</div>';
	strTable+='</td>'
	strTable+='</tr>'
	strTable+='</table>'
	strTable+='</div>'
	}
    
    //alert(strTable);
    return strTable;
}



function writeText(strTitle, titleLength, keyword) {
    titleLength = titleLength-1;

    strTitle=strCut(strTitle,titleLength);

    if(keyword.length>1){
        return rp(strTitle,keyword);
    }else
        return strTitle;
}


function strCut(myString, nLength){
    var i = 0,j=0,index=0;
    
    while(i<2*nLength){   
        if (myString.charCodeAt(j++) > 255) // 表示是汉字
            i += 2;
        else 
            i += 1;

        if (i > 2*nLength)
            break;
        index++;
    }

    return myString.substr(0,index);

}

var divStyle;
var adDescStyle;
var adTitleStyle;
var titleStyle;
var titleBackStyle;
var adBackStyle;

function strReplace(s, f, t){
 return s.replace(new RegExp(f,"g"),t);
}
function build(paraValue){
    if(paraValue.length > 30){
        var temArray = paraValue.split(",");
    
        template=new Template()
        template.size = temArray[2]
        template.row = temArray[0]
        template.col= temArray[1]
        template.ggbks=temArray[6]
        template.ggds=temArray[7]
        template.ggbtzs=temArray[8]
        template.ggmszs=temArray[9]
        template.ggydtds=temArray[10]
        template.ggydtzs=temArray[12]
        template.descCount=temArray[3]
        template.isHeader=temArray[4]
        template.headerAd=temArray[13]
        template.middleAD=temArray[14]
        template.footerAD=temArray[15]
        
        divStyle=    'FILTER:revealTrans(transition=13,duration=1);POSITION:absolute;'
        adDescStyle = 'font-size:12px;color:#'+template.ggmszs+';'
        adTitleStyle = 'font-size:12px;color:#'+template.ggbtzs+';text-decoration: underline;'

        titleStyle = 'font-size:12px;color:#'+template.ggbtzs+';'
        titleBackStyle = 'background:#'+template.ggydtds+';'
        adBackStyle = 'background:#'+template.ggds+';padding:3;line-height:1.5;'
        var returnStr = '';
        if(template.isHeader==1){
              returnStr += makeHeader(template)
        }
        
        returnStr += makeTableContent(template)
        return returnStr;
    }else{
      
      var a='';
      a+='<body topmargin=0 marginheight=5>'
      a+='<center>'
      a+='<div id=l1 onmouseover=s(1) onmouseout=s(0) style="FILTER:revealTrans(transition=13,duration=1);POSITION:absolute;">'
      a+='<table width=565 cellpadding=0 cellspacing=0 border=0 bgcolor=#FDF9E1 style="border:1px solid #FEC100;">'
      a+='  <tr>'
      a+='  <td height=39 colspan=3 align=center>'
      a+='    <table width=100% border=0 cellpadding=0 cellspacing=0>'
      a+='      <tr>'
      a+='        <td width=17 height=39 align=center bgcolor=#F8DA38 style="font-size:12px;line-height:1.5;text-decoration:none;color:#ca3708;font-weight: bold"><a href=http://www.narrowad.com target=blank style="text-decoration:none"><b><font color=#ca3708>窄 告</font></b></a></td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">NADTITLE1<br>NADDESC11</td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">NADTITLE2<br>NADDESC12</td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">NADTITLE3<br>NADDESC13</td>'
      a+='        <td width=19 align=center bgcolor=#F8DA38  style="font-size:12px;line-height:1.5;text-decoration:none;color:#ca3708;font-weight: bold"><a href=http://www.narrowad.com target=blank style="text-decoration:none"><b><font color=#ca3708>投 放</font></b></a></td>'
      a+='      </tr>'
      a+='    </table>'
      a+='  </td>'
      a+='  </tr>'
      a+='</table>'
      a+='</div>'
      a+='<div id=l2 onmouseover=s(1) onmouseout=s(0) style="FILTER:revealTrans(transition=13,duration=1);POSITION:absolute;visibility:hidden">'
      a+='<table width=565 cellpadding=0 cellspacing=0 border=0 bgcolor=#FDF9E1 style="border:1px solid #FEC100;">'
      a+='  <tr>'
      a+='  <td height=39 colspan=3 align=center>'
      a+='    <table width=100% border=0 cellpadding=0 cellspacing=0>'
      a+='      <tr>'
      a+='        <td width=17 height=39 align=center bgcolor=#F8DA38 style="font-size:12px;line-height:1.5;text-decoration:none;color:#ca3708;font-weight: bold"><a href=http://www.narrowad.com target=blank style="text-decoration:none"><b><font color=#ca3708>窄 告</font></b></a></td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">'+document.getElementById("s21").value+'</td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">'+document.getElementById("s22").value+'</td>'
      a+='        <td width=175 style="font-size:12px;line-height:1.5">'+document.getElementById("s23").value+'</td>'
      a+='        <td width=19 align=center bgcolor=#F8DA38  style="font-size:12px;line-height:1.5;text-decoration:none;color:#ca3708;font-weight: bold"><a href=http://www.narrowad.com target=blank style="text-decoration:none"><b><font color=#ca3708>投 放</font></b></a></td>'
      a+='      </tr>'
      a+='    </table>'
      a+='  </td>'
      a+='  </tr>'
      a+='</table>'
      a+='</div>'
      l=a
      for(ad=1;ad<=adLinks.length-1;ad++){
       var o;
       if(adLinks[ad-1].keyword.length>1)
       o="<a title="+adLinks[ad-1].keyword+" href=# onClick=\"go('"+adLinks[ad-1].clickdata+"','"+adLinks[ad-1].href+"');\">";
       else
       o="<a href=# onClick=\"go('"+adLinks[ad-1].clickdata+"','"+adLinks[ad-1].href+"');\">";
       o+='<b>'+strCut(adLinks[ad-1].title,12)+'</b>';
       o+="</a>";
       l=strReplace(l,"NADTITLE"+ad,o);
       l=strReplace(l,"NADDESC1"+ad,strCut(adLinks[ad-1].detail,12,adLinks[ad-1].keyword));
       }
       return l;
    }
}
