// no conflicting with mooTools please
jQuery.noConflict();


jQuery(document).ready(function(){
	
	// initially set both drop downs to the current timestamp if exists
	var b = get().toString();
	var temp = new Array();
	temp = b.split('=');
	// get third element
	var str = new String();
	var new_temp = parseInt(temp[2])*1000;
	if(new_temp){
		var myDate = new Date(new_temp);
		var init_year = myDate.getFullYear();
		var init_month = myDate.getMonth();
		if(temp[3] != null){
			jQuery('#filters #month').val(parseInt(init_month+1));						
		}else {
			jQuery('#filters #month').val(parseInt(init_month));			
		}
		jQuery('#filters #year').val(parseInt(init_year));
	} else {
		var myDate = new Date();
		var init_year = myDate.getFullYear();
		var init_month = myDate.getMonth();
				
		jQuery('#filters #month').val(init_month+1);
		jQuery('#filters #year').val(init_year);		
	}
	
	jQuery("#filters #month").change(function(){
		var month = jQuery('#filters #month').val();
		var year = jQuery('#filters #year').val();
		var timestamp = fetch_unix_timestamp(month, year);
		
		//SEG Edit
		if(document.getElementById('cat_id').value > 0)
		window.location = 'http://' + document.domain + '/browse_calendar.php?view=month&date='+timestamp+'&cat_id='+document.getElementById('cat_id').value;
		else
		window.location = 'http://' + document.domain + '/browse_calendar.php?view=month&date='+timestamp;
		
	});
	
	jQuery("#filters #year").change(function(){
		var month = jQuery('#filters #month').val();
		var year = jQuery('#filters #year').val();
		var timestamp = fetch_unix_timestamp(month, year);
		
		//SEG Edit
		if(document.getElementById('cat_id').value > 0)
		window.location = 'http://' + document.domain + '/browse_calendar.php?view=month&date='+timestamp+'&cat_id='+document.getElementById('cat_id').value;
		else
		window.location = 'http://' + document.domain + '/browse_calendar.php?view=month&date='+timestamp;
		
	});
	
	jQuery("#reset-filter").click(function(){
	
		window.location = 'http://' + document.domain + '/browse_calendar.php?view=month';
	});	
});

function get(){
    var url = window.location.href;
    var array = url.indexOf('#') == -1 ?
                url.substring(url.indexOf('?') + 1).split(/&;/):
                url.substring(url.indexOf('?') + 1, url.indexOf('#')).split(/&;/);
		return array;
}

function fetch_unix_timestamp(month, year)
{
	var newYear = parseInt(year);
	var newMonth = parseInt(month);
	
	var realTime = (Date.UTC(newYear, newMonth, 1) * 1000);
	var finalTime = parseInt(realTime.toString().substring(0, 10));

	return finalTime;
}