window.addEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};
window.removeEvent=function(e,ev,f){if(e.removeEventListener){e.removeEventListener(ev,f,false);}else if(e.detachEvent){e.detachEvent("on"+ev,e[ev+f]);e[ev+f]=null;e["e"+ev+f]=null;}else{e['on'+ev]=null;}};

function flash_obj (id)
{
	var obj;
	if (navigator.appName.indexOf("Microsoft") != -1) {
		obj = eval('window.' + id);
	}
	else {
		obj = eval('window.document.' + id);
	}
	return obj;
}

var Carrousel = {
	items_width: 0,
	carrousel_width: 0,
	multiplier:0,
	items: new Array(),
	
	initialize : function() {
		Carrousel.carrousel_width = jQuery('#footercontent').width();
		Carrousel.items_width = jQuery('#carrousel').width();
		Carrousel.multiplier = Math.ceil(Carrousel.carrousel_width / Carrousel.items_width);
		Carrousel.items[0] = jQuery('#carrousel');
		
		for (var i=1; i<=Carrousel.multiplier; i++) {
			jQuery('#carrousel').clone().attr('id', 'carrousel_'+i).appendTo('#footercontent');
			Carrousel.items[i] = jQuery('#carrousel_'+i);
		}
	}
}

function initialize ()
{
	// Replace h1-tags (sIFR)
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/flash/sifr.swf", sColor:"#cccc99",  sBgColor:"#000000", sCase:"upper", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left", sWmode:"transparent"}));
	};
	
	// Add the music player to the page
	var mp = new SWFObject("/flash/xspf_player_slim.swf", "fl_mp", "170", "15", "8", "#000000");
	mp.addVariable("playlist_url", "/flash/playlist.xspf");
	mp.addVariable("autoload", "true");
	mp.addVariable("autoplay", "true");
	mp.addVariable("repeat_playlist", "true");
	mp.addVariable("repeat_one", "true");
	mp.addParam("wmode", "transparent");
	mp.write("musicplayer");
	
	replace_forms();
}

function replace_forms ()
{
	jQuery('form').submit(function (){
		var form = jQuery(this);
		var values = new Object();
		
		jQuery('input', form).each(function (j, input){
			input = jQuery(input);
			if (input.attr('type')=='checkbox' && input.attr('checked')) {
				values[input.attr('name')] = input.val();
			}
			else if (input.attr('type')=='radio' && input.attr('checked')) {
				values[input.attr('name')] = input.val();
			}
			else if (input.attr('type')!='radio' && input.attr('type')!='checkbox') {
				values[input.attr('name')] = input.val();
			}
		});
		
		jQuery('textarea', form).each(function (k, ta){
			ta = jQuery(ta);
			values[ta.attr('name')] = ta.val();
		});
		
		jQuery('select', form).each(function(l, sel){
			var selectedIndex = sel.selectedIndex;
			var opts = sel.options;
			sel = jQuery(sel);
			values[sel.attr('name')] = opts[selectedIndex != -1 ? selectedIndex : 0].value;
		});
		
		var d = new Date();
		jQuery.post(form.attr('action') + '?time=' + d.getTime(), values, function (data){
			jQuery('#content').html(jQuery('#content', data));
			
			// Replace h1-tags (sIFR)
			if(typeof sIFR == "function"){
				sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/flash/sifr.swf", sColor:"#cccc99",  sBgColor:"#000000", sCase:"upper", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left", sWmode:"transparent"}));
			};
			
			jQuery("#content a[target!=_blank][target!=_self]").bind("click", function() {
				load_page(this.href);
				return false;
			});
			
			replace_forms();
		});
		return false;
	});
}

var previous_page = false;
var loading_page = false;
function load_page (page)
{
	loading_page = page;
	jQuery("#content").load(page + " #content", function(responseText, textStatus, XMLHttpRequest) {
		if (textStatus!='error') {
			// Replace h1-tags (sIFR)
			if(typeof sIFR == "function"){
				sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/flash/sifr.swf", sColor:"#cccc99",  sBgColor:"#000000", sCase:"upper", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left", sWmode:"transparent"}));
			};
			
			replace_forms();
			
			// Rewrite urls in the content so they would load dynamically using AJAX
			jQuery("#content a[target!=_blank][target!=_self]").bind("click", function() {
				load_page(this.href);
				return false;
			});
			
			if (loading_page.substring(0,6)  == '/album' || loading_page.substr(0,28) == 'http://www.overtime.nl/album' || loading_page.substr(0,24) == 'http://overtime.nl/album') {
				previous_page = loading_page;
				var sa = new SWFObject("/flash/imagerotator.swf", "fl_sa", "600", "180", "8", "#000000");
				sa.addVariable("width", "600");
				sa.addVariable("height", "400");
				sa.addVariable("file", "/pictures/playlist.xml");
				sa.addVariable("backcolor", "0x000000");
				sa.addVariable("frontcolor", "0xFFFFFF");
				sa.addVariable("lightcolor", "0xEEEEEE");
				sa.addVariable("screencolor", "0x000000");
				sa.addVariable("overstretch", "true");
				sa.addVariable("showicons", "false");
				sa.addVariable("shownavigation", "false");
				sa.addVariable("repeat", "true");
				sa.addVariable("transition", "blocks");
				sa.addVariable("kenburns", "true");
				sa.addVariable("shuffle", "false");
				sa.addVariable("rotatetime", "7");
				sa.addParam("wmode", "transparent");
				sa.write("last-album");
				
				setTimeout(grow_header, 1000);
				
				$('a[@rel*=lightbox]').lightbox();
			}
			else if (flash_obj('fl_sa')) {
				previous_page = false;
				slink_header();
			}
		}
	});
}

function grow_header ()
{
	if (loading_page.substring(0,6)  == '/album' || loading_page.substr(0,28) == 'http://www.overtime.nl/album' || loading_page.substr(0,24) == 'http://overtime.nl/album') {
		$('#wrapper').animate({paddingTop: 530}, 2000);
		$('#header').animate({height: 525}, 2000);
		$('#last-album').animate({height: 400}, 2000);
		$('#musicplayer').animate({top: 542}, 2000);
		$('#fl_sa').animate({height: 400}, 2000);
	}
}

function slink_header ()
{
	$('#wrapper').animate({paddingTop: 310}, 2000);
	$('#header').animate({height: 305}, 2000);
	$('#last-album').animate({height: 180}, 2000);
	$('#musicplayer').animate({top: 322}, 2000);
	$('#fl_sa').animate({height: 180}, 2000);
	setTimeout(replace_rotator, 2000);
}

function replace_rotator ()
{
	ss = new SWFObject("/flash/imagerotator.swf", "fl_ss", "600", "180", "8", "#000000");
	ss.addVariable("width", "600");
	ss.addVariable("height", "180");
	ss.addVariable("file", "/pictures/header/playlist.xml");
	ss.addVariable("backcolor", "0x000000");
	ss.addVariable("frontcolor", "0xFFFFFF");
	ss.addVariable("lightcolor", "0xEEEEEE");
	ss.addVariable("screencolor", "0x000000");
	ss.addVariable("overstretch", "true");
	ss.addVariable("showicons", "false");
	ss.addVariable("shownavigation", "false");
	ss.addVariable("repeat", "true");
	ss.addVariable("transition", "blocks");
	ss.addVariable("rotatetime", "10");
	ss.addParam("wmode", "transparent");
	ss.write("last-album");
}

//window.addEvent(window, 'load', Carrousel.initialize, false);
window.addEvent(window, 'load', initialize, false);