$(document).ready(function() {
	var totalItems = $('.div-content-item').length;
	var minStageWidth = 1250;
	current = 0;

	$.backstretch('./images/background0.jpg');
	$("#backstretch").append($("#backstretch").html());
	
	$("#div-page").css({
		'width': $(window).width()
	});
	
	$("#div-long").css({
		'width': $(window).width() * totalItems
	});
	
	$(".div-content-item").css({
		'width': $(window).width()
	});

	// Logo click
	$("#div-logo").click(function() {
		navigationClickHandler('musicinbrands');
		$("#musicinbrands-link").stop().animate({
			top: '0px'
		}, {duration: 500, height: 'easeOutBounce'});
		moveContent(0);
	});
	
	// logo animator
			
	var image = 1;
	$("#div-logo .logo-item").hide();
	$("#div-logo .logo-item.item1").show();
	setInterval (function () {
			
		
		if(image == 4){
			
			$("#div-logo .logo-item.item" + image).fadeOut("slow");
			$("#div-logo .logo-item.item1").fadeIn("slow");
			
			image = 1;
		}else{
			$("#div-logo .logo-item.item" + image).fadeOut("slow");
			image ++;
			$("#div-logo .logo-item.item" + image).fadeIn("slow");
		}
		
	},5000);
	

	// Window resize
	$(window).resize(function() {
		
		if( $(window).width() < minStageWidth ){
			$("#div-page").css({
				'width': minStageWidth
			});
			
			$("#div-long").css({
				'width': minStageWidth * totalItems
			});
			
			$(".div-content-item").css({
				'width': minStageWidth
			});
			
			$(".div-content").css({
				'left': '520px'
			});
		}

		if( $(window).width() > minStageWidth ){
			$("#div-page").css({
				'width': $(window).width()
			});
			
			$("#div-long").css({
				'width': $(window).width() * totalItems
			});
			
			$(".div-content-item").css({
				'width': $(window).width()
			});
			
			if(548 + ((($(window).width() - 548) - 755) / 2) > 570)
				$(".div-content").animate({
					'left': 548 + ((($(window).width() - 548) - 755) / 2) 
				},250);
			else {
				$(".div-content").animate({
					'left': '520px'
				},250);
			}
		}
		
		goAnimate(current);
	});

	var currentActive;


	// Start animation navigation
	function start(element) {
		currentActive = element;
		
		$("#" + element).addClass('openActive');
		$("#musicinbrands-link").addClass('start');


		
		$(".div-menu-item").stop().animate({
			top: '0px'
		}, function(){
			
			
			if(!$(this ).hasClass('start')){	
				$(this).delay(1500).animate({
					top: '296px'
				}, {duration: 500, height: 'easeOutBounce'}, function(){
				
				});
			}

			
		});
		
		
		titleAnimate(-5);
	
		if( $(window).width() < minStageWidth ){
			$("#div-page").css({
				'width': minStageWidth
			});
			
			$("#div-long").css({
				'width': minStageWidth * totalItems
			});
			
			$(".div-content-item").css({
				'width': minStageWidth
			});
			
			$(".div-content").css({
				'left': '520px'
			});
		}

		if( $(window).width() > minStageWidth ){
			$("#div-page").css({
				'width': $(window).width()
			});
			
			$("#div-long").css({
				'width': $(window).width() * totalItems
			});
			
			$(".div-content-item").css({
				'width': $(window).width()
			});
			
			if(548 + ((($(window).width() - 548) - 755) / 2) > 570)
				$(".div-content").animate({
					'left': 548 + ((($(window).width() - 548) - 755) / 2) 
				},250);
			else {
				$(".div-content").animate({
					'left': '520px'
				},250);
			}
		}
		
		goChangeBG(0);
	}


	// Start
	var startHash = window.location.hash;

	switch(startHash)
	{
		case '#kontaktaoss': 
			start('kontaktaoss');
			goAnimate(8);
			titleAnimate(-165);
		break;
		
		case '#referenser': 
			start('referenser');
			goAnimate(7);
			titleAnimate(-145);
		break;
		
		case '#vilkaarvi':
			start('vilkaarvi');
			goAnimate(6);
			titleAnimate(-125);
		break;
		
		case '#rattigheter': 
			start('rattigheter');
			goAnimate(5);
			titleAnimate(-105);
		break;
		
		case '#fitnessplayer': 
			start('fitnessplayer');
			goAnimate(4);
			titleAnimate(-85);
		break;
		
		case '#tekniken': 
			start('tekniken');
			goAnimate(3);
			titleAnimate(-65);
		break;
		
		case '#riktatljud': 
			start('musicinbrands');
			goAnimate(2);
			titleAnimate(-45);
		break;
		
		case '#audiobranding': 
			start('audiobranding');
			goAnimate(1);
			titleAnimate(-25);
		break;
		
		default: start('musicinbrands');
	}

	
	// Navigation click handler  
	function navigationClickHandler(element){
		$("#" + currentActive + "-link").stop().animate({
			top: '296px'
		}, {duration: 500, height: 'easeOutBounce'});
		
		$('.div-menu-item').removeClass('openActive');
		$('#' + element).addClass('openActive');
		
		currentActive = element;
		moveContent(element);
	}
	

	// Hover menu
	$(".div-menu-item").hover(function() {
		if(!$(this).hasClass('openActive')){
			$("#" + $(this).attr('id') + "-link").stop().animate({
				top: '0px'
			}, {duration: 100, height: 'easeOutBounce'});
		}
	}, function() {
		if(!$(this).hasClass('openActive')){
			$("#" + $(this).attr('id') + "-link").stop().animate({
				top: '296px'
			}, {duration: 500, height: 'easeOutBounce'});
		}
	});

	// Click menu
	$(".div-menu-item").click(function() {
		if(!$(this).hasClass('openActive')){
			navigationClickHandler($(this).attr('id'));
		}
	});

	// Content mover
	function moveContent(menuItem) {
		if (menuItem == 'kund') {
			goAnimate(9);
			titleAnimate(-185);
		} else if (menuItem == 'kontaktaoss') {
			goAnimate(8);
			titleAnimate(-165);
		} else if (menuItem == 'referenser') {
			goAnimate(7);
			titleAnimate(-145);
		} else if (menuItem == 'vilkaarvi') {
			goAnimate(6);
			titleAnimate(-125);
		} else if (menuItem == 'rattigheter') {
			goAnimate(5);
			titleAnimate(-105);
		} else if (menuItem == 'fitnessplayer') {
			goAnimate(4);
			titleAnimate(-85);
		} else if (menuItem == 'tekniken') {
			goAnimate(3);
			titleAnimate(-65);
		} else if (menuItem == 'riktatljud') {
			goAnimate(2);
			titleAnimate(-45);
		} else if (menuItem == 'audiobranding') {
			goAnimate(1);
			titleAnimate(-25);
		} else if (menuItem == 'musicinbrands') {
			goAnimate(0);
			titleAnimate(-5);
		}
	}

	function goAnimate(number) {
		old = current;
		current = number;

		if (old > current) {
			speed = 5000 * ((old - current) / 10);
		} else {
			speed = 5000 * ((current - old) / 10);
		}
		
		var width;

		if( $(window).width() < minStageWidth ){
			width = minStageWidth
		}

		if( $(window).width() > minStageWidth ){
			width = $(window).width()
		}
		
		$("#div-long").css({
			'width': width * totalItems
		});

		$("#div-long").stop().animate({
			left: '-' + (width * number) + 'px'
		}, {duration: speed, width: 'easeInQuad'});

		goChangeBG(number);

	}

	function titleAnimate(number) {
		$(".div-topic").stop().animate({
			top: number + 'px'
		}, {duration: 300, width: 'easeInQuad'});
	}

	function goChangeBG(number) {
		$("#backstretch img:first-child").attr('src', './images/background' + number + '.jpg');
	}

	$(function() {
		$('.scroll-pane').jScrollPane();
	});

	function sendCode(){
		$.ajax({
			type: "POST",
			url: "./ajax_code.php",
			cache: false,
			data: "code=" + $("#code").val() + "&language=" + language,
			success: function(msg){
				
				
				if (msg == 'ERROR') {
					msg = messageError;
				}
				
				$("#code-content-holder").show();
				$("#code-content").html(msg);
			}
		});
	}
	
	// on hit "enter"
	$('#code').keypress(function(e) {
        if(e.which == 13) {
			sendCode();
        }
    });
	
	$("#kund-submit").click(function() {
		sendCode();
	});
	
	$("#code-content-holder").hide();
	
	$("#kontaktaoss-submit").click(function() {
		$.ajax({
			type: "POST",
			url: "./ajax_contact.php",
			cache: false,
			data: "name=" + $("#input-name").val() + "&email=" + $("#input-email").val() + "&message=" + $("#input-message").val(),
			success: function(msg){
				msg.trim();
				
				if (msg == 'SUCCESS') {
					msg = messageSuccess;
				} else {
					msg = messageError2;
				}
				
				$("#kontaktaoss-new-content").html(msg);
			}
		});
	});
	
	var e1 = 'in' + 'fo' + String.fromCharCode(29 + 35) + 'mu' + 'sic' + 'i' + 'nb' + 'ra' + 'nds' + '.' + 's' + 'e';
	$('#e1').attr({href : 'mailto:' + e1} ).text(e1);
	$('#e2').attr({href : 'mailto:' + e1} ).text(e1);
	
	$("#sv").click(function () {
		window.location = './sv';
	});
	
	$("#en").click(function () {
		window.location = './en';
	});
	
	(function($) {
		var cache = [];
		// Arguments are image paths relative to the current page.
		$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}})(jQuery)
	
	jQuery.preLoadImages("images/background0.jpg", "images/background1.jpg", "images/background2.jpg", "images/background3.jpg", "images/background4.jpg", "images/background5.jpg", "images/background6.jpg", "images/background7.jpg", "images/background8.jpg", "images/background9.jpg");
		
});

