/**
	@desc: Calendario del sistema
	@author: Fabian Ramirez <framirez@ayerviernes.com>
	@filename: calendario.js
*/

var ObjetoCalendario = {

	bindearEventos: function() {
		var url_sitio = window.top.location;
		var url_sitio = url_sitio.toString().split("/calendario/");
		url_sitio = url_sitio[0];
		
		//if hash
		if (window.location.hash){
			var id_desc = window.location.hash;
			jQuery(id_desc).addClass('active-hash');
			jQuery(id_desc.replace('info','desc')).toggle();
		}
		
		// Filtramos las categorías
		jQuery("#calendar-container .event-categories ul li").live('click', function() {
			colorFiltro = jQuery(this).children('span').css('background-color');

			jQuery('.bullets .cat-name').hide();
			jQuery('.bullets .cat-name').each(function(i, val) {									
				if(jQuery(val).css('background-color') == colorFiltro)
					jQuery(val).show();
			});

			jQuery('.listaEventos li .cat-name').each(function(i, val) {									
				if(jQuery(val).css('background-color') != colorFiltro) {
					jQuery(val).parents('li').hide();
				} else {
					jQuery(val).parents('li').show();
				}
			});							
			
			jQuery('.event-categories p').text("Ver todos los eventos").css('cursor','pointer').click(function() {
					jQuery('.bullets .cat-name').show();
					jQuery('.listaEventos li .cat-name').each(function(i, val) {		
						jQuery(val).parents('li').show();
					});

					jQuery(this).text('Filtrar por Categorías').css('cursor','text');
					
					return false;
			});
			
			return false;
		});


		// Anterior y siguiente
		jQuery(".mesAnterior, .mesSiguiente").live('click', function() {
			mes = jQuery(this).attr('id');
			jQuery.get(url_sitio+"/wp-content/plugins/eventos/ajax.php?fecha=" + mes + "&sede=" + jQuery("#sede").val(), function(html) {
				
				// Removemos los objetos anteriores
				jQuery('.calendarioEventos tbody td').remove();
				
				jQuery('#calendar-container').replaceWith(html);
				ObjetoCalendario.bindearEventos();
				return false;
			});

			//jQuery("#calendar-conainer").html( );
			return false;
		});

		// Celdas de tabla
		jQuery('.calendarioEventos tbody td').click(function() {

			// Necesito Agregar la clase hoy al hacer click
			jQuery('.hoy').removeClass("hoy");
			jQuery(this).addClass('hoy');

			// Cargamos el día
			jQuery.get(url_sitio+"/wp-content/plugins/eventos/ajax.php?sede=" +  jQuery("#sede").val() + "&dia=" + jQuery(this).attr('id').replace("fecha-",""), function(html){
				jQuery("#calendar-events").html(html);
				return false;
			});
		});

		jQuery('.ver_mas a').live('click', function() {
			div = this.hash.replace("#","");
			if(jQuery("#" + div).css('display') == "none") {
				jQuery(this).text('Cerrar');
				jQuery(this).parents('li').toggleClass('active');
			} else {
				jQuery(this).text('Ver más');
				jQuery(this).parents('li').toggleClass('active');
			}

			jQuery("#" + div).toggle();
			return false;
		});
		
		return false;
	}
	
}				
jQuery(document).ready(function($){	
	//var event_id = window.top.location.search.substring(1).toString();
	//if(event_id.match('info-event')){
	//	var valorDiv = event_id.match('&') ? event_id.split('&info-event-')[1] : event_id.split('info-event-')[1];
	//	jQuery.scrollTo($('#info-event-'+valorDiv),{ duration: 1000 });
	//}
	ObjetoCalendario.bindearEventos();
});
