/* preloading loading image */

loadingImage = new Image();  
loadingImage.src = "images/loading.gif";

// image resizing

function windowWidth () {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 1000;
  }
}

function windowHeight () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 700;
  }
}

function scaleImage(id, width, height) {
	
	var scaleWidth = 0;
	var scaleHeight = 0;
	var offsetHeight = 130;
	var offsetWidth = 0;
	var speed = 0.2;
	
	if (document.getElementById(id).className == "unresized") {

		if(width>windowWidth()-offsetWidth) {
			scaleWidth = windowWidth()-offsetWidth;
		}
		if(height>windowHeight()-offsetHeight) {
			scaleHeight = windowHeight()-offsetHeight;
		}
		
		if(scaleWidth>0 || scaleHeight >0) {
			if(scaleWidth>0 && scaleHeight >0) {

				if(scaleHeight*(width/height) > scaleWidth) 
					scaleHeight = 0;
				else
					scaleWidth = 0;

			}
			if(scaleWidth>0) {
					for(i = 0; (width-i) >= scaleWidth; i=i+2) {
						setTimeout("scaleSize('" + id +"', " + (width-i) + ", 0)", i*speed);
						document.getElementById(id).className = 'resize_x';
					}
			}
			else if(scaleHeight>0) {
					for(i = 0; (height-i) >= scaleHeight; i=i+2) {
						setTimeout("scaleSize('" + id + "', 0, " + (height-i) + ")", i*speed);
						document.getElementById(id).className = 'resize_y';
					}
			}
		}			
	} else {
		if (document.getElementById(id).className == "resize_x") {
			var current = document.getElementById(id).width;
			for(i = 0; current+i <= width; i++) {
				setTimeout("scaleSize('" + id +"', " + (current+i) + ", 0)", i*speed);
			}
		}
		else {
			var current = document.getElementById(id).height;
			for(i = 0; current+i <= height; i++) {
				setTimeout("scaleSize('" + id +"', 0, " + (current+i) + ")", i*speed);
			}
		}
		document.getElementById(id).className = 'unresized';
	}
}

function scaleSize(id, width, height) {
	if(width>0) {
		document.getElementById(id).removeAttribute('height');
		document.getElementById(id).width = width;
	} else {
		document.getElementById(id).removeAttribute('width');
		document.getElementById(id).height = height;
	}
}

/* Init */

$(document).ready(function(){

	/* hideFirst effect */
	
	$("body").mousemove(function() {

		if($(".hideFirst:first").css('display') == "none") {
			$(".hideFirst").fadeIn(500);
		}

	});

	/* index functions */

	$("#optionsToggler").toggle(function () {
		$("#options").fadeIn(500);
		$("#options_image").attr("src", "http://www.bilderkiste.org/images/minus.gif");
	}, function () {
		$("#options").fadeOut(500);
		$("#options_image").attr("src", "http://www.bilderkiste.org/images/plus.gif");
	});

	$("#uploadForm").submit(function() {
		$('#upload_form').fadeOut(300);
		$('#upload_loading').show();
	});

	/* fading effect for upload result */
	$("#uploadContainer").fadeIn(500);

	/* image loading animation */
	
	var loadingSelector = '';
	if($('#detailimage').length == 0)
		loadingSelector = '.cntImage img';
	else
		loadingSelector = '#detailimage';
	
	$(loadingSelector).hide();
	$('<div class="loading"></div>').insertBefore(loadingSelector);

	$(window).load( function() {
		$('.loading').hide();
		$(loadingSelector).fadeIn(500);
	});

	/* Admin JS */

	if($("#adminimages").length) {

		function loadImages() {
			if(($('body').height()-(windowHeight()+$('body').scrollTop())) < 100) {
				$.get('img.php?pw=' + $("#adminPasswort").html() + '&img=true&day=' + $("#adminDay").html() + '&start=' + $(".adminImg").length, function(data) {
					$('#adminimages').append(data);
				});
			}
		}
		
		loadImages();
		$(window).scroll(loadImages);

	}

	if($("#adminimages2").length) {
	
		function loadImages2() {
			if(($('body').height()-(windowHeight()+$('body').scrollTop())) < 100) {
				$.get('verst.php?pw=' + $("#adminPasswort").html() + '&img=true&day=' + $("#adminDay").html() + '&start=' + $(".adminImg").length, function(data) {
					$('#adminimages2').append(data);
				});
			}
		}
		
		loadImages2();
		$(window).scroll(loadImages2);

	}
	
	/* account JS */
	
	$("#userbar a").click(function() {
		$("#userbarData").slideToggle(500);
	});

	$("#userbarData").mouseout(function() {
		$("#userbarData").slideToggle(500);
	});
	
	/* delete js */
	
	$("#deleteActions input[name='cancel']").click(function() {
		window.location.href = '/';
	});

});
