$(document).ready(function(){

	if($('#slider').length){
		$('#slider').bxSlider({
			auto: true,
			pause: 5000,
			autoHover: true
		});
	}

	
	if($('a[rel=gallery-1]').length){
		$('a[rel=gallery-1]').fancybox({			
            'transitionIn'		: 'none',
            'transitionOut'		: 'none',
            'titlePosition' 	: 'over',
            'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
            }
        });		
	}
	
	if($('a[rel=collection-gallery-1]').length){
		$('.collection-gallery-1, .collection-gallery-2, .collection-gallery-3').fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
	}
	
		
	$('#sidebar-tabbed-content .tab-cal a').live("click", function() { 
		$('#sidebar-tabbed-content .tab-featured').removeClass('active');
		$('#sidebar-tabbed-content .tab-cal').addClass('active');
		$('#sidebar-featured-event').hide();
		$('#sidebar-mini-calendar').show();
		return false;
	});
	
	$('#sidebar-tabbed-content .tab-featured a').live("click", function() { 
		$('#sidebar-tabbed-content .tab-cal').removeClass('active');
		$('#sidebar-tabbed-content .tab-featured').addClass('active');
		$('#sidebar-mini-calendar').hide();
		$('#sidebar-featured-event').show();
		return false;
	});
	
	
	// main calendar date switcher
	if($('#fc_calendar thead tr th a').length){
		$('#fc_calendar thead tr th a').live("click", function() { 
	
			$.ajax({
			   type: "post",
			   url: $(this).attr('href'),
			   cache: false,
			   data: "filter_field="+$('input#filter_field').val(),
			   success: function(data){	   		
					$('#fc_calendar').html(data);
					activateDayEvents();
			   }
			 });
		
			return false; 
		});
	}
	
	
	if($('#event-filter').length){
		$('#event-filter ul li a').live("click", function() {
			
			var eventCatId = $(this).attr('id');
			$('input#filter_field').val(eventCatId);
			$('#event-filter ul li a').removeClass('active');
			$(this).addClass('active');
			
			$('#calendar_desc .desc').hide();
			
			if(eventCatId == 'show_all'){
				$('#fc_calendar .event').show();
				$('#calendar_desc #desc_show_all').show();
			}
			else{
				$('#calendar_desc #desc_'+eventCatId).show();
				$('#fc_calendar .event').hide();
				$('#fc_calendar .'+eventCatId).show();
			}	
		
			activateDayEvents();						

			return false;
		});
		activateDayEvents();
	}	
	
	
	// Open Event in the sidebar featured event tab
	if($('#fc_calendar .eventslist').length){
		$('#fc_calendar .eventslist .event a, #fc_calendar .exhibition-events a.event').live("click", function() {
		
			$('#sidebar-tabbed-content .tab-cal').removeClass('active');
			$('#sidebar-tabbed-content .tab-featured').addClass('active');
			$('#sidebar-mini-calendar').hide();
			$('#sidebar-featured-event').show();
			
			
			$.ajax({
			   type: "post",
			   url: $(this).attr('href'),
			   cache: false,
			   data: "datetime="+$(this).attr('rel'),
			   success: function(data){	   		
					$('#sidebar-featured-event').html(data);	   
			   }
			 });
	
			return false;
		});
		
		$('#fc_calendar .eventslist .event').live("click", function() {
			$('a',this).trigger('click');
		});
		$('#fc_calendar .exhibition-events').live("click", function() {
			$('a.event',this).trigger('click');
		});
	}
	
	
	
	// mini calendar date switcher
	$('#mc_calendar thead th a.icon').live("click", function() { 
		
		if($(this).hasClass('disabled')) return false;
		
		$.ajax({
		   type: "post",
		   url: $(this).attr('href').replace('/month/', '/mini/'),
		   cache: false,
		   success: function(data){	   		
				$('#mc_calendar').html(data);	   
		   }
		});
	
		return false; 
	});
	
	
	// Navigate in the sidebar featured events
	if($('#feat-event-nav').length){
		$('#feat-event-nav ul li a').live("click", function() {
						
			$.ajax({
			   type: "post",
			   url: "/calendar/featured/",
			   cache: false,
			   data: "nav="+$(this).attr('id')+"&entry_id="+$('input#entry_id').val()+"&datetime="+$('input#datetime').val(),
			   success: function(data){	   		
					$('#sidebar-featured-event').html(data);
			   }
			 });
		
			return false; 
		});
	}
	
	
	
	// Navigate in the sidebar mini cal
	if($('#sidebar-mini-calendar').length){
		$('#sidebar-mini-calendar div.mc_date a').live("click", function() {
						
			$.ajax({
			   type: "post",
			   url: "/calendar/featured/",
			   cache: false,
			   data: "nav=date&date="+$(this).attr('id'),
			   success: function(data){	   					   
					$('#sidebar-featured-event').html(data);
					$('#sidebar-mini-calendar').hide();
					$('#sidebar-featured-event').show();
					$('#sidebar-tabbed-content .tab-cal').removeClass('active');
					$('#sidebar-tabbed-content .tab-featured').addClass('active');
			   }
			 });
		
			return false; 
		});
	}
	
	if($('.blog-archives')){
		$('a.link_year').live("click", function() {
			var zeclassname = $(this).attr('id');
			$('.' + zeclassname).toggleClass('month_off');
			$('.' + zeclassname).toggleClass('month_on');
			return false; 		
		});	
	}
	

});



//Activate the class on the days with events
var activateDayEvents = function(){
	var visibleEls = 0;
	$('#fc_calendar td.day_cell:not(.out_of_range)').each(function(index){
		visibleEls = $(this).find(".event:visible:not(a.event)").not("script");
		if(visibleEls.length == 0) $(this).removeClass('has_events');
		else $(this).addClass('has_events');
	});
}


