/* COMMON BANNER FUNCTIONS */
function setBannerCookie( name, cokie_expire ){
	var expire = new Date();
	expire.setTime( expire.getTime() + parseInt(cokie_expire) );
	var str = "banner_showed_" + name + "_=1; expires=" + expire.toGMTString() + "; path=/";
	document.cookie = str;
}

function getBannerCookie( name ) {
	var prefix = "banner_showed_" + name + "_=";
	var begin = document.cookie.indexOf( "; " + prefix );
	if ( begin == -1 ) {
		begin = document.cookie.indexOf( prefix );
		if ( begin != 0 ) {
			return false;
		}
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf( ";", begin );
	if (end == -1){
		end = document.cookie.length;
	}
	var result = unescape(document.cookie.substring(begin + prefix.length, end));
	return result;
}

function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie6=((navigator.appName.indexOf("Microsoft")!=-1 || this.ver.indexOf("MSIE 6")>-1) && this.dom)?1:0;
	this.ie5=((navigator.appName.indexOf("Microsoft")!=-1 || this.ver.indexOf("MSIE 5")>-1) && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.gecko=(navigator.userAgent.indexOf("Gecko")!=-1)?1:0;
	this.mac=((navigator.appName.indexOf("Mac")!=-1 || this.ver.indexOf("Mac")>-1) && this.dom)?1:0;

    var test_cookie = setBannerCookie( 'test_cookie', 5 * 1000 );
    this.cookie=getBannerCookie( 'test_cookie' )?1:0;

	return this
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}



/* BANNER INIT FUNCTIONS */

/* Top Layer Redirect banner */
function toplayerRedirect(indexUrl,bannerName){
    var toplayerShowed = false;
    var browser = checkBrowser();
    if( browser.cookie ){
        var cookieExpire = 24 * 60 * 60 * 1000;
        var toplayerShowed = getBannerCookie( bannerName );
        //if( ( ( browser.ie4 || browser.ie5 || browser.ie6 || browser.ns4 || browser.ns5 ) && ( !browser.mac && !browser.gecko ) ) && !toplayerShowed ){
        if( !toplayerShowed ){
	        setBannerCookie( bannerName, cookieExpire );
	        document.location = indexUrl;
        }
    }
}

/* Pop-Up / Pop-Under */
function popupStart(indexUrl,width,height,bannerName,popUnder){
    var popupShowed = false;
    var popupShowed = getBannerCookie( bannerName );
    var browser = checkBrowser();

    if ( !popupShowed && ( browser.ie4 || browser.ie5 || browser.ie6 ) ){
        var cookieExpire = 6 * 60 * 60 * 1000;
        var popupTop = ( screen.height - height ) / 2;
        var popupLeft = ( screen.width - width ) / 2;
        var popup = window.open( indexUrl , bannerName,'top=' + popupTop + ',left=' + popupLeft + ',location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,width=' + width + ',height=' + height + '');
        if ( popUnder ){
            popup.blur();
        } else {
            popup.focus();
        }
        setBannerCookie( bannerName, cookieExpire );
    }
}

/* Lux Flag banner */
var luxStepPx = 5;
var luxStepMs = 10;
var luxInterval = false;
function luxStart(bannerName,bannerId,bannerColor,bannerPause){
    var luxShowed = false;
    var luxShowed = getBannerCookie( bannerName );
    var browser = checkBrowser();

    /*
    if ( !luxShowed && ( browser.ie4 || browser.ie5 || browser.ie6 ) && browser.dom ){
    */
    if ( !luxShowed ){
        var cookieExpire = 3 * 60 * 60 * 1000;  // changed to 3h earlier version 6h
        var bust = Math.floor(89999999*Math.random()+10000000);
        var millis = new Date().getTime();
        if ( document.all ){ // stulbajam IE ir problemas ar atributu insertoshanu
            var luxIframe = document.createElement('<iframe src="http://adv.tvnet.lv/advertpro/servlet/view/banner/html/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis+'" height="156" width="770" hspace="0" vspace="0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" id="'+bannerName+'"></iframe>')
        } else { // Visi normali browseri saprot sho :)
            luxIframe = document.createElement('iframe');
            luxIframe.setAttribute('src','http://adv.tvnet.lv/advertpro/servlet/view/banner/html/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis);
            luxIframe.setAttribute('id',bannerName);
            luxIframe.setAttribute('marginwidth','0');
            luxIframe.setAttribute('marginheight','0');
            luxIframe.setAttribute('width','770');
            luxIframe.setAttribute('height','156');
        }
        var luxBox = document.getElementById('lux-banner');
        luxBox.appendChild(luxIframe);
        luxBox.style.backgroundColor = bannerColor;
        luxBox.style.height = 156 + 'px';
        setBannerCookie( bannerName, cookieExpire );
        setTimeout('luxWait("'+bannerName+'")', bannerPause * 1000)
    }
}
function luxWait(bannerName){
    luxInterval = setInterval( 'luxResize("'+bannerName+'")', luxStepMs );
}
function luxResize(bannerName){
    var luxBox = document.getElementById('lux-banner');
    var luxIframe = document.getElementById(bannerName);
    if (parseInt(luxBox.style.height) - luxStepPx <= 0){
        luxBox.style.height=0+'px';
        luxBox.style.display='none';
        luxBox.removeChild(luxIframe);
        clearInterval(luxInterval);
    } else {
        luxBox.style.height = (parseInt(luxBox.style.height) - luxStepPx) + 'px';
    }
}

/* Flying DHTML banner */
var flyingStepXPx = 5; // for cross banner
var flyingStepXPx_horizont = 3; // for hoizontal banner
var flyingStepYPx = 5;
var flyingStepMs = 50;
var flyingInterval;

function flyingStart(bannerSrc,bannerName,bannerId,width,height,flyingOffsetTop,flyingOffsetLeft,flyingOffsetRight,flyingOffsetBottom,bannerDirection)
{
    setTimeout ( "flyingStarttime('" + bannerSrc + "','" + bannerName + "','" + bannerId + "','" + width + "','" + height + "','" + flyingOffsetTop + "','" + flyingOffsetLeft + "','" + flyingOffsetRight + "','" + flyingOffsetBottom + "','" + bannerDirection + "')", 0 );
}

function flyingStarttime(bannerSrc,bannerName,bannerId,width,height,flyingOffsetTop,flyingOffsetLeft,flyingOffsetRight,flyingOffsetBottom,bannerDirection) {

    var flyingOffsetTop = 390;
    var flyingOffsetRight = 300;
    var browser = checkBrowser();
    var flyingShowed = false;
    var flyingShowed = getBannerCookie( bannerName );
    var isFlash = false;

    // nochekojam vai flash
    if (bannerSrc.substring(bannerSrc.length-3) == 'swf'){ isFlash = true; }

    if ( !flyingShowed )
    {
        var cookieExpire = 6 * 60 * 60 * 1000;
        var bust = Math.floor(89999999*Math.random()+10000000);
        var millis = new Date().getTime();
        var flying = document.getElementById( 'flying-banner' );

        if (!isFlash)
        {
            // ja tiek padota bilde
            // iframe
            var flyingIframe = document.createElement('iframe');
            flyingIframe.setAttribute('src','http://adv.tvnet.lv/advertpro/servlet/view/banner/html/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis);
            flyingIframe.setAttribute('id',bannerName);
            flyingIframe.setAttribute('marginwidth','0');
            flyingIframe.setAttribute('marginheight','0');

            flyingIframe.setAttribute('width','1');
            flyingIframe.setAttribute('height','1');

            // anchor
            var flyingA = document.createElement('a');
            flyingA.setAttribute('src','http://adv.tvnet.lv/advertpro/servlet/click/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis);
            flyingA.setAttribute('target','_top');

            // image
            var flyingImg = document.createElement('img');
            flyingImg.setAttribute('src',bannerSrc);
            flyingImg.setAttribute('alt','ClickHere!');

            flyingImg.onclick = new Function("openUrl_banner('http://adv.tvnet.lv/advertpro/servlet/click/media?mid="+bannerId+"&pid=0&random="+bust+"&millis="+millis+"');");

            flyingImg.setAttribute('width',width);
            flyingImg.setAttribute('height',height);

            // kraameejam iekshaa
            flyingA.appendChild(flyingImg);
            flying.appendChild(flyingA);
            //flying.appendChild(flyingIframe);
        }
        else
        {
            // ja tiek padots flash
            var flyingDiv = document.createElement('div');
            flyingDiv.setAttribute('id',bannerName);
            flyingDiv.style.visibility = 'visible';
            flyingDiv.style.width = width;
            flyingDiv.style.height = height;

            var flashObj = '<object height="'+height+'" width="'+width+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" hspace="0" vspace="0" border="0" ><param name="movie" value="'+bannerSrc+'" /><param name="quality" value="high" /><param name="bgcolor" value="" /><param  name="loop" value="true"/><param name="menu" value="true" /><param name="wmode" value="transparent" /><param name="play" value="true" /><param name="allowscriptaccess"  value="always" /><param name="wmode"  value="opaque" /><param name="flashvars" value="clickTAG='+'http://adv.tvnet.lv/advertpro/servlet/click/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis+'"><embed src="'+bannerSrc+'" quality="high" pluginspace="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" height="'+height+'" width="'+width+'" hspace="0" vspace="0" border="0" wmode="transparent" flashvars="clickTAG='+'http://adv.tvnet.lv/advertpro/servlet/click/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis+'" allowscriptaccess="always" play="true" loop="true" bgcolor="" menu="true" /></object>';
            flyingDiv.innerHTML=flashObj;

            flying.appendChild(flyingDiv);
        }

        // uztaisam pogu
        var button = document.createElement( "a" );
	    button.className = "hide-banner";
	    button.setAttribute( "cookieexpire", cookieExpire );
	    button.onclick = function(){
		    flyingStop();
	    }
	    var buttonText = document.createTextNode( "" );
	    buttonText.nodeValue = "Slēpt šo reklāmu";
	    button.appendChild( buttonText );
	    flying.appendChild( button );

        // uzliekam saakuma poziicijas
        flying.style.top = flyingOffsetTop + 'px';
        flying.style.left = flyingOffsetLeft + 'px';
        flying.style.width = width + 'px';
        flying.style.height = height + 'px';
        flying.style.visibility = 'visible';
        flyingInterval = setInterval( "flyingMove('" + bannerDirection + "'," + width + "," + height + "," + flyingOffsetRight + "," + flyingOffsetBottom + ")", flyingStepMs );
	    setBannerCookie( bannerName, cookieExpire );
    }
}

function flyingMove(bannerDirection,width,height,flyingOffsetRight,flyingOffsetBottom) {
    switch (bannerDirection){
        // horizontal = from left to right
        // vertical = from top to bottom	NOT IMPLEMENTED
        // cross = cross (top left to bottom right)
        // z = the trajectory if Z			NOT IMPLEMENTED
        // bounce = bounce around			NOT IMPLEMENTED
        case 'horizontal':
	        var flying = document.getElementById( 'flying-banner' );
	        var flyingPosition = parseInt( flying.style.left );
            //flying.style.left = flyingPosition + flyingStepXPx + 'px';
	        flying.style.left = flyingPosition + flyingStepXPx_horizont + 'px';
	        if ( Math.abs( flyingPosition ) >= ( document.body.clientWidth - width - flyingOffsetRight ) ) {
		        flyingStop();
	        }
       	break;
        case 'cross':
            var flying = document.getElementById( 'flying-banner' );
	        var flyingPositionX = parseInt( flying.style.left );
	        var flyingPositionY = parseInt( flying.style.top );
	        if ( flyingStepYPx == 1 ) {
		        flyingStepYPx = 2;
	        } else {
		        flyingStepYPx = 1;
	        }
	        flying.style.top = flyingPositionY + flyingStepXPx + 'px';
	        flying.style.left = flyingPositionX + flyingStepYPx + 'px';
	        if ( Math.abs( flyingPositionX ) >= ( document.body.clientWidth - width - flyingOffsetRight ) || Math.abs(flyingPositionY) >= (document.body.clientHeight - height - flyingOffsetBottom) ) {
		        flyingStop();
	        }
        break;
    }
}
function flyingStop() {
	var flying = document.getElementById( 'flying-banner' );
	flying.style.visibility = 'hidden';
    flying.innerHTML='';
	clearInterval(flyingInterval);
}

/* Scrolling banner */
var scrollingStepPx = 1;
var scrollingStepMs = 50;
var scrollingInterval;

function scrollingStart(bannerSrc,bannerName,bannerId,width,height){
    var browser = checkBrowser();
    var scrollingShowed = false;
    var scrollingShowed = getBannerCookie( bannerName );
    /* eriks -- nav vajadziigs uz iipashiem browseriem */
    /*( !scrollingShowed && ( browser.ie5 || browser.ie6 ) && browser.dom && 1==1 ){ */
    if ( !scrollingShowed )
    {
        var cookieExpire = 12 * 60 * 60 * 1000;
        var bust = Math.floor(89999999*Math.random()+10000000);
        var millis = new Date().getTime();

        // izveidojam ifreimu, anchoru un img objektus ar visiem parametriem
        var scrollingIframe = document.createElement('iframe');
        scrollingIframe.setAttribute('src','http://adv.tvnet.lv/advertpro/servlet/view/banner/html/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis);
        scrollingIframe.setAttribute('id',bannerName);
        scrollingIframe.setAttribute('marginwidth','0');
        scrollingIframe.setAttribute('marginheight','0');
        scrollingIframe.setAttribute('width','1');
        scrollingIframe.setAttribute('height','1');
        var scrollingA = document.createElement('a');
        scrollingA.setAttribute('href','http://adv.tvnet.lv/advertpro/servlet/click/media?mid='+bannerId+'&pid=0&random='+bust+'&millis='+millis);
        scrollingA.setAttribute('target','_top');
        var scrollingImg = document.createElement('img');
        scrollingImg.setAttribute('src',bannerSrc);
        scrollingImg.setAttribute('alt','ClickHere!');
        scrollingImg.setAttribute('width',width);
        scrollingImg.setAttribute('height',height);

        // ievietojam izveidotos objektus scrolling-content div elementaa
        var scrollingContent = document.getElementById('scrolling-content');
        scrollingImgA = scrollingImg.cloneNode(true);
        scrollingImgB = scrollingImg.cloneNode(true);
        scrollingA.appendChild(scrollingImg);
        scrollingA.appendChild(scrollingImgA);
        scrollingA.appendChild(scrollingImgB);
        scrollingA.style.width = width * 3 + 5 + 'px';
        scrollingA.style.height = height + 'px';
        scrollingContent.appendChild(scrollingA);
        scrollingContent.appendChild(scrollingIframe);
        // uzliekam scrolling-contentam augstumu platumu un poziiciju
        scrollingContent.style.width = width * 3 + 5 + 'px';
        scrollingContent.style.height = height + 'px';
        scrollingContent.style.left = '0px';
        scrollingContent.style.top = '0px';

        // uztaisam bannera aizveershanas pogu
        var scrolling = document.getElementById('scrolling-banner');
        var button = document.createElement( "a" );
	    button.className = "hide-banner";
	    button.setAttribute( "cookieexpire", cookieExpire );
        button.setAttribute( "bannername", bannerName );
	    button.onclick = function()
        {
          setBannerCookie(this.getAttribute('bannername'), this.getAttribute('cookieexpire') )
          scrollingStop();
        }
	    var buttonText = document.createTextNode( "" );
	    buttonText.nodeValue = "Slēpt šo reklāmu";
	    button.appendChild( buttonText );
	    scrolling.appendChild( button );

        scrolling.style.left = "0px";
        if ( document.all )
        {
		    // ie
            scrolling.style.top = ( document.documentElement.scrollTop + document.documentElement.clientHeight - height ) + "px";
        }
	    else
        {
		    // paareejiem browseriem
            scrolling.style.top = ( window.innerHeight - height ) + "px";
        }
	    scrolling.style.width = document.body.clientWidth + "px";
        scrolling.style.height = height + 'px';
        if ( window.attachEvent )
        {
		    window.attachEvent( "onscroll", scrollingRePosition );
		    window.attachEvent( "onresize", scrollingRePosition );
	    }
        else
        {
		    window.addEventListener( "resize", scrollingRePosition, 1 );
	    }
	    scrolling.style.visibility = 'visible';
        scrollingInterval = setInterval( "scrollingMove(" + width + ")", scrollingStepMs );
	    //setBannerCookie( bannerName, cookieExpire );
    }

}
function scrollingRePosition(){
    var scrolling = document.getElementById('scrolling-banner');
    var height = parseInt(scrolling.style.height);

    // nochekojam vai sestais jo sestajam gljuks
    var agt=navigator.userAgent.toLowerCase();
    var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie6 = (is_ie && (agt.indexOf("msie 6.")!=-1) );
    if (is_ie6)
    {
        scrolling.style.top = ( document.documentElement.scrollTop + document.documentElement.clientHeight - height ) + "px"; //ie6
    }
    else if (document.all)
    {
        scrolling.style.top = document.documentElement.clientHeight - height +"px"; // ie7
    }
    else
    {
        scrolling.style.top = ( window.innerHeight - height ) + "px"; //other
    }
	scrolling.style.width = document.body.clientWidth + "px";
}
function scrollingMove(width) {
	var scrollingContent = document.getElementById( 'scrolling-content' );
	var scrollingContentPosition = parseInt( scrollingContent.style.left );
	scrollingContent.style.left = scrollingContentPosition - scrollingStepPx + 'px';
	if ( Math.abs(scrollingContentPosition) >= width ) {
		scrollingContent.style.left = ( - scrollingStepPx ) + 'px';
	}
}
function scrollingStop() {
	var scrolling = document.getElementById( 'scrolling-banner' );
	scrolling.style.visibility = 'hidden';
	clearInterval(scrollingInterval);
}
function openUrl_banner(url){
    window.location=url;
}