/* ADVERTISING BLOCK */

var advCurTmStamp = new Date();
document.usedAdvData = "";
var isInnerHtmlSupported = -1;
var shownAdvPlaces = '';
var shownAdvPlacesCnt = 0;
var advReferrer = ''
var advPlaceCnts = new Array();
if(document.referrer)
    advReferrer = document.referrer;
function processShownAdvPlaces(shownAdvPlaces){
    counter = 0;
    pos = -1;
    res = "";
    while((pos = shownAdvPlaces.indexOf(';', pos+1)) >= 0){
        if((pos1 = shownAdvPlaces.indexOf(';', pos+1)) < 0)
            pos1 = shownAdvPlaces.length;
        if(pos1-pos-1 > 0){
            curAdvPlace = shownAdvPlaces.substr(pos+1, pos1-pos-1);
            
            if((cpos = curAdvPlace.indexOf('_')) >= 0){
                counter = curAdvPlace.substr(cpos+1)-1;
                curAdvPlace = curAdvPlace.substr(0, cpos);
            }
            
            advPlaceCnts[curAdvPlace] = ++counter;
            res += ';'+curAdvPlace+"_"+counter;
        }
    }
    if(res.length > 0)
        res += ';';
    return res;
}
function showAdvPlace(idPlace, viewURL){
    if(isInnerHtmlSupported == -1){
        if(document.body.innerHTML)
            isInnerHtmlSupported = 1;
        else
            isInnerHtmlSupported = 0;
    }
    // After the document is loaded (new scheme)
    if(isInnerHtmlSupported == 1){
        shownAdvPlacesCnt ++;
        document.write('<span id="advp_'+idPlace+'_'+(advPlaceCnts[idPlace] ? advPlaceCnts[idPlace] : shownAdvPlacesCnt)+'"></span>');
        shownAdvPlaces += (shownAdvPlaces == '' ? ';' : '')+idPlace+';';
    // Old scheme - inline method
    }else{
        if (!document.usedAdvData)
            document.usedAdvData = '';
        rndseed = new String(Math.random()); rndseed = rndseed.substring(2,11);
        document.write ("<" + "script language='JavaScript' type='text/javascript' src='"+viewURL+"aproc.php?action=view&rs="+advCurTmStamp.getTime()+rndseed+"&place="+idPlace+"&used="+document.usedAdvData+"&curl="+escape(viewURL)+"&referer="+escape(advReferrer)+"'><"+"/script>");
    }
}

function showAdvBanner(id, content){
    if(document.usedAdvData)
        document.usedAdvData += ','+id+',';
    else
        document.usedAdvData  = ','+id+',';
    document.writeln(content);
}

function aLnkClick(lnkID){
    var aLink = frontBaseHref+"aproc.php?action=lclick&id="+lnkID+"&page_url="+escape(document.location.href);
    advClick(aLink);
}

function advClick(clickURL){
    rndseed = new String(Math.random()); rndseed = rndseed.substring(2,11);
    clickURL+="&rs="+advCurTmStamp.getTime()+rndseed;
    img = new Image();
    img.src = clickURL;
}

var advPlacesContent = new Array();
var fillAdvPlaceWatcher = new Array();
function fillAdvPlace(idPlace, idCnt, placeContent){
    if(advPlacesContent[idPlace+'_'+idCnt] == undefined)
        advPlacesContent[idPlace+'_'+idCnt] = placeContent;
    if(fillAdvPlaceWatcher[idPlace+'_'+idCnt] == undefined)
        fillAdvPlaceWatcher[idPlace+'_'+idCnt] = 0;
    if(document.getElementById && document.getElementById('advp_'+idPlace+'_'+idCnt) && document.getElementById('advp_'+idPlace+'_'+idCnt).innerHTML != undefined){
        document.getElementById('advp_'+idPlace+'_'+idCnt).innerHTML = advPlacesContent[idPlace+'_'+idCnt];
    }else{
        fillAdvPlaceWatcher ++;
        if(fillAdvPlaceWatcher < 1000)
            setTimeout("fillAdvPlace('"+idPlace+"', '"+idCnt+"');", 100);
    }
}

// Returns cookie value
function getCookie(name) {
    var search = name + "=";
    if (document.cookie.length > 0) {
        // if there are any cookies
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            // if cookie exists
            offset += search.length;
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, end));
        }
    }
}

// {{{ window.onLoad events queue implementation

var onLoadEvents = new Array ();
var previousOnLoadEvent;

function addOnLoadEvent(event) {
    onLoadEvents[onLoadEvents.length] = event;
}

function runOnLoadEventsQueue() {
    if (previousOnLoadEvent) {
        previousOnLoadEvent();
    }
    for (var i = 0 ; i < onLoadEvents.length; i++) {
        onLoadEvents[i]();
    }
}

function savePreviousOnLoadEvent() {
    previousOnLoadEvent = window.onload;
    window.onload = runOnLoadEventsQueue;
}
