window.addEvent('domready', function() {	
   	update_date_label();
   	$$('input[type=radio]').each(function(el) {
   		el.addEvent('click', function() {
   			if(el.checked) {
   				add_get_filter(el.getProperty('id'));
   			}
   		});
   	});

   	$('film_series_select').addEvent('change', function() {
   		add_get_filter($('film_series_select').getProperty('value'));
   	});
	
	setup_sliders();
});

function toggle(el) {
	if(el.innerHTML == 'close') {
		el.innerHTML = 'open';
		el.style.background = 'transparent url(/images/films-film-calendar-more.gif) no-repeat scroll right 7px';
	}
	else {
		el.innerHTML = 'close';
		el.style.background = 'transparent url(/images/films-film-calendar-close.gif) no-repeat scroll right 7px';
	}
}

function setup_sliders() {
	var sliders = {};
	
	$$('a[id^=film_description_toggle-]').each(function(el) {
		var id = el.getProperty('id').split('-')[1];
		sliders['toggle-'+id] = new Fx.Slide($('film_description-'+id), {
			duration: 500,
			onComplete: function() { toggle(el); }
			});
		
		if(el.getProperty('lang') != 'start_open') {
			sliders['toggle-'+id].hide();
			el.innerHTML = 'open';
			el.style.background = 'transparent url(/images/films-film-calendar-more.gif) no-repeat scroll right 7px';
		}
		
		el.addEvent('click', function() {
			sliders['toggle-'+id].toggle();
		});
	});
}

function add_get_filter(filter) {	
	//check to see if there are already GET params
	if(!window.location.href.match('\\?')) {
		//if theres none, just add the filter
		window.location.href = '?filter='+filter;
	}
	else {
		//get all the get params and rebuild the url
		var params = window.location.href.substring(window.location.href.indexOf('?')+1).split('&');
		var new_get = '?';
		for(var i = 0; i < params.length; i++) {
			var pieces = params[i].split('=');
			if(pieces[0] != 'filter')
				new_get += pieces[0]+'='+pieces[1]+'&';
		}
		new_get += 'filter='+filter;
		window.location.href = window.location.href.substr(0, window.location.href.indexOf('?'))+new_get;
	}
}



var months = [];
months[0] = 'JAN';
months[1] = 'FEB';
months[2] = 'MAR';
months[3] = 'APR';
months[4] = 'MAY';
months[5] = 'JUN';
months[6] = 'JUL';
months[7] = 'AUG';
months[8] = 'SEP';
months[9] = 'OCT';
months[10] = 'NOV';
months[11] = 'DEC';

function update_date_label(){
	var current_start_date = $('current_start_date').getProperty('value');
	var start_date = new Date();
	var end_date = new Date();
	
	//js does time in millisections..
	start_date.setTime(current_start_date * 1000);
	end_date.setTime(start_date.getTime() + (27 * (86400 * 1000)));
	var label = months[start_date.getMonth()];
	if(start_date.getMonth() != end_date.getMonth())
		label += ' / '+months[end_date.getMonth()];
	
	label += ' '+end_date.getFullYear();
	$('calendar_month_label').innerHTML = label;
}


