// Inställningar för tooltip
var ttOffset=[10,10]; // x,y offset från muspekaren
var ttHeight = 260;	// Maximal höjd
var ttWidth = 300; // Bredd

function toolTip(){

	$('body').append('<div id="tooltip"></div>');
	$('#tooltip').css('width',ttWidth);
	$('#tooltip').css('opacity',0.95);

	$('img.trail[title]').each(function() {
        $this = $(this);
        $.data(this, 'title', $this.attr('title'));
        $this.removeAttr('title');
    });

	$('img.trail').mousemove(function(e){
      
		var xcoord=ttOffset[0];
		var ycoord=ttOffset[1];
		
		if ($(window).width() - e.pageX < (ttWidth+25)){
			xcoord = e.pageX - xcoord - ttWidth-10; // Move to the left side of the cursor
		} else {
			xcoord = e.pageX;
		}
		
		if ($(window).height() - e.pageY < ttHeight){
			ycoord += e.pageY - Math.max(0,(ttHeight + e.pageY - $(document).height()));
		} else {
			ycoord += e.pageY;
		}
      
        $("#tooltip").css({
            top: (ycoord + ttOffset[0]) + "px",
            left: (xcoord + ttOffset[1]) + "px"
        });
	
		var orig_text = $(this).data('title').replace(/\[[^\]]*\]/g, '');
		var text_arr = orig_text.split("|");
		
		var title = text_arr[0];
		var description = text_arr[1];
		
		if(description != undefined){
			$('#tooltip').html('<div class="inner"><h3>' + title + '</h3><p>' + description + '</p><div class="clear"></div></div>');		
		} else {
			$('#tooltip').html('<div class="inner"><h3>' + title + '</h3><div class="clear"></div></div>');
		}

		$('#tooltip').show();
		
	}).mouseout(function(e){    
 		
		$('#tooltip').hide();
		
   	});
   
}

function goTo(theHref){
	window.location = theHref;
}

function newWin(){

	$("a.newWin").each(function(){
	
		$(this).attr('target', '_blank');

	});	

}

function slideShow(){

	if($('#top-content-left.inblick').length != 0){
	
		$('#top-content-left.inblick').crossSlide({
		  sleep: 6,
		  fade: 1
		}, [
		  { src: '/images/slideshow/i1.jpg' },
		  { src: '/images/slideshow/i2.jpg' },
		  { src: '/images/slideshow/i3.jpg' },
		  { src: '/images/slideshow/i4.jpg' },
		  { src: '/images/slideshow/i5.jpg' },
		  { src: '/images/slideshow/i6.jpg' },
		  { src: '/images/slideshow/i7.jpg' },
		  { src: '/images/slideshow/i8.jpg' },
		  { src: '/images/slideshow/i9.jpg' },
		  { src: '/images/slideshow/i10.jpg' },
		  { src: '/images/slideshow/i11.jpg' },
		  { src: '/images/slideshow/i12.jpg' },
		  { src: '/images/slideshow/i13.jpg' },
		  { src: '/images/slideshow/i14.jpg' }
		]);
		
	} else if($('#top-content-left.overblick').length != 0){

		$('#top-content-left.overblick').crossSlide({
		  sleep: 6,
		  fade: 1
		}, [
		  { src: '/images/slideshow/s2.jpg' },
		  { src: '/images/slideshow/s3.jpg' },
		  { src: '/images/slideshow/s4.jpg' },
		  { src: '/images/slideshow/s30.jpg' },
		  { src: '/images/slideshow/s6.jpg' },
		  { src: '/images/slideshow/s8.jpg' },
		  { src: '/images/slideshow/s29.jpg' },
		  { src: '/images/slideshow/s26.jpg' },
		  { src: '/images/slideshow/s27.jpg' },
		  { src: '/images/slideshow/s22.jpg' },
		  { src: '/images/slideshow/s21.jpg' },
		  { src: '/images/slideshow/s24.jpg' }
		]);
	
	}

}

function noLoginClick(){

	$("a.nologin").click(function(e){
	
		e.preventDefault();
		
		if($("#nologin").is(":hidden")){
			$("#nologin").show("blind", { direction: "vertical" }, 500);
		} else if($("#nologin").css("opacity") == 1) {
			$("#nologin").show("pulsate", { times:3 }, 500);
		}		

	});
	
}

$(document).ready(function() {

	toolTip();
	newWin();
	slideShow();
	noLoginClick();
	
	$("a.lanktips").click(function(e){
	
		e.preventDefault();
		
		if($("#lanktips").is(':hidden')) {
			$(this).attr('title', 'Göm länktips');
		} else {
			$(this).attr('title','Visa länktips');
		}
		
		$("#lanktips").slideToggle(1000);	

	});	

	$('#tidsmeny a').each(function() {
        $this = $(this);
        $.data(this, 'title', $this.attr('title'));
        $this.removeAttr('title');
    });

	$('#tidsmeny a').mouseenter(function(e){		
		
		var theId = $(this).attr('id');
		var theImg = theId.replace(/[^0-9]/g,'');
		
		$('#sw').attr("src", "/images/design/tidslinjer/line-"+theImg+".gif");

			$('#besktxt').data('rubr', $(this).html());
			$('#besktxt').data('besk', $(this).data('title'));
		
		$('#besktxt').clearQueue().fadeTo('fast',0, function(){		

			$(this).html('<h3>Tidslinje: '+$(this).data('rubr')+'</h3><hr /><p>'+$(this).data('besk')+'</p>');
			
		}).fadeTo('fast',1);		
	
	});
	
	$('a.showInfo').click(function(e){
	
		e.preventDefault();
		
		var no = $(this).attr('id').replace(/[^0-9]/g,'');
		var theChapter = '#chapter-'+no;
		var theImg = '#kap-'+no;
		var theInfo = '#besk-'+no;
		var theIcon = '#info-'+no;
		var delay = 0;
		
		if($(theChapter).is(":visible")){
			$(theChapter).hide('blind', {direction:'vertical'}, 400);
			$(theImg).attr('src','/images/icon/add_24.png');
			$(theImg).attr('title','Visa kapitel');			
			delay = 450;
		}
	
		if($(theInfo).is(":hidden")){
			$(theIcon).attr('title','Göm beskrivning');
		} else {
			$(theIcon).attr('title','Visa beskrivning');
		}	
		
		$(theInfo).delay(delay).toggle('blind', {direction:'vertical'}, 400);				
		
	});
	
	$('a.showChapters').click(function(e){
	
		e.preventDefault();
		
		var no = $(this).attr('id').replace(/[^0-9]/g,'');
		var theInfo = '#besk-'+no;
		var theChapter = '#chapter-'+no;
		var theImg = '#kap-'+no;
		var theIcon = '#info-'+no;
		var delay = 0;
	
		if($(theInfo).is(":visible")){
			$(theInfo).hide('blind', {direction:'vertical'}, 400);
			$(theIcon).attr('title','Visa beskrivning');
			delay = 450;
		}
		
		if($(theChapter).is(":hidden")){
			$(theImg).attr('src','/images/icon/remove_24.png');
			$(theImg).attr('title','Göm kapitel');
		} else {
			$(theImg).attr('src','/images/icon/add_24.png');
			$(theImg).attr('title','Visa kapitel');
		}
		
		$(theChapter).delay(delay).toggle('blind', {direction:'vertical'}, 400);			
		
	});
	
	$('#show_more').click(function(e){
	
		e.preventDefault();

		if($('#news_more').is(":hidden")){
			$('#show_more').html('Visa färre &uarr;');
		} else {
			$('#show_more').html('Visa fler &darr;');
		}

		$('#news_more').toggle('blind', {direction:'vertical'}, 400);
		
	});
			
	$('a.open_bildserie').popupWindow({
		height:640, 
		width:960
	});

	$('a.open_demo').popupWindow({
		height:660, 
		width:990
	});

	$('a.open_doc').popupWindow({
		height:640, 
		width:960
	});
	
	$('a.open_tidslinje').popupWindow({
		height:600, 
		width:955
	});	

	$('a.open_karta').popupWindow({
		height:640, 
		width:960
	});	
	
	$('a.open_kartresa').popupWindow({
		height:600, 
		width:900
	});
	
	$("#loginform").submit(function() {
		
		if ($('#anvandarnamn').val() == '') {
			alert('Du måste ange användarnamn');
			$('#anvandarnamn').focus();
			return false;
		} else {
			return true;
		}
	
	});
	
	$("a.ajax").fancybox({
		'padding'		:	0,
		'margin'		:	0,
		'overlayColor'	:	'#000'
	});	
	
});