﻿/*************************************************************
* 
* JQuery Skript für einfache Galerie
*
**************************************************************/

//-----------------------------------------------
//Container für Variablen
//-----------------------------------------------

var i_ag_imagecount = 0;
var i_ag_currentimg = 0;
var i_ag_canvaswidth = 0;
var i_ag_canvasheight = 0;
var i_ag_progresswidth = 0;
var i_ag_progresspiece = 0;

var s_ag_caption = "";
var s_ag_captionprefix = "Bild";
var s_ag_next = "nächstes";
var s_ag_prev = "vorheriges";
var s_ag_progress = "";
var s_ag_spinner = "images/a-gallery/ajax-loader.gif";
var s_ag_icon_active = "&Omicron;";
var s_ag_icon_inactive = "&bull;";

var b_ag_isrunning = false;

//-----------------------------------------------
//Beim Laden der Seite initalisieren
//-----------------------------------------------
$(document).ready(function(){
	if(aginitialize()){
		// Debug
		 //window.setInterval("agdebug()",200);
		
		// Activate controls
		agshowcontrols();
		// Render first image		
		agnext(0);
		}
});

//-----------------------------------------------
//Debugging
//-----------------------------------------------
function agdebug(){
	$("#agdebug").html("i_ag_imagecount:"+i_ag_imagecount+"<br />"
					+ "i_ag_currentimg:"+i_ag_currentimg+"<br />"
					+ "i_ag_canvaswidth:"+i_ag_canvaswidth+"<br />"
					+ "i_ag_canvasheight:"+i_ag_canvasheight+"<br />"
					+ "i_ag_progresswidth:"+i_ag_progresswidth+"<br />"
					+ "i_ag_progresspiece:"+i_ag_progresspiece+"<br />"
					+ "b_ag_isrunning:"+b_ag_isrunning+"<br />"
					+ "s_ag_caption:"+s_ag_caption+"<br />"
					
					
					);
}

//------------------------------------------------
//Initialisieren
//------------------------------------------------
function aginitialize() {
	i_ag_imagecount = $("#a-gallery-canvas img.a-gallery-image").length;			
	i_ag_canvaswidth = $("#a-gallery-canvas").width();
	i_ag_canvasheight = $("#a-gallery-canvas").height();	
	i_ag_progresswidth = $("#a-gallery-progress").width();
	i_ag_progresspiece = Math.floor(i_ag_progresswidth / i_ag_imagecount);		
	
	//Mini-Icons rendern
	var ag_i_temp = 0;
	while(ag_i_temp < i_ag_imagecount){
		s_ag_progress = s_ag_progress + "<span name=\""+ag_i_temp+"\" class=\"a-gallery-span a-gallery-progress-inactive\">"+s_ag_icon_inactive+"</span>";
		ag_i_temp ++;
	}
	$("#a-gallery-progress").html(s_ag_progress);
	$(".a-gallery-span").width(i_ag_progresspiece);
	
	
	//Imagebox rendern
	$("#a-gallery-img-container").width(i_ag_canvaswidth);
	$("#a-gallery-img-container").height(i_ag_canvasheight);
	
	//Hintergrund ausblenden
	$("#a-gallery-canvas").css("text-indent",i_ag_canvaswidth+"px");
	$("#a-gallery-img-container").css("text-indent","0");
	
	//Navigationstexte zeigen
	$("#a-gallery-right").attr("title", s_ag_next);
	$("#a-gallery-left").attr("title", s_ag_prev);
	
	return true;
}

//------------------------------------------------
//Steuerung aktivieren
//------------------------------------------------
function agshowcontrols() {
	// Buttons
	$("#a-gallery-right").click(function() {
		agnext(1);
	});
	$("#a-gallery-left").click(function() {
		agprev(-1);
	});	
	//Indexleiste aktivieren
	$(".a-gallery-progress-inactive").click(function() {
		agjump(this);
	});	
	// Tastatur links / rechts 
	$(document).keydown(function(e){
	    if (e.keyCode == 37) { 
	       agprev(-1);
	       return false;
	    }
	});
	$(document).keydown(function(e){
	    if (e.keyCode == 39) { 
	       agnext(1);
	       return false;
	    }
	});	
}

//------------------------------------------------
// Bild rendern
//------------------------------------------------
function agshowimg(number) {
	$("#a-gallery-img-container").empty();	
	ag_s_nextimg=".a-gallery-image:eq("+(number)*1+")";
	$(ag_s_nextimg).clone().appendTo("#a-gallery-img-container");
	$("#a-gallery-title").hide();
	i_ag_currentimg=number*1;
	s_ag_caption=$("#a-gallery-img-container img").attr("title")+" ("+s_ag_captionprefix+" "+(i_ag_currentimg*1+1)*1+"/"+i_ag_imagecount+")";
	$("#a-gallery-title").text(s_ag_caption);
	$("#a-gallery-title").fadeIn(500);	
	
	// Aktiven Index setzen
	$(".a-gallery-span").removeClass("a-gallery-progress-active").html("&bull;");
	$(".a-gallery-span:eq("+(number)*1+")").addClass("a-gallery-progress-active").html(s_ag_icon_active);
}

//------------------------------------------------
//Bild von rechts hereinfahren
//------------------------------------------------
function agmovein(number) {
	if (i_ag_imagecount<=number) return false;
	if (0>number) return false;
	
	// Nach rechts rausfahren
	$("#a-gallery-img-container").animate({
		left: -1000
	}, 200, function() {
		//Nach links einblenden	
		$("#a-gallery-img-container").animate({
			left: 1000
		}, 1, function() {
			//Nach links einblenden	
			agshowimg(number);
			$("#a-gallery-img-container").animate({
				left: 0
			}, 600, function() {
				// Animation complete.				
				b_ag_isrunning = false; // Release lock;
			});	
		});						
	});		
	return true;
}

//------------------------------------------------
//Bild von links hereinfahren
//------------------------------------------------
function agmoveinleft(number) {
	if (i_ag_imagecount<number) return false;
	if (0>number) return false;
	
	// Nach rechts rausfahren
	$("#a-gallery-img-container").animate({
		left: 1000
	}, 200, function() {
		//Nach links einblenden	
		$("#a-gallery-img-container").animate({
			left: -1000
		}, 1, function() {
			//Nach links einblenden	
			agshowimg(number);
			$("#a-gallery-img-container").animate({
				left: 0
			}, 600, function() {
				// Animation complete.				
				b_ag_isrunning = false; // Release lock;
			});	
		});						
	});		
	return true;
}

//------------------------------------------------
//Nächstes Bild
//------------------------------------------------
function agnext(agstep){
	
	if (b_ag_isrunning) return false;
	b_ag_isrunning = true; // Avoid double click;
	if(agmovein(i_ag_currentimg+agstep)){			
		// Animation
	}
	else {
		// No Animation, first / last image 
		b_ag_isrunning = false; // Release lock;
	}
}

//------------------------------------------------
//Vorheriges Bild
//------------------------------------------------
function agprev(agstep){
	if (b_ag_isrunning) return false;
	b_ag_isrunning = true; // Avoid double click;
	if(agmoveinleft(i_ag_currentimg+agstep)){			
		// Animation
	}
	else {
		// No Animation, first / last image 
		b_ag_isrunning = false; // Release lock;
	}
}

//------------------------------------------------
//Schnellsprung aus Indexleiste
//------------------------------------------------
function agjump(agobject){
	
	if (b_ag_isrunning) return false;
	b_ag_isrunning = true; // Avoid double click;
	
	var i_ag_jumpto = $(agobject).attr("name")*1;
	
	if (i_ag_jumpto > (i_ag_currentimg)){
		if(agmovein(i_ag_jumpto)){			
			// Animation
		}
		else {
			// No Animation, first / last image 
			b_ag_isrunning = false; // Release lock;
		}	
	}
	
	if (i_ag_jumpto < (i_ag_currentimg)){
		if(agmoveinleft(i_ag_jumpto)){			
			// Animation
		}
		else {
			// No Animation, first / last image 
			b_ag_isrunning = false; // Release lock;
		}	
	}
	
	//$(agobject).html("&loz;");			
	
}
