jQuery(document).ready(function($) {
    //Function to get the date by class or by ID format output is (mmmmmmm dd, YYYY)
    function get_date_byClass($mclass,$mtype) {
        var $month_class = jQuery($mclass).attr($mtype),
            $month_arr = Array (
                'Today',
                'January',
                'February',
                'March',
                'April',
                'May',
                'June',
                'July',
                'August',
                'September',
                'October',
                'November',
                'December' );
        $month_split = $month_class.split('-');
        $year = $month_split[$month_split.length-3];
        $day = $month_split[$month_split.length-1];
        $month_num = $month_split[$month_split.length-2];
        if($month_num.charAt(0) == 0) {$month_num = $month_num.charAt(1);}
        $month = $month_arr[$month_num];
        return ($month+' '+$day+', '+$year);
    }
    
    //Create effect for mini calendar to show the events of the day
    function miniCalendarClickEvent($minievent_elem) {
        var $mini_popup_link = jQuery($minievent_elem),
            $mini_event_elem = jQuery('#mini_calendar_datetime');
        
        $mini_popup_link.each(function() {
            $(this).click(function(a) {
            a.preventDefault();

            var $this_mini_popup_link = $(this),
                $mini_popup_length = $this_mini_popup_link.parents('div.inner').find('div.cal_mini_content').length,
                $mini_popup_html = $this_mini_popup_link.parents('div.inner').find('div.cal_mini_content'),
                $mini_popup_date = get_date_byClass($this_mini_popup_link.parents('td'),'id');
				
                $mini_event_elem.fadeOut('slow', function() {
                    $mini_event_elem.find('div').remove();
                    $cur_mini_event = $mini_event_elem.html();
                    $mini_popup_html.each(function() {
                        $cur_mini_event = $cur_mini_event+$(this).parent('div').html();
                        $mini_event_elem.html($cur_mini_event);
                    });
                    $mini_event_elem.find('h3').html($mini_popup_date);
                    $mini_event_elem.find('div').removeClass('cal_mini_content').addClass('mini_event_wrapper');
                });
				
            $mini_event_elem.fadeIn();
			
            });

        });

        //Set Default event content on right side of mini calendar to event today    
	if(jQuery('#page_calendar_mini table tr td').is('.today') ) {
		if(jQuery('#page_calendar_mini table tr td.today').find('div.inner div').is('.calendar-empty')) {
			//alert('No Event');
			var $today_elem = jQuery('#page_calendar_mini table tr td.today div.cal_mini_content'),
			$today_event_elem = jQuery('#mini_calendar_datetime'),
			$today_date = get_date_byClass('#page_calendar_mini table tr td.today','id');
			$today_event_elem.find('div').remove();
			$today_mini_event = $today_event_elem.html();	    
			$today_event_elem.html('<h3>'+$today_date+'</h3><div><p>No Event Today</p></div>');
		} else {
		//alert('Today Event');
		var $today_elem = jQuery('#page_calendar_mini table tr td.today div.cal_mini_content'),
			$today_event_elem = jQuery('#mini_calendar_datetime'),
			$today_event_list = jQuery('#mini_calendar_datetime div'),
			$today_date = get_date_byClass('#page_calendar_mini table tr td.today','id');
			$today_event_elem.find('div').remove();
			$today_mini_event = $today_event_elem.html();	    
			$today_elem.parents('div.inner').find('div.cal_mini_content').each(function() {
				$today_event_list = $today_event_list.html($today_event_list.html() + jQuery(this).parent('div').html());
			});
			$today_event_list.find('div').removeClass('cal_mini_content').addClass('mini_event_wrapper');
			$today_event_elem.html('<h3>'+$today_date+'</h3><div>'+$today_event_list.html()+'</div>');
			/*
			var $today_elem = jQuery('#page_calendar_mini table tr td.today div.cal_mini_content'),
			$today_event_elem = jQuery('#mini_calendar_datetime'),
			$today_date = get_date_byClass('#page_calendar_mini table tr td.today','id');
			$today_event_elem.find('div').remove();
			$today_mini_event = $today_event_elem.html();	    
			$today_event_elem.html('<h3>'+$today_date+'</h3><div><p>No Event Today</p></div>');*/
		}
	}
	//End Function
    }

// Display animated loading gif during the load event
var loading = "<center><img class='loading' src='/sites/all/themes/zentheme/soncnv/images/loadingsmall.gif' alt='loading...' /></center>";
function nextLoad(){	
	// Get the nextURL from the pagers next link 
	var nextUrl = $(".calendar-calendar .date-next a")[0].href;  	
	// Identify the content/container you wish to replace
	$(".view-training-cal-home")
	// Add the loading image
	.html(loading)
	.css({ opacity: 0 }).fadeTo("normal",1)
	// Load the nextUrl + return the specific content by specifing the css class or id + bind the click function tp the newly loaded content
	.load(nextUrl+ ".view-training-cal-home .calendar-calendar", bindClick );
};
function prevLoad(){	 
	var prevUrl = $(".calendar-calendar .date-prev a")[0].href;  
	$(".view-training-cal-home")
	.html(loading)
	.css({ opacity: 0 }).fadeTo("normal",1)
	.load(prevUrl+ ".view-training-cal-home .calendar-calendar", bindClick );
};
function bindClick(){
	$(".date-prev a").click(function(){  							 
	prevLoad();
	return false;
	});
	$(".date-next a").click(function(){  
	nextLoad();
	return false;
	}); 
        miniCalendarClickEvent('#page_calendar_mini table tr td div.month a');
};
// Append the click function to the on the initial page load
bindClick();
});

