$(document).ready(function () {

	//set some vars
	//sidebar width

	var sidebarWidth = 247;

	var scrollHeight = 570

	//antal billeder
	var images = $("#thumbnails ul.catlist").children();
	var antalimages = images.size(); 

	var imgsrc= $("#imagecontent img").attr('src');

	if (imgsrc == "") {


		//image navn
		var image = $("#thumbnails ul.catlist li a").eq(0).attr('rel');
		var url = '/files/assets/'+image+'.jpg';
		$("#imagecontent img").attr("src",url).load(function() {

			resizeImage();
			$("#loader").hide();

		});		

	}

	function doScrollWrapper() {

		//cleanup
		$("#terning").html('')

		//get antal billeder
		var num_thumbnails = $("#num_thumbnails").val();


		//window height
		var winHeight = $(window).height();	
		var previewHeight = 57;

		//scrolwrapper height
		i = 10;
		while (i > 0) {

			var newheight = 57 * i;


			if ((winHeight - 128 - newheight) > 0) {
				$("#scroll_wrapper").height(newheight);
				scrollHeight = newheight;
				var perPage = i;



				break;	
			}

			i--;
		}

		var num_dots = num_thumbnails / perPage;
		num_dots = Math.ceil(num_dots);

		if (num_dots > 1) {
			var i = 0;
			while (i < num_dots) {
				var output = '<li class="dot ';
				if (i == 0) {
					var output = output + 'active';
				}

				var output = output + '"><a href="#" rel="'+i+'">'+i+'</a></li>';
				$("#terning").append(output);

				i++;
			}
		}
	}


	//scroll thumbnails

	$(".dot a").live("click",function() {

		$(".dot").removeClass('active');

		var number = $(this).attr('rel');
		var scrollAmount = number * scrollHeight;

		$("#scroll_pane").animate({"top": "-"+scrollAmount+"px"}, "1200", function() { });
		$(this).parent('li').addClass('active');

	});

	//switch category
	$(".showgallery").click(function() {
		
		
		//preload
		
		




		$("#thumbnails").clearQueue();

		//reset thumbs
		$("#thumbnails").css('left',0);


		$(".active").removeClass('active');

		$(this).addClass('active');

		//cleanup
		$("#videocontent").fadeOut();
		$("#videocontent").html('');

		$("#tekstcontent").fadeOut();

		//vis swapper
		$(".swapper").fadeIn();

		//get the url
		var url = $(this).attr('href');
		
		_gaq.push(['_trackPageview', url]);


		//load the previews
		$.ajax({
			url: url,
			success: function(html){
			$("#thumbnails").html(html).fadeIn(function() {

				//antal billeder
				images = $("#thumbnails ul.catlist").children();
				antalimages = images.size(); 

				//vis det første billede
				showImage(0);

				var check = $("#thumbnails ul.catlist").hasClass('cat-Film');

				doScrollWrapper();

				if (check == false) {

					$("#tab").fadeIn();
					$("#trigger").show().addClass('disable-trigger');
					$("#thumbnails").delay('3500').animate({"left": "-=68px"}, "1200", function() { });
				} else {

					$("#trigger").hide();

					$("#tab").fadeOut();

				}
			});

		}
		});

		return false;
	});


	//showthumbs
	$("#tab:not(.hide)").live('click',function() {

		$("#thumbnails").clearQueue();

		$("#thumbnails").animate({"left": "0px"}, "1200", function() { 



		});

		$(this).addClass('hide');

	});

	$("#tab.hide").live('click',function() {

		$("#thumbnails").clearQueue();


		$("#thumbnails").animate({"left": "-=68px"}, "1200", function() { });


		$(this).removeClass('hide');
	});


	//show infopage
	$(".showinfo").click(function() {


		$(".active").removeClass('active');

		$(this).addClass('active');


		//cleanup
		$("#thumbnails").fadeOut();
		$("#videocontent").fadeOut();
		$("#videocontent").html('');
		//vis swapper
		$(".swapper").fadeOut();


		//get the url
		var url = $(this).attr('href');
		
		_gaq.push(['_trackPageview', url]);
		
		
		//fadeout the old content, then load it with the new
		$("#tekstcontent").fadeOut(function() {


			$.ajax({
				url: url,
				success: function(html){
				$("#tekst_inner").html(html);
                
                resizeImageView();


				$("#tekstcontent")
				.fadeIn();
			}
			});

		});

		return false;


	});

	//Resize image viewvier
	function resizeImageView() {

		var winWidth = $(window).width();
		var newWidth = winWidth - sidebarWidth;
		$("#imagecontent").width(newWidth);
		$("#tekstcontent").width(newWidth);
		$("#videocontent").width(newWidth);

		var triggerWidth = newWidth - 78;
		$("#trigger").width(triggerWidth);
		
		
		if (newWidth < 850) {

			$(".canhide").hide();
			
		} else {
			
			
			$(".canhide").show();
		}


	}

	//Resize af selve billeder
	function resizeImage() {
		
		//orginal dimensions
		var imageHeight = $("#imagecontent img").height();
		var imageWidth = $("#imagecontent img").width();

		var orginal_ratio = imageHeight / imageWidth;

		var imagecontentHeight = $("#imagecontent").height();
		var imagecontentWidth = $("#imagecontent").width();

		var ratio = imagecontentHeight / imagecontentWidth;


		if (orginal_ratio >= 1) {

			$("#imagecontent img").css("height",imagecontentHeight).css("width","auto");


		} else {

			if (ratio > 0.6666) {
				$("#imagecontent img").css("height",imagecontentHeight).css("width","auto");
			} else {
				$("#imagecontent img").css("width",imagecontentWidth).css("height","auto");
			}

		}



		var imageHeight = $("#imagecontent img").height();
		var imageWidth = $("#imagecontent img").width();

		var modTop = (imagecontentHeight - imageHeight) / 2;
		var modleft = (imagecontentWidth - imageWidth) / 2;


		$("#imagecontent img").css("top",modTop);
		$("#imagecontent img").css("left",modleft);

		//center billeder hvis det bliver klippet
		//  calcTop = 
		// $("#imagecontent img").css("top",calcTop);

	}


	//do onload
	resizeImageView();

	$("#imagecontent img").load(function() {

		resizeImage();
		$(this).fadeIn();

	});



	//triggers
	$(window).resize(function() {
		resizeImageView();
		resizeImage();

		doScrollWrapper();

	});


	//view video

	$(".viewvideo").live('click', function() {

		var url = $(this).attr('href');

		$.ajax({
			url: url,
			success: function(html){
			$("#videocontent").html(html).fadeIn();

		}
		});


		$(".swapper").fadeOut();

		return false;


	});


	//swap in image
	$(".swapimage").live('click', function() {



		curimage = $(".swapimage").index(this);

		//image navn
		var image = $(this).attr('rel');

		//check om det er det samme billede vi viser
		var curImageId = $("#imagecontent img").attr('id');
		var newImageId = 'imageid-'+image;

		if (curImageId != newImageId) {

			var url = '/files/assets/'+image+'.jpg';


			_gaq.push(['_trackPageview', url]);

			//preload billedet
			imageObj = new Image();
			imageObj.src = url;




			$("#imagecontent img").fadeOut(function() {



				//set the background
				$("#imagecontent img").attr('id','imageid-'+image);

				$("#imagecontent img").attr("src",url).load(function() {


					resizeImage();
					$("#imagecontent img").fadeIn();


				});	

			});



		}

		return false;
	});


	// image flip functions

	if (!curimage) {
		var curimage = 0;
	}


	$("a#do_right_swap").click(function() {
		if (curimage == "0") {

			//we are at the beginning
			curimage++;
			showImage(curimage);

		} else if (curimage == antalimages - 1) {

			//wraparund
			curimage = 0;
			showImage(curimage);


		} else {

			//we can go back
			curimage++;
			showImage(curimage);

		}

		return false;


	});



	$("a#do_left_swap").click(function() {
		if (curimage == "0") {


			//wraparund
			curimage = antalimages - 1;
			showImage(curimage);


		} else if (curimage == antalimages - 1) {

			//we are at the end
			curimage--;
			showImage(curimage);


		} else {

			//we can go back
			curimage--;
			showImage(curimage);
		}


		return false;
	});


	function showImage(index) {

		$("#thumbnails ul.catlist li a").eq(index).trigger('click');
	}



	$(document).keydown(function(e){
		if (e.keyCode == 37) { 
			$("a#do_left_swap").trigger('click');
			return false;
		}
		if (e.keyCode == 39) { 
			$("a#do_right_swap").trigger('click');
			return false;
		}


	});


	//mouseover effekter
	$("#trigger:not(.disable-trigger)").live("mouseenter", function(){

		$("#thumbnails").animate({"left": "-=68px", queue:false}, "500");
		$(this).addClass("disable-trigger");
		$("#thumbnails").removeClass("disable-strip");

	});


	$("#thumbnails:not(.disable-strip)").live("mouseenter", function(){

		$("#thumbnails").clearQueue();


		$("#thumbnails").animate({"left": "0px", queue:false}, "500");
		$(this).addClass("disable-strip");
		$("#trigger").removeClass("disable-trigger");

	});
});
