/* ---------------------------------------------------------------------------------------------------
   presentation_event.js $Revision: 1.1.2.1 $
   --------------------------------------------------------------------------------------------------- */

/* - Image Preview ----------------------------------------------------------------------------------- */

var preloadInterval ="";
function preloadImage(ImageID) {
// ---------------------------------------------------------------------------------------------------
// preloadImage([id of an img tag])
// ---------------------------------------------------------------------------------------------------
// This function can be used in the previewArea only!
// It tests if the download of an image is complete. On positive result it:
//  - displays the image by changing its display properties
//  - changes the size of large images
//  - removes its own interval
//
// Usage example:
// if(preloadInterval!="") clearInterval(preloadInterval);            // remove possible old interval
// preloadInterval = setInterval("preloadImage('"+ImageID+"')",100);  // start this function in an interval

    //settings for height and width according to source of functioncall
    var ImgWidth = 190;
    var ImgHeight= 140;
    if(Caller == 1) {   // storefront image format
        ImgWidth = 200;
        ImgHeight = 200;
    }

	var IsComplete=(document.getElementById(ImageID).width!=0 || browser.isIE) && document.getElementById(ImageID).complete;

    if(IsComplete) {                         // do nothing while the image is not loaded yet
    // create dummy image to get (non-cached) image width & height
        testImage=document.createElement("img");
        testImage.src=document.getElementById(ImageID).src;

        percent = 100;
        if(testImage.width != 0) {                                          // do nothing while the width is unset
            document.getElementById(ImageID).style.display="block";
            if(testImage.width> ImgWidth || testImage.height> ImgHeight) {  // resize large images
                horPercent = 100*ImgWidth/testImage.width;
                verPercent = 100*ImgHeight/testImage.height;
                if(horPercent > verPercent) {
                    document.getElementById(ImageID).height =ImgHeight;
                    document.getElementById(ImageID).width =testImage.width * verPercent / 100;
                    percent = verPercent;
                }  else {
                    document.getElementById(ImageID).width =ImgWidth;
                    document.getElementById(ImageID).height =testImage.height * horPercent / 100;
                    percent = horPercent;
                }
            } else {                                                         // reset image sizes for 1:1 Images
                document.getElementById(ImageID).width = testImage.width;
                document.getElementById(ImageID).height = testImage.height;
            }

            // calculate new postion
            newMarginLeft = document.getElementById(ImageID).width/2;
            newMarginTop = document.getElementById(ImageID).height/2;

            if (!browser.isOpera) {                                         // positioning for opera
                document.getElementById(ImageID).style.left="50%";
                document.getElementById(ImageID).style.top="50%";
                document.getElementById(ImageID).style.marginLeft="-"+newMarginLeft+"px";
                document.getElementById(ImageID).style.marginTop="-"+newMarginTop +"px";
            } else {
               document.getElementById(ImageID).style.margin='auto';
            }

            newTextnode= document.createTextNode("("+Math.floor(percent)+"%)");
            try {document.getElementById("Percentage").replaceChild(newTextnode, document.getElementById("Percentage").firstChild); } catch(ex) {}
            try {document.getElementById("Loading").style.display="none";} catch(ex) {}
            try {document.getElementById("Percentage").style.display="inline";} catch(ex) {}
            clearInterval(preloadInterval);
        }
    }
}

function changePreviewImage(ImageID, ImageURL, ImageTitle) {
// changes the PreviewImage in the PreviewArea and starts the image preloader
    changeImage(ImageID, ImageURL, ImageTitle);
    document.getElementById(ImageID).style.display="none";      // hide preview image while loading
    try{document.getElementById("Loading").style.display="inline"; } catch(ex) {} // display Loading info
    try{document.getElementById("Percentage").style.display="none";} catch(ex) {} // hide image size while loading
    try{document.getElementById("PreviewArea").style.textAlign="left";} catch(ex) {}
    if(preloadInterval!="") clearInterval(preloadInterval);
    preloadInterval = setInterval("preloadImage('"+ImageID+"')",100);
}

function resizeImage(ImageID, ImgWidth, ImgHeight) {
// ? unused	function
	var IsComplete=(document.getElementById(ImageID).width!=0 || browser.isIE) && document.getElementById(ImageID).complete;

    if(IsComplete) {
        testImage=document.createElement("img");
        testImage.src=document.getElementById(ImageID).src;

        percent = 100;
        if(testImage.width != 0) {                                                     // do nothing while the width is unset
            document.getElementById(ImageID).style.display="block";
            if (testImage.width< ImgWidth) {
                document.getElementById(ImageID).width = testImage.width;
                document.getElementById(ImageID).height = testImage.height;
            }
        }

        document.getElementById(ImageID).style.display="inline";
        newMarginLeft = document.getElementById(ImageID).width/2;
        newMarginTop = document.getElementById(ImageID).height/2;

        if (((!browser.isOpera)&&(testImage.height < ImgHeight))||((testImage.height < testImage.width)&&(!browser.isOpera))) {
            document.getElementById(ImageID).style.top="0";
            if(testImage.height < testImage.width) {
                document.getElementById(ImageID).style.marginTop = (ImgHeight/2) - (((ImgWidth*testImage.height)/testImage.width)/2) +"px";
            } else {
                document.getElementById(ImageID).style.marginTop = ImgHeight/2 - testImage.height/2 +"px";
            }
        } else {
          document.getElementById(ImageID).style.margin='auto';
        }
    } else {
        setTimeout("resizeImage('"+ImageID+"', 150, 150)",100);
    }
}

/* ---------------------------------------------------------------------------------------------------
   - SlideShow (BackOffice) --------------------------------------------------------------------------
   --------------------------------------------------------------------------------------------------- */

var PlayerArray = new Array();
var PlayerCount=0;
var PlayerListLength =0;
var PlayerStatus = -1; // -1 = not started (initial), 0 = rotate, 1= stop
var PlayerInterval ="";
var Caller=0;


// The player can be started by calling:
// function SlideShowPlay(string)
// function SlideShowSkip(integer, string);

function initPlayer(productImagePath) {
// This function scans all hidden checkboxes on the current page with the
// name attribute "ImageSlideShow" and if the checkbox is "checked" the image
// will be added to an image array
    imageArray=new Array();
    for(i =0; i<document.getElementsByName("ImageSlideShow").length; i++) {
        if(document.getElementsByName("ImageSlideShow")[i].checked==true) {
            // - The id attribute (of these checkboxes) contains an img identifier eg. "file_lii_1" "external_ssi_2".
            // - Depending on the first part oft the id the slideshow can make a difference between files on "our" server and an external URL
            //   file - files on "our" server
            //   external - an external URL
            var idcheck = document.getElementsByName("ImageSlideShow")[i].getAttribute("id");
            var splitter = idcheck.split('_');
            if(splitter[0]!="external") {
                fileName=productImagePath+document.getElementsByName("ImageSlideShow")[i].value;
            } else {
                fileName=document.getElementsByName("ImageSlideShow")[i].value;
            }
            // Create new Image
            bild = new Image();
            bild.src=fileName;
            // Add Image to the Image Array
            imageArray.push(bild);
        }
    }
    // return the Image Array
    return imageArray;
}

function rotatePicture() {
    if (PlayerStatus==0 && PlayerListLength>0) {
        PlayerCount++;
        if (PlayerCount==PlayerListLength) PlayerCount=0;
        if (PlayerCount<0) PlayerCount=PlayerListLength-1;

        changePreviewImage("PreviewImage", PlayerArray[PlayerCount].src, PlayerArray[PlayerCount].src)

        if(PlayerInterval!="") clearInterval(PlayerInterval);
        PlayerInterval = setInterval("rotatePicture()", 2000);
    }
}

function SlideShowInit(productImagePath){
    PlayerArray = initPlayer(productImagePath);
    PlayerListLength = PlayerArray.length;
    PlayerCount=-1;
    var Caller = 0;
    if (PlayerStatus==0) rotatePicture();
}

function SlideShowPlay(productImagePath){
    if (PlayerStatus==-1) {
        PlayerStatus=0;
        SlideShowInit(productImagePath);
    } else {
        PlayerStatus=0;
        rotatePicture();
    }
}

function SlideShowSkip(direction, productImagePath) {
    if (PlayerStatus==-1) {
        SlideShowInit(productImagePath);
    }
    PlayerStatus=1;                                                // stop Slide Show Player
    if(preloadInterval!="") clearInterval(preloadInterval);        // stop preload interval
    if(PlayerArray.length>0) {
        PlayerCount=PlayerCount+direction;
        if (PlayerCount==PlayerListLength) PlayerCount=0;
        if (PlayerCount<0) PlayerCount=PlayerListLength-1;

        changePreviewImage("PreviewImage", PlayerArray[PlayerCount].src, PlayerArray[PlayerCount].src)
    }
}


function SlideShowPause(){
    PlayerStatus=1;
}

function SlideShowStop(){
    PlayerStatus=-1;                                               // stop Slide Show Player
    if(preloadInterval!="") clearInterval(preloadInterval);        // stop preload interval
    document.getElementById("PreviewImage").style.display="none";  // hide preview image if slideshow is running
    document.getElementById("Loading").style.display="none";       // hide Loading info
    document.getElementById("Percentage").style.display="none";    // hide image size while loading
}