/* 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();
        }
    }
}




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

function scrollingStart(bannerSrc,bannerName,bannerUrl,width,height){
    var browser = checkBrowser();
    var scrollingShowed = false;
    var scrollingShowed = getBannerCookie( bannerName );
    if ( !scrollingShowed )
    {
        var cookieExpire = 12 * 60 * 60 * 1000;
        var bust = Math.floor(89999999*Math.random()+10000000);
        var millis = new Date().getTime();            
        
        var scrollingA = document.createElement('a');
        scrollingA.setAttribute('href','http://www.baltkom.lv/fi_ads/?id='+bannerName+'&r='+bannerUrl);
        scrollingA.setAttribute('target','_blank');
        var scrollingImg = document.createElement('img');
        scrollingImg.setAttribute('src',bannerSrc);
        scrollingImg.setAttribute('alt','ClickHere!');
        scrollingImg.setAttribute('width',width);
        scrollingImg.setAttribute('height',height);

        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.style.width = width * 3 + 5 + 'px';
        scrollingContent.style.height = height + 'px';
        scrollingContent.style.left = '0px';
        scrollingContent.style.top = '0px';

        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 )
        { 
            scrolling.style.top = ( document.documentElement.scrollTop + document.documentElement.clientHeight - height ) + "px";
        }
        else
        {
            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 );
    }

}
function scrollingRePosition(){
    var scrolling = document.getElementById('scrolling-banner');
    var height = parseInt(scrolling.style.height);   
    
    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);
}
