var OTP_status;
var OTP_starttime;
function OTP(flash_url, fallback_image_url, fallback_url, cookieName, width, height, status, days_to_expire) {
    var width = (typeof(width)!=='undefined')? width:800,        /* the width of the ad */
        height = (typeof(height)!=='undefined')? height:600,     /* the height of the ad */
        timeout = 8,      /* number of seconds before autoclose */
        cookieValue = 1,  /* not significant, but the cooke must contain something */
        cookieExpire = (typeof(days_to_expire)!=='undefined'  &&  days_to_expire != 0  &&  days_to_expire != '')? Number(days_to_expire):7; /* number of days until the cookie expires */
    OTP_status = status;
        
    cookieName = 'sol_OTP_ad_' + cookieName.replace('/','');
    if (!cookieEnabled()  ||  getCookie(cookieName) != false) {
        return;
    }
    
	runningOTP = true;  // globally set in functions.js
    OTP_show_other_ads(0);

    /* replace '[timestamp]' (in lowercase) with a timestamp: */
    var reg_exp = /\[timestamp\]/;
    var flash_url = flash_url.replace(reg_exp, timeformatted);
    var fallback_image_url = fallback_image_url.replace(reg_exp, timeformatted);
    var fallback_url = fallback_url.replace(reg_exp, timeformatted);

    /* draw add */
    var close_button = '<button type="button" class="close" onclick="closeOTP(\'close button\')">X</button>';
    var fallback = '<a href="'+fallback_url+'"><img src="'+fallback_image_url+'" alt="" /></a>';

    if (status=='test fallback') {
        var html = fallback;
    } else {
        swfobject.switchOffAutoHideShow();
        swfobject.registerObject('the_object_OTP', '7', '');
        var html = '<object id="the_object_OTP" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+width+'" height="'+height+'">'+
            '<param name="movie" value="'+flash_url+'" />'+
            '<param name="allowScriptAccess" value="always" />'+
            '<!--[if !IE]>-->'+
            '<object type="application/x-shockwave-flash" data="'+flash_url+'" width="'+width+'" height="'+height+'">'+
                '<param name="allowScriptAccess" value="always" />'+
            '<!--<![endif]-->'+
                fallback+
            '<!--[if !IE]>-->'+
            '</object>'+
            '<!--<![endif]-->'+
        '</object>';
    }

    document.getElementById('ad_OTP').style.display = 'block';
    var OTPelement = document.getElementById('the_ad_OTP');
    OTPelement.innerHTML = close_button + html;
    OTPelement.style.display = 'block';
    OTPelement.style.width = width+'px';
    OTPelement.style.height = height+'px';
    if (navigator.appName.indexOf("Microsoft")==-1) {  /* for ie we cannot find the window height until after the body-tag is drawn */
        OTPelement.style.top = (window.innerHeight-height)/2 + 'px';
        OTPelement.style.left = ((980-width)/2)+20 + 'px';
    }
    
    OTP_starttime = Math.round(new Date().getTime() / 1000);
    
    if (status == 'on') {  /* if it's not a test */
        setTimeout("closeOTP('timeout')", timeout*1000);       /* close after 'timeout' seconds */
        setCookie(cookieName, cookieValue, cookieExpire);
    }
}


/* close the whole thing */
function closeOTP(log_txt) {
    if (OTP_status != 'on') {
        alert('CloseOTP called by: '+log_txt);
    }

    // close the OTP (but check that it is not already closed)
    var el = document.getElementById('ad_OTP')
    if (el.style.display != 'none') {
        el.style.display = 'none';
        document.getElementById('the_ad_OTP').style.display = 'none';
        OTP_show_other_ads(1);
        
        // analytics
        var closetime = Math.round(new Date().getTime() / 1000);
            runtime = closetime - OTP_starttime,
            log_txt_one = log_txt.split(' ').join('')  // make a one-word version
        ;
        $.get('http://sol.linkpulse.com/go/e/OTP_'+runtime+'_'+log_txt_one+'/http://www.sol.no/OTPlogs/closeOTP/'+log_txt_one+'/'+runtime+'/');
        var _gaq = _gaq || [];
        _gaq.push(['_trackEvent', 'OTP', 'closeOTP', log_txt, runtime]);
        //_gaq.push(['_trackEvent', 'OTP', 'closeOTPtime', ''+runtime]);
    }
}


/* toggle other possible interfering ads on and off */
var OTP_timeout;
function OTP_show_other_ads(toggle, counter) {  /* toggle=0 (zero) is hide,  toggle=1 is show ads. */
    if (toggle==1  &&  typeof(counter) == 'undefined') {
        clearTimeout(OTP_timeout);   // lets stop removing ads, if we still are trying, when we shall put them back
    }
    var list = new Array('ad_topbanner', 'ad_midtbanner', 'ad_sticker', 'ad_netboard', 'ad_netboard2', 'ad_thinbanner', 'ad_slimbanner');
    for (i in list) {
        var element = document.getElementById(list[i]);
        if (element == null) {
            counter = (counter==undefined)? 1:(counter==1)? 2:counter+1;
            if (counter <= 30) {
                OTP_timeout = setTimeout('OTP_show_other_ads('+toggle+', '+counter+')', 1000);  /* try again in one sec */
            }
            return;
        }
        element.style.visibility = (toggle==0)? 'hidden':'visible';
        /*console.log('OTP: ' + list[i] + ', toggle: ' + toggle + ', counter: ' + counter);*/
    }
}

