var marker_offset = {x: 194, y: -2}
var slider_width = 210;
var is_sliding = false;

var scroll_pane_settings = {
	scrollbarMargin: 20, 
	showArrows: true
};

jQuery.fn.extend({
	rnd: function() {
		var index = Math.floor(Math.random()*this.length);
		return $(this[index]);
	},
	move_by: function(x) {
		this.css({ left: (x + parseInt(this.css('left'))) + "px" });
	},
	get_pos: function() {
		a = this.get(0);
		if(a.offsetParent){
			for(var x = 0, y = 0; a.offsetParent; a = a.offsetParent){
				x += a.offsetLeft;
				y += a.offsetTop;
			}
			return {x: x, y: y};
		} 
		else {
			return {x: a.x, y: a.y};
		}
	},
	x: function() {
		return  parseInt(this.css('left'));
	}
});	



$(document).ready(function(){	        	
	center_container();
	toggle_image();
	$(".scroll-pane").jScrollPane(scroll_pane_settings);
	$("#scroll-pane-people").jScrollPane(scroll_pane_settings);
	$("div#people div.links a").click( function(){
		if($("div#loading").css("display") != "block"){
			$("div#content").hide();
			$("div#loading").show();
			//$("div#people_click").css({ top: $(this).get_pos().y + marker_offset.y, left: marker_offset.x }).hide();
			$("div#facts_click").hide();
			$("div#people_click").fadeIn(400);
			$.get(this.href, {hide_framework: true}, function(data){
				$("div#content").html(data);
				$("div#loading").hide();
				$("div#content").show();
				$(".scroll-pane").jScrollPane(scroll_pane_settings);
			});      
		}	       
		return false;
	});
	/*$("div#people div.links a").mouseover( function(){
		$("div#people_mo").css({ top: $(this).get_pos().y + marker_offset.y, left: marker_offset.x }).show();
	});
	$("div#people div.links a").mouseout( function(){
		$("div#people_mo").hide();
	});*/
	$("div#people div.links a").focus( function(){
		this.blur();
	});
	
	$("div#facts div.links a").not("#film").not("#HomeLink").click( function(){
		if($("div#loading").css("display") != "block"){
			$("div#content").hide();
			$("div#loading").show();
			$("div#facts_click").css({ top: $(this).get_pos().y + marker_offset.y, left: marker_offset.x }).hide();
			$("div#people_click").hide();
			$("div#facts_click").fadeIn(400);
			$.get(this.href, {hide_framework: true}, function(data){
				$("div#content").html(data);
				$("div#loading").hide();
				$("div#content").show();
				$(".scroll-pane").jScrollPane(scroll_pane_settings);
			});      
		}	       
		return false;
	});
	$("div#facts div.links a#bildspel").click( function(){
		open_bildspel(this.href);
		return false;
	});	
	$("div#facts div.links a#film").click( function(){
		open_film(this.href);
		return false;
	});	
	$("div#facts div.links a").mouseover( function(){
		$("div#facts_mo").css({ top: $(this).get_pos().y + marker_offset.y, left: marker_offset.x }).show();
	});
	$("div#facts div.links a").mouseout( function(){
		$("div#facts_mo").hide();
	});
	$("div#facts div.links a").focus( function(){
		this.blur();
	});
	
	$("div#mo_catcher").mouseover( function(event){
		is_sliding = false;		
	});
	$("div#mo_catcher").mouseout( function(){
		is_sliding = true;
	});
	$("div#mo_catcher").click( function(event){
		x = event.pageX || event.clientX;
		x -= ($("div#container").x() + $("div#slider").x());
		$("div.slider_div[name='sliding']").each( function(i){
			if(x >= $(this).x() && x <= ($(this).x() + slider_width)){
				$("div#people div.links a#" + this.id.slice(1)).click();	
			} 
		});	
	});

	create_slide_show();
	

	
});

    
$(window).resize(center_container);

function center_container(){
	$("div#container").css("top", 10);
	$("div#container").css("left",( ($(window).width()-$("div#container").width()) > 0 ? ($(window).width()-$("div#container").width())/2 : 0));
}

function toggle_image(){
	$("div.slide_show div.slide_in").css("zIndex", 20).fadeOut(
		slide_show_settings.fade_out_time,
		function(){ this.className = "slide_out"; }
	);
	$("div.slide_show div").not(".slide_out").not(".slide_in").rnd().css("zIndex", 10).fadeIn(
		slide_show_settings.fade_in_time,
		function(){ this.className = "slide_in"; }
	);
	$("div.slide_show div.slide_out").each( function(i){ this.className = ""; });	
	setTimeout("toggle_image()", slide_show_settings.fixed_time_to_wait + Math.floor( Math.random() * slide_show_settings.random_time_to_wait ));
};

function create_slide_show(){
	$("div#people div.links a").each( function(i){
		var html = "<div class='slider_div' id='a" + this.id + "'>";
			html += "<div class='face'><img src='images/" + this.href.slice(this.href.lastIndexOf('/'), this.href.lastIndexOf('.')) + "_p.jpg' /></div>";
			html += "<div class='info'><div class='name'>" + $(this).html().slice(0,$(this).html().indexOf(' ')).toUpperCase() + "<br/>" + $(this).html().slice($(this).html().indexOf(' ')).toUpperCase() + "</div>" + this.title + "</div>";
		html += "</div>";
		var div = $(html);
		div.hide();
		div.css('visibility','hidden');
		$("div#slider").append(div); 
	});
	for(var i=0; i<4; i++){
		$("div.slider_div").not("[name='sliding']").rnd().css({ left: (i * slider_width) }).show().css('visibility','visible').get(0).name = 'sliding';
	}
	is_sliding = true;
	slide();	
}
function slide(){
	if(is_sliding){
		div_to_remove = false;
		last_div = 0;
		$("div.slider_div[name='sliding']").each( function(i){
			$(this).move_by(-sliding_people_settings.sliding_step);
			if(parseInt($(this).css('left')) > last_div) last_div = parseInt($(this).css('left'));
			if(parseInt($(this).css('left')) < -slider_width) div_to_remove = $(this);
		});	
		if(div_to_remove){
			div_to_remove.hide().css('visibility','hidden').get(0).name = '';
			$("div.slider_div").not("[name='sliding']").rnd().css({ left: (last_div + slider_width) }).show().css('visibility','visible').get(0).name = 'sliding';
		}
	}
	setTimeout("slide()", 1000 / sliding_people_settings.frames_per_second);
}
function open_bildspel(url){
	win = window.open(url, "", "width=517,height=435")	
	win.focus();
}
function open_rollups(url){
	win = window.open(url, "", "width=440,height=600")	
	win.focus();
}

function open_film(url){
	win = window.open(url, "", "width=522,height=406")	
	win.focus();
}
function open_film2(url) {
    win = window.open(url, "", "width=500,height=420")
    win.focus();
}

