/*
 * the instance of Lightbox class will be stored here, so it's available in global context
 */
var globalLightbox;


/**
 * Image and text in web layout are changed.
 * 
 * @param string galId: UID of gallery record, where the data should be replaced
 * @param string picId: UID of picture record, which replaces the old one
 */
function z7smoothgallerychange(galId, picId) {
	
	// change image
	document.getElementById('z7smooth-image-'+galId).alt = z7_smoothgallery_pi1_images[picId].alt;
	document.getElementById('z7smooth-image-'+galId).src = 'clear.gif';
	document.getElementById('z7smooth-image-'+galId).src = z7_smoothgallery_pi1_images[picId].middleImg;
	document.getElementById('z7smooth-image-'+galId).style.width = z7_smoothgallery_pi1_images[picId].middleWidth+'px';
	document.getElementById('z7smooth-image-'+galId).style.height = z7_smoothgallery_pi1_images[picId].middleHeight+'px';
	
	// change text
	document.getElementById('z7smooth-number-'+galId).innerHTML = z7_smoothgallery_pi1_images[picId].number;
	document.getElementById('z7smooth-text-'+galId).innerHTML = z7_smoothgallery_pi1_images[picId].text;
	
	// remember the current choosen number
	z7_smoothgallery_currImg[galId] = z7_smoothgallery_pi1_id2counter[galId][picId];

	// hide or show the previous/next buttons
	z7smoothgalleryDisplayPrevNext(galId);
}


/**
 * Displays an other picture record, depending on the current and the size of the step.
 *
 * @param string galId: UID of gallery record, where the picture should be changed
 * @param integer steps: Should be -1 for previous image and +1 for next image. Other values cause bigger steps. 
 */
function z7smoothgallerystepper(galId, steps) {

	// find image to display
	counter = z7_smoothgallery_currImg[galId] + steps;
	picId = z7_smoothgallery_counter2id[galId][counter];
	
	if(picId != undefined) {
		z7smoothgallerychange(galId, picId);
	}
}


/**
 * Opens the lightbox with the needed data from current gallery. The first displayed image is the same as the current image in web layout.
 * 
 * @param string galId: UID of gallery record, for which the lightbox is opened. 
 */
function z7smoothgalleryLightbox(galId) {
	counter = z7_smoothgallery_currImg[galId]-1;
	imageArray = z7_smoothgallery_pi1_imageArray[galId];
	
	// call lightbox with needed information
	globalLightbox.start([counter, imageArray]);
}


/*
 * we save the status, we want for the elements
 */
var z7smoothgalleryControllsToDo = {};
var z7smoothgalleryControllsBlocked = {};


/**
 * Manages to hide and show the controll elements. We do not directly hide/show the elements, because that
 * would cause some troubles with mousout/mouseover events. So we save the status in a global var, wait some
 * millisecons and then, we decide, what to do in an other function.
 *
 * @param string galId: UID of gallery to hide or show
 * @param string action: The action to perform. Must be "hide" or "show". 
 */
function z7smoothgalleryControlls(galId, action, timeToWait) {
	
	// remember, what user want's
	z7smoothgalleryControllsToDo[galId] = action;
	
	if(timeToWait == null) {
		timeToWait = 10;
	}
	
	// wait ... and do
	window.setTimeout('z7smoothgalleryControllsInternal("'+galId+'")', timeToWait);
}

/**
 * This function really hides or shows the controll elements, when the user changes the status. 
 *
 * @param string galId: UID of gallery to hide or show
 */
function z7smoothgalleryControllsInternal(galId, action) {

	var options = { duration: 0.5 };

	// do action if it isn't blocked by an other process
	if(!z7smoothgalleryControllsBlocked[galId]) {
	
		z7smoothgalleryControllsBlocked[galId] = true;
	
		if(z7smoothgalleryControllsToDo[galId] == 'show') {
			Effect.Appear('z7smooth-image-controll-'+galId, options);
			if($('z7smooth-thumbs-trigger-'+galId)) { Effect.Appear('z7smooth-thumbs-trigger-'+galId, options); }
		}
		else if(z7smoothgalleryControllsToDo[galId] == 'hide') {
			Effect.Fade('z7smooth-image-controll-'+galId, options);
			if($('z7smooth-thumbs-trigger-'+galId)) { Effect.Fade('z7smooth-thumbs-trigger-'+galId, options); }
		}
		
		// release blockade after duration time
		window.setTimeout('z7smoothgalleryControllsBlocked["'+galId+'"]=false;', options.duration*1000);
	}
	// try again after duration time
	else {
		window.setTimeout('z7smoothgalleryControllsInternal("'+galId+'")', options.duration*1000);
	}
}


/**
 * Hide and show the thumbs.
 */
function z7smoothgalleryThumbs(galId, action) {
	
	var options = {};
	
	// activate more effects
	moreEffects();

	if(action == 'show') {
		Effect.BlindRight('z7smooth-thumbs-'+galId, options);
		$('z7smooth-thumbs-off-'+galId).style.display = 'block';
		$('z7smooth-thumbs-on-'+galId).style.display = 'none';
	}
	else if(action == 'hide') {
		Effect.BlindLeft('z7smooth-thumbs-'+galId, options);
		$('z7smooth-thumbs-off-'+galId).style.display = 'none';
		$('z7smooth-thumbs-on-'+galId).style.display = 'block';
	}
}


/**
 * Activate more script.aculo.us effects. We need to do it in a function, which is called when it's needed,
 * because the Effect class is not instantiated, when we load the document.
 */
function moreEffects() {

	// see http://wiki.github.com/madrobby/scriptaculous/effect-blinddown
	Effect.BlindRight = function(element) {
	  element = $(element);
	  var elementDimensions = element.getDimensions();
	  return new Effect.Scale(element, 100, Object.extend({
	    scaleContent: false,
	    scaleY: false,
	    scaleFrom: 0,
	    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
	    restoreAfterFinish: true,
	    afterSetup: function(effect) {
	      effect.element.makeClipping().setStyle({
	        width: '0px',
	        height: effect.dims[0] + 'px'
	      }).show();
	    },
	    afterFinishInternal: function(effect) {
	      effect.element.undoClipping();
	    }
	  }, arguments[1] || { }));
	};
	
	// see http://wiki.github.com/madrobby/scriptaculous/effect-blindup
	Effect.BlindLeft = function(element) {
	  element = $(element);
	  element.makeClipping();
	  return new Effect.Scale(element, 0,
	    Object.extend({ scaleContent: false,
	      scaleY: false,
	      scaleMode: 'box',
	      scaleContent: false,
	      restoreAfterFinish: true,
	      afterSetup: function(effect) {
	        effect.element.makeClipping().setStyle({
	          height: effect.dims[0] + 'px'
	        }).show();
	      },
	      afterFinishInternal: function(effect) {
	        effect.element.hide().undoClipping();
	      }
	    }, arguments[1] || { })
	  );
	};
}


/*
 * gloval vars to manage the thumb slider
 */
var z7smoothgalleryThumbsSpeedMax = 12; // maximum speed
var z7smoothgalleryThumbsSpeed; // current speed, calculated by position
var z7smoothgalleryThumbsMaxPos = 0; // the maximum position
var z7smoothgalleryThumbsPos = 0; // the current position
var z7smoothgalleryThumbsOnOff = false; // indicates if slider is switched On/Off
var z7smoothgalleryThumbsRunning = false; // indicates if setTimeout is waiting to call function again


/**
 * Get the absolute mouse position, the absolute position of slider container,
 * the width of the slider container and calculate the speed to move slider.
 * Start movement, if needed. (Not already running and slider element not to small)
 *
 * @param event e: Mouse move element.
 * @param string galId: gallery UID of slider to move
 */
function z7smoothgallerySpeed4Pos(e, galId) {
	var posx = 0;

	if (!e)
		e = window.event;
	if (e.pageX || e.pageY){
		posx = e.pageX;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	}
	
	posx -= ($('page').offsetLeft + 43);
	var halfWidth = $('z7smooth-slider-'+galId).offsetWidth/2;
	var relPos = posx - halfWidth;
	var speed = relPos/halfWidth; 
	
	z7smoothgalleryThumbsSpeed = z7smoothgalleryThumbsSpeedMax * speed * -1;

	if(z7smoothgalleryThumbsSpeed != 0) {
	
		z7smoothgalleryThumbsMaxPos = $('z7smooth-slider-'+galId).getElementsByTagName('table')[0].offsetWidth * -1 + $('z7smooth-slider-'+galId).offsetWidth;
		z7smoothgalleryThumbsPos = parseInt($('z7smooth-slider-'+galId).getElementsByTagName('table')[0].style.left + 0);
		
		if(   z7smoothgalleryThumbsOnOff == true // slider is switched on
		   && z7smoothgalleryThumbsRunning == false // slider is not moving
		   && $('z7smooth-slider-'+galId).getElementsByTagName('table')[0].offsetWidth > $('z7smooth-slider-'+galId).offsetWidth // no problems with positions
		  ) {
			z7smoothgalleryThumbsRunning = true;
			z7smoothgalleryThumbsMoveAction(galId);
		}
	}
}


/**
 * Set on/off flag true, so the other functions know, they can do their action.
 */
function z7smoothgalleryThumbsMoveStart() {
	z7smoothgalleryThumbsOnOff = true;
}


/**
 * Set on/off flag false, so the other functions know, there's nothing to do.
 */
function z7smoothgalleryThumbsMoveStop() {
	z7smoothgalleryThumbsOnOff = false;
}


/**
 * Do movement and call function again and again and again, as long as on/off flag is set true.
 */
function z7smoothgalleryThumbsMoveAction(galId, doit) {

	z7smoothgalleryThumbsPos = (z7smoothgalleryThumbsPos + z7smoothgalleryThumbsSpeed);
	
	// check left end
	if(z7smoothgalleryThumbsSpeed > 0 && z7smoothgalleryThumbsPos >= 0) {
		z7smoothgalleryThumbsPos = 0;
		z7smoothgalleryThumbsSpeed = 0;
	} 
		
	// check right end
	if(z7smoothgalleryThumbsSpeed < 0 && z7smoothgalleryThumbsPos <= z7smoothgalleryThumbsMaxPos) {
		z7smoothgalleryThumbsPos = z7smoothgalleryThumbsMaxPos;
		z7smoothgalleryThumbsSpeed = 0;
	} 
		
	// new position
	$('z7smooth-slider-'+galId).getElementsByTagName('table')[0].style.left = Math.round(z7smoothgalleryThumbsPos)+'px';

	// do it again?
	if(z7smoothgalleryThumbsOnOff == true && z7smoothgalleryThumbsSpeed != 0) {
		window.setTimeout("z7smoothgalleryThumbsMoveAction('"+galId+"')", 40);
	}
	else {
		z7smoothgalleryThumbsRunning = false;
	}
}


function z7smoothgalleryShowBlock(galId, blockNr) {
	var newPos = (blockNr-1) * 760;
	new Effect.Move('biggallery'+galId+'-block', { x: (newPos*-1), y: 0, mode: 'absolute' });

	$('biggallery'+galId+'-range').innerHTML = z7_smoothgallery_pi2_ranges[galId][blockNr];

	// bring active css class to another element
	$('biggallery'+galId+'-pagelink'+z7_smoothgallery_pi2_currBlock[galId]).className = '';
	$('biggallery'+galId+'-pagelink'+blockNr).className = 'active';
	
	// set focus to an other element
	$('biggallery'+galId+'-image').focus();

	// remember current block
	z7_smoothgallery_pi2_currBlock[galId] = blockNr;
}


function z7smoothgalleryBlockStepper(galId, steps) {
	var currentBlock = parseInt(z7_smoothgallery_pi2_currBlock[galId]);
	var newStep = currentBlock+parseInt(steps);
	if(newStep < 1) { newStep = 1; }
	if(newStep > z7_smoothgallery_pi2_maxBlock[galId]) { newStep = z7_smoothgallery_pi2_maxBlock[galId]; };
	
	z7smoothgalleryShowBlock(galId, newStep);
}


/**
 * Image and text in web layout are changed.
 * 
 * @param string galId: UID of gallery record, where the data should be replaced
 * @param string picId: UID of picture record, which replaces the old one
 */
function z7smoothgalleryShowImageBig(galId, picId) {

	// change image
	$('biggallery'+galId+'-image').alt = z7_smoothgallery_pi2_images[picId].alt;
	$('biggallery'+galId+'-image').src = 'clear.gif';
	$('biggallery'+galId+'-image').src = z7_smoothgallery_pi2_images[picId].middleImg;
	$('biggallery'+galId+'-image').style.width = z7_smoothgallery_pi2_images[picId].middleWidth+'px';
	$('biggallery'+galId+'-image').style.height = z7_smoothgallery_pi2_images[picId].middleHeight+'px';
	
	// remember the current choosen number
	z7_smoothgallery_currImg[galId] = z7_smoothgallery_pi2_id2counter[galId][picId];
	
	// delete active css class from other links
	var links = $('biggallery'+galId+'-block').getElementsByTagName('a');
	for(i=0, n=links.length; i<n; i++) {
		links[i].className = '';
	}
	
	// mark current link with active css
	$('biggallery'+galId+'-piclink'+picId).className = 'active';
	
	// show correct block of currently selected image
	if(z7_smoothgallery_pi2_id2block[picId] != z7_smoothgallery_pi2_currBlock[galId]) {
		z7smoothgalleryShowBlock(galId, z7_smoothgallery_pi2_id2block[picId]);
	}
	
	// hide or show the previous/next buttons
	z7smoothgalleryDisplayPrevNext(galId);
}


/**
 * Displays an other picture record, depending on the current and the size of the step.
 *
 * @param string galId: UID of gallery record, where the picture should be changed
 * @param integer steps: Should be -1 for previous image and +1 for next image. Other values cause bigger steps. 
 */
function z7smoothgalleryBigStepper(galId, steps) {

	// find image to display
	var counter = z7_smoothgallery_currImg[galId] + steps;
	var picId = z7_smoothgallery_counter2id[galId][counter];
	
	if(picId != undefined) {
		z7smoothgalleryShowImageBig(galId, picId);
	}
}


/**
 * Opens the lightbox with the needed data from current gallery. The first displayed image is the same as the current image in web layout.
 * 
 * @param string galId: UID of gallery record, for which the lightbox is opened. 
 */
function z7smoothgalleryBigLightbox(galId) {
	counter = z7_smoothgallery_currImg[galId]-1;
	imageArray = z7_smoothgallery_pi2_imageArray[galId];
	
	// call lightbox with needed information
	globalLightbox.start([counter, imageArray]);
}


/**
 * Hide or show the previous and next buttons, when the first or last image is shown.
 *
 * @param string galId: UID of gallery record, where the picture should be changed
 */ 
function z7smoothgalleryDisplayPrevNext(galId) {

	// default status
	var displayPrev = '';
	var displayNext = '';
	
	// don't show previous button for first image
	if(z7_smoothgallery_currImg[galId] == 1) {
		displayPrev = 'none';
	}
	// don't show next button when there is no next image
	else {
		var counter = z7_smoothgallery_currImg[galId] + 1;
		var picId = z7_smoothgallery_counter2id[galId][counter];
	
		if(picId == undefined) {
			displayNext = 'none';
		}
	}
	
	// change the display status
	if($('gallery-image-controll-previous-'+galId))	$('gallery-image-controll-previous-'+galId).style.display = displayPrev;
	if($('gallery-image-controll-next-'+galId)) $('gallery-image-controll-next-'+galId).style.display = displayNext;
}
