var show = false;
var w_h;
var w_w;
var allPics = false;
var numPics = 0;
var curImg = 1;
var kontakt = false;
var mapDim = [2414,1472];
var stepper;
var info = false;
var proI = false;
var numLoad = 0;
var nextPro = 0;
var curData = [];
var start = true;
var fadeTime;
var moveTime;
var block = false;
var ipad = false;
var agentID;
var orient;

$(window).load(function(){
	var deviceAgent = navigator.userAgent.toLowerCase();
	agentID = deviceAgent.match(/(ipad)/);
	if(agentID){updateLayout()};
});

function updateLayout(){
		
	currentWidth = window.innerWidth;
	orient = currentWidth == 981 ? "profile" : "landscape";
	document.body.setAttribute("orient", orient);
	
	if(orient == "landscape"){
		$('#iPadcursor').css("top","530px");
		$('#bg').css("top","275px");
		$('#bg > img').css("width","100%");
		$('#bg > img').css("height","auto"); 
		    
	}else{
		$('#bg').css("top","0px");
		$('#bg > img').css("width","auto");
		$('#bg > img').css("height","100%");     
	}
}

$(document).ready(function(){
	randomizer()
	var deviceAgent = navigator.userAgent.toLowerCase();
	agentID = deviceAgent.match(/(ipad)/);
	if (agentID){
		
		ipad = true;
		$('#cursor').css({"background":"none"});
		$('#iPadcursor').css("display","block");
		setTimeout("RemoveIpadCursor()",2000);
		mapDim = [1207,736];
		setInterval(updateLayout, 800);
	}
	
	$(window).resize(function(){
		resizer();
	});

	if($.browser.webkit){
		fadeTime = 400;
		var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
		console.log(chrome)
		if(chrome) {
			steppAmount = 1;
			moveTime = 1;
			$('#main').css("margin-left","272px");
			$('#projekte').css("margin-left","272px");
		}else{
			steppAmount = 40;
			moveTime = 800;
		}
	}else if($.browser.mozilla){
		fadeTime = 1;
		moveTime = 1;
		steppAmount = 1;
	}else if($.browser.msie){
		fadeTime = 1;
		moveTime = 1;
		steppAmount = 1;
	}else{
		fadeTime = 400;
		moveTime = 800;
		steppAmount = 40;
	}
	
	$('#cursor').click(function(e){
		if(proI) removeProInf();
		console.log(block);
		if(!block){
			block = true;		
			$('#bg > img:nth-child('+curImg+')').stop().animate({opacity:0.0},fadeTime,function(){
				if(e.clientX > w_w/2){
					if(curImg < numPics) curImg++;
					else curImg = 1;
				}else{
					if(curImg > 1) curImg--;
					else curImg = numPics;
				}
				$('#bg > img:nth-child('+curImg+')').stop().animate({opacity:1.0},fadeTime);
				setCounter();
				block = false;
			});
		}
	});
	
	$('#proInfTxt').click(function(e){
		removeProInf();		
		$('#bg > img:nth-child('+curImg+')').stop().animate({opacity:0.0},fadeTime,function(){
			if(e.clientX > w_w/2){
				if(curImg < numPics) curImg++;
				else curImg = 1;
			}else{
				if(curImg > 1) curImg--;
				else curImg = numPics;
			}
			$('#bg > img:nth-child('+curImg+')').stop().animate({opacity:1.0},fadeTime);
			setCounter();
			block = false;
		});
	});
	
	$('#logo,#main').mouseenter(function(e){
		if(e.currentTarget.id == "logo"){
			$('#frame').stop().animate({top:41},200,'easeOutQuint');
			show = false;
		}
	});
	
	$('*').mousemove(function(e){
		if(!kontakt && !info){
			if(e.clientX > 40 && e.clientY > 80 && w_w - e.clientX > 40 && w_h - e.clientY > 143){
				$('#cursor').css('display','block');
				if($.browser.msie) $('body').css('cursor','url(pics/cursore.cur),default');
				else $('body').css('cursor','url(pics/cursore.gif),none');
				$('#cursor').css('left', e.clientX - 40).css('top', e.clientY - 80);
				if(e.clientX < w_w/2){
					$('#cursor').css('backgroundPosition','0 -160px');
				}else{
					$('#cursor').css('backgroundPosition','0 0');
				}
			}else{
				$('#cursor').css('display','none');
				$('body').css('cursor','auto');
			}
		}else if(kontakt){
			$('#cursor').css('display','none');
			$('body').css('cursor','default');
			if(stepper == steppAmount){
				stepper = 0;
				var pl = e.clientX/w_w;
				var pt = e.clientY/w_h;
				var maxl = (mapDim[0] - w_w )*-1;
				var maxt = (mapDim[1] - w_h )*-1;
				var px = Math.floor(pl*maxl);
				var py = Math.floor(pt*maxt);
				$('#karte').stop().animate({"left":px+"px","top":py+"px"},moveTime,'easeOutQuint');
			}else{
				stepper++;
			}
		}else{
			$('#cursor').css('display','none');
			$('body').css('cursor','auto');
		}
	});
	
	$('#pro').mouseenter(function(){
		if(!show){
			show = true;
			$(this).addClass('act');
			$('#frame > div > a').stop().css('marginTop','41px');
			$('#frame').stop().animate({top:0},200,'easeOutQuint');
			setTimeout("showLnks()",40);
		}		
		if(info) removeInf();
		if(proI) removeProInf();
	});
	
	$('#inf,#con').mouseenter(function(){
		show = false;
		$('#frame').stop().animate({top:41},200,'easeOutQuint');
	});	

	$('#frame').mouseleave(function(){
		show = false;
		$('#frame').stop().animate({top:41},200,'easeOutQuint');
	});
	
	$('#frame > div > a').click(function(){
		nextPro = $(this).attr("id");
		$(this).siblings().removeClass("act");
		$(this).addClass("act");
		loader();
	});		
	
	$('#kon').click(function(){
		if(info) removeInf();
		if(proI) removeProInf();
		if(ipad){
			var l = -150;
			var t = -50;	
			$('#adress').css({"left":"645px","top":"265px","fontSize":"8px"}).addClass("adressIpad");
			$('#log').attr("src","pics/blank.gif");
			$('#karte > img').css({"width":"1207","height":"736px"})
		}else{
			var l = (w_w - mapDim[0])/2;
			var t = (w_h - mapDim[1])/2;			
		}

		$('#karte').css({"left":l+"px","top":t+"px"});
		
		$('#kontakt').css({'display':'block'});
		$('#karte').css({'opacity':'0.0'}).animate({opacity:1.0},1000,'easeOutQuint',function(){
			kontakt = true;
			stepper = 0
		});
		
		$('#leiste').animate({opacity:0.0},1000,'easeOutQuint',function(){
			$(this).css("display","none");
		});
	});
	
	$('#imgKarte').click(function(){
		if(kontakt){
			kontakt = false;
			$('#cursor').css('display','block');
			$('body').css('cursor','url(pics/cursore.gif),none');
			$('#karte').animate({opacity:0.0},1000,'easeOutQuint',function(){
				$('#kontakt').css({'display':'none'});
			});
			$('#leiste').css("display","block").animate({opacity:1.0},2000,'easeOutQuint');
			$('#pro').addClass('act');
		}
	});
	
	$('#inf').click(function(){
		if(proI) removeProInf();
		if(!info){
			info = true;
			var t = w_h - 62;
			var t2 = t - 500;
			if(t2 < 20) t2 = 20;
			$(this).addClass("act").siblings().removeClass("act");
			$('#info').css({"top": t+"px","display":"block"}).animate({top:t2},1200,'easeOutQuint');
		}
	});
	
	$('#bg, #info').click(function(){
		if(proI) removeProInf();
		if(info){
			removeInf();
			$('#pro').addClass('act');
			$('#cursor').css('display','block');
			$('body').css('cursor','url(pics/cursore.gif),none');
		}
	});
	
	$('#proI').click(function(){
		if(info) removeInf();
		show = false; 
		$('#infoTxt').html(curData[0][curImg-1].description);
		$('#frame').stop().animate({top:41},200,'easeOutQuint');
		if(!proI){
			proI = true;
			var t = w_h - 62;
			var t2 = t - 200;
			if(t2 < 20) t2 = 20;
			$(this).addClass("act");
			$('#proInfTxt').css({"top": t+"px","display":"block"}).animate({top:t2},1200,'easeOutQuint');
		}
	});
	
});

function RemoveIpadCursor(){
	$('#iPadcursor').animate({opacity:0.0},1000,function(){$(this).css("display","none")});
}

function randomizer(){
	var num = $('#frame > div').children().size()+1;
	var p = Math.floor(Math.random()* num);
	nextPro = $('#frame > div > a:nth-child('+p+')').attr("id");
	if($('#frame > div > a:nth-child('+p+')').text() == "vw" || $('#frame > div > a:nth-child('+p+')').text() == "ZaC") {
		randomizer();
	}else{
		$('#frame > div > a:nth-child('+p+')').addClass("act");
		 loader();
	}
}

function loader(){
	$.ajax({
	   type: "get",
	   url: "ajaxDispatcher.php",
	   dataType: "json",
	   data: "action=getProject&id=" + nextPro,
	   error: function(jqXHR, textStatus, errorThrown){
	   	console.log(jqXHR);
	   	console.log(textStatus);
	   	console.log(errorThrown);
	   },
	   success: function(data){
	   		curData = data;
	   		numLoad = 0;
	   		numPics = curData[0].length;
			$('#proName').text(curData[1]);
			$('#katTxt').text(curData[2]);
			/* $('#infoTxt').html(curData[3]); */
			$('#count').text("1 / " + numPics);
	     	loadpics();
	   }
	});
}

function loadpics(){	
	$("#bg").image("fotos/" + curData[0][numLoad].media,function(){
		numLoad++;
		if(numLoad == 1){
			if(!start){
				$('#bg > img:last').animate({opacity:1.0},fadeTime,function(){
					$('#bg > img').each(function(){
						if($(this).attr("id") != nextPro) $(this).remove();
					});
				});
			}else{
				start = false;
			}
			curImg = 1;
			$('#bg > img:first').animate({opacity:1.0},fadeTime);
		}
		if(numLoad < curData[0].length) loadpics(); 
		resizer();
	});
}

$.fn.image = function(src, f){ 
	return this.each(function(){ 
	 	var i = new Image(); 
	 	i.src = src; 
	 	i.id = nextPro;
	 	i.onload = f;
	 	this.appendChild(i);
	}); 
} 

function resizer(){
	w_h = document.documentElement.clientHeight;
	w_w = document.body.clientWidth;
	
	if(!agentID){
		if(w_h*1.4995 < w_w) {
			$('#bg > img').css("height","auto");
			$('#bg > img').css("width","100%");	
		}else{
			$('#bg > img').css("height","100%");
			$('#bg > img').css("width","auto");	
		}
	}else{
		if(orient == "landscape"){
			$('#iPadcursor').css("top","530px");
			$('#bg').css("top","275px");
			$('#bg > img').css("width","100%");
			$('#bg > img').css("height","auto"); 
			    
		}else{
			$('#bg').css("top","0px");
			$('#bg > img').css("width","auto");
			$('#bg > img').css("height","100%");     
		}
	}
	if(info){
		var t2 = w_h - 562;
		if(t2 < 20) t2 = 20;
		$('#info').css({"bottom":"62px","top":t2+"px"});
	}
	if(kontakt){
		var l = (w_w - mapDim[0])/2;
		var t = (w_h - mapDim[1])/2;
		$('#karte').css({"left":l+"px","top":t+"px"});
	}
	if(proI){
		var t2 = w_h - 262;
		$('#proInfTxt').css({"bottom":"62px","top":t2+"px"});
	}

}

function removeInf(){
	info = false;
	$('#inf').removeClass("act");
	var t = w_h - 62;
	$('#info').stop().animate({top:t},400,'easeOutQuint',function(){
		$(this).css({"display":"none"});
	});
}

function removeProInf(){
	proI = false;
	$('#proI').removeClass("act");
	var t = w_h - 62;
	$('#proInfTxt').stop().animate({top:t},400,'easeOutQuint',function(){
		$(this).css({"display":"none"});
	});
}

function setCounter(){
	$('#count').text((curImg) + " / " + numPics);
}

function showLnks(){
	var n = $('#frame > div').children().size();
	for(var i=0;i<=n;i++){
		$('#frame > div > a:nth-child('+i+')').delay(i*50).animate({marginTop:0},250,'easeOutQuint');
	}
}

