$(document).ready(function() {	
	$("a.fancybox").fancybox({
		padding : 0,
		overlayOpacity : 0.6,
		overlayColor : '#000',
		titleShow : false,
		'onClosed': function() {
			//parent.location.reload(true);
		}
	});
	$(".Masonry").masonry();
	$("select").uniform();
	$("#googleMap").css("height", $(document).height());
	$(window).resize(function() {
		$("#googleMap").css("height", $(document).height());
		removeFooter();
		setContentPlace();
		resizeLiElements();
		FrontPageCarousel.reload();
	});	
	checkSize(); setContentPlace();
	function resizeLiElements(){
		var x = $('#ContentResize ul li');
		x.attr("style", "height:"+myHeight);
		x.attr("style", "width: 100%");
	}
	function setContentPlace(){
		var x = $('.ContentResize');
		if(myHeight>(x.height()*1.5)){
			var y = (myHeight/2)-(x.height()/1.5);
			x.attr("style","top:"+ y +"px; position:absolute;");
		}
	}
		
	
	$(".topNavi").hover(
		function () {
			if($(this).parent().hasClass("active")) {
			
			} else {
				$("#Navigation li.active .subnavigation").hide();
				$(this).siblings(".subnavigation").show();
				$(this).siblings(".subnavigation").css("z-index","10");
				$(this).siblings(".subnavigation").css("opacity", 0);
				$(this).siblings(".subnavigation").animate({"opacity": 1}, 800, "easeOutExpo");
			}
		},
		function () {
			if($(this).parent().hasClass("active")) {
				$(this).siblings(".subnavigation").css("z-index","0");
			} else {
				$(this).siblings(".subnavigation").css("z-index","0");
				$(this).siblings(".subnavigation").hide();
				$("#Navigation li.active .subnavigation").show();
			}
		}
	);
	$(".subnavigation").hover(
		function () {
			if($(this).parent().hasClass("active")) {
			
			} else {
				$("#Navigation li.active .subnavigation").hide();
				$(this).show();
				$(this).css("z-index","10");
			}
		},
		function () {
			if($(this).parent().hasClass("active")) {
				$(this).css("z-index","9");
				$(this).show();
			} else {
				$(this).css("z-index","0");
				$(this).hide();
				$("#Navigation li.active .subnavigation").show();
			}
		}
	);
	var myHeight;
	removeFooter();
	
	function checkSize(){
	 if( typeof( window.innerWidth ) == 'number' ) {  
		myHeight = window.innerHeight; 
	} 	
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 

		//IE 6+ in 'standards compliant mode'  
		myHeight = document.documentElement.clientHeight; 

	} 	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

		//IE 4 compatible  
		myHeight = document.body.clientHeight; 

	}
	}
	function removeFooter(){
	
	 if( typeof( window.innerWidth ) == 'number' ) {  
		myHeight = window.innerHeight; 
	} 	
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 

		//IE 6+ in 'standards compliant mode'  
		myHeight = document.documentElement.clientHeight; 

	} 	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { 

		//IE 4 compatible  
		myHeight = document.body.clientHeight; 

	}
	var sJob = $('.selectJobRestaurant');
	 
	if(myHeight < 800){
		$('.footerFrontpage').hide();
		$('.footerFoodElement').hide();
		$('.footerJob').hide();
	}
	else if(myHeight>=800){
		$('.footerFrontpage').show();
		$('.footerFoodElement').show();
		$('.footerJob').show();
	}
	}
	
	$(".footerWrap").hover(function(){
		checkSize();
		if(myHeight<800){
		$('.footerFrontpage').show();
		$('.footerFoodElement').show();
		$('.footerJob').show();
		if(relatedCarousel!=null){
			relatedCarousel.reload();
			}
		}
	}, function(){
		checkSize();
		if(myHeight<800){
		$('.footerFrontpage').hide();
		$('.footerFoodElement').hide();
		$('.footerJob').hide();
		$('#selectJobRestaurant').mouseleave(function(event) { event.stopPropagation(); });
		$('#selectJobPost').mouseleave(function(event) { event.stopPropagation(); });
		}
	});
	$("#FooterBar").hover(function(){
		checkSize();
		if(myHeight<800){
			$('.footerFrontpage').show();
			$('.footerFoodElement').show();
			$('.footerJob').show();
			$('#selectJobRestaurant').mouseleave(function(event) { event.stopPropagation(); });
			$('#selectJobPost').mouseleave(function(event) { event.stopPropagation(); });
				if(relatedCarousel!=null){
				relatedCarousel.reload();
				}
			}
	},function(){
		checkSize();
		if(myHeight<800){
		$('.footerFrontpage').hide();
		$('.footerFoodElement').hide();
		$('.footerJob').hide();
		$('#selectJobRestaurant').mouseleave(function(event) { event.stopPropagation(); });
		}
	});
	
	//Food hover
	$(".foodOverview ul li span.image").hoverIntent(
		function () {
			if($(window).width()>1025){
			$(this).siblings("div").show();
			$(this).siblings("div").css("opacity", 0);
			$(this).siblings("div").animate({"opacity": 1}, 800, "easeOutExpo");
			}
		},
		function() {}
	);
	$(".foodOverview ul li div span.link").hover(
		function () {
			$(this).parent().hide();
		},
		function() {}
	);
	$(".foodOverview ul li div").hover(
		function() {},
		function () {
			$(this).hide();
		}
	);
	$(".foodCategory ul li span.image").hoverIntent(
		function () {
			if($(window).width()>1025){
			$(this).siblings("div").show();
			$(this).siblings("div").css("opacity", 0);
			$(this).siblings("div").animate({"opacity": 1}, 800, "easeOutExpo");
			}
		},
		function() {}
	);
	$(".foodCategory ul li div span.link").hover(
		function () {
			$(this).parent().hide();
		},
		function() {}
	);
	$(".foodCategory ul li div").hover(
		function() {},
		function () {
			$(this).hide();
		}
	);
	
	//Carousels
	var FrontPageCarousel;
	function frontpageCallback(carousel) {
		FrontPageCarousel = carousel;
		$("#frontpageControls a").bind('click', function() {
			carousel.scroll($.jcarousel.intval($(this).text()));
			return false;
		});
		$("#frontpageArrows .left").bind("click", function() {
			carousel.prev();
			return false;
		});
		$("#frontpageArrows .right").bind("click", function() {
			carousel.next();
			return false;
		});
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	function frontpageFirstInCallback(carousel, item, idx, state) {
		$("#frontpageControls a.active").removeClass("active");
		$("#frontpageControls a").eq(idx-1).addClass("active");
	}
	$("#MainFrontpage").jcarousel({
		visible: 1,
		scroll: 1,
		auto: 6,
		wrap: 'last',
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: frontpageCallback,
		itemFirstInCallback: frontpageFirstInCallback
	});
	function faqcarousel_initCallback(carousel) {
		$("#faqArrows .left").bind("click", function() {
			carousel.prev();
			return false;
		});
		$("#faqArrows .right").bind("click", function() {
			carousel.next();
			return false;
		});
	};
	
	function faqcarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
		$("#faqCarousel li:nth-child("+idx+")").css({'opacity':'1'});
	};
	function faqcarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
		$("#faqCarousel li:nth-child("+idx+")").css({'opacity':'0.5'});
	};
	
	$("#faqCarousel").jcarousel({
		scroll: 1,
		initCallback: faqcarousel_initCallback,
		itemVisibleInCallback: {
            onBeforeAnimation:  faqcarousel_itemVisibleInCallbackAfterAnimation
        },
        itemVisibleOutCallback: {
            onBeforeAnimation:  faqcarousel_itemVisibleOutCallbackAfterAnimation
        },
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	function presscarousel_initCallback(carousel){
		$(".nextPress").bind("click", function(){
			carousel.next();
			return false;
		});
		$(".prevPress").bind("click", function(){
			carousel.prev();
			return false;
		});
	};
	$("#pressCarousel").jcarousel({
		scroll: 1,
		vertical: true,
		initCallback: presscarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
		});
	
	function newscarousel_initCallback(carousel) {
		$("#newsControls a").bind('click', function() {
			carousel.scroll($.jcarousel.intval($(this).text()));
			return false;
		});
		$("#newsArrows .left").bind("click", function() {
			carousel.prev();
			return false;
		});
		$("#newsArrows .right").bind("click", function() {
			carousel.next();
			return false;
		});
	};
	function newscarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
		$("#newsCarousel li:nth-child("+idx+")").css({'opacity':'1'});
	};
	function newscarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
		$("#newsCarousel li:nth-child("+idx+")").css({'opacity':'0.5'});
	};
	$("#newsCarousel").jcarousel({
		scroll: 4,
		initCallback: newscarousel_initCallback,
		itemVisibleInCallback: {
            onBeforeAnimation:  newscarousel_itemVisibleInCallbackAfterAnimation
        },
        itemVisibleOutCallback: {
            onBeforeAnimation:  newscarousel_itemVisibleOutCallbackAfterAnimation
        },
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	function bookscarousel_initCallback(carousel) {
		$("#bookArrows .left").bind("click", function() {
			carousel.prev();
			return false;
		});
		$("#bookArrows .right").bind("click", function() {
			carousel.next();
			return false;
		});
	};
	var relatedCarousel;
	function related_initCallback(carousel) {
		relatedCarousel = carousel;
		$(".prevProd").bind("click", function() {
			carousel.prev();
			return false;
		});
		$(".nextProd").bind("click", function() {
			carousel.next();
			return false;
		});
	};
	$("#relatedProds").jcarousel({
		scroll: 1,
		visible: 2,
		initCallback: related_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemFallbackDimension: 320
	});
	$("#contactCity").change(function() {
		var value = $("#contactCity option:selected").val();
		$("#Kontakt .right .active").slideUp(500);
		$("#Kontakt .right .active").removeClass("active");
		$("#Kontakt .right .kontakt"+value).addClass("active");
		$("#Kontakt .right .kontakt"+value).delay(500).slideDown();
	});
	
	$(".imgNavi a").live("click", function() {
		
		var id = $(this).text();
		$(".imgNavi a.active").removeClass("active");
		$(this).addClass("active");
		$(".prodImg").hide();
		$("#prodImg"+id).fadeIn();
		return false;
	});
	window.setInterval(function(){
		if($(".imgNavi").length!=0){
		var $active = $('.imgNavi .active');
		var $next = $active.next().length > 0 ? $active.next() : $('.first'); 
			$active.removeClass("active");
			$next.addClass("active");
			var id = $next.text();
			$(".prodImg").hide();
			$("#prodImg"+id).fadeIn();
		}
		}, 5000);
	$("#foodArrows .right").live("click", function(){
		if($(".imgNavi").length!=0){
		var $active = $('.imgNavi .active');
		var $next = $active.next().length > 0 ? $active.next() : $('.first'); 
			$active.removeClass("active");
			$next.addClass("active");
			var id = $next.text();
			$(".prodImg").hide();
			$("#prodImg"+id).fadeIn();
		}
	});
	$("#foodArrows .left").live("click", function(){
		if($(".imgNavi").length!=0){
		var $active = $('.imgNavi .active');
		var $prev = $active.prev().length > 0 ? $active.prev() : $('.imgNavi .last'); 
			$active.removeClass("active");
			$prev.addClass("active");
			var id = $prev.text();
			$(".prodImg").hide();
			$("#prodImg"+id).fadeIn();
		}
	});
	if($(".imgNavi").length==0){
		$("#foodArrows").hide();
	}
	
	$("#selectJobRestaurant").change(function() {
		$(".jobpost").hide();
		$(".noresults").hide();
		$(".data").hide();
		$(".loader").show();
		var id = $(this).val();
		$.getJSON("/json.php?mode=getRestJobId&id="+id, function(data) {
			if(data.jobIdSet) {
				$.ajax({
					type: "GET",
					url: "/includes/ungarbejde.php?id="+data.jobId,
					dataType: "xml",
					success: function(xml) {
						$("#selectJobPost").empty();
						$("#selectJobPost").append("<option>Vælg jobopslag</option>");
						$(xml).find("annonce").each(function(){
							var jobTitle = $(this).find("Title").text();
							var jobId = $(this).find("annonceId").text();
							$("#selectJobPost").append("<option value=\""+jobId+"\">"+jobTitle+"</option>");
						});
						var nummer = $(xml).find("annonce").size();
						$.uniform.update("#selectJobPost");
						$(".loader").hide();
						$(".initial").hide();
						$(".jobpost").show();
						$(".secondary").fadeIn();
					}
				});
			} else {
				$(".initial").hide();
				$(".loader").hide();
				$(".noresults").fadeIn();
			}
		});
	});
	
	$("#selectJobPost").change(function() {
		$(".data").hide();
		$(".secondary").hide();
		$(".secondload").fadeIn();
		var id = $(this).val();
		$.ajax({
		    type: "GET",
		    url: "/includes/ungarbejdeJob.php?id="+id,
		    dataType: "xml",
		    success: function(xml) {
		    	var jobWorkTime = $(xml).find("WorkTime").text();
		    	var jobLink = $(xml).find("annonceUrl").text();
		    	var jobTitle = $(xml).find("Title").text();
		    	var descr = $(xml).find("Descp").text();
		    	
		    	$(".jobWorkTime").text(jobWorkTime);
		    	$(".jobLink").attr("href", jobLink);
		    	$(".jobTitle").text(jobTitle);
		    	$(".descr").html(descr);
		    	$(".secondload").hide();
		    	$(".data").fadeIn();
		    }
		});
	});
	
});
