// This file is for showing advertisment.
//
// 1.0 - Nick G.      - Initial
// 1.1 - Ferdinand S. - Using HASH lookup for faster lookup and nicer config. Using URL falldowncheck.
// 1.2 - Ferdinand S. - Dart added
//
// This file uses the macguffin_places.js for the ids

var Ads_kid=0, Ads_bid=0, Ads_xp="", Ads_yp="",  Ads_opt=0,  Ads_wrd="",  Ads_prf, Ads_par="", Ads_cnturl="",  Ads_sec=0,  Ads_channels="";
var DEBUG = false;
var DISABLE_BANNERS = false;
var UsedBannerposities = '';
var tilecounter = 0; // So same banner pos on 1 page get diff ads
var random = Math.round(Math.random()*100000000); // random number generated for each pageview

// Doubleclick / DART implementation
//
// Oldstyle was size,identifier Newstyle is only identifier
function showBanner(identifier,identifier2) {
    if ( DISABLE_BANNERS ) { return }
    if ( identifier2 ) { identifier=identifier2 }

    // Consts
    var prefix = "http://ad.doubleclick.net/";
    //var domain = "P4219.endeca.vacaturekrant.nl/"; // for test
    var domain = "P3851.vacaturekrant.nl/"; // for live
    var countdown=3;

    // Save data for Omniture statistics
    if ( UsedBannerposities.length > 0 ) { UsedBannerposities += ',' }
    UsedBannerposities += identifier;

    // Get URL and make site depended changes
    var checkedpath=exceptionCheck(location.pathname);

    var ad={ zone: null };

    // Loop through all locations
    while ( ! ad["zone"] ) {
        // Handle max rounds
        if ( countdown == 0 ) { debug('<p style="font-size:8px">SET PATH TO "/"</p>'); checkedpath = '/' }
        if ( checkedpath=="" || countdown < 0 ) { debug('<p style="font-size:8px">NO ADD FOUND</p>'); break }

        // Get the add
        ad=getDartAd(identifier.toLowerCase(),checkedpath);
        if ( ad["zone"] ) { break; }

        // Remove last part and try again
        debug('<p style="font-size:8px;white-space:nowrap">'+checkedpath+" ("+countdown+") NotFound</p>");
        checkedpath=checkedpath.replace(/[^\/]+\/?$/,"");

        countdown--;
    }

    // Give only the size if defined
    if ( ! ad["width"]  ) { ad["width"]=0  }
    if ( ! ad["height"] ) { ad["height"]=0 }
    var size = ad["width"] + 'x' + ad["height"];

    var keywordMapping = new Array('merk','model');

    // Process the keywords
    var keywords = '';
    if ( Ads_wrd && Ads_wrd != '' ) {
        var keys = Ads_wrd.split(';');
        for(var i=0; i < keys.length; i++) {
	    keywords += ';' + keywordMapping[i] + '=' + keys[i];
        }
    }


    var url = prefix + 'adj/' + domain + ad["zone"] + ";sz=" + size + ";tile=" + (++tilecounter) + keywords;
	if (typeof segQS != 'undefined') url += ';' + segQS;
	url += ";ord=" + random + "?";

    debug('<table bgcolor="yellow" border="1" style="z-index:99999;position:absolute"><tr><td style="font-size:9px"> AdWord:'+Ads_wrd+'<br>Path:'+checkedpath+'<br>Size:'+size+'<br>Identifier:'+identifier+'<br>adId:<b>'+ad["zone"]+ '<br>url:<b>' + url + '</b></table>');

    var tag = '<scr'+'ipt type="text/jav'+'ascript" language="jav'+'asc'+'ript" ';
        tag += 'src="' + url + '"></scr'+'ipt>\n';
    document.write(tag);
}

// Function for adding vars to URL
function getVars(requestedVar,justvalue){
    var pairs = unescape(document.location.search.substring(1)).split(/;|&/);
    for (var i=0; i < pairs.length; i++) {
        var values = pairs[i].split("=");
        if (values[0] == requestedVar) {
	        if (justvalue) { return(values[1]) }
            return("?"+values[0]+"="+values[1]);
        }
    }
    return("");
}

// Gets DART ad
function getDartAd(identifier,path){
    if ( mac_dart_ids[identifier][path] ) {
        return mac_dart_ids[identifier][path];
    }
    else {
        return { zone: null };
    }
}

// Debug
function debug(str) {
    if ( DEBUG == true ) {
        document.write(str);
    }
}

