var selectedImage = 0;
var selectedVideo = 0;
var jsonObject;
var animated = false;
var aktiv;
var mode = "picture";

var faktor;
var width;
var height;
var mtop;
var left;
var galleryId;

var author = "";

var IMAGE_CONTAINER_HEIGHT = 400;

var linkToThumbnail = "../uploads/thumbnail/t";

function renderImages(id) {
	

	galleryId = id;
	
	$('#selecetGalleryCategory').bind("change", function(event) {
		$('#galleryForm').submit();
   });
   $('#selecetGalleryCategory').bind("blur", function(event) {
		$('#galleryForm').submit();
   });

	documentReadyPicture();

	if(mode != "publication") {
		$("#publicationContainer").hover(
  			function () {
				$(this).addClass("publicationContainerSelected");
  			}, 
			function () {
				$(this).removeClass("publicationContainerSelected");
  			}
		);
	}
	
	if(mode != "picture") {
		$("#pictureIconContainer").hover(
  			function () {
				$(this).addClass("pictureIconContainerSelected");
  			}, 
			function () {
				$(this).removeClass("pictureIconContainerSelected");
  			}
		);
	}
	
	if(mode != "video") {
		$("#videoIconContainer").hover(
  			function () {
    			$(this).addClass("videoIconContainerSelected");
  			}, 
			function () {
				$(this).removeClass("videoIconContainerSelected");
  			}
		);
	}
	
	$('#videoIconContainer').click(function() {
		window.location.href='portal3.php?mode=video';
	});
	
	$('#publicationContainer').click(function() {
		window.location.href='portal3.php?mode=publication';
	});
	
	$('#pictureIconContainer').click(function() {
		window.location.href='portal3.php?mode=picture';
	});
	
	if (mode == "video") {
		setTimeout("sendEvent('play','true')",500);
	}
};

function documentReadyPicture() {
	$('#panelOverlay').css('opacity',0.8);

	$('#nextButton').click(function() {
		nextImage(500);
	});
	
	$('#previousButton').click(function() {
		previousImage(500);
	});
	
	if (mode == "video") {
		linkToThumbnail = "";
	}
	var data = "";
	
	if(mode == "video") {
		data = "type=video";
	}
	else if(mode=="publication") {
		data = "type=publication";
	}
	
	if(mode == "picture") {
		if(galleryId) {
			data += "&galleryCatId="+galleryId; 
		}
		else {
			data += "galleryCatId=all"
		}
	}

	var json = $.ajax({
		type: 'GET',
		url: 'getimages.php',
		dataType: 'json',
		success: function() { },
		data: data,
		async: false
	});
	jsonObject = eval('(' + json.responseText + ')');
	var length = 6;
	if(jsonObject.length<6) {
		length = jsonObject.length;
	}
	for (i=0; i<length; i++) {  //Set the backgroundimages
		$('#img'+ (i+2) + ' img').attr("src", linkToThumbnail + jsonObject[i].url);
		$('#img'+ (i+2) + ' img').attr("id", "previewimg_" +i);
		
		if(jsonObject[i].width <= jsonObject[i].height) {
			$('#img'+ (i+2) + ' img').attr("width", "104");
			faktor = jsonObject[i].width/104;
			height = jsonObject[i].height/faktor;
			mtop = (height-104)/2;
			if(mtop > 0) {
				$('#img'+ (i+2) + ' img').css("margin-top","-"+mtop+"px");
			}
			$('#img'+ (i+2) + ' img').css("margin-left","0px");
		}
		else {
			$('#img'+ (i+2) + ' img').attr("height", "94");
			faktor = jsonObject[i].height/94;
			width = $('#img'+ (i+2) + ' img').attr("width");
			width = jsonObject[i].width/faktor;
			left = (width-94)/2;
			if(left > 0) {
				$('#img'+ (i+2) + ' img').css("margin-left","-"+left+"px");
			}			
		}
	}
	$('#currentImage img').attr("src", "../uploads/medium/" + jsonObject[0].url);
	width = jsonObject[0].picturewidth;
	left = (width-605)/2;
	if(left < 0) left = 0;
	$('#currentImage img').css("margin-left","-"+left+"px");	
	height = jsonObject[0].pictureheight;
	mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
	if(mtop < 0) mtop = 0;
	$('#currentImage img').css("margin-top","-"+mtop+"px");
	
	if(jsonObject.length > 1) {
		$('#nextImage img').attr("src", "../uploads/medium/" + jsonObject[1].url);
		width = jsonObject[1].picturewidth;
		left = (width-605)/2;
		if(left < 0) left = 0;
		$('#nextImage img').css("margin-left","-"+left+"px");
		height = jsonObject[1].pictureheight;
		mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
		if(mtop < 0) mtop = 0;
		$('#nextImage img').css("margin-top","-"+mtop+"px");
	}
	
	if(mode == "picture") {
		if(jsonObject[(selectedImage)].author) {
			author = jsonObject[(selectedImage)].author;
			author = author.replace("Foto:","");
			author = author.replace("Fotos:","");
			$('#author').html("Foto: " + jsonObject[0].author);
	  	}
	  	else {
			$('#author').html("");
	  	}
		$('#enlargeButton').attr("href","../uploads/"+ jsonObject[0].url);
	}
	else if(mode == "publication") {
		$('#author').html(jsonObject[0].description);
		$('#enlargeButton').attr("href", jsonObject[0].flashurl);
		$('#currentImage a').attr("href", jsonObject[0].flashurl);
	}
	
	if(mode == "picture") {
		aktiv = window.setInterval("nextImage(1000)", 10000);
	}
}

function nextImage(time) {
	var lastId = jsonObject.length - 1;	
	if(selectedImage == lastId) {
		return 0;
	}
	
	if (animated) {
		return 0;
	}
	else {
		animated = true;
	}
	
	if(mode == "picture") {
		window.clearInterval(aktiv);
	}
	else if (mode == "video") {
		if (selectedVideo >= lastId) {
			return 0;
		}
		selectedVideo++;
		sendEvent('playitem', selectedVideo);
	}
	
	selectedImage++;
	
	$('#currentImage img').stop();
	
	
	if (selectedImage == 1) {
		$('#pointer').animate({left:"138px"},time, function () {
			animated = false;
		});
	}
	else if (selectedImage == 2) {
		$('#pointer').animate({left:"249px"},time, function () {
			animated = false;
		});
	}
	else if((jsonObject.length == 4) && (selectedImage == (lastId))) {
		$('#pointer').animate({left:"360px"},time, function () {
			animated = false;
		});
	}
	else if(selectedImage == (lastId-1)) {
		$('#pointer').animate({left:"360px"},time, function () {
			animated = false;
		});
	}
	else if(selectedImage == (lastId)) {
		$('#pointer').animate({left:"471px"},time, function () {
			animated = false;
		});
	}
	else {
	$('#img1').animate({width:"0px", marginRight:"0px"},time, function() {
		$('#img1').remove();
		$('#img2').attr("id", 'img1');
		$('#img3').attr("id", 'img2');
		$('#img4').attr("id", 'img3');
		$('#img5').attr("id", 'img4');
		$('#img6').attr("id", 'img5');
		$('#img7').attr("id", 'img6');
		$('#scrollContainer').append('<div onclick="divclick(this.id)" id="img7" class="previewImageContainer"><img src=""/></div>');
		
		if ((selectedImage+3) > lastId) {
		}
		else {
			$('#img7 img').attr("src", linkToThumbnail + jsonObject[(selectedImage+3)].url);
			$('#img7 img').attr("id", "previewimg_" + (selectedImage+3));
			if(jsonObject[(selectedImage+3)].width <= jsonObject[(selectedImage+3)].height) {
				$('#img7 img').attr("width", "104");
				faktor = jsonObject[(selectedImage+3)].width/104;
				height = jsonObject[(selectedImage+3)].height/faktor;
				mtop = (height-104)/2;
				if(mtop < 0) mtop = 0;
				$('#img7 img').css("margin-top","-"+mtop+"px");
			}
			else {
				$('#img7 img').attr("height", "94");
				faktor = jsonObject[(selectedImage+3)].height/94;
				width = jsonObject[(selectedImage+3)].width/faktor;
				left = (width-94)/2;
				if(left < 0) left = 0;
				$('#img7 img').css("margin-left","-"+left+"px");
			}
		
	  }
	  animated = false;
	});
	}

	if((mode == "picture") || (mode == "publication")) {
	  	$('#nextImage').fadeIn(time);
		$('#currentImage').fadeOut(time, function(){
			$('#nextImage').attr("id", 'currentImage1');
			$('#currentImage').attr("id", 'nextImage');
			$('#currentImage1').attr("id", 'currentImage');
			if((selectedImage+1) <= lastId) {
				$('#nextImage img').attr("src", "../uploads/medium/" + jsonObject[(selectedImage+1)].url);
				width = jsonObject[(selectedImage+1)].picturewidth;
				left = (width-605)/2;
				if(left < 0) left=0; 
				$('#nextImage img').css("margin-left","-"+left+"px");
				height = jsonObject[(selectedImage+1)].pictureheight;
				mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
				if(mtop < 0) mtop=0; 
				$('#nextImage img').css("margin-top","-"+mtop+"px");
				
			}
			$('#previousImage img').attr("src", "../uploads/medium/" + jsonObject[(selectedImage-1)].url);
			width = jsonObject[(selectedImage-1)].picturewidth;;
			left = (width-605)/2;
			if(left < 0) left = 0;
			$('#previousImage img').css("margin-left","-"+left+"px");
			height = jsonObject[(selectedImage-1)].pictureheight;
			//alert(height);
			mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
	  		if(mtop < 0) mtop = 0;
			$('#previousImage img').css("margin-top","-"+mtop+"px");
			
			var marginTop = $('#currentImage img').css("margin-top");
			//alert(marginTop);
			marginTop = marginTop.substr(0, marginTop.length-2);
			//alert(marginTop);
			marginTop = marginTop.replace(/,/, ".");  // Beacaus Google makes, instead .
	  		if (marginTop < (-150)) {
				$('#currentImage img').animate({marginTop:"-50px"}, parseInt(8000));
			}
			
			if(mode=="publication") {
				$('#currentImage a').attr("href", jsonObject[selectedImage].flashurl);
			}
		});
	
		if (mode=="picture") {
			if(jsonObject[(selectedImage)].author) {
				author = jsonObject[(selectedImage)].author;
				author = author.replace("Foto:","");
				author = author.replace("Fotos:","");
				$('#author').html("Foto: " + author);
	  		}
	  		else {
				$('#author').html("");
	  		}
			$('#enlargeButton').attr("href","../uploads/"+ jsonObject[selectedImage].url);
		}
		else {
			$('#author').html(jsonObject[selectedImage].description);
			$('#enlargeButton').attr("href", jsonObject[selectedImage].flashurl);
		}

		if (mode=="picture") {
			aktiv = window.setInterval("nextImage(1000)", 10000);
		}
	}
}

function previousImage(time) {

	if (animated) {
		return 0;
	}
	else {
		animated = true;
	}

	if(mode == "picture") {
		window.clearInterval(aktiv);
	}
	else if (mode == "video") {
		if(selectedVideo == 0) {
			animated = false;
			return 0;
		}
		selectedVideo--;
		sendEvent('playitem', selectedVideo);
	}
	
	
	var lastId = jsonObject.length - 1;	

	if (selectedImage == 0) {
		aktiv = window.setInterval("nextImage(1000)", 10000);
		animated=false;
		return 0; 
	}
	
	selectedImage--;
	
	$('#currentImage img').stop();
	
	if (selectedImage == 0) {
		$('#pointer').animate({left:"27px"},time, function () {
			animated = false;
		});
	}
	else if (selectedImage == 1) {
		$('#pointer').animate({left:"138px"},time, function() {
			animated = false;
		});
	}
	else if((jsonObject.length == 4) && (selectedImage == 2)) {
		$('#pointer').animate({left:"249px"},time, function() {
			animated = false;
		});
	}
	else if(selectedImage == (lastId-1)) {
		$('#pointer').animate({left:"360px"},time, function () {
			animated = false;
		});
	}
	else if(selectedImage == (lastId-2)) {
		$('#pointer').animate({left:"249px"},time, function () {
			animated = false;
		});
	}
	else {
		$('#scrollContainer').append('<div onclick="divclick(this.id)" id="img0" style="width:0px; margin-right:0px;" class="previewImageContainer"><img src=""/></div>');
		$("#img0").insertBefore("#img1");
		$('#img0').animate({width:"104px", marginRight:"7px"},time, function() {
			$('#img7').remove();
			$('#img6').attr("id", 'img7');
			$('#img5').attr("id", 'img6');276
			$('#img4').attr("id", 'img5');
			$('#img3').attr("id", 'img4');
			$('#img2').attr("id", 'img3');
			$('#img1').attr("id", 'img2');
			$('#img0').attr("id", 'img1');

			if((selectedImage-3) < 0) {
			}
			else {
				$('#img1 img').attr("src", linkToThumbnail + jsonObject[(selectedImage-3)].url);
				$('#img1 img').attr("id", "previewimg_" + (selectedImage-3));
				if(jsonObject[(selectedImage-3)].width <= jsonObject[(selectedImage-3)].height) {
					$('#img1 img').attr("width", "104");
					height = $('#img1 img').attr("height");
					mtop = (height-104)/2;
					$('#img1 img').css("margin-top","-"+mtop+"px");
				}
				else {
					$('#img1 img').attr("height", "94");
					var width = $('#img1 img').attr("width");
					var left = (width-94)/2;
					$('#img1 img').css("margin-left","-"+left+"px");
				}

				//$('#img1').css("background-image","url("+ linkToThumbnail + jsonObject[(selectedImage-3)].url + ")");
				//$('#img1').css("backgroundPosition","50% 50%");
			}
			animated = false;
		});
	}
	
	if((mode == "picture") || (mode == "publication")) {
		$('#previousImage').fadeIn(time);
		$('#currentImage').fadeOut(time, function(){
			$('#previousImage').attr("id", 'currentImage1');
			$('#currentImage').attr("id", 'previousImage');
			$('#currentImage1').attr("id", 'currentImage');
			$('#nextImage img').attr("src", "../uploads/medium/" + jsonObject[(selectedImage+1)].url);
			width = jsonObject[(selectedImage+1)].picturewidth;
			left = (width-605)/2;
			if(left < 0) left = 0;
			$('#nextImage img').css("margin-left","-"+left+"px");
			height = jsonObject[(selectedImage+1)].pictureheight;
			mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
			if(mtop < 0) mtop = 0;
			$('#nextImage img').css("margin-top","-"+mtop+"px");
			if((selectedImage-1) >= 0) {
				$('#previousImage img').attr("src", "../uploads/medium/" + jsonObject[(selectedImage-1)].url);
				width = jsonObject[(selectedImage-1)].picturewidth;
				left = (width-605)/2;
				if(left < 0) left = 0;
				$('#previousImage img').css("margin-left","-"+left+"px");
				height = jsonObject[(selectedImage-1)].pictureheight;
				mtop = (height-IMAGE_CONTAINER_HEIGHT)/2;
				if(mtop < 0) mtop = 0;
				$('#previousImage img').css("margin-top","-"+mtop+"px");
				
			}
			
			var marginTop = $('#currentImage img').css("margin-top");
			marginTop = marginTop.substr(0, marginTop.length-2);
			marginTop = marginTop.replace(/,/, ".");  // Beacaus Google makes, instead .
	  		if (marginTop < (-150)) {
				$('#currentImage img').animate({marginTop:"-50px"}, parseInt(8000));
			}
			
			if(mode=="publication") {
				$('#currentImage a').attr("href", jsonObject[selectedImage].flashurl);
			}
		});
		
		if (mode=="picture") {
			if(jsonObject[(selectedImage)].author) {
				author = jsonObject[(selectedImage)].author;
				author = author.replace("Foto:","");
				author = author.replace("Fotos:","");
				$('#author').html("Foto: " + author);
	  		}
	  		else {
				$('#author').html("");
	  		}
			$('#enlargeButton').attr("href","../uploads/"+ jsonObject[selectedImage].url);
	  }
	  	else {
			$('#author').html(jsonObject[selectedImage].description);
			$('#enlargeButton').attr("href", jsonObject[selectedImage].flashurl);
	  }

		if (mode=="picture") {
			aktiv = window.setInterval("nextImage(1000)", 10000);
		}
	}
}

/* VIDEOPLAYER */

// some variables to save
	var currentPosition;
	var currentVolume;
	var currentItem;
	var currentState = 0;
	var timeRemaining = -1;
	var videoPlayClicked = false;
	var sendVideoPlayClickedEvent = true;

	// these functions are caught by the JavascriptView object of the player.
	function sendEvent(typ,prm) { 
		thisMovie("mpl").sendEvent(typ,prm); 
	};

	function getUpdate(typ,pr1,pr2,pid) {	
      
	};

	// These functions are caught by the feeder object of the player.
	function loadFile(obj) { thisMovie("mpl").loadFile(obj); };
	function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }
	function removeItem(idx) { thisMovie("mpl").removeItem(idx); }
	function getItemData(idx) {
		var obj = thisMovie("mpl").itemData(idx);
		/*
		var nodes = "";		
		for(var i in obj) { 
			nodes += "<li>"+i+": "+obj[i]+"</li>"; 
		}*/
		//document.getElementById("data").innerHTML = nodes;
	};

	// This is a javascript handler for the player and is always needed.
	function thisMovie(movieName) {
	    if(navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	};

/* VIDEOPLAYER END */


function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return strReturn;
}

function divclick(elementid) {
	var nameid = $("#" + elementid + " img").attr("id");
	var id = nameid.split("_")[1];
	if(selectedImage < id) {
		var steps = id-selectedImage;
		var timer = 20;
		for(i=0; i<steps; i++) {
			setTimeout("nextImage(200)",timer);
			timer = timer+300;
		}
	}
	else if (selectedImage > id) {
		var steps = selectedImage-id;
		var timer = 20;
		for(i=0; i<steps; i++) {
			setTimeout("previousImage(200)",timer);
			timer = timer+300;
		}
	}
}

