
 jQuery.noConflict();
 jQuery(document).ready(function(j) {
 
	function generateEvents(tabla){
	 if(tabla == null){return ;}
		j('.date_has_event',tabla).each(function () {
			// options
			var distance = 10;
			var time = 150;
			var hideDelay = 100;
		
			var hideDelayTimer = null;
		
			// tracker
			var beingShown = false;
			var shown = false;
		
			var trigger = j(this);
			var popup = j('.events ul', this).css('opacity', 0);		
		
			// set the mouseover and mouseout on both element
			j([trigger.get(0), popup.get(0)]).mouseover(function () {
				// stops the hide event if we move from the trigger to the popup element
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
		
				// don't trigger the animation again if we're being shown, or already visible
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
		
					// reset position of popup box
					popup.css({
						bottom: 20,
						left: -76,
						display: 'block' // brings the popup back in to view
					})
		
					// (we're using chaining on the popup) now animate it's opacity and position
					.animate({
						bottom: '+=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() {
						// once the animation is complete, set the tracker variables
						beingShown = false;
						shown = true;
					});
				}
			}).mouseout(function () {
				// reset the timer if we get fired again - avoids double animations
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
		
				// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					popup.animate({
						bottom: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						// once the animate is complete, set the tracker variables
						shown = false;
						// hide the popup entirely after the effect (opacity alone doesn't do the job)
						popup.css('display', 'none');
					});
				}, hideDelay);
			});
		});
	}
	
	
	
	function setCalendari(mes, ano){
			j.ajax({
				url: "modules/mod_calendar/function.php",
				type: "POST",
				async: false,
				data: "action=aCalendari&mes="+mes+"&ano="+ano,
				success: function(response){
					j("#eCalendar").html("").append(response)
						.hide().fadeIn(1000)
						.append('<div id="ant" style="color: white;position: absolute;top:150px;" > << </div>')
						.append('<div id="sig" style="color: white;position: absolute;top:150px;left: 665px;" > >> </div>');
						
					j("#sig").css("cursor", "pointer");
					j("#ant").css("cursor", "pointer");
					
					j("#sig").unbind("click");
					j("#ant").unbind("click");
					
					j("#sig").click(function(){
						j("#eCalendar").animate({'left': '699px'},1000,function(){
							j("#eCalendar").css('left','0px');
							setCalendari(mes+1,ano);
						});
					});
					
					j("#ant").click(function(){
						j("#eCalendar").animate({'left': '-699px'},1000,function(){
							j("#eCalendar").css('left','0px');
							setCalendari(mes-1,ano);
						});
					});
						
					j("#eCalendar").dialog({autoOpen: false, modal: true, height: 300, width: 700, resizable: false, title: "Calendari: " + mes + " / " + ano});
					j("td","#eCalendar").css('backgroundColor','black');
					j("#eCalendar").css('backgroundColor', 'black');
					j("#eCalendar .calendar").css("margin","auto");
					j("#eCalendar").dialog("open");
				}
			});
			generateEvents(j(".calendar","#eCalendar"));
			j(".events ul", "#eCalendar").css("width", '200px');
			j(".events ul span.title", "#eCalendar").css({'fontSize':'10px'});
			j(".events ul span.desc", "#eCalendar").css({'fontSize':'10px'});
	}
	
	generateEvents(j(".calendar"));
	
	j("#toplogo").click(function(event){
		var date = new Date();
		setCalendari(date.getMonth()+1,date.getFullYear());
	}).css("cursor","pointer");
	
	j("#toplogoca").click(function(event){
		var date = new Date();
		setCalendari(date.getMonth()+1,date.getFullYear());
	}).css("cursor","pointer");
	
	j(".link").button().click(function(event){
		link = j(this);
		j.ajax({
			url: "modules/mod_calendar/eEvent.php",
			data: "action=detall&id="+link.attr("id"),
			type: "POST",
			async: false,
			success: function(response){
				if(response.indexOf("Error") == -1){
					j("#eDialog").html("").append(response);
					j("#eDialog").dialog({autoOpen: false, modal: true, height: 500, width: 500,title: "Detall"});
					j("#eDialog").dialog("open");
				}else{
					alert("Error");
				}
			},
			error: function(response){
				alert("Error enviando ajax"+response.response);
			}
		});
		event.preventDefault();
	});
});
