var tooltips=[];


var q={
 section:'schedule',
 month:'',
 year:'',
 filter:'',
 watchday:'',
 watchmonth:'',
 watchyear:'', typecontent:'',extended:''};

var mutex=0;
function initScheduleSection(){
	if (!$('toolip'))
		new Insertion.Top(document.body,'<div id="tooltip" style="display:none;z-index:100"><div id="tooltipcontent"></div></div>');
	
	currentParameters=document.URL.match(/^.*?\?(.*)$/)[1];
	currentParameters=currentParameters.replace(/lang=.*?($|&)/,'');
	Behaviour.apply();
}

var myrules = {
		'.Calendar_day_links a': function(el){
			var oldhref=el.href;
			if (oldhref=='javascript:void(0);')
				return;
			el.href='javascript:void(0);';
			var month;
			try{
				month=oldhref.match(/(&|^)month=(.*?)(&|$)/)[2] || q.month;
			}catch(e){
				month=oldhref.match(/(&|^)watchmonth=(.*?)(&|$)/)[2] || q.month;
			}
			var year;
			try{
				year=oldhref.match(/(&|^)year=(.*?)(&|$)/)[2] || q.year;
			}catch (e){
				year=oldhref.match(/(&|^)watchyear=(.*?)(&|$)/)[2] || q.year;
			}
			var watchday=oldhref.match(/(&|^)watchday=(.*?)(&|$)/)[2];
			var typecontent=oldhref.match(/(&|^)typecontent=(.*?)(&|$)/)[2] || q.typecontent;
			el.onmouseover=function(){Element.update('tooltipcontent','');updateTooltip(watchday,year,month,typecontent)};
			var tooltip=new Tooltip(el, 'tooltip',{delta_x:8,delta_y:-128});
			tooltips[tooltips.length]=tooltip;
			el.onclick=function(){
				updateDetailsDay(month,year,watchday,typecontent);
			}
		},
		'a.Calendar_menu_fwdmonth':moveCalendarData,
		'a.Calendar_menu_backmonth':moveCalendarData,
		'.calendar_events_back a':function(el){
			var oldhref=el.href;
			if (oldhref=='javascript:void(0);')
				return;
			el.href='javascript:void(0);';
			el.onclick=function(){
				updateDetailsDay('','','','');
			}
		},
		'a.filter':function(el){
			var oldhref=el.href;
			if (oldhref=='javascript:void(0);')
				return;
			el.href='javascript:void(0);';
			
			var filter='';
			try{
				filter=oldhref.match(/(&|^)filter=(.*?)(&|$)/)[2];
			}catch(e){}
			el.onclick=function(){
				mutex=1;
				updateCalendarData(null, null, filter);
				updateDetailsDay(null, null, null, null);
				$$('a.filter').each(function(f){
					Element.removeClassName(f,'seleccionado');
				});
				Element.addClassName(this,'seleccionado');
			}
		},
		'div.despliegaboton a':function(el){
			el.href='javascript:void(0);';
			var parts=el.parentNode.id.split('_');
			var idNewDiv='extended_'+parts[1]+'_'+parts[2];
			el.onclick=function(){
				if ($(idNewDiv)!=null){
					Effect.BlindUp(idNewDiv,{afterFinish:function(){$(idNewDiv).style.display='none';Element.remove(idNewDiv), Element.update(el,'+')}});
				}else{
					new Insertion.After(el.parentNode,'<div style="display:none"  class="extendedinfo" id="'+idNewDiv+'"></div>');
					new Ajax.Updater(idNewDiv,'ajaxdata.php',{
						method:'get',
						parameters:{agenda_details:'',type:parts[1],id:parts[2]},
						onComplete:function(transport){
							Effect.BlindDown(idNewDiv,{afterFinish:function(){Element.update(el,'-')}});
						}
							
					})
				}
			}
			

		}
		
	};


function moveCalendarData(el){
	var oldhref=el.href;
	if (oldhref=='javascript:void(0);')
		return;
	el.href='javascript:void(0);';
	var month=oldhref.match(/(&|^)month=(.*?)(&|$)/)[2];
	var year=oldhref.match(/(&|^)year=(.*?)(&|$)/)[2];
	el.onclick=function(){
		updateCalendarData(month,year);
	}
}

function updateLangButton(){
	var lang=$('swaplang_action').href.match(/(&|^)(lang=.*?)(&|$)/)[2];
	var href='?';
	

	$H(q).each(function(e){

		href+=e.key+'='+e.value+'&';
	});
	href+=lang;
	$('swaplang_action').href=href;
}

function updateTooltip(iday,iyear,imonth,itype){
	new Ajax.Updater('tooltipcontent','ajaxdata.php',{
		method:'get',
		parameters:{tooltip_schedule_day:'',day:iday,month:imonth,year:iyear,type:itype}
	})
}

function updateCalendarData(month, year, filter){
	q.month=month || q.month;
	q.year=year || q.year;
	q.filter=(filter=='')?'': filter || q.filter;
	var qtemp=Object.clone(q);
	qtemp.update_calendar_data=1;
	destroyTooltips();
	Effect.Fade('schedule_calendar',{to:0.5,duration:0.1, afterFinish:function(){
		new Ajax.Updater('schedule_calendar','ajaxdata.php',{
		method:'get',
		parameters:qtemp,
		onComplete:function(req){
			if(mutex>0){
				mutex--;
			}else{
				Behaviour.apply();
				updateLangButton();
			}
			Effect.Appear('schedule_calendar',{from:0.5,duration:0.1});
		}
	});
	}});
	
}

function updateDetailsDay(month,year,day,typecontent){
	q.watchmonth=(month=='') ? '': month || q.watchmonth;
	q.watchyear=(year=='') ? '': year || q.watchyear;
	q.watchday=(day=='') ? '': day || q.watchday;
	q.typecontent=(typecontent=='') ? '': typecontent || q.typecontent;
	var qtemp=Object.clone(q);
	qtemp.update_calendar_details=1;
	Effect.BlindUp('schedule_details',{duration:0.5, afterFinish:function(){
		new Ajax.Updater('schedule_details','ajaxdata.php',{
		method:'get',
		parameters:qtemp,
		onComplete:function(req){
			if(mutex>0){
				mutex--;
			}else{
				Behaviour.apply();
				updateLangButton();
			}
			Effect.BlindDown('schedule_details',{duration:0.5});
		}
	});
	}});
}

function destroyTooltips(){
	$A(tooltips).each(function(e){
		e.destroy();
	})
	tooltips=[];
}
Behaviour.register(myrules);


