﻿var currentImage = null;
var displayTime = 0;
var showingNext = false;
var showingPrevious = false;
var timer = null;

function timeOutDisplay() {
    PrepareForDisplay();
}

function PrepareForDisplay() {
    currentImage = graphicAds[currentAdIndex];
    currentAdIndex += 1;
    if (currentAdIndex > graphicAds.length) {
        currentAdIndex = 0;
        if (timer != null)
            clearTimeout(timer);
        clearAdsArray(graphicAds);
        GetAdFeedContents(publisherId, adFeedId);
    }
    if (currentImage != null) {
        if (timer != null)
            clearTimeout(timer);
        DisplayAd();
        timer = setTimeout(timeOutDisplay, displayTime * 1000);
    }
    return true;
}

function DisplayAd() {
    if (currentImage == null)
        return;
    showingNext = false;
    showingPrevious = false;
    HandleControls('auto');
    ShowImage();
    return true;
}

function PlayAuto() {
    if (timer != null)
        clearTimeout(timer);
    currentAdIndex += 1;
    PrepareForDisplay();
}

function PauseAuto() {
    if (timer != null)
        clearTimeout(timer);

    showingNext = true;
    showingPrevious = true;
    HandleControls('pause');
    ShowImage();
}

function NextImage() {
    if (showingNext == true) {
        currentAdIndex += 1;
    }
    showingNext = true;
    showingPrevious = true;
    if (currentAdIndex < graphicAds.length) {
        currentImage = graphicAds[currentAdIndex];
    }
    else {
        currentAdIndex = 0;
        currentImage = graphicAds[currentAdIndex];
    }
    ShowImage();
}

function PreviousImage() {
    if (showingPrevious == true) {
        currentAdIndex -= 1;
    }
    else {
        currentAdIndex -= 2;
    }
    showingPrevious = true;
    showingNext = true;
    if (currentAdIndex >= 0) {
        currentImage = graphicAds[currentAdIndex];
    }
    else {
        currentAdIndex = graphicAds.length - 1;
        currentImage = graphicAds[currentAdIndex];
    }
    ShowImage();
}

function printme() {
    var image = new Image();
    image.src = currentImage.Link;
    src = image.src;
    link = "about:blank";
    var pw = window.open(link, "_new");
    pw.document.open();
    pw.document.write(makepage(src));
    pw.document.close();
}

function makepage(src) {
    //break the closing script tag in half to prevent the HTML parser from seeing it as a part of the *main* page.
    return "<html>\n" +
    "<head>\n" +
    "<title>Temporary Printing Window</title>\n" +
    "<script>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
    "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad='step1()'>\n" +
    "<img src='" + src + "'/>\n" +
    "</body>\n" +
    "</html>\n";
}
function magnifyme() {
    if (timer != null)
        clearTimeout(timer);
    timer = setTimeout(timeOutDisplay, 120 * 1000);

    showingNext = true;
    showingPrevious = true;
    HandleControls('pause');
    ShowImage();

    PositionX = 100;
    PositionY = 100;

    // Set these value approximately 20 pixels greater than the size of the largest image to be used (needed for Netscape)
    defaultWidth = 1000;
    defaultHeight = 1000;

    if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
        var isNN = (navigator.appName == "Netscape") ? 1 : 0;
        var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? 1 : 0;
    }
    var optNN = 'scrollbars=yes,resizable=yes,status=no,width=' + defaultWidth + ',height=' + defaultHeight + ',left=' + PositionX + ',top=' + PositionY;
    var optIE = 'scrollbars=yes,resizable==yes,status=no,width=150,height=100,left=' + PositionX + ',top=' + PositionY;

    if (isNN) { imgWin = window.open('about:blank', '', optNN); }
    if (isIE) { imgWin = window.open('about:blank', '', optIE); }
    with (imgWin.document) {
        writeln('<html><head><title>Loading...</title><style>body{margin:0;background-color:White;}</style>'); writeln('<sc' + 'ript>');
          writeln('var isNN,isIE;'); writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
          writeln('isNN=(navigator.appName=="Netscape")?1:0;'); writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
          writeln('function reSizeToImage(){'); writeln('if (isIE){'); writeln('window.resizeTo(300,300);');
          writeln('width=330-(document.body.clientWidth-document.images[0].width);');
          writeln('height=350-(document.body.clientHeight-document.images[0].height);');
          writeln('width=document.images[0].width+30;');
          writeln('height=document.images[0].height+90;');
          writeln('window.resizeTo(width,height);}'); writeln('if (isNN){');
          writeln('window.innerWidth=document.images["LPIMG"].width;'); writeln('window.innerHeight=document.images["LPIMG"].height;}}');
          writeln('</sc' + 'ript>');
          writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();self.focus()">')
          writeln('<div style="height: 100%; overflow: auto;"><img name="LPIMG" src=' + currentImage.Link + ' style="display:block;"></div></body></html>');
          close();
    } 
}

function ShowImage() {
    var obj = document.getElementById("lblAdSummary");
    var divO = document.getElementById("overlay");
    var divE = document.getElementById("external");
    var imgOv = document.getElementById("overlayImage");
    var imgH = 0;
    var imgW = 0;
    var adImg = '';
    var tempW = 0;
    var tempH = 0;
    var isShrunk = false;
    var extLink = '';
    adImg = currentImage.Link;
    extLink = currentImage.ExternalLink;
    var imgL = document.getElementById("extLinkImage");
    if (extLink != '') {
        var lnk = document.getElementById("lnkE");
        lnk.href = extLink;
        if (divE.style.display == 'none') {
            divE.style.display = 'block';
        }
        if(lang == 'DE')
            imgL.src = controlImages[11];
        else
            imgL.src = controlImages[12];
    }
    else {
        if (divE.style.display == 'block') {
            divE.style.display = 'none';
        }
        imgL.src = controlImages[10];
    }

    imgH = currentImage.Height;
    imgW = currentImage.Width;
    if (imgW > maxWidth || imgH > maxHeight) {
        var widthR = imgW / maxWidth;
        var heightR = imgH / maxHeight;
        var ratio = Math.max(widthR, heightR);
        tempH = imgH / ratio;
        tempW = imgW / ratio;
        isShrunk = true;
        widthR = null;
        heightR = null;
        ratio = null;
    }
    else {
        tempW = imgW;
        tempH = imgH;
    }
    var newImg;
    var imagO = new Image();
    if (isShrunk) {
        if (divO.style.display == 'none') {
            divO.style.display = 'block';
            divO.style.top = parseInt(tempH / 2) + "px";
        }
        imagO.src = controlImages[8];
        imgOv.src = imagO.src;
        newImg = "<img border='0' onclick='magnifyme();' class='PopBoxImageSmall' title='Click to magnify' height='" + tempH + "px'" + " width='" + tempW + "px' src='" + currentImage.Link + "' />";
    }
    else {
        if (divO.style.display == 'block')
            divO.style.display = 'none';
        imagO.src = controlImages[10];
        imgOv.src = imagO.src;
        newImg = "<img border='0' height='" + tempH + "px'" + " width='" + tempW + "px' src='" + currentImage.Link + "' />";
    }
    obj.innerHTML = newImg;
    newImg = null;
    obj = null;
    imagO = null;
    divO = null;
    imgOv = null;
    return true;
}

function HandleControls(contr) {
    var imgP = document.getElementById("imgPlay");
    var imgPa = document.getElementById("imgPause");
    var image1 = new Image();
    var image2 = new Image();
    if (contr == 'auto') {
        image1.src = controlImages[4];
        imgPa.src = image1.src;
        image2.src = controlImages[3];
        imgP.src = image2.src;
    }
    if (contr == 'pause') {
        image1.src = controlImages[5];
        imgPa.src = image1.src;
        image2.src = controlImages[2];
        imgP.src = image2.src;
    }
    if (contr == 'prev') {
        image1.src = controlImages[5];
        imgPa.src = image1.src;
        image2.src = controlImages[2];
        imgP.src = image2.src;
    }
    if (contr == 'next') {
        image1.src = controlImages[5];
        imgPa.src = image1.src;
        image2.src = controlImages[2];
        imgP.src = image2.src;
    }

    image1 = null;
    image2 = null;
    imgP = null;
    imgPa = null;
    return;
}


