// init
$(function() {
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	jQuery.historyInit(ca.navigate, "jquery_history.html");
	ca.initiateHistoryLinks();
	
	/*
	 * Plugins
	 */
	ca.initPlugins();
	
	
	$(window).bind("resize", function() {
		ca.checkPageSize();
	});
	
	/*
	 * Bild bewegen (Maus)
	 */
	$('#moveUp,#moveDown').mousemove(ca.setSpeed);
	$('#moveUp,#moveDown').hover(ca.startMove, ca.stopMove);
	
	/*
	 * Tastensteuerung
	 */
	$(window).keydown(function(e) {
		var key = e.keyCode ? e.keyCode : e.charCode;
		switch(key) {
			//right arrow
			case 39:
				$('#navNext').click();
				break;
				
			//left arrow
			case 37:
				$('#navPrev').click();
				break;
				
			//up arrow
			case 38:
				ca.moveDirection = 10;
				ca.moveObj = document.getElementById('fullScreen').childNodes.item(1);
				ca.startMove(e);
				break;
				
			//down arrow
			case 40:
				ca.moveDirection = -10;
				ca.moveObj = document.getElementById('fullScreen').childNodes.item(1);
				ca.startMove(e);
				break;
			
			//space
			case 32:
				$('#image').click();
				break;
			
			//esc
			case 27:
				ca.exitFullScreen();
				break;
		}
		
	});
	
	$(window).keyup(function(e) {
		var key = e.keyCode ? e.keyCode : e.charCode;
		switch(key) {
			//up arrow
			case 38:
				ca.moveDirection = 0;
				ca.stopMove(e);
				break;
				
			//down arrow
			case 40:
				ca.moveDirection = 0;
				ca.stopMove(e);
				break;
		}
		
	});
});

var ca = {
	
	pageSize:		"default",
	
	initPlugins:	function() {
						ca.initiateHistoryLinks();
						ca.checkPageSize();
						if ($('.scrollbarWrap').attr('class')) {
							$('.scrollbarWrap').tinyscrollbar({
								axis: 'x'
							});
						}
					},
	
	initiateHistoryLinks:	function() {
								// set onlick event for buttons
								$("a[rel*=history]").click(function(){
									var hash = this.href;
									hash = hash.replace(/^.*#/, '');
									// moves to a new page. 
									// pageload is called at once. 
									// hash don't contain "#", "?"
									$.historyLoad(hash);
									return false;
								});
							},
						
	checkPageSize:	function() {
						
						ca.pageSize = (!$.cookie('pageSize') ? 'default' : $.cookie('pageSize'));
						
						var hash = (document.location.hash.replace(/^[#]/, "") == '' ? ca.defaultHash : document.location.hash.replace(/^[#]/, ""));
						
						if($(window).height() < 735 || $(window).width() < 925) {
							$.cookie('pageSize', null);
							$.cookie('pageSize', 'small');
							if(ca.pageSize == "default") {
								ca.pageRequest(hash + '?resized');
							}
						} else {
							$.cookie('pageSize', null);
							$.cookie('pageSize', 'default');
							if(ca.pageSize == "small") {
								ca.pageRequest(hash + '?resized');
							}
						}
						
						ca.pageSize = $.cookie('pageSize');
						
						//center page
						if(($(window).height()-$('#main').height()) > 0) {
							$('#title').stop().animate({
								'margin-top': Math.floor(($(window).height() - $('#main').height() - 30) / 2) + 'px',
								'opacity': ((hash.indexOf('info.html') != -1 || hash.indexOf('imprint.html') != -1) ? 1 : 0)
							});
						}
					},
					
	lastHashs:		[],
	
	pageRequest:	function(hash) {
						$.get(hash.replace(/^[!]\//, ca.approot), function(res) {
							var response = jQuery("<div>").append(res.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ""));
							
							$('#main').attr('class', ca.pageSize);
							
							// neues Bild laden
							if(hash.indexOf('home.html') != -1) {
								response.find('#image').css({'opacity': 0}).preload({
									onComplete: function() {
										var height = (ca.pageSize == "small" ? 510 : 621);
										$('#content').html(response.find('#content').html());
										//Bildüberblendung - Abschluss
										$('#image').css({
											'margin-top': Math.max(0, Math.floor((height - $('#image').height()) / 2)) + 'px'
										}).animate({
											'opacity': 1
										}, 500);
										ca.initPlugins();
									}
								});
							}
							
							// Info oder Impressum laden
							if(hash.indexOf('info.html') != -1 || hash.indexOf('imprint.html') != -1) {

								if(!$('#info').attr('id')) {
									//Einbledeanimation
									$('#content').append('<div id="info" />');

								
									if(ca.pageSize == "small") {
										var startObj = {
											"width": "18px",
											"height": "14px",
											"bottom": "-37px",
											"left": "453px",
											"opacity": 0
										};
										
										var endObj = {
											"top": 0,
											"left": 0,
											"width": "740px",
											"height": "254px",
											"padding-top": "135px",
											"padding-bottom": "135px",
											"padding-left": "50px",
											"padding-right": "50px",
											"opacity": 1
										}
									} else {
										var startObj = {
											"width": "18px",
											"height": "14px",
											"bottom": "-37px",
											"left": "453px",
											"opacity": 0
										};
										
										var endObj = {
											"top": 0,
											"left": "-32px",
											"width": "890px",
											"height": "365px",
											"padding-top": "135px",
											"padding-bottom": "135px",
											"padding-left": "50px",
											"padding-right": "50px",
											"opacity": 1
										}
									}
									
									$('#info').css(startObj).animate(endObj, function() {
										ca.fillInfo(response);
									});
									
								} else {
									ca.fillInfo(response);
								}
								
								if (hash.indexOf('info.html') != -1) {
									$('#btnClose').click(function() {
										history.back();
									});
								}
								
								if (hash.indexOf('imprint.html') != -1) {
									$('#btnClose').click(function() {
										history.back(-2);
									});
								}
							}
							
							
						}, "html");
					},
					
	fillInfo:		function(response) {
						$('#images').remove();
						$('#info').html(response.find('#info').html()).children().css('opacity', 0).animate({
							'opacity': 1
						});
						ca.initPlugins();
					},
	
	navigate:		function(hash) {
						//webkit bug
						if(hash == ca.lastHashs[(ca.lastHashs.length - 1)] && hash != '') {
							return;
						}
						
						if(hash == '') {
							hash = ca.defaultHash;
						}
						
						//Bildüberblendung - Einleitung
						$('#title').animate({
							'opacity': 0
						});
						$('#images').animate({'opacity': 0}, 500);
						
						//Thumbs ausblenden
						if (ca.lastHashs.length > 0) {
							if (ca.lastHashs[(ca.lastHashs.length - 1)].indexOf('info.html') != -1 && hash.indexOf('home.html') != -1) {
								$('#info').fadeOut();
							}
						}
						
						ca.pageRequest(hash);
						
						ca.lastHashs.push(hash);
					},
	
	fullScreen:		function(imageId) {
						$('#main').fadeOut();
						$('#fullScreen').fadeIn();
						$.post("ajax.php", {
							"action": "getImage",
							"id":	imageId,
							"width": $(window).width() + 20
						}, function(res) {
							$('#fullScreen img').css('display', 'none').attr('src', res.file).preload({
								onComplete: function() {
									$('#fullScreen img').css({
										'margin-top': Math.floor((res.height - $(window).height()) / -2) + 'px'
									}).fadeIn();
									$('#moveUp,#moveDown').fadeIn();
								}
							});
						}, "json");
					},
					
	exitFullScreen: function() {
						$('#main').fadeIn();
						$('#fullScreen, #moveUp,#moveDown').fadeOut();
						$('#fullScreen img').css('display', 'none');
					},
	
	mouseEvent:		'',
	moveInterval:	'',
	moveObj:		'',
	moveDirection:	0,
	
	startMove:	function(e) {
					ca.mouseEvent = e;
					ca.moveObj = this;
					ca.moveInterval = setInterval(ca.move, 20);
				},
				
	stopMove:	function() {
					clearInterval(ca.moveInterval);
					ca.mouseEvent = '';
				},
				
	setSpeed:	function(e) {
					ca.mouseEvent = e;
				},
	
	move:		function() {
					if ($('#fullScreen img').height() > $(window).height()) {
						var offset = $(ca.moveObj).offset();
						if (ca.moveObj.id == 'moveUp') {
							offset.top = Math.floor($(window).height() / 2);
						}
						var maxMoveUp = ($('#fullScreen img').height() - $(window).height()) * -1;
						if (ca.mouseEvent.pageY) {
							var deltaMove = Math.ceil((ca.mouseEvent.pageY - offset.top) / -30);
						} else {
							var deltaMove = ca.moveDirection;
						}
						$('#fullScreen img').css({
							"margin-top": Math.min(0, Math.max(maxMoveUp, parseInt($('#fullScreen img').css('margin-top')) + deltaMove)) + 'px'
						});
					}
				}
};
