﻿function findPosX(obj) { var curleft = 0; if (obj.offsetParent) { while (obj.offsetParent) { curleft += obj.offsetLeft; obj = obj.offsetParent; } } else if (obj.x) curleft += obj.x; return curleft; } 
function findPosY(obj) { var curtop = 0; if (obj.offsetParent) { while (obj.offsetParent) { curtop += obj.offsetTop;	obj = obj.offsetParent; } } else if (obj.y) curtop += obj.y; return curtop; } 

var windowW=800,windowH=600;
var adPerct = 0.25; //25% to decide if above fold or expandable
//alert(document.body.clientWidth+'x'+document.body.clientHeight);
if (typeof(window.innerWidth)!="undefined") {
    windowW = window.innerWidth;
    windowH = window.innerHeight;
} else { //IE
    windowW = document.documentElement.clientWidth;
    windowH = document.documentElement.clientHeight;
}
//alert("window (infold?): "+windowW+"x"+windowH+", expansion width: "+expTotalW);
//if (typeof(btgdocW)=="undefined") { var btgdocW = 800; var btgdocH = 600; try {btgdocH=document.body.clientHeight;btgdocW=document.body.clientWidth;} catch(btge1){} }
function expansionAllowance(position) {
    try { 
    adplaceholder =document.getElementById(position); 
    var btgsize = position.replace(/_[0-9]/gi,'').replace(/ad\-/gi,'').replace(/pos\-/gi,'');var btgszW=parseInt(btgsize.split("x")[0]);var btgszH=parseInt(btgsize.split("x")[1]); 
    var btgdisexpansion = "";
    if ((findPosX(adplaceholder) > btgszW*adPerct) && (findPosX(adplaceholder)+btgszW < windowW - btgszW*adPerct))
        btgdisexpansion = ";exp=both";
    else if (findPosX(adplaceholder) > btgszW*adPerct)
        btgdisexpansion = ";exp=left";
    else
        btgdisexpansion = ";exp=right";
    } catch (btge2) {  } 
    return btgdisexpansion;
}

function foldPositioning(position) {
    try { 
    adplaceholder =document.getElementById(position); 
    var btgsize = position.replace(/_[0-9]/gi,'').replace(/ad\-/gi,'').replace(/pos\-/gi,'');var btgszW=parseInt(btgsize.split("x")[0]);var btgszH=parseInt(btgsize.split("x")[1]); 
    var btginfold = "";
    if ((findPosX(adplaceholder) < windowW - btgszW*adPerct ) && (findPosY(adplaceholder) < windowH - btgszH*adPerct)) {btginfold = ";loc=af"}
    else {btginfold = ";loc=bf"}
    } catch (btge3) { } 
    return btginfold;
}


function insertIntoPlaceHolder(obj,lyr)
{
//alert("the new with: "+obj);
try {
//Option 1
    //document.getElementById(obj).parentNode.replaceChild(document.getElementById(lyr),document.getElementById(obj));
    //Option 2
    var moveFunc = function() {
        var allscripts = document.getElementById(lyr).getElementsByTagName("script"); 
        for (var i = allscripts.length-1; i >= 0; i--)
        { 
            //document.getElementById(lyr).removeChild(allscripts[i]);
            if(allscripts[i].src.match(/adj\/btg/gi)||allscripts[i].src.match(/1x1/gi)) {
                //alert(allscripts[i].src);
                allscripts[i].parentNode.removeChild(allscripts[i]);
                
            }
        }
        
        /* 1x1 IMAGES ARE REMOVED HERE... RE-ENABLE HERE
        var allimgs = document.getElementById(lyr).getElementsByTagName("img"); 
        for (var i = allimgs.length-1; i >= 0; i--)
        { 
            if(allimgs[i].src.match(/1x1/gi)) {
                allimgs[i].parentNode.removeChild(allimgs[i]);
                
            }
        }
        */
        document.getElementById(obj).appendChild(document.getElementById(lyr));
        //alert("moved "+lyr.id+" to "+obj.id);
        };
        //genAttachListenerToOnLoadEvent(moveFunc);
        moveFunc();
        
        
    //setTimeout('alert("'+obj+'");document.getElementById("'+obj+'").appendChild(document.getElementById("'+lyr+'"));',15000);
    // Get the list of positions on the page





/*    var allscripts = document.getElementById(lyr).getElementsByTagName("script"); 
    for (var i = allscripts.length-1; i >= 0; i--)
    { 
        document.getElementById(lyr).removeChild(allscripts[i]);
    }
    document.getElementById(obj).appendChild(document.getElementById(lyr));*/
} catch(setLyrnewerr){
    //alert("error: "+document.getElementById(lyr).getElementsByTagName("script"));
    }
}

if (typeof(setLyr) == "undefined") 
    setLyr = function(a,b,c) {insertIntoPlaceHolder(a,b);};

function isThereACrawlerInThePage() {
    try {
       return (document.getElementById("catfish") || document.getElementById("crawler") ||document.getElementById("crawler_cont") || bnParsArray['bnsm_name'] == "crawler");
    } catch (istc) {
       return false;
    }
}

/********************* Wall Papper Functions ******************************/

function wall_get_WindowWidth() { 
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE 
    myWidth = window.innerWidth; 
  } else if( document.documentElement && document.documentElement.clientWidth) { 
    //IE 6+ in 'standards compliant mode' 
    myWidth = document.documentElement.clientWidth; 
  } else if( document.body &&  document.body.clientWidth  ) { 
    //IE 4 compatible
    myWidth = document.body.clientWidth; 
  } 
    return myWidth; 
}

function wallpaper_bg(bg_image,bg_attach,bg_color){
document.write("<style type='text/css'>body {background-color:"+bg_color+";background-image:url("+bg_image+");background-position:center top;background-repeat:no-repeat;background-attachment:"+bg_attach+";_background-attachment: scroll;}#container2{background-color: #FFFFFF;}</style>");	  
}


var linkContainerArray = new Array();
var linkContainersInserted = false;
var linkContainersScheduled = false;
function wallpaper_btn(b_pos,b_img,b_link,b_hor,b_vert,b_attach){

try { 
    if (wall_get_WindowWidth() >= 1220){
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		   var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			if (ieversion<=6){
			b_attach = "absolute";
    		}
        }         
        var insert_buttons_html = "<div style='position:"+b_attach+"; bottom:"+b_vert+"px; "+b_pos+":"+b_hor+"px'><a href='"+b_link+"' target='_blank'><img src='"+b_img+"'/></div>";
        var div_name = "insert_buttons_"+b_pos+"" 
    			  
	    var link_container = document.createElement('div');         
        link_container.setAttribute('id', div_name);
        link_container.innerHTML = insert_buttons_html;
        linkContainerArray[linkContainerArray.length] = link_container;
        genAttachListenerToOnLoadEvent(insertLinkContainer);
        //attachEventListener(document.body,"load",insertLinkContainer,true,insertLinkContainer);
        //alert(typeof document.body);
        /*if (!linkContainersScheduled) {
            linkContainersScheduled = true
            setTimeout('insertLinkContainer()',12000);
        }*/
    }   
 
} catch(GarE) {}
}

function insertLinkContainer() {
    try {
        //alert("setlink");
        if (!linkContainersInserted) {
            linkContainersInserted = true;
            for (var i=0;i<linkContainerArray.length;i++)
                document.body.appendChild(linkContainerArray[i]);
                
        }
        //alert("setlink done");
    } catch(insertLinkEx) {
        linkContainersInserted = false;
        setTimeout('insertLinkContainer()',5000);
    }
    return !linkContainersInserted;
}

function genAttachListenerToOnLoadEvent(functionRef) {
    if( window.addEventListener ) {
      window.addEventListener('load',functionRef,false);
    } else if( document.addEventListener ) {
      document.addEventListener('load',functionRef,false);
    } else if( window.attachEvent ) {
      window.attachEvent('onload',functionRef);
    }
}


/***************************** VIDE OVERLAY ***********************************/

function setLyrvOff(obj,lyr,container,voffset)
{
 try {
 var o = document.getElementById(obj);
 var newX = findPosX(o);
 var newY = findPosY(o);
 var leftcontainer1= findPosX(document.getElementById(container));
 var x = document.getElementById(lyr);
 x.style.top = (newY+voffset) + 'px';
 newX= newX - leftcontainer1;
 x.style.margin = '0 0' + newX + 'px 0';
 } catch(sLP) {}
}

function hideVideoOverlay() {
    document.getElementById("v-overlay").style.display = "none";
}

function showVideoOverlay(objid,option,var1,var2) {
    if (option == 0) {
        try{$(document).ready(function(){
            $("#"+objid).fadeIn(var2);
            //$("#ad-test").show(2000);
            //$("#ad-test").fadeIn("slow");
        });
        }catch(jqerrorbat){}
    }
    if (option == 1) {
        try{$(document).ready(function(){
            $("#"+objid).animate({"height": "-=80px"}, 0);
            $("#"+objid).animate({"height": "+=80px"}, var2);
            document.getElementById(objid).style.display = "";
            //$("#v-overlay").fadeIn(5000);
            //$("#ad-test").show(2000);
            //$("#ad-test").fadeIn("slow");
        });
        }catch(jqerrorbat){}
    }
    if (option == 2) {
        try{$(document).ready(function(){
            $("#"+objid).animate({"top": "+="+var1+"px"}, 0);
            document.getElementById(objid).style.display = "";
            $("#"+objid).animate({"top": "-="+var1+"px"}, var2);
            //$("#v-overlay").fadeIn(5000);
            //$("#ad-test").show(2000);
            //$("#ad-test").fadeIn("slow");
        });
        }catch(jqerrorbat){}
    }
    if (option == 3) {
        try{$(document).ready(function(){
            $("#"+objid).animate({"height": "=0px"}, 0);
            $("#"+objid).animate({"height": "=80px"}, var2);
            document.getElementById(objid).style.display = "";
            //$("#v-overlay").fadeIn(5000);
            //$("#ad-test").show(2000);
            //$("#ad-test").fadeIn("slow");
        });
        }catch(jqerrorbat){}
    }
}

function overLayTestSimulateDART(option,var1,var2) {
    //showOverLay('http://m1.2mdn.net/2072122/728x90_jd.swf',3,250,3500);
    //showOverLay('http://m1.2mdn.net/2072122/728x90_jd.swf',1,250,3500);
    showOverLay('http://batanga.http.internapcdn.net/batanga_vitalstream_com/traffic/2009/absolut/mango.swf',0,0,5000,5000,20000);
}

function showOverLay(flashUrl,option,var1,var2,showtime,hidetime) {
    document.write('<'+'div id="v-overlay-c" style="width:440px;height:80px;overflow:hidden">');
    document.write('<'+'div id="v-overlay" style="position:absolute; z-index:10001; width:440px; height:80px; padding-top:0px; overflow:hidden;display:none;border">');
    document.write('<'+'embed src="'+flashUrl+'" quality="high" width="440px" height="80px" name="miciudad" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent"><'+'/embed>');
    //document.write('<'+'script src="http://code.jquery.com/jquery-latest.js"><'+'/script>');
    document.write('<'+'/div>');
    document.write('<'+'/div>');
    document.write('<'+'script>');
    document.write('setLyrvOff("playerdiv","v-overlay","v-overlay-c",250);');
    document.write('setTimeout(\'showVideoOverlay("v-overlay",'+option+','+var1+','+var2+')\','+showtime+');');
    if (hidetime > 0)
        document.write('setTimeout(\'hideVideoOverlay()\','+(showtime+hidetime)+');');
    document.write('<'+'/script>');
}

/***************************** Insert 1x1 by time ***********************************/

function b_ad_timer(time,src)
{
setTimeout("insert_pixel_by_time('"+src+"');",time*1000);
}

function insert_pixel_by_time(src){

pixel_image = document.createElement("img");
pixel_image.width = 1;
pixel_image.height= 1;

var random_src = src. replace("b_random",Math.random());

pixel_image.src = random_src;
document.body.appendChild(pixel_image);

}