// THIS FUNCTION IS COPYRIGHT BY BABYSOFT
// NO CHANGE, MOTIFY, RE-DISTRIBUTE, COPY OR RESELL IS ALLOWED
// BUG REPORT: its@babysoft.ca
// BABYSOFT CONTACT: 1-905-940-5379

// NOTE: THE FOLLOWING ID ***MUST*** CONTAINS IN THE babyImageView.html FILE
//       THE REST OF THE ID CAN BE CUSTOMIZED FOR THE LAYOUT NEEDS
// pageNumber (span) => The page number display on the image viewer
// prevButton (button) => The button used to display the next image
// nextButton (button) => The button used to display the next image
// slideShow  (span)   => The container contains slide show button
// screen     (span)   => The real screen part
// index_pass (hidden) => hidden value used to pass index value over
// fileFlag_pass (hidden) => hidden value used to pass prefix of the image filename
// location_pass (hidden) => hidden value used to pass the location of the images

// GLOBAL SETTING
var SLIDESHOW_SPEED = 2000; // in ms

// GLOBAL VARIABLE
var SLIDESHOW_STATUS;
var NEW_WINDOW;
var MOZILLA_TIMER;
var FILE_PREFIX;
var FILE_LOCATION;

// INTERNAL FUNCTIONS
function babyControlDisable_PREV(){
document.getElementById("prevButton").disabled = true;
}
function babyControlDisable_NEXT(){
document.getElementById("nextButton").disabled = true;
// MAKE SURE IF THE SLIDE SHOW ON, OFF AT THIS PAGE
clearInterval(SLIDESHOW_STATUS);
}



// Stop the slide show.
// this function will stop the slide show
// Return: always return false
function stopSlideShow(){
    clearInterval(SLIDESHOW_STATUS);
    return false;
}


// Start the slide show.
// this function will keep calling loadImage until no image found
// Return: always return false
function startSlideShow(){
    document.getElementById("index_pass").value = 0; // reset back to the first image

    SLIDESHOW_STATUS = setInterval("loadImage(1)", SLIDESHOW_SPEED); // change every n msec
    return false;
}



// check if the image passin image exist or not,
// if not, return false, else return true
// param1: Image full path
// param2: which button is currently checking
//         VALUE can only be 'prev' or 'next'
// return: Global:IMAGE_STATUS is true if the image exist, else false
function checkButton(url,flag){
    var testImg = new Image();

    // reset all button value first
    document.getElementById("nextButton").disabled = false;
    document.getElementById("prevButton").disabled = false;

    // check to see if the filename exist
    if (flag == 'prev'){
        testImg.onerror = babyControlDisable_PREV;
    }else if (flag == 'next'){
        testImg.onerror = babyControlDisable_NEXT;
    }

    testImg.src = url;
    return;

}


// Display the page in on the "SCREEN"
// NOTE: the display id MUST called screen
//       the page number id MUST called pageNumber
// Param1: (INT) the index different (eg. 1, -1, etc);
//         if loading previous page, use -1
//         if loading next page, use 1
// Return: always return false
function loadImage(value){
    var index = document.getElementById("index_pass").value;
    var fileFlag = document.getElementById("fileFlag_pass").value;
    var location = document.getElementById("location_pass").value;
    var newIndex = parseInt(index) + parseInt(value);
    var filename = prevname = nextname = '';

    filename = location + fileFlag + newIndex + '.jpg';
    prevname = location + fileFlag + (newIndex -1) + '.jpg';;
    nextname = location + fileFlag + (newIndex +1) + '.jpg';;

    checkButton(prevname,'prev');
    checkButton(nextname,'next');

    document.getElementById("screen").innerHTML = '<img src='+filename+' border=0>';
    document.getElementById("pageNumber").innerHTML = newIndex;

    // update the value
    document.getElementById("index_pass").value = newIndex;
    // document.getElementById("fileFlag_pass").value;
    // document.getElementById("location_pass").value;

    return false;

}





// ---------------------------------------------- CUSTOMIZABLE PART STARTS HERE --------------------------------------------

// CUSTOMIZE THIS FUNCTION FOR DATA PASSING TO THE IMAGE VIEWER PAGE
// NOTE: SOME DATA HERE IS REQURIED FOR BABY IMAGE VIEWER
function passImageData(){
    if (NEW_WINDOW.document.getElementById("index_pass") != null && NEW_WINDOW.document.getElementById("index_pass") != ''){
	NEW_WINDOW.document.getElementById("index_pass").value = 1;
	NEW_WINDOW.document.getElementById("fileFlag_pass").value = FILE_PREFIX;
	NEW_WINDOW.document.getElementById("location_pass").value = FILE_LOCATION;
	NEW_WINDOW.document.getElementById("pageNumber").innerHTML = 1;

	// Load the first image
	NEW_WINDOW.loadImage(0);

	// STOP THE TIMER FOR MOZILLA - DO NOT REMOVE
	clearInterval(MOZILLA_TIMER);
    }

}


// THE MAIN PART OF IMAGE VIEWER
// CUSTOMIZE THIS FUNCTION TO FIT YOUR JOB NEEDS
// THE FULL IMAGE VIEW LAYOUT MAINTAIN HERE
// Param1: the additional prefix where you want to add to the filename when
//         the image viewer loaded. default starts at 1.jpg, 2.jpg... etc
//         if fileFlag is not empty, it will cat to the front of the filename
//         eg. if fileFlag is babysoft, the image viewer will start by:
//         babysoft1.jpg, babysoft2.jpg and so on.
// Param2: Image Location (eg. http://www.babysoft.ca/icons/)
//         NOTE: MUST HAVE THE LEADING '/'.
// Return: always return false;
function loadImageViewer(fileFlag,location){
    var date=new Date();
    var inndex = '';
    NEW_WINDOW = window.open("./babyImageViewer.html","babysoft_ImageViewer","width=820, height=680, toolbar=no, location=no,directories=no, status=yes, menubar=no,scrollbars=yes, resizable=yes,copyhistory=no");
    var browser = navigator.appName;

    // get ready the value for data transfer
    FILE_PREFIX = fileFlag;
    FILE_LOCATION = location;

    // THIS PROCESS USED TO AVOID SEMAPHORE (MULTPROCESS) PROBLEM
    // WHILE THE PAGE IS ALREADY LOADED
    if (browser == "Microsoft Internet Explorer"){
	while (NEW_WINDOW.document.readyState != 'complete'){}
	passImageData();
    }
    else if (browser == "Mozilla"){
	MOZILLA_TIMER = setInterval("passImageData()", 100);
    }
    else if (browser == "Netscape"){
	MOZILLA_TIMER = setInterval("passImageData()", 100);
    }else{
	MOZILLA_TIMER = setInterval("passImageData()", 100);
    }

    NEW_WINDOW.focus();

    return false;

}






