var hash = window.location.hash.replace("#", "");
var current_id = 0;
var cal_wait = 0;
var cal_change = 0;
var cal_old = 0;
                   


var mouse_on = false;

function highlightSwitch() {
 	if (mouse_on == true) return false;
 	var active = $('#highlights div.high_item.active');

 	if ( active.length == 0 ) $active = $('#highlights div.high_item:last');

 	var next = active.next("#highlights div.high_item").length ? active.next("#highlights div.high_item")
 	: $('#highlights div.high_item:first');

 	active.animate({opacity: 0.0}, 2000, function() {
 	active.removeClass('active');
	});
 	next.animate({opacity: 0.0}, 0).addClass('active');
 	next.animate({opacity: 1.0}, 2000, function() {
 	$(this).addClass('active');
	});
}

$.fn.rotator = function(content_type, loop){
	if ($(content_type, $(this)).length < 2) return;
	var main_container = this;
	if (loop == undefined) loop = true;
	$("*:not("+content_type+")", this).remove();
	if ($(content_type + ".active", this).length == 0) $(content_type + ":first", this).addClass("active");
	$(content_type, this).addClass("slideready");
    
	var interval = setInterval(
		function () {            
		 var active = $(content_type + '.active', main_container);
		 if ( active.length == 0 ) active = $(content_type+':last', main_container);

		 var next = active.next(content_type, main_container).length ? active.next(content_type, main_container)
		 : $(content_type + ':first', main_container);
		 active.animate({opacity: 0.0}, 2000, function() {
		 active.removeClass('active');
			});
		 next.animate({opacity: 0.0}, 0).addClass('active');
		 next.animate({opacity: 1.0}, 2000, function() {
		 $(this).addClass('active');
			});
		}                                     
	, 5000);
};

function show_bio() {               
	var links = $("div#event div.event_content a:not(.share, .ical)");
	links.attr("rel", "#overlay");
	
	var overlay_container = $("<div/>");
	overlay_container
		.attr("id", "overlay")
		.append("<div class='content_overlay'/>");

	overlay_container.prependTo("body");
	links.overlay({
		// 	
		expose: '#292418',
		// effect: 'apple',
	
		onBeforeLoad: function() {	
			var wrap = this.getContent().find(".content_overlay");   
			wrap.load(this.getTrigger().attr("href") + " #bio");
	  
		}
	});
	
}

function show_video() { 
	var links = $("a.youtube");
	links.attr("rel", "#video_overlay");
	
	var overlay_container = $("<div/>");
	overlay_container
		.attr("id", "video_overlay")
		.append("<div class='content_overlay'/>");

	overlay_container.prependTo("body");
	links.overlay({
		// 	
		expose: '#292418',
		// effect: 'apple',
	
		onBeforeLoad: function() {	
			var wrap = this.getContent().find(".content_overlay");
			$(".videoframe").remove();  
			wrap.append("<iframe class='videoframe' src='"+ this.getTrigger().attr("href") + "'>");
	 
		}
	});
	
}


$(function() {
	// add the hash to languagelinks  
	
	if (window.location.hash != "") {
		$("div#lngselect a").each(function() {
			var index = $(this).attr('href').indexOf('#');
			if (index != '-1')
				$(this).attr('href', $(this).attr('href').substr(0, index) + window.location.hash);
			else
				$(this).attr('href', $(this).attr('href') + window.location.hash);
			
		});
	}

    // Eventpage ajax stuff
	if ($("div.singlevent").length > 0) {   
		
		$.fn.eventExpand = function(speed, f_callback) {
				var href = $("a.event", this).attr("href");
				var event_container = $("<div/>");
				var selector = $(this);
				var remove = $("div.event_container:first");

				if (f_callback == undefined) { f_callback = function() {return false}; }
				
				event_container
					.addClass("event_container")   
					.appendTo(selector)
					.load(href + " #event", function () {                                            
						var height = event_container.height();
						$("a.event", selector).fadeOut(speed, function() { 
							event_container.fadeIn(speed);
							if (remove != undefined) remove.eventFold();
							$("html, body").animate({ scrollTop: $(event_container).offset().top },500); 
							show_bio();
						});                         
						f_callback();
					});              

				return event_container;
		};

		$.fn.eventFold = function(f_callback) {
			if (f_callback == undefined) { f_callback = function() {return false}; }

			$(this).fadeOut("slow", function() {
				var a = $("a:first", $(this).parent());
				var padding = (a.css("padding-top") == undefined)?0:a.css("padding-top").replace("px", "");
				// $(this).parent().height(parseInt(a.height()) + parseInt(padding)*2);
				a.fadeIn("fast");
				$(this).remove();
				f_callback();
			});			
		};


		var expand_callback = function() {$('div#t'+hash+' div.event_image').rotator('img');};
		if (hash != "") $("div#t"+hash+".singlevent").eventExpand(0, expand_callback);
		// else if ($("div#t1900.singlevent").length > 0) {
		// 	$("div#t1900.singlevent").eventExpand(0, expand_callback);
		// 	window.location.hash = "#1900";
		// 	hash = window.location.hash.replace("#", "");
		// }
		// else {  
		// 	$("div.singlevent:first").eventExpand(0, expand_callback);
		// 	window.location.hash = "#"+ $("div.singlevent:first").attr("id").replace("t", "");
		// 	hash = window.location.hash.replace("#", "");
		// }

		setInterval(function() {   

			var newhash = window.location.hash.replace("#", "");
			if (newhash != hash) {
				// add the hash to languagelinks
				$("div#lngselect a").each(function() {
					var index = $(this).attr('href').indexOf('#');    
					if (index != '-1')
						$(this).attr('href', $(this).attr('href').substr(0, index) + window.location.hash);
					else
						$(this).attr('href', $(this).attr('href') + window.location.hash);

				});
				hash = newhash;
				if ($("div#t"+hash+".singlevent").length > 0) {
					$("div#t"+hash+".singlevent").eventExpand("slow", function() {
						$("img.loader").hide("fast");       
		   				$('div#t'+hash+' div.event_image').rotator('img');					
					}); 									
				}
			}
		}, 300);	 

		$("div.singlevent > a", "#page").bind("click", function() { 
			$("img.loader").hide("fast");
			$("img.loader", this).show();
			window.location.hash = "#" + $(this).parent().attr("id").replace("t", "");
			return false;
		});
		
	}	
	  
	 

	// Sitemap stuff
	var sitemaplinktext = $("#showsitemap").text();
	var sitemapnewlinktext = $("#showsitemap").attr("rel");
	$("#sitemap").css("opacity", "0")
	$("#showsitemap").toggle(function(){
		$(this).text(sitemapnewlinktext);
		// $("#sitemap").fadeIn("slow");
		$("#sitemap").css("display", "block").animate({opacity: 0.92},1000);
		}, function() {
		$(this).text(sitemaplinktext);
		// $("#sitemap").fadeOut("slow");
		$("#sitemap").animate({opacity: 0},1000, function () {
			 $("#sitemap").css("display", "none")
		});
		
	});
	
	// Category thumbs
	$(".categorythumb").click(function(){
		var category_link = $(this).find("a").attr('href');
	        window.location = category_link;
	});
	
	// Android
	
	if( navigator.userAgent.match(/Android/i) ) {
		$("#android").css("display", "block");
		$("#android a.close").click(function(){
			$("#android").css("display", "none");
		});
	}
	


	// Highlights
	
	$("#highlights div").addClass("slideReady");
 	$("#highlights div:first").addClass("active");
 	setInterval("highlightSwitch()", 8000 );
 	$("#highlights").mouseover(function() {
 	mouse_on = true;
 	}).mouseout(function () {
 	mouse_on = false;
	});

	// Preload Css Images
	$.preloadCssImages();    

	// calendar
	
	active_day = $('div#datenav').attr('class');
	$("#calendar").find('a[rel="'+ active_day + '"]').addClass("active");
	$('#minicalendar a[href$='+active_day+']').addClass("active");
	
	//alternating lines table
	$("#page table tr:even td").addClass("alt");
	
	// Rotate images in single events.
	// $('div.event_image').rotator('img');
	
	
	// youtube
	
	show_video();
	
	// ajax
    $("#newspages a").live("click", function() {
		var href = $(this).attr("href");
		var content = $("#sidebar");
		$("#newslist").fadeOut();		
		content.load(href + " #sidebar", function () {
			$("#newslist").hide();
			$("#newslist").fadeIn();		
		});

		return false;
	});
		    
	
	// select #flowplanes and make it scrollable. use circular and navigator plugins 
    $("#flowpanes").scrollable({size: 1, clickable: false}).navigator({ 
 
        // select #flowtabs to be used as navigator 
        navi: "#flowtabs"

	   });
	
	var max_height = 0;
	$('#flowpanes div').each(function() {
	max_height = max_height > $(this).height() ? max_height : $(this).height();
	});
	$('#flowpanes').height(max_height);

	//share
	
	$('a.share').live('click',function(e) {
		var link = $(this).attr('href');
		var image = $('.event_image img:first').attr('src');
		$('a.share').attr('href', link + image );
		// alert($('a.share').attr('href'));
	})

});

