jQuery(function( $ ){
	var testme = 'adf';
/* fadein of content after loading */
	if( $('#content').css('display')=='none' ){
		$('#content').fadeIn();
	}
/* fadein of content after loading */

//	$('.thumbnails ul:last').css({'height':'150px'});
	
	$('.scroll-pane').jScrollPane({
		verticalDragMinHeight: 50,
		verticalDragMaxHeight: 50,
	});
	
/* AJAX driven navigation */
	/* fade out content and load #content-container from href */
	$('#wrapper>header>a,#footer ul li a, #shots-small a').live("click",function(){//,#shots-small a
		if($("#overlay").length<1){
			if($("#gallery").css("display")!='none'&&$("#gallery").length>0){
				closeGallery();
				console.log("wrong"+$(this).attr('href'));
			} else {
				console.log("clicked"+$(this).attr('href'));
				var href = $(this).attr('href');
				if($('#content').length<1)	$('#content-outer').append('<section id="content"/>');
				$('#content').fadeOut("slow",function(){
					$('#content-outer').load( href+' #content', function(){
						$('#content').fadeIn();	
					});
				});
			}
		} else {
			closeMediaPlayer();
		}
		return false;
	});
	$('#shots').live('click',function(){
		$('#shots-small a').click();
		return false;
	});
	/*$("#shots-small a").live("click",function(){
		initoverlay();
		$("#overlay").html('<div class="comingsoon">COMING SOON...<br/><div class="comingsoonsub">click anywhere to continue browsing the website</div></div>').css({"z-index":"9999"});
		$("#overlay>div.comingsoon").css({"top":((screen.height-$("#overlay>div").height())/2)+"px","left":(screen.width-$("#overlay>div").width())/2})
		return false;
	});*/
	$('nav').hover(function() { 
	    $(this).stop().animate({"opacity": 1}); 
	},function() { 
	    $(this).stop().animate({"opacity": 0.3}); 
	});
	
	$('nav ul li a').click(function(){
		$('nav ul li a').removeClass("active");
		$(this).addClass("active");
		var href = $(this).attr('href');
		if($('#content').length<1)	$('#content-outer').append('<section id="content"/>');
		$('#content').fadeOut("slow",function(){
			$('#content-outer').load( href+' #content', function(){
				$('#content').fadeIn('slow',function(){
					if($("#thpics").length>0){
						/* fit thumbnail container and fadein thumbnails */
						$('#thpics ul li a img').each(function(){
							$(this).parent().animate({
								width: $(this).width()
							},800,function(){
								$('img',this).fadeIn();
							});
						});
					}
					if($("#thvideo").length>0){
						/* fit thumbnail container and fadein thumbnails */
						/*$('#thvideo ul li a img').each(function(){
							$(this).parent().animate({
								width: $(this).width()
							},800,function(){
								$('img',this).fadeIn();
							});
						});*/
					}
					$('.scroll-pane').jScrollPane({
						verticalDragMinHeight: 50,
						verticalDragMaxHeight: 50,
					});
				});
				$('#content-outer').append('<span id="galouter-container"></span>');
				$('#content-outer>span#galouter-container').load( href+' #galouter' );
			});
		});
		return false;
	});
	/* fade out content and load #content-container from href */
/* AJAX driven navigation */
	
/* agree button in shots-form */
	$("#iagree").live("click",function(){
		if($(this).css("backgroundPosition")=="0% 0%"){
			$("#agreed").val("1");
			$(this).css({"backgroundPosition":"left -10px"});
		} else {
			$("#agreed").val("0");
			$(this).css({"backgroundPosition":"left top"});
		}
	});
/* agree button in shots-form */
	
/* thumbnails container is present, we want a gallery */
	/* a thumbnail is clicked, build the cycle gallery and initialize it */
	$("#thpics ul li a").live("click",function(){
		/* copy thumbrow into gallery thumbnail container */
//		$('#galthumbs').html('<ul>'+$(this).parent().parent().html()+'</ul>').delay(1500).fadeIn();
		$('#galthumbs').html('<ul>'+$(this).parent().parent().html()+'</ul>').delay(500).show().animate({ top: 670, opacity: 1 },800);
		/* set active thumbnail */
		$('#galthumbs ul li:nth-child('+($(this).parent().index()+1)+')').find('a').attr("class","active");
		buildCycle($(this).attr("rel"),$(this).parent().index());
		return false;
	});
	/* bind gallery thumbnail links to cycle plugin */
	$("#galthumbs ul li a").live("click",function(){
		if($("#thvideo").length>0) {
			$("#galthumbs ul li a").removeClass("active");
			$(this).addClass("active");
			$("#gallery").html('<iframe width="725" height="460" frameborder="0" allowfullscreen="" webkitallowfullscreen="" src="http://player.vimeo.com/video/'+$(this).attr("href")+'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" class="vimeoiframe"></iframe>');
//			$("#gallery .vimeoiframe").attr("src","http://player.vimeo.com/video/"+$(this).attr("href")+"?title=0&byline=0&portrait=0&autoplay=1");
		} else {
			$("#gallery").cycle($(this).parent().index());
			/* set active thumbnail */
			$("#galthumbs ul li a").removeClass('active');
			$(this).attr('class','active');
		}
		return false;
	});
	/* close/overview links - fadeout gallery elements and destroy cycle plugin */
	$("#close, #overview").live("click",function(){
		closeGallery();
		return false;
	});
	$("#overviewv").live("click",function(){
		closeMediaPlayer();
		return false;
	});
	/* download link - redirect to download file */
	$('#download').live("click",function(){
		document.location = 'index.php?eID=shotseid&func=downloadFile&file='+$('#gallery article[style*="display: block"] header img').attr('rel');
		return false;
	});
	
	// not needed with ajax navigation, but stays for static version
	if($("#thpics").length>0){
		/* fit thumbnail container and fadein thumbnails */
		$('#thpics ul li a img').load(function(){
			$(this).parent().animate({
				width: $(this).width()
			},800,function(){
				$('img',this).fadeIn();
			});
		});
	}
/* thumbnails container is present, we want a gallery */


/* video thumbnails container is present, we want a video gallery */
	
	/* a thumbnail is clicked, build the media player and initialize it */
	$("#thvideo ul li").live("click",function(){
		var moviedata = $(this).next().find("div.velem:first");
		var movietitle = $("a",moviedata).html();
		var moviedescription = $(".moviedescription .mdsc2",this).html();		
//		var moviedescription = movietitle;
		var galthumbs = '';
		var gcnt = 0;
		var active = ' class="active"';
		if($(this).next().find("div.velem").length>1){
			galthumbs = '<ul>';
			$(this).next().find("div.velem").each(function(){
				if(gcnt==1) active = '';
				galthumbs += '<li><a href="'+$("a",this).attr("href")+'"'+active+'>'+$(".velem-thumb",this).html()+'</a></li>';
				gcnt++;
			});
		}
		
		if($("#overlay").length<1){
			$('body').prepend('<div id="overlay"></div>');
		}
		$('#wrapper>nav,#wrapper>footer,#shots-small').fadeOut(); // #wrapper>header,
		$("#gallery").show().html('<iframe class="vimeoiframe" src="http://player.vimeo.com/video/'+$("a",moviedata).attr("href")+'?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="725" height="460" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
		$("#content").fadeOut("slow",function(){
			$("#video-outer>footer>h3").html(movietitle);
			$("#video-outer>footer div:first").html(moviedescription);
			$("#video-outer").fadeIn();
			$("#video-outer #galnav").fadeIn();
			$('#galthumbs').html(galthumbs).delay(500).show().animate({ top: 670, opacity: 1 },800);
		});
		return false;
	});
	/*
	$("#thvideo ul li").live("click",function(){
		alert($("iframe",this).html());
		alert("click");
	
		$("iframe body>div:first",this).click();
	});*/
	
	/* close link - remove media player */
	$('#vclose,#overlay').live("click",function(){
		closeMediaPlayer();
		return false;
	});
/* video thumbnails container is present, we want a video gallery */

	
/* build the cycle gallery and initialize it */
	function buildCycle(rel,startingslide){
		$("#gallery").html("");
		var items = $("#thpics ul li a[rel='"+rel+"']");
		var textToInsert = '';
		var cnt = 0;
		var height = 460;
		var totalwidth = 725;
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
		if(isiPad) {
			var height = 397;
			var totalwidth = 620;
		}
		items.each(function(){
			var href = $(this).attr("href").split(" ")[0];
			var width = $(this).attr("href").split(" ")[1];
			var height = $(this).attr("href").split(" ")[2];
			var paddingtop = 0;
			if(height<460) paddingtop = Math.round((460-height)/2);
			var aheight = 460;
			var uid = $("div",this).html();
			if(isiPad) var aheight = 397;
			textToInsert  += '<article><header style="padding-top:'+paddingtop+'px;height:'+(aheight-paddingtop)+'px;"><img src="'+href+'" width="'+width+'" alt="" id="galimg'+cnt+'" rel="'+uid+'" /></header><footer>'+$(this).attr("title")+'</footer></article>'; //  height="'+height+'"
			cnt++;
		});
		$('#gallery').html(textToInsert);
		
		/* position gallery images and fadein */
		$('#gallery article header img').load(function(){
			var padding = Math.round( (totalwidth-$(this).attr('width'))/2 );
			$(this).css({'padding':'0 0 0 '+padding+'px'});
			$(this).delay(800).fadeIn();
		});
		/* position gallery images and fadein */
		
		$("#thpics").fadeOut("slow",function(){
			$("#galnav").fadeIn();
			$("#close").fadeIn();
			$("#gallery").fadeIn();
		});
		$("#gallery").cycle({
			fx: 'fade',
			prev: '#backward', 
		    next: '#forward', 
		    timeout: 0,
			startingSlide: startingslide,
		});
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
		var isiPod = navigator.userAgent.match(/iPod/i) != null;
		if(isiPad||isiPod) {
			$('#gallery article header img').touchwipe({
				wipeLeft: function() {
					$("#forward").click();
				},
				wipeRight: function() {
					$("#backward").click();
				}
			});
		}
	}
/* build the cycle gallery and initialize it */
	
/* close gallery and destroy cycle plugin */
	function closeGallery(){
		$("#galnav").fadeOut();
//		$("#galthumbs").fadeOut();
		$('#galthumbs').animate({top:540,opacity:0},400).hide();
		$("#close").fadeOut();
		$("#gallery").fadeOut("slow",function(){
			$("#thpics").fadeIn();
			$('#gallery').cycle('destroy');
		});
	}
/* close gallery and destroy cycle plugin */
	
/* close and remove media player */
	function closeMediaPlayer(){
		$('#overlay').fadeOut("slow",function(){
			$("#gallery").html("");
			$(this).remove();
		});
		$('#galthumbs').animate({top:540,opacity:0},400).hide();
		$("#video-outer").fadeOut("slow",function(){
			$('#content,#wrapper>nav,#wrapper>header,#wrapper>footer,#shots-small').fadeIn();
		});
	}
/* close and remove media player */

/* SHOTS formular */
if($(".shotswrapper").length<1){
	$('#submit').live("click",function(){
		$("#contactform").fadeOut(500,function(){
			$.post($("#cf-form").attr('action'), { name:  $('#name').val(), email: $('#email').val() }, function(data) {
				var intro = $('#contactintro').html();
				$('#contactform').html('<div id="contactintro">'+intro+'</div>');
				$('#contactform').append(data);
				// remove intro if submit was succesfull
				if( $("#contactform .feedback").length>0 ) $("#contactintro").remove();
				$("#contactform").fadeIn();
			});
		});
		return false;
	});
} else {
	$('#submit').live("click",function(){
		$("#cf-form").submit();
	});
}
/* SHOTS formular */

/* CONTACT */
	$('#hamburg>a:first, #ddorf>a:first, #paris>a:first, #newyork>a:first').live("mouseover",function(){
		var content = $(this).next().html();
		$('#address').fadeOut("slow",function(){
			$('#address').html( content ).fadeIn();
		});
		return false;
	});
	$('#hamburg>a:first, #ddorf>a:first, #paris>a:first, #newyork>a:first').live("click",function(){
		return false;
	});
/* CONTACT */	
	
	$(document).keyup(function(e) {		
		/* use keyboard in gallery mode */
		if( $('#gallery').length>0 && $.trim($('#gallery').html())!='' ){
			if (e.keyCode == 27) {
				closeGallery();
				e.preventDefault();
			} else if(e.keyCode==37){
				$('#backward').click();
				e.preventDefault();
			} else if(e.keyCode==39){
				$('#forward').click();
				e.preventDefault();
			}
		}
		/* use keyboard in gallery mode */
		
		/* use keyboard in video gallery mode */
		if( $('#video-outer').length>0 && $('#video-outer').css('display')!='none' ){
			if (e.keyCode == 27) {
				closeMediaPlayer();
				e.preventDefault();
			}
		}
		/* use keyboard in video gallery mode */	
	});	
	
	/* LOGIN / LOGOUT */
	$('#loginsubmit').live('click',function(){
		$.post("index.php?eID=mpLoginEid", { username: $('#hssi_email').val(), password: $('#hssi_password').val() }, function(data) {
			if(data!='1'){
				$('#contactform').html(data);
			} else {
//				$('#contactform').html(data);
				shotsmagazine = window.open("http://shots.oliverstefan.com/","Shots! Magazine","height="+screen.height+",width="+screen.width+",status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0");
				shotsmagazine.moveTo(0, 0);
			}
		});
		return false;
	});
	
	$('#logoutsubmit').live('click',function(){
		window.location.href = '/index.php?logintype=logout';
		return false;
	});
	/* LOGIN / LOGOUT */
	
	$.backstretch("fileadmin/templates/img/back.jpg");
	
	function initoverlay(){
		if($("#overlay").length>0){
			destroyoverlay();
		}
		$('body').prepend('<div id="overlay"></div>');
//		$("#overlay").fadeIn();
		$("#overlay").css({"height":(screen.height+25)}).fadeIn();
	}
	function destroyoverlay(){
		$("#overlay").fadeOut("slow",function(){
			$(this).remove();
		});
	}
});
