$(window).load(function (){
	// Hide all elements and then fade them in
	$("#container, #menu li").fadeTo(0, 0);
	$("#container, #menu li").fadeTo(2000, 1);
	$('#main .page:not("#home")').fadeTo(0, 0);
	// Display headline in a nicer font by replacing it with a picture
	$(".jqir").jQIR("jpg", "images/");
	// Display main page, logo and menu
	$(".page#home, #logo, #menu").removeClass("hide");
	//$(".page#home, #logo, #menu").css('opacity', 1);
	$(".page#home, #logo, #menu").fadeTo(2400, 1);
	$('#ajax-loader').css({display: 'none'});
});

$(document).ready(function(){
	//$(".picture").attr('id').replace(/\w+\d+\d+/, "");
	$.timer(1000,function(){$(".picture1").jQIR("jpg", "images/");});
	$.timer(3000,function(){$(".picture").jQIR("jpg", "images/");});
	
	// Show active page
	$('#menu a').click(function(){
		$('.page').stop();
		$('.col2 img').removeClass("hide");
		$("#menu a").removeClass("active"); 
		$(this).toggleClass("active");
		var page = $(this).attr('href');
		$('#main .page:not(' + page + ')').fadeTo(1000, 0);
		$('#main .page:not(' + page + ')').addClass("hide");
		$('.page' + page).removeClass("hide");
		$('.page' + page).fadeTo(1000, 1);
		$('#imageslider-holder div').fadeTo(0, 0);
		$('#simpleshow-holder div, #imageslider-holder .imageslider div').css({display: 'none'})
		$('.scroll').scrollTo(0);
	});
	
	$(".imageslider-link").click(function(){
		$("#projContent a").removeClass("active"); 
		$(this).toggleClass("active");
	});
	
	$('#menu a').hover(function() {
		$("#menu a.active").addClass('hideactivetab');
	}, function() {
		$("#menu a").removeClass('hideactivetab');
	});
	
	$('#projContent a').hover(function() {
		$("#projContent a.active").addClass('hideactivetab');
	}, function() {
		$("#projContent a").removeClass('hideactivetab');
	});
	
	// Show image on ljusstyrnings page
	$("#ljus-link").click(function(){
		$(".picture2").jQIR("gif", "images/");
	})
	
	// Hide and show submenu1
	$("#omoss-list").mouseover(	function(){$("#subMenu1").removeClass("hide");})
	$("#omoss-list").mouseout(	function(){$("#subMenu1").addClass("hide");})
	
	// Hide and show submenu2
	$("#ref-list").mouseover(	function(){$("#subMenu2").removeClass("hide");})
	$("#ref-list").mouseout(	function(){$("#subMenu2").addClass("hide");})
	
	$("#ref-link").click(function(){
		$('#simpleshow').css({display: 'block'})
		$('#simpleshow-holder').fadeTo(0, 1);
		$('#simpleshow-holder .active').removeClass("active");
		$('#simpleshow-holder .first').fadeTo(0, 0);
		$('#simpleshow-holder .first').addClass("active");
		$('#simpleshow-holder .active').fadeTo(2400, 1);
	})
	
	$("#proj-link").click(function(){
		$('#imageslider1 .first').css({display: 'block'})
		$('#imageslider-holder div#imageslider1').fadeTo(2000, 1);
		$('#imageslider-holder div:not("#imageslider1")').addClass("hide");
		$('#imageslider-holder div#imageslider1').removeClass("hide");
		$('#imageslider-holder div#imageslider1').fadeTo(2000, 1);
		$('#imageslider-holder div#imageslider1').addClass("iefadefix");
		$('#imageslider-holder div#imageslider1 div').fadeTo(2000, 1);
		$('.imageslider-link').removeClass("active");
		$('#imageslider1-link').addClass("active");
	});
	
	// Show active slideshow on project page
	$('#projContent a').click(	function(){
		$('#imageslider-holder div').fadeTo(0, 0);
		var slide = $(this).attr('href');
		$(slide + ' .first').css({display: 'block'})
		$('#imageslider-holder div:not(' + slide + ')').addClass("hide");
		$('#imageslider-holder div' + slide).removeClass("hide");
		$('#imageslider-holder div' + slide).fadeTo(2000, 1);
		$('#imageslider-holder div' + slide).addClass("iefadefix");
		$('#imageslider-holder div' + slide + ' div').fadeTo(2000, 1);
	});
	
	// Pause all slideshows
	$("#logo, .cross-link, .imageslider-link").click(function(){
		show.pause();	show2.pause();	show3.pause();
		show4.pause();	show5.pause();	show6.pause();
		show7.pause();	show8.pause();	show9.pause();
	})
	
	// Start slideshow on referense page
	var show = $("#simpleshow").simpleshow({speed:4000});
	$("#ref-link").click(function(){show.start();})
	
	// Start slideshow1 on referense page
	var show2 = $("#imageslider1").simpleshow({speed:4000});
	$("#proj-link, #imageslider1-link").click(function(){show2.start();})
	
	// Start slideshow2 on projekt page
	var show3 = $("#imageslider2").simpleshow({speed:4000});
	$("#imageslider2-link").click(function(){show3.start();})
	
	// Start slideshow3 on projekt page
	var show4 = $("#imageslider3").simpleshow({speed:4000});
	$("#imageslider3-link").click(function(){show4.start();})
	
	// Start slideshow4 on projekt page
	var show5 = $("#imageslider4").simpleshow({speed:4000});
	$("#imageslider4-link").click(function(){show5.start();})
	
	// Start slideshow5 on projekt page
	var show6 = $("#imageslider5").simpleshow({speed:4000});
	$("#imageslider5-link").click(function(){show6.start();})
	
	// Start slideshow6 on projekt page
	var show7 = $("#imageslider6").simpleshow({speed:4000});
	$("#imageslider6-link").click(function(){show7.start();})
	
	// Start slideshow7 on projekt page
	var show8 = $("#imageslider7").simpleshow({speed:4000});
	$("#imageslider7-link").click(function(){show8.start();})
	
	// Start slideshow8 on projekt page
	var show9 = $("#imageslider8").simpleshow({speed:4000});
	$("#imageslider8-link").click(function(){show9.start();})
	
	// Controllpanel for referens page
	$("#ref #pause").click(	function(){show.pause();});
	$("#ref #start").click(	function(){show.start();});
	$("#ref #prev").click(	function(){show.prev();});
	$("#ref #next").click(	function(){show.next();});
	
	// Adds scroll function to down arrows	
	var iPhone = navigator.userAgent.indexOf('iPhone') != -1;
	var iPad = navigator.userAgent.indexOf('iPad') != -1;
	if(iPhone || iPad){
		$('.scroller .scrollDown').click(	function(){
			var page = $(this).attr('rel');
			var speeds = $(this).attr('rev');
			$('#' + page + 'Content').scrollTo("+=50px", 500);
		});
	} else {
		$('.scroller .scrollDown').mousedown(	function(){
			var page = $(this).attr('rel');
			var speeds = $(this).attr('rev');
			$('#' + page + 'Content').scrollTo(500,2000*speeds);
		});
	}
	
	
	// Adds scroll function to up arrows
	if(iPhone || iPad){
		$('.scroller .scrollUp').click(	function(){
			var page = $(this).attr('rel');
			var speeds = $(this).attr('rev');
			$('#' + page + 'Content').scrollTo("-=50px", 500);
		});
	} else {
		$('.scroller .scrollUp').mousedown(	function(){
			var page = $(this).attr('rel');
			var speeds = $(this).attr('rev');
			$('#' + page + 'Content').scrollTo(0,2000*speeds);
		});
	}
	
	// Pauses the scroll function when mouseup or mouseout
	$('.scrollDown, .scrollUp').mouseup(	function(){$('.content').stop()});
	$(".scrollDown, .scrollUp").mouseout(	function(){$('.content').stop()})
	
	// Pauses the slideshow when pressing the scrollers
	//$('.selector').mousedown(	function(){show.pause();});
	//$(document).mouseup(		function(){show.start();});
	
	// Stops all actions in col2 when clicking a scroller
	//$('.scroller a').mousedown(	function(){$('.col2').stop();});
	
	// Start slideshow if holding mouse button down and draging outside the scroller
	$(".selector").mouseout(	function(){show.start();})
	
	// Controllpanel for project page
	$("#proj #pause2").click(function(){
		// Gets active sildeshow id
		var id = $('#imageslider-holder div:not(".hide")').attr('id');
		// Pause the active sildeshow
		if (id == "imageslider1"){show2.pause();}else
		if (id == "imageslider2"){show3.pause();}else
		if (id == "imageslider3"){show4.pause();}else
		if (id == "imageslider4"){show5.pause();}else
		if (id == "imageslider5"){show6.pause();}else
		if (id == "imageslider6"){show7.pause();}else
		if (id == "imageslider7"){show8.pause();}else
		if (id == "imageslider8"){show9.pause();}else{
			alert('sorry it seems there is no pause function for imageslider' + id);
		}
	});
	
	$("#proj #start2").click(function(){
		// Gets active sildeshow id
		var id = $('#imageslider-holder div:not(".hide")').attr('id');
		// Starts the active sildeshow
		if (id == "imageslider1"){show2.start();}else
		if (id == "imageslider2"){show3.start();}else
		if (id == "imageslider3"){show4.start();}else
		if (id == "imageslider4"){show5.start();}else
		if (id == "imageslider5"){show6.start();}else
		if (id == "imageslider6"){show7.start();}else
		if (id == "imageslider7"){show8.start();}else
		if (id == "imageslider8"){show9.start();}else{
			alert('sorry it seems there is no start function for imageslider' + id);
		}
	});
	
	$("#proj #prev2").click(function(){
		// Gets active sildeshow id
		var id = $('#imageslider-holder div:not(".hide")').attr('id');
		// Prev the active sildeshow
		if (id == "imageslider1"){show2.prev();}else
		if (id == "imageslider2"){show3.prev();}else
		if (id == "imageslider3"){show4.prev();}else
		if (id == "imageslider4"){show5.prev();}else
		if (id == "imageslider5"){show6.prev();}else
		if (id == "imageslider6"){show7.prev();}else
		if (id == "imageslider7"){show8.prev();}else
		if (id == "imageslider8"){show9.next();}else{
			alert('sorry it seems there is no next function for imageslider' + id);
		}
	});
	
	$("#proj #next2").click(function(){
		// Gets active sildeshow id
		var id = $('#imageslider-holder div:not(".hide")').attr('id');
		// Prev the active sildeshow
		if (id == "imageslider1"){show2.next();}else
		if (id == "imageslider2"){show3.next();}else
		if (id == "imageslider3"){show4.next();}else
		if (id == "imageslider4"){show5.next();}else
		if (id == "imageslider5"){show6.next();}else
		if (id == "imageslider6"){show7.next();}else
		if (id == "imageslider7"){show8.next();}else
		if (id == "imageslider8"){show9.next();}else{
			alert('sorry it seems there is no next function for imageslider' + id);
		}
	});
});