﻿// Image document logic
/* Dynamisk antall gallerier - bruk tabell */

var isAnimating = new Array();

var nDocIndex = new Array();
var nDocWidth = new Array();
var nAppCount = new Array();
var nGalleryCount = 0;

jQuery(document).ready(function(){
    setupImageElements();
});

function setupImageElements()
{
    nGalleryCount = jQuery(".document_list>div").length;
    
    for(x = 0;x<=nGalleryCount;x++){
        isAnimating[x] = false;
        nDocIndex[x] = 0;
        nAppCount[x] = jQuery("#image_container_" + x + ">div").length;
        jQuery('#prev_button_' + x).hide();
        
        if(nAppCount[x] > 4){
            var elmFirst = jQuery("#image_container_" + x + ">div")[0];
            //nDocWidth[x] = jQuery(elmFirst).width() + 25;
            nDocWidth[x] = jQuery(elmFirst).width();
            //displayNaviButtons(x);
        }
    }
}

function navigate(index){
	var distance = index - nDocIndex;

	nDocIndex = index;
	var newPos = parseInt(-nDocIndex * nDocWidth);
	var cont = jQuery("#image_container");
	cont.animate({ 
		left: newPos + "px"
	  }, { duration: 500, easing: "swing", callback : endAnimate() } );
}

function endAnimate(x)
{
    isAnimating[x] = false;
}

function nextImage(x)
{
	//Bugfix her... - Onload funket ikke i IE.
    nAppCount[x] = jQuery("#image_container_" + x + ">div").length;

    if(nAppCount[x] > 1)
    {
		elmFirst = jQuery("#image_container_" + x + " div")[0];
        //nDocWidth[x] = jQuery(elmFirst).width() + 25;
        nDocWidth[x] = jQuery(elmFirst).width();
    }

    if(!isAnimating[x])
    {
    
        if((nDocIndex[x]) < nAppCount[x])
        {
	        isAnimating[x] = true;
            nDocIndex[x]++;
            var newPos = parseInt(-nDocIndex[x] * nDocWidth[x]);
            newPos--;       //Bugfix border
            
            var cont = jQuery("#image_container_" + x);
            cont.animate({ 
                left: newPos + "px"
              }, { duration: 500, easing: "swing", callback : endAnimate(x) } );
			displayNaviButtons(x);
        }
    }
}

function prevImage(x)
{
    if(!isAnimating[x])
    {
        if(nDocIndex[x] > 0)
        {
	        isAnimating[x] = true;
            nDocIndex[x]--;
            var newPos = parseInt(-nDocIndex[x] * nDocWidth[x]);
            newPos++;       //Bugfix border
            var cont = jQuery("#image_container_" + x);
            cont.animate({ 
                left: newPos + "px"
              }, { duration: 500, easing: "swing", callback : endAnimate(x) } );
			displayNaviButtons(x);
        }
    }
}

function displayNaviButtons(x)
{

    if(nDocIndex[x] > 0)
    {
        jQuery('#prev_button_' + x).show();
        jQuery('#prev_button_deactive_' + x).hide();
    }
    else
    {
        jQuery('#prev_button_deactive_' + x).show();
        jQuery('#prev_button_' + x).hide();
    }

    if(nDocIndex[x] + 4 >= (nAppCount[x]))
    {
        jQuery('#forward_button_off_' + x).show();
        jQuery('#forward_button_' + x).hide();
    }
    else
    {
        jQuery('#forward_button_off_' + x).hide();
        jQuery('#forward_button_' + x).show();
    }
}