﻿// GLobal Var
var Pagenumber = 1;
var maxPage = 1;


/********************************************************************************************/
/* Image Resize Code */
/********************************************************************************************/


function scaleImage(imgid, maxW, maxH, thumb) {

    var newW = 0;
    var newH = 0;
    var image = document.getElementById(imgid);
    var newImg = new Image(); // clone image so we can access its 'native' width/height
    newImg.src = image.src;

    var iWidth = newImg.width;
    var iHeight = newImg.height;

    if (iWidth > 0 && iHeight > 0 && maxH > 0 && maxW > 0) { //only work if the numbers are positive
        if (iWidth > maxW || iHeight > maxH) { // Is larger than max?
            if (iWidth > iHeight) { // is landscape?

                if (maxH > iHeight) { maxH = iHeight; }
                if (maxW > iWidth) { maxW = iWidth; }

                if (thumb) {
                    newW = iWidth / iHeight * maxH;
                    newH = maxH;
                } else {
                    newH = iHeight / iWidth * maxW;
                    newW = maxW;
                }

            } else { // is portrait?

                if (maxH > iHeight) { maxH = iHeight; }
                if (maxW > iWidth) { maxW = iWidth; }

                if (thumb) {
                    newH = iHeight / iWidth * maxW;
                    newW = maxW;
                } else {
                    newW = iWidth / iHeight * maxH;
                    newH = maxH;
                }
            }
            image.height = newH; // Set width/height to new values
            image.width = newW;
        } else {
            image.height = iHeight; // Set width/height to 'native' settings
            image.width = iWidth;
        }
    }
}

/********************************************************************************************/
/* AJAX code */
/********************************************************************************************/

function initImages(a) {

    var rowCount = 1;
    var imgId;

    while (rowCount <= a.length - 1) {
        imgId = 'Img' + (rowCount - 1);
        document.getElementById(imgId).src = a[rowCount];
        rowCount++;
    }
    var mainsrc = document.getElementById('ImgMain').src;
    
    if (mainsrc.substring(mainsrc.length - 10, mainsrc.length) == 'spacer.gif'){
    document.getElementById('ImgMain').src = document.getElementById('Img0').src;
    scaleImage('ImgMain', 500, 400, false);
    }
    
    
    var spareThumbs;
    if (a.length < 13) {
        spareThumbs = 13 - a.length; //a.l = 5, spareT = 8, img 5+

        
        var Count = 0;
        
        while ( Count <= (spareThumbs - 1)) {
            
            imgId = 'Img' + (Count + (a.length - 1)) ;
            
            document.getElementById(imgId).src = '/plugins/lpg/gallery/spacer.gif';
            Count++;
        }
    }
}

function getArray(page) {


    if (Pagenumber <= 1) {
        Pagenumber = 1;
        document.getElementById('prevButton').style.display = 'none';
    }   

    var url = "/plugins/lpg/gallery/cmd.aspx";
    url = url + "?page=" + page;
    
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            var cmdResponse = xmlhttp.responseText;
            var a = cmdResponse.split(',');

            initImages(a);
            maxPage = a[0];
            navCheck();
        }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();

    
    
}

function setToLoading() {

    var Count = 0;
    var imgId;

    while (Count <= 11) {
        imgId = 'Img' + Count;
        document.getElementById(imgId).src = '/plugins/lpg/gallery/loading.gif';
        Count++;
    }

}


/********************************************************************************************/
/* Paging code */
/********************************************************************************************/

function Nextpage() {
    setToLoading();
    Pagenumber = Pagenumber + 1;
    getArray(Pagenumber);
}

function Prevpage() {
    setToLoading();
    Pagenumber = Pagenumber - 1;
    getArray(Pagenumber);
}

function navCheck() {
    
    // NEXT PAGE //

    // Hide nextpage if at last page
    if (Pagenumber >= maxPage) { 
        Pagenumber = maxPage;
        document.getElementById('nextButton').style.display = 'none';
    } 
    // show if not
    if (document.getElementById('nextButton').style.display == 'none' && maxPage > Pagenumber) { document.getElementById('nextButton').style.display = '' }
    
    
    //PREVIOUS PAGE //
    
    //Hide back if at page 1
    if (Pagenumber <= 1) {
        pagenumber = 1;
        document.getElementById('prevButton').style.display = 'none';
    }

    //show if not page 1
    if (document.getElementById('prevButton').style.display == 'none' && Pagenumber > 1) { document.getElementById('prevButton').style.display = '' }
}


/********************************************************************************************/
/* Image Transition Code */
/********************************************************************************************/

var inProgress = 0; //Global flag to stop users changing images mid transition.

function setOpacity(_object, _opacity) {
    _opacity = (_opacity == 100) ? 99.999 : _opacity; // Sets opacity to 99.999% to stop a firefox flicker bug
    _object.style.filter = "alpha(opacity:" + _opacity + ")"; // IE/Win
    _object.style.KHTMLOpacity = _opacity / 100; // Safari<1.2, Konqueror
    _object.style.MozOpacity = _opacity / 100; // Older Mozilla and Firefox
    _object.style.opacity = _opacity / 100; // Safari 1.2, newer Firefox and Mozilla, CSS3
}

function fadeIn(_objId, _opacity) {
    var obj = document.getElementById(_objId);
    if (_opacity <= 100) {
        setOpacity(obj, _opacity);
        _opacity += 10;
        window.setTimeout("fadeIn('" + _objId + "'," + _opacity + ")", 50); // waits 50 miliseconds then calls itself until opacity is 100%
    } else {
        inProgress = 0; // Set inProgress back to 0 so the user can change images again.
    }
}

function fadeOut(_objId, _opacity, _img) {
    if (document.getElementById) {
        inProgress = 1; // Set inProgress flag to 1 so the user can't change while in transition.
        var obj = document.getElementById(_objId);
        if (_opacity >= 0) {
            setOpacity(obj, _opacity);
            _opacity = _opacity - 10;
            window.setTimeout("fadeOut('" + _objId + "'," + _opacity + ", '" + _img + "')", 50); // waits 50 miliseconds then calls itself until opacity is 0%
        } else {
            if (_img) {

                var oImage = document.getElementById('ImgMain');

                oImage.src = _img; // Switch old imge for new one while opacity is 0%
                scaleImage('ImgMain', 500, 400, false);
            }
            fadeIn(_objId, _opacity); // if opacity is 0 or less, start fading in.
        }
    }
}

function loadImage(_img) {
    if (inProgress === 0) { // Prevents changing images mid transition
        if (document.getElementById('ImgMain').src != _img.src || _img.src != '/plugins/lpg/gallery/spacer.gif' || _img.src != '/plugins/lpg/gallery/loading.gif') { // Prevents changing an image to itself or spacers/loading images.
            fadeOut('ImgMain', 100, _img.src);
        }
    }
}
