window.onresize = function(){
	if (!$.browser.msie) {
		var url = location.href;
		var urlWhithoutHash = url.split("#");
		location = urlWhithoutHash[0];
	} else {
	//	window.location.reload();
	}
}

$(document).ready(function(){
	
	// Set tooltips to only become active when clicked
	$.fn.qtip.inactiveEvents = ["click"];

	if ($.browser.msie && parseInt($.browser.version) == 6) {
		// do nothing
	} else {
		$('.draggable img').qtip({
		  content: 'you can drag me - or click me twice for more information',
		  position: {
			 my: 'top left',
			 target: 'mouse',
			 viewport: $(window), // Keep it on-screen at all times if possible
			 adjust: {
				x: 10,  y: 10
			 }
		  },
		  hide: {
			 fixed: true // Helps to prevent the tooltip from hiding ocassionally when tracking!
		  },
		  style: {
			 classes: 'ui-tooltip-tipsy ui-tooltip-shadow'
		  }
	   });
	}
		
	$('area').hover(function() {
	 $(this).css('cursor','pointer');
	 }, function() {
	 $(this).css('cursor','auto');
	});
	
	var myzindex = 8;
	$('.draggable').draggable({
		cursor: 'move',
		start: function(event, ui) { 
			$(this).css("z-index", myzindex++); 
		}
	});
	
	$(".draggable img").dblclick( function () { 
		// get the current MapID
		var mapID = $(this).attr('usemap');
		var url = $(""+mapID+" area").attr('href'); 
		var color = $(""+mapID+" area").attr('rel');
		$.fancybox({
			'titlePosition'	: 'inside',
			'width'			: 630,
			'height'		: 540,
			'href'			: url,
			'overlayColor'  : color,
			'type' 			: 'iframe'
		});
	});
	
	 $('.polaroidContainer div.polaroid').click(function() {
		$(this).addClass('top').removeClass('bottom');
		$(this).siblings().removeClass('polaroidtop').addClass('polaroidbottom');
		$(this).css("z-index", myzindex++);

	});
		
	$(".polaroidContainer .polaroid img").each( function() {
		var numLow = -30;
		var numHigh = 30;
		var containerWidth = $('.contentContainer').width()-$(this).width();
		var containerheight = $('.contentContainer').height()-$(this).height();
		var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
		var numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
		var tLeft = Math.floor(Math.random()*containerWidth), tTop  = Math.floor(Math.random()*containerheight);

	  	$(this).css( {   
			'-webkit-transform': 'rotate('+numRand+'deg)',
			'-moz-transform': 'rotate('+numRand+'deg)' ,
			'-o-transform': 'rotate('+numRand+'deg)' ,
			'-ms-transform': 'rotate('+numRand+'deg)' ,
			position:'absolute', 
			left: tLeft+100+'px', 
			top: tTop+70+'px',
			visibility: 'visible'
	 	});  
	});

	

	$('map > area.fancybox').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		var title = $(this).attr('title');
		var color = $(this).attr('rel');
		
		$.fancybox({
			'title'			: title,
			'width'			: 630,
			'height'		: 540,
			'scrolling'		: 'auto',
			'titlePosition'	: 'inside',
			'href' 			: url,
			'overlayColor'  : color,
			'type' 			: 'iframe'
		});
	});


	$("a.fancybox").fancybox({
		'titlePosition'	: 'inside',
		'width'			: 655,
		'height'		: 540,
		'type' 			: 'iframe'
	});
	
	$("a.fancyboxnews").fancybox({
		'titlePosition'	: 'inside',
		'width'			: 655,
		'height'		: 540,
		'autoDimensions': true,
		'scrolling'		: 'auto',
		'autoScale'		: true,
		'type' 			: 'iframe'
	});
	
	$("a[rel=fancy_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
		
});

function verschlHref(s) {	
	var n=0;
	var r="";
	for(var i=0; i < s.length; i++) {
		n=s.charCodeAt(i);
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(1));
	}
	return r;
}
	
function verschl(s) {
	location.href=verschlHref(s);
}

function initFreiburgHeight(){

	if ($("#contentFreiburg .meineHoehe").height()+20 < $("#Freiburg .andyHinkel img").height() ) {
		$("#contentFreiburgContainer").css({
			minHeight : 260,
			height: ($("#contentFreiburg .meineHoehe").height() + 80)
		});
	} else {
		$("#contentFreiburgContainer").css({
			height: ($("#Freiburg .andyHinkel img").height() - 20)
		});
	}
}

$(window).load( function() {
	$("#contentFreiburgContainer").css({
		marginLeft: ($("#Freiburg .andyHinkel img").width()),
		width: ($(".contentContainer.freiburg").width() - 240 - $("#Freiburg .andyHinkel img").width() ),
	});
	$("#contentFreiburg").css({
		height: ($("#Freiburg .andyHinkel img").height() - 80),
	});
	
	initFreiburgHeight();
	//Scrollpane
	$(function() {
		var api = $('.scroll-pane').jScrollPane(
			{
				showArrows:true,
				maintainPosition: false
			}
		).data('jsp');
		
		$('#naviFreiburg li.clickable a').bind(
			'click',
			function()
			{
				var toLoad = $(this).attr('href');
				$('#naviFreiburg li.clickable a').removeClass("active");
				$(this).addClass("active");
				api.getContentPane().load(
					toLoad,
					function()
					{
						// Höhe berechnen
						initFreiburgHeight();
						// Scrollpane erneut aufrufen
						api.reinitialise();
						// Fancybox erneut aufrufen
						$("a.fancyboxnews").fancybox({
							'titlePosition'	: 'inside',
							'width'			: 655,
							'height'		: 540,
							'autoDimensions': false,
							'scrolling'		: 'auto',
							'autoScale'		: true,
							'type' 			: 'iframe'
						});
					}
				);
				return false;
			}
		);
	});
	
	$('#contentFreiburgContainer').css({
		"visibility" : "visible",
	});

});
