﻿var jGallery = {};

jGallery.Gallery = function(id, imageCount, imageWidth, imageHeight, speed, imageArray, linkArray, descArray, margin, target) {
	if(!target)
{target = "_SELF";}
    this.start = function() {
        jGal_moving = true;
        jGal_moveImages();
    }

    this.stop = function() {
        jGal_moving = false;
    }

    var jGal_ImageDisplay = imageCount;
    var jGal_ImageCount = imageArray.length;
    var jGal_moveSpeed = 1;
    var jGal_Frameduration = speed;
    var jGal_wrapperId = id;

    var jGal_imageWidth = imageWidth + (margin || 0);
    var jGal_imageHeight = imageHeight;

    var jGal_image = imageArray; //store the ID of the image
    var jGal_desc = descArray;
    var jGal_link = linkArray;
    var jGal_Newleft = new Array(); //store the ending position of image sliding
    var jGal_OldLeft = new Array(); //store the starting position of an image slide
    var jGal_moving = false; //TF if the images are in motion
    var jGal_LeftMost = 0;
    var jGal_RightMost = 0;
    var jGal_LeftMostID = 0;
    var jGal_RightMostID = 0;


    //document.getElementById(jGal_wrapperId).style.width = ((jGal_ImageDisplay * jGal_imageWidth) - 4) + 'px';
    //document.getElementById(jGal_wrapperId).style.height = (jGal_imageHeight + 45) + 'px';
    for (var i = 0; i < jGal_ImageCount; i++) {
        jGal_thisleft = ((i - 1) * jGal_imageWidth);
        //for lightbox integration
        //jGal_newDiv = '<div id="' + jGal_wrapperId + '_pic'+i+'" style="position:absolute; top: 1px; left: '+jGal_thisleft+'px;"><a href="'+jGal_image[i]+'" rel="lightbox[gal]"><img src="'+jGal_image[i]+'" class="gallery" style="height: '+jGal_imageHeight+ 'px; border: 1px #eee solid; padding:2px; width: '+ (jGal_imageWidth-10) + 'px;" /></a></div>';

        jGal_newDiv = '<div id="' + jGal_wrapperId + '_pic' + i + '" style="position:absolute; top: 1px; left: ' +
        jGal_thisleft + 'px;"><a href="' + jGal_link[i] + '" target="'+ target +'"><img src="' + jGal_image[i] +
        '" class="gallery" style="height: ' + jGal_imageHeight + 'px; border: 1px #eee solid; padding:2px; width: ' +
        (jGal_imageWidth - margin) + 'px;" alt="' + jGal_desc[i] + '" /></a></div>';
        jGal_image[i] = jGal_wrapperId + "_pic" + i; //change the var now to the element ID that contains it
        document.getElementById(jGal_wrapperId).innerHTML += jGal_newDiv;

    }




    if (((jGal_ImageDisplay + 2) > jGal_ImageCount) || (jGal_ImageCount < 2)) {

        //document.getElementById('jGal_prevbut').style.display = 'none';
        //document.getElementById('jGal_nextbut').style.display = 'none';
    }
    //jGal_moveImages(jGal_wrapperId, imageArray, -1);
    //jGal_next();
    //setInterval("jGal_next()", 1150);
    //slide images to show the next image in the row
    /*function jGal_prev() {
    //if the images are in a slide motion, hard move them to their ending position, then wait a split second and move again
    if (jGal_moving) {
    jGal_moving = false;
    jGal_hardMove();
    setTimeout("jGal_prev()", 25);
    return;
    } //if
    //move the images if they are at a stand still
    jGal_moveImages(jGal_imageWidth);
    } //prev function

//slide the images to show the next image in a row
    function jGal_next() {
    //if the images are moving already, force them to their landing spot, then move again
    if (jGal_moving) {
    jGal_moving = false;
    jGal_hardMove();
    setTimeout("jGal_next()", 25);
    return;
    } //if
    //alert('next');
    //move images to the next spot
    jGal_moveImages(-jGal_imageWidth);
    } //next function*/

    //this function does the work of finding the images current / next spot
    //it also detect when an image needs to be moved from the left to right or vice versa to keep
    //the liquid flow of images from either side
    function jGal_moveImages() {
        jGal_LeftMost = 1;
        jGal_RightMost = jGal_imageWidth + 1;
        //move next/prev based on the inc value
        jGal_slider = -jGal_moveSpeed;

        //loop through the images and calculate their current / next position. If its the last
        //image on the left or right being pushing into the screen, grab the one from the other side
        //to keep the flow of images continuous
        for (var i = 0; i < jGal_ImageCount; i++) {
            //determine current position
            jGal_left = document.getElementById(jGal_wrapperId + "_pic" + i).style.left;
            jGal_left = jGal_left.replace(/px/, '');
            jGal_left = parseFloat(jGal_left);
            jGal_OldLeft[i] = jGal_left;
            //build new position + move if needed to keep flow of things
            jGal_newLeft = jGal_left + parseFloat(jGal_slider);
            //save the lowest left value px and ID
            if (jGal_newLeft <= jGal_LeftMost) {
                jGal_LeftMost = jGal_newLeft;
                jGal_LeftMostID = i;
            } //if left most
            //save the highest left value px and ID
            if (jGal_newLeft >= jGal_RightMost) {
                jGal_RightMost = jGal_newLeft;
                jGal_RightMostID = i;
            } //if left most

            jGal_Newleft[i] = jGal_newLeft;

        } //for loop

        //now we need to swap these images out to leep a liquid flow...
        //- we've got no images outside the left of the box, grab the far right image then...
        if (jGal_LeftMost == 0) {
            jGal_Newleft[jGal_RightMostID] = "-" + jGal_imageWidth;
        }
        if (jGal_RightMost <= ((jGal_ImageDisplay - 1) * jGal_imageWidth)) {
            //alert(jGal_LeftMostID + "---" + jGal_Newleft[jGal_LeftMostID] + " " + jGal_OldLeft[jGal_LeftMostID] + " " + document.getElementById(jGal_wrapperId + "_pic" + jGal_LeftMostID).style.left);
            jGal_Newleft[jGal_LeftMostID] = ((jGal_ImageDisplay) * jGal_imageWidth);
            jGal_OldLeft[jGal_LeftMostID] = jGal_Newleft[jGal_LeftMostID];
            //for (var z = 0; z < jGal_ImageCount; z++)
            //document.getElementById(jGal_wrapperId + "_pic" + jGal_LeftMostID).style.left = jGal_Newleft[jGal_LeftMostID] + 'px';
            //alert(jGal_LeftMostID + "---" + jGal_Newleft[jGal_LeftMostID] + " " + jGal_OldLeft[jGal_LeftMostID] + " " + document.getElementById(jGal_wrapperId + "_pic" + jGal_LeftMostID).style.left);
        }
        //everything has been determined, set moving to true
        jGal_moving = true;

        //slide the image now that we have their positions calculated	

        jGal_slideImages(jGal_slider, 0);
    }

    //slide images after finding their positions
    function jGal_slideImages(jGal_slider, jGal_int) {
        //in someone clicks next during a transition, we exit this process
        if (!jGal_moving)
            return false;
        //CLEAN THIS BIT UP, MAKE THIS SCRIPT FLEXIBLE	
        jGal_stopat = jGal_imageWidth;
        jGal_stopat2 = -jGal_imageWidth;
        //loop through the images, slide them based on the int + old value
        for (var i = 0; i < jGal_ImageCount; i++) {
            jGal_moveVal = jGal_OldLeft[i] + jGal_int;
            document.getElementById(jGal_wrapperId + "_pic" + i).style.left = (jGal_moveVal) + 'px';
        }

        var test = undefined;
        eval("test = window." + jGal_wrapperId + "_jGal_slideImages;");
        if (!test)
            eval("window." + jGal_wrapperId + "_jGal_slideImages = jGal_slideImages;");

        //if the images have reached their final destination, quit sliding

        if ((jGal_int <= jGal_stopat - 2) && (jGal_int >= (jGal_stopat2) + 2) && (jGal_moving == true))
            setTimeout("window." + jGal_wrapperId + "_jGal_slideImages(" + jGal_slider + "," + (jGal_int + jGal_slider) + ")", jGal_Frameduration);
        else {
            jGal_moving = false;
            jGal_moveImages();

        }
        //else {
        //when we are done sliding, hard move these images to their final spot to ensure they
        //are exactly where we need them to be
        //jGal_moving = false;
        //} //if else
        //
    } //slide images function

    function x(jGal_slider, jGal_int) {
        jGal_slideImages(jGal_slider, jGal_int);
    }


    //this forces the images to their landing spot should we need to move then
    //mid transition
    function jGal_hardMove() {
        for (var z = 0; z < jGal_ImageCount; z++)
            document.getElementById(jGal_wrapperId + "_pic" + z).style.left = jGal_Newleft[z] + 'px';
    } // hard move


}