$(document).ready(function() {

	// basic UI scrits

	var language = 'en';
	$("#projectcontainer").hide();

	$("div[project^='f']").each(function(i) {
		var project = $(this).attr("project");
		$(this).load('projects/' + project + 'en.html #fetch');
		$("#en").hide();
		$("#de").show();
		$(".en").show();
		$(".de").hide();
	});
	$("div[project^='p']").each(function(i) {
		var project = $(this).attr("project");
		$(this).load('projects/' + project + 'en.html #fetch');
		$("#en").hide();
		$("#de").show();
		$(".en").show();
		$(".de").hide();
	});

	$("#en").click(function() {
		$("div[project^='p']").not("#projectcontainer").each(function(i) {
			var project = $(this).attr("project");
			$(this).load('projects/' + project + 'en.html #fetch');
		});
		$("div[project^='f']").not("#projectcontainer").each(function(i) {
			var project = $(this).attr("project");
			$(this).load('projects/' + project + 'en.html #fetch');
		});
		var pcproject = $("#projectcontainer").attr("project");
		$("#projectcontainer").load('projects/' + pcproject + 'en.html #fetchall', function() {
			$("#projectcontainer #bacontainer").beforeAfter({
				animateIntro : true,
				introDelay : 3000,
				introDuration : 1000,
				showFullLinks : false
			});
			$(".halfsize img").click(function() {
				var imgwidth = $(this).width();
				if(imgwidth > '400') {
					$(this).parent().css('maxWidth', '400px');
					$(this).parent().width('400px');
					$(this).width('400px');
				} else {
					$(this).parent().css('maxWidth', '800px');
					$(this).parent().width('800px');
					$(this).width('800px');
				}
			});
		});
		$("#en").hide();
		$("#de").show();
		$(".en").show();
		$(".de").hide();
		language = 'en';
	});
	$("#de").click(function() {
		$("div[project^='p']").not("#projectcontainer").each(function(i) {
			var project = $(this).attr("project");
			$(this).load('projects/' + project + 'de.html #fetch');
		});
		$("div[project^='f']").not("#projectcontainer").each(function(i) {
			var project = $(this).attr("project");
			$(this).load('projects/' + project + 'de.html #fetch');
		});
		var pcproject = $("#projectcontainer").attr("project");
		$("#projectcontainer").load('projects/' + pcproject + 'de.html #fetchall', function() {
			$("#projectcontainer #bacontainer").beforeAfter({
				animateIntro : true,
				introDelay : 3000,
				introDuration : 1000,
				showFullLinks : false
			});
			$(".halfsize img").click(function() {
				var imgwidth = $(this).width();
				if(imgwidth > '400') {
					$(this).parent().css('maxWidth', '400px');
					$(this).parent().width('400px');
					$(this).width('400px');
				} else {
					$(this).parent().css('maxWidth', '800px');
					$(this).parent().width('800px');
					$(this).width('800px');
				}
			});
		});
		$("#de").hide();
		$("#en").show();
		$(".en").hide();
		$(".de").show();
		language = 'de';
	});
	//remove href from loaded project and add jquery animation to it

	$(".ablock").live('click', (function(e) {
		e.preventDefault();
		var currentproject = $(this).closest("div[project*='p']").attr("project");
		$("#projectcontainer").load('projects/' + currentproject + language + '.html #fetchall', function() {
			$("#projectcontainer").show("slow");
			$("#projectcontainer #bacontainer").beforeAfter({
				animateIntro : true,
				introDelay : 3000,
				introDuration : 1000,
				showFullLinks : false
			});
			$(".halfsize img").click(function() {
				var imgwidth = $(this).width();
				if(imgwidth > '400') {
					$(this).parent().css('maxWidth', '400px');
					$(this).parent().width('400px');
					$(this).width('400px');
				} else {
					$(this).parent().css('maxWidth', '800px');
					$(this).parent().width('800px');
					$(this).width('800px');
				}
			});
		});
		$('html, body').animate({
			scrollTop : 0
		}, 'slow');
		if($(".slideshow:first").is(":visible")) {
			$(".slideshow").hide("slow");
			$("#firstspacer").hide();
			$('#slider').data('nivo:vars').stop = true;
			//Stop the Slider
		};
		$(".home").show("slow");
		$("#projectcontainer").attr('project', currentproject);
	}));
	
	$("#homebutton").live('click', (function() {
		if($(".slideshow:first").is(":hidden")) {
			$(".slideshow").show("slow");
			$('#slider').data('nivo:vars').stop = false;
			//Start the Slider
			$("#firstspacer").show();
		};
		$("#projectcontainer").hide("slow");
		$('html, body').animate({
			scrollTop : 0
		}, 'slow');
		$(".home").hide("slow");
	}));
	
	$(".halfsize img").click(function() {
		var imgwidth = $(this).width();
		if(imgwidth > '400') {
			$(this).parent().css('maxWidth', '400px');
			$(this).parent().width('400px');
			$(this).width('400px');
		} else {
			$(this).parent().css('maxWidth', '800px');
			$(this).parent().width('800px');
			$(this).width('800px');
		}
	});
	
	// Collapse function for press page
	$(".pressinformation").click(function() {

		if($(this).children(".backside").is(":visible")) {
			$(this).children(".backside").slideUp();
			$(this).find(".showpress").show();
		} else {
			$(".backside").slideUp();
			$(".showpress").show();
			$(this).children(".backside").slideDown();
			$(this).find(".showpress").hide();
		}
	});
	// Shuffle images 'all projects'-thumbs

	var chooseFrom = ['http://www.designyougo.com/img/projects/155px/01.jpg', 'http://www.designyougo.com/img/projects/155px/02.jpg', 'http://www.designyougo.com/img/projects/155px/03.jpg', 'http://www.designyougo.com/img/projects/155px/04.jpg', 'http://www.designyougo.com/img/projects/155px/05.jpg', 'http://www.designyougo.com/img/projects/155px/06.jpg', 'http://www.designyougo.com/img/projects/155px/07.jpg', 'http://www.designyougo.com/img/projects/155px/08.jpg', 'http://www.designyougo.com/img/projects/155px/09.jpg', 'http://www.designyougo.com/img/projects/155px/10.jpg', 'http://www.designyougo.com/img/projects/155px/11.jpg', 'http://www.designyougo.com/img/projects/155px/12.jpg', 'http://www.designyougo.com/img/projects/155px/13.jpg', 'http://www.designyougo.com/img/projects/155px/14.jpg', 'http://www.designyougo.com/img/projects/155px/15.jpg', 'http://www.designyougo.com/img/projects/155px/16.jpg', 'http://www.designyougo.com/img/projects/155px/17.jpg', 'http://www.designyougo.com/img/projects/155px/18.jpg', 'http://www.designyougo.com/img/projects/155px/19.jpg', 'http://www.designyougo.com/img/projects/155px/20.jpg', 'http://www.designyougo.com/img/projects/155px/21.jpg', 'http://www.designyougo.com/img/projects/155px/22.jpg'];
	$(function() {
		shuffle(chooseFrom);
		$(".apimg").each(function(index) {
			$(this).attr('src', chooseFrom[index]);
		});
	});
	function shuffle(a) {
		for(var i = a.length; i > 0; i--) {
			var j = Math.floor(Math.random() * i);
			var temp = a[i - 1];
			a[i - 1] = a[j];
			a[j] = temp;
		}
		return a;
	}

});

