//Date: 23 Feb 2012
//Week: 8
var today = new Date();
var now = today.getTime();

var bannerIds = new Array();
var bannerTitles = new Array();
var pictures = new Array();
var percentages = new Array();
var widths = new Array();
var heights = new Array();
var altTexts = new Array();
var params = new Array();
var urlLinks = new Array();

var bcnt = 0;
bannerIds[bcnt] = "3413";
bannerTitles[bcnt] = "Fonderie Viterbesi - Leader Board";
pictures[bcnt] = "/13/banner/00/34/13/768x90.gif";
percentages[bcnt] = 50;
widths[bcnt] = 728;
heights[bcnt] = 90;
altTexts[bcnt] = "";
params[bcnt] = "";
urlLinks[bcnt++] = "/fonderie-viterbesi/company.html";
bannerIds[bcnt] = "3614";
bannerTitles[bcnt] = "Fonderie Viterbesi - Leader Board-2";
pictures[bcnt] = "/13/banner/00/36/14/Fonderie Viterbesi 728x60-v2.gif";
percentages[bcnt] = 50;
widths[bcnt] = 728;
heights[bcnt] = 90;
altTexts[bcnt] = "";
params[bcnt] = "";
urlLinks[bcnt++] = "/fonderie-viterbesi/company.html";
bannerIds[bcnt] = "3613";
bannerTitles[bcnt] = "Mappy skyscraper banner 160 x 600";
pictures[bcnt] = "/13/banner/00/36/13/mappy_skyscraper.jpg";
percentages[bcnt] = 50;
widths[bcnt] = 160;
heights[bcnt] = 600;
altTexts[bcnt] = "";
params[bcnt] = "";
urlLinks[bcnt++] = "/mappy-middle-east/company.html";
bannerIds[bcnt] = "3616";
bannerTitles[bcnt] = "Faisal Jassim skyscraper banner 160 x 600";
pictures[bcnt] = "/13/banner/00/36/16/fjtco_skyscraper_160x600_2.jpg";
percentages[bcnt] = 50;
widths[bcnt] = 160;
heights[bcnt] = 600;
altTexts[bcnt] = "Faisal Jassim Trading Co - We bring life to your building";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.faisaljassim.ae";
bannerIds[bcnt] = "3672";
bannerTitles[bcnt] = "Dubai Woodshow 2012 - 234x60";
pictures[bcnt] = "/13/banner/00/36/72/woodshow_2012_234x60.jpg";
percentages[bcnt] = 100;
widths[bcnt] = 234;
heights[bcnt] = 60;
altTexts[bcnt] = "Dubai Woodshow 2012 - click for more information";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.dubaiwoodshow.com";
bannerIds[bcnt] = "3618";
bannerTitles[bcnt] = "Bricscad V12 234x60";
pictures[bcnt] = "/13/banner/00/36/18/bricscad_banner_234x60-.gif";
percentages[bcnt] = 100;
widths[bcnt] = 234;
heights[bcnt] = 60;
altTexts[bcnt] = "";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.bricsys.com";
bannerIds[bcnt] = "3674";
bannerTitles[bcnt] = "Big 5 Saudi 2012 - 234 x 60";
pictures[bcnt] = "/13/banner/00/36/74/234x60-v2-eng-anim.gif";
percentages[bcnt] = 100;
widths[bcnt] = 234;
heights[bcnt] = 60;
altTexts[bcnt] = "The Big 5 Saudi Arabia";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.thebig5saudi.com/aec1";
bannerIds[bcnt] = "3686";
bannerTitles[bcnt] = "Office Exhibition 2012 - 468x60";
pictures[bcnt] = "/13/banner/00/36/86/office_2012.gif";
percentages[bcnt] = 100;
widths[bcnt] = 468;
heights[bcnt] = 60;
altTexts[bcnt] = "Office Exhibition 2012 - click for more information";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.theofficeexhibition.com/aec";
bannerIds[bcnt] = "3687";
bannerTitles[bcnt] = "Hotel Show 2012 - 468x60";
pictures[bcnt] = "/13/banner/00/36/87/THSbanner468x60.gif";
percentages[bcnt] = 100;
widths[bcnt] = 468;
heights[bcnt] = 60;
altTexts[bcnt] = "The Hotel Show 2012 - click for more information";
params[bcnt] = "";
urlLinks[bcnt++] = "http://www.thehotelshow.com/";


var swfs = new Array();
for (var i = 0; i < bcnt; i++) {
    swfs[i] = null;
}

function getBookedBannerIds(position) {
    var bookedBannerIds = new Array();
    if (position == null) position = "";
    if (position == "000042") {
        bookedBannerIds[0] = "3413";
        bookedBannerIds[1] = "3614";
    }
    if (position == "000043") {
        bookedBannerIds[0] = "3613";
        bookedBannerIds[1] = "3616";
    }
    if (position == "000044") {
        bookedBannerIds[0] = "3672";
    }
    if (position == "000045") {
        bookedBannerIds[0] = "3618";
    }
    if (position == "000046") {
        bookedBannerIds[0] = "3674";
    }
    if (position == "000051") {
        bookedBannerIds[0] = "3686";
    }
    if (position == "000052") {
        bookedBannerIds[0] = "3687";
    }
    return bookedBannerIds;
}

var usedPositions = new Array();
var currentBannerIndexes = new Array();
var MINPOSITIONID = 37;
var MAXPOSITIONID = 52;

function bannerClickCount(positionStr) {
    if (positionStr) {
        var positionIndex = -1;
        for (var i = 0; ((i < usedPositions.length) && (positionIndex == -1)); i++) {
            if (usedPositions[i] == positionStr) positionIndex = i;
        }
        if (positionIndex >= 0) {
           docu_stats_clicktru_no_time('BANNER', bannerIds[currentBannerIndexes[positionIndex]], null, 13);
        }
    }
}

function bannerViewCount(bannerIndex) {
    docu_stats_bannerview_no_time(bannerIds[bannerIndex], null, 13);
}

function bannerViewCountByBannerId(bannerId) {
    docu_stats_bannerview_no_time(bannerId, null, 13);
}

function showBanner(position, bannerIndex) {
    //alert(position + " banner " + bannerIndex);
    if (document.getElementById('AdPosition' + position)) {
        document.getElementById('AdPosition' + position).style.display = "";
        if (altTexts[bannerIndex] == "Flash") {
            // Flash banner
            if (swfs[bannerIndex] == null) {
                var bannerWidth = widths[bannerIndex];
                if (bannerWidth == 0) bannerWidth = 468;
                var bannerHeight = heights[bannerIndex];
                if (bannerHeight == 0) bannerHeight = 60;
                swfs[bannerIndex] = new SWFObject(pictures[bannerIndex], "pullContent", bannerWidth, bannerHeight, "8");
                swfs[bannerIndex].addParam("wmode", "opaque");
                if (params[bannerIndex] != "") {
                    var parameters = params[bannerIndex] + ",";
                    var commaIndex = parameters.indexOf(",");
                    while (commaIndex != -1) {
                        var firstPart = parameters.substring(0, commaIndex);
                        var equalsIndex = parameters.indexOf("=");
                        if (equalsIndex != -1) {
                            var param = firstPart.substring(0, equalsIndex);
                            var value = firstPart.substring(equalsIndex + 1, firstPart.length);
                            swfs[bannerIndex].addVariable(param, value);
                        }
                        parameters = parameters.substring(commaIndex + 1, parameters.length);
                        commaIndex = parameters.indexOf(",");
                    }
                }
            }
            if (document.getElementById('FlashBanner' + position)) {
	               if (document.getElementById('AdImage' + position)) document.getElementById('AdImage' + position).style.display = "none";
                if (document.getElementById('BannerText' + position)) document.getElementById('BannerText' + position).style.display = "none";
                swfs[bannerIndex].write('FlashBanner' + position);
                document.getElementById('FlashBanner' + position).style.display = "";
            }
        } else {
            if (document.getElementById('AdLink' + position)) document.getElementById('AdLink' + position).href = urlLinks[bannerIndex];
            if (pictures[bannerIndex] == "") {
                // Text link
                if (document.getElementById('AdLink' + position)) document.getElementById('AdLink' + position).firstChild.nodeValue = bannerTitles[bannerIndex];
                if (document.getElementById('BannerText' + position)) {
	                   if (document.getElementById('AdImage' + position)) document.getElementById('AdImage' + position).style.display = "none";
                	   if (document.getElementById('FlashBanner' + position)) document.getElementById('FlashBanner' + position).style.display = "none";
                    document.getElementById('BannerText' + position).firstChild.nodeValue = altTexts[bannerIndex];
                    document.getElementById('BannerText' + position).style.display = "";
                    document.getElementById('BannerText' + position).blur();
                }
            } else {
                // Image banner
                var pictureName = pictures[bannerIndex];
                if (pictureName.indexOf("?") == -1) pictureName += "?ord=" + now;
                if (params[bannerIndex] != "") pictureName += "&" + params[bannerIndex];
                if (document.getElementById('AdImage' + position)) {
                	   if (document.getElementById('FlashBanner' + position)) document.getElementById('FlashBanner' + position).style.display = "none";
                	   if (document.getElementById('BannerText' + position)) document.getElementById('BannerText' + position).style.display = "none";
                    document.getElementById('AdImage' + position).src = pictureName;
                    document.getElementById('AdImage' + position).alt = altTexts[bannerIndex];
                    document.getElementById('AdImage' + position).title = altTexts[bannerIndex];
                    document.getElementById('AdImage' + position).style.display = "";
                    document.getElementById('AdImage' + position).blur();
                }
            }
        }
        bannerViewCount(bannerIndex);
    }
}

function drawBanner(bannerIndexes) {
    var selectedBannerIndex = -1;
    var maxPercentage = 0;
    for (var i = 0; i < bannerIndexes.length; i++) {
        maxPercentage += percentages[bannerIndexes[i]];
    }
    if (maxPercentage > 100) maxPercentage = 100;
    var randomNumber = Math.floor(Math.random() * maxPercentage); // generates a random integer between 0 and maxPercentage - 1 (0 and maxPercentage -1 included)
    var start = 0;
    var end = 0;
    for (var i = 0; ((i < bannerIndexes.length) && (start < 100) && (selectedBannerIndex == -1)); i++) {
        end += percentages[bannerIndexes[i]];
        if ((randomNumber >= start) && (randomNumber < end)) {
            selectedBannerIndex = bannerIndexes[i];
        }
        start = end;
    }
    return selectedBannerIndex;
}

function rotateBanners() {
    for (var i = MINPOSITIONID; i <= MAXPOSITIONID; i++) {
        var positionStr = '' + i;
        while (positionStr.length < 6) {
            positionStr = '0' + positionStr;
        }
        var bookedBannerIds = getBookedBannerIds(positionStr);
        if (bookedBannerIds.length > 1) {	// Rotation is only possible when there's more than one banner for a position
            var bookedBannerIndexes = new Array(bookedBannerIds.length);
            for (var j = 0; j < bookedBannerIds.length; j++) {
                for (var k = 0; ((k < bannerIds.length) && (bookedBannerIndexes[j] == null)); k++) {
                    if (bookedBannerIds[j] == bannerIds[k]) bookedBannerIndexes[j] = k;
                }
            }
	           var positionIndex = -1;
	           for (var j = 0; ((j < usedPositions.length) && (positionIndex == -1)); j++) {
	               if (usedPositions[j] == positionStr) positionIndex = j;
	           }
	           if (positionIndex >= 0) {
	        	   var currentBannerIndex = currentBannerIndexes[positionIndex];	// Current banner for the position
	        	   if (currentBannerIndex >= 0) {
	        	       var indexInBookedArray = -1;
	        		   for (var j = 0; ((j < bookedBannerIndexes.length) && (indexInBookedArray == -1)); j++) {
			               if (bookedBannerIndexes[j] == currentBannerIndex) indexInBookedArray = j;
			           }
			           if (indexInBookedArray >= 0) {
			        	   var nextBookedBannerIndex = indexInBookedArray + 1;
			        	   if (nextBookedBannerIndex > (bookedBannerIndexes.length - 1)) nextBookedBannerIndex = 0;
			        	   var newBannerIndex = bookedBannerIndexes[nextBookedBannerIndex];
			               showBanner(positionStr, newBannerIndex);
		                   currentBannerIndexes[positionIndex] = newBannerIndex;
	           		   }
	           	   }
	           }
	       }
    }
}

function startBannerShow() {
 setTimeout("startBannerShowWithDelay()", 250);
}
function startBannerShowWithDelay() {
    var usedCounter = 0;
    for (var i = MINPOSITIONID; i <= MAXPOSITIONID; i++) {
        var positionStr = '' + i;
        while (positionStr.length < 6) {
            positionStr = '0' + positionStr;
        }
        var bookedBannerIds = getBookedBannerIds(positionStr);
        if (bookedBannerIds.length > 0) {
            var selectedBannerIndex = -1;
            var preferredBannerId = -1;
            // Look up start banner on page
            var startBannerDiv = document.getElementById('startbannerDefinition');
            if (startBannerDiv) {
            	   if (startBannerId) {
                    preferredBannerId = startBannerId;
                }
            }
            // Look up indexes in banner array
            var bookedBannerIndexes = new Array(bookedBannerIds.length);
            for (var j = 0; j < bookedBannerIds.length; j++) {
                for (var k = 0; ((k < bannerIds.length) && (bookedBannerIndexes[j] == null)); k++) {
                    if (bookedBannerIds[j] == bannerIds[k]) {
                        bookedBannerIndexes[j] = k;
                        if (bookedBannerIds[j] == preferredBannerId) selectedBannerIndex = k;
                    }
                }
            }
            if (selectedBannerIndex < 0) selectedBannerIndex = drawBanner(bookedBannerIndexes);
            if (selectedBannerIndex >= 0) {
                showBanner(positionStr, selectedBannerIndex);
                usedPositions[usedCounter] = positionStr;
                currentBannerIndexes[usedCounter] = selectedBannerIndex;
                usedCounter++;
            }
        }
    }
    setInterval("rotateBanners()", 10000);
}


