$(document).ready(function() {
	$("body").addClass("js");
	$(window).bind("load", function(event) {
		navigation.init();
	});
	panel.init();
	companyListModule.init();
	$("#toparea > ul > li > a").toggle(function(e) {
		e.preventDefault();
		$("#toparea .active > a").trigger("click");
		$(this).parent("li").addClass("active");
		$(this).siblings("ul").addClass("dropped").animate({ height: "225px" },{duration: 500, 
			complete: function() {
				$(this).animate({height:"215px"},200,function(){
					if($.browser.msie){
						$(this).find(">li").addClass("a");
					}else{
						$(this).find(">li").fadeIn(100);	
					}	
				});
				$(this).find(".has-children > a:not(a:contains('Kontakta oss'))").mouseenter(function() {
					if(!$(this).siblings("ul").hasClass("dropped")){
						$(this).siblings("ul").addClass("dropped").animate({ height: "155px" },{duration: 300, 
							complete: function() {
								$(this).animate({height:"145px"},200,function(){
									if($.browser.msie){
										$(this).find(">li").addClass("a");
									}else{
										$(this).find(">li").fadeIn(100);	
									}	
								});
							}
						});	
					}else{
						clearTimeout(timeout);
					}
				}).mouseleave(function() {
					var timeout,
					that = $(this);
					timeout=setTimeout(function() {
						that.siblings("ul").removeAttr("style").removeClass("dropped").find("li").removeClass("a").removeAttr("style");
					}, 400);
					$(this).siblings("ul").mouseenter(function(){
						clearTimeout(timeout);
					}).mouseleave(function(){
						timeout=setTimeout(function() {
							that.siblings("ul").removeAttr("style").removeClass("dropped").find("li").removeClass("a").removeAttr("style");
						}, 400);
					});
				});
			}
		});
	}, function(e) {
		e.preventDefault();
		$(this).parent("li").removeClass("active");
		$(this).siblings("ul").removeAttr("style").removeClass("dropped").find("li").removeAttr("style");
	}).blur(function() {
		var that = $(this),
	 	timeout = setTimeout(function() {
			that.parent("li").removeClass("active");
			that.siblings("ul").removeAttr("style").removeClass("dropped").find("li").removeAttr("style");
		}, 500);
	});
});
var companyListModule = {
	init:function(){
		var module = $("#company-list"),
		img = module.find("img"),
		origSrc = img.attr("src");
		module.find("ul").mouseleave(function(){
			if(img.attr("src") !== origSrc){
				img.fadeOut(200,function(){
					$(this).attr("src",origSrc).fadeIn(300);
				});				
			}
		});
		module.find("a[data-image]").each(function() {
			var dataImage = $(this).attr("data-image"),
			preLoad = $(document.createElement("img")).attr("src",dataImage).addClass("preload").appendTo("body");
			$(this).mouseenter(function() {
				if(img.attr("src") !== dataImage){
					img.fadeOut(100,function(){
						img.attr("src",dataImage).fadeIn(200);
					});
				}
			});	
		});
	}
};
navigation={
	init:function(){
		if($(".middle-nav").length>0){
			var selected = $(".middle-nav li.selected a").length>0?$(".middle-nav li.selected a"):$(".middle-nav li.selected-path a"),
			links = $(".middle-nav");
			links.find("a").mouseenter(function(){
				links.animate({"background-position": navigation.getPosition($(this))+"px"+" 100%"},300);
			}).focusin(function() {
				links.animate({"background-position": navigation.getPosition($(this))+"px"+" 100%"},300);
			}).click(function(){
				$(this).addClass("clicked");
			});
			links.css({"background-position": navigation.getPosition(selected)+"px"+" 100%"}).mouseleave(function(){
				if($(this).find(".clicked").length===0){
					links.animate({"background-position": navigation.getPosition(selected.find("a"))+"px"+" 100%"},300);	
				}
			})	
		}
	},
	getPosition:function(element){
		var links = $(".middle-nav");
		return ((element.offset().left-links.offset().left -15) + element.outerWidth()/2)
	}
}
panel={
	init:function(){
		if($(".panel").length>0){
			panel.position();
			panel.collapse();
		}
	},
	position:function(){
		var panelCount = $(".panel").length;
		var panelWidth = ($(".panelpage").width()) / panelCount;
		$(".panel").each(function(i) {
			$(this)
				.css({"width":panelWidth * (i+1),zIndex:(panelCount-i)})
				.find("h1").css({"width":panelWidth});
			$(".panel h1").fadeIn(200);
		});
	},
	collapse:function(){
		$(".panel").click(function() {
			if(!$(this).hasClass("active")){
				$(this).addClass("active");
				$(".panelpage").addClass("collapsed");
				$(".panel h1").fadeOut(200);
				$(".panel").not(this).animate({width:0},600);
				$(".panel.active").animate({width:$(".panelpage").width()},600, function(){
					$(this).find("div").animate({paddingTop:"50px","height":470},300,function(){
						$(this).find("p").fadeIn(300);
					});
				});
				$(".return").click(function(e) {
					e.stopPropagation();
					$(".active").find("div,p").removeAttr("style")
					$(".active").removeClass("active");
					$(".collapsed").removeClass("collapsed");
					$(".panel h1").fadeIn(200);
					panel.position();
				});				
			}
		});
	}
}
/* old functions */
function viewImage(src, width, height){
    var widthAndHeight = "width=" + width + ",height=" + height;
    var newWindow = window.open(src, "ViewImageWindow", "toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, fullscreen=no,"+ widthAndHeight);
    newWindow.document.clear();
    newWindow.document.focus();
    newWindow.document.writeln('<html> <head><title> Axel Johnson<\/title> <\/head> <body style="padding: 0; margin: 0;">');
    newWindow.document.writeln('<img src=' + src + '  \/>');
    newWindow.document.writeln('<\/body> <\/html>');
    newWindow.document.close();
    newWindow.focus();
}
function ToggleVisible(width, height) { 

    var div = document.getElementById("slideshow");
    if(typeof width != 'undefinded' && typeof height != 'undefinded'){
        
   	    if (div.style.display == "block") {
	        div.style.display = "none";    
	    } else {
	        div.style.display = "block";
	        
	    }
    }else{
        div.style.width = width;
        div.style.height = height;
    }
   
}
