// JavaScript Document

(function($) {
	jQuery.fn.coeLarge = function(settings) {
		
		var settings = jQuery.extend({
		}, settings);
		// Caching the jQuery object with all elements matched
		var container = this; // This, in this context, refer to jQuery object
		var containerID = this.attr("id");
		
		function _initialize(){
			jQuery('#'+containerID+' #navigation-previous-month a').click(function(){
				changeMonth(this,'prev');
				return false;
			});
			jQuery('#'+containerID+' #navigation-next-month a').click(function(){
				changeMonth(this,'next');
				return false;
			});
			jQuery('#'+containerID+' td.hasevent').click(function(){
				_showTodaysEvents(jQuery('a:first',this));
			});
			_setUpButtons();
			jQuery('#'+containerID+' .dots-list li').each(function(index, element) {
																   
				jQuery('a:first',this).click(function(){
					_showTodaysEvents(this);
					return false;
				});
				
				jQuery(element).click( function(event) {
					
				})
				.bind( 'mouseenter', function(event) {
					// highlight day
					jQuery(this).closest('td.hasevent').addClass('active');
					
					var href = jQuery('a:first',this).attr("href");
					var id = href.substring(href.lastIndexOf("-")+1,href.length);
					var date = href.substring(href.lastIndexOf("/")+1,href.lastIndexOf("-"));
					
					if(jQuery('#event-balloon-'+id+date).hasClass('event-balloon')){
						if( !jQuery.browser.msie ) {
							jQuery('#event-balloon-'+id+date).fadeIn("fast");
						} else {
							jQuery('#event-balloon-'+id+date).show();							
						}
						var h = jQuery('#event-balloon-'+id+date+' .event-balloon-data').height()+20;
						jQuery('#event-balloon-'+id+date+' .event-balloon-back').height(h);
					} else {
						jQuery(this).append('<div id="event-balloon-'+id+date+'" class="event-balloon"><div class="event-balloon-back loading"></div></div>');
							
						if((jQuery(this).closest('td').hasClass('day-1')) || (jQuery(this).closest('td').hasClass('day-2')) || (jQuery(this).closest('td').hasClass('day-3'))) {
							jQuery('#event-balloon-'+id+date).addClass('left');
						}
						if( !jQuery.browser.msie ) {
							jQuery('#event-balloon-'+id+date).hide().fadeIn("fast");
						} else {
							jQuery('#event-balloon-'+id+date).hide().show();
						}
						jQuery.getJSON(coe_ajax.path+"/coeajax.php", {"eventID":id, "postID":coe_ajax.postID, "date":date, "func":"getEventInfo"}, function(data){
							var newHTML = '<div class="event-balloon-data">';
							newHTML += '<h4>'+data.title+'</h4>';
							newHTML += '<span class="event-type">'+data.typename+'</span>';
							newHTML += '<span class="event-time">'+data.time+'</span>';
							newHTML += '<span class="event-location">'+data.location+'</span>';
							newHTML += '<div class="event-details">'+data.description+'</div>';
							newHTML += '<ul class="event-links">';
							newHTML += '<li class="event-rsvp">'+data.rsvp+'</li>';
							newHTML += (data.contact_email != "") ? '<li class="event-contact"><a href="mailto:'+data.contact_email+'">E-mail '+((data.contact_name != "") ? data.contact_name : 'Contact')+'</a></li>' : '';
							newHTML += (data.contact_website != "") ? '<li class="event-website"><a href="'+data.contact_website+'" target="_blank">View Website</a></li>' : '';
							newHTML += (data.map != "") ? '<li class="event-map"><a href="'+data.map+'" target="_blank">View Map</a></li>' : '';
							newHTML += '<li class="event-fullday"><a href="'+href+'">View Full Day</a></li>';
							newHTML += '</ul>';
							newHTML += '<script>tb_init(\'a.thickbox\')</script>';
							
							jQuery('#event-balloon-'+id+date).prepend(newHTML);
							var h = jQuery('#event-balloon-'+id+date+' .event-balloon-data').height()+20;
							if( !jQuery.browser.msie ) {
								jQuery('#event-balloon-'+id+date+' .event-balloon-data').hide().fadeIn("slow");
							} else {
								jQuery('#event-balloon-'+id+date+' .event-balloon-data').hide().show();								
							}
							jQuery('#event-balloon-'+id+date+' .event-balloon-back').removeClass('loading').animate({height:h},200);
							
							jQuery('#event-balloon-'+id+date+' .event-fullday a').click(function(){
								_showTodaysEvents(this);
								return false;
							});
						});
					}
				})
				.bind( 'mouseleave', function(event) {
					// lowlight day
					jQuery(this).closest('td.hasevent').removeClass('active');
					
					var href = jQuery('a:first',this).attr("href");
					var id = href.substring(href.lastIndexOf("-")+1,href.length);
					var date = href.substring(href.lastIndexOf("/")+1,href.lastIndexOf("-"));
					
					if( !jQuery.browser.msie ) {
						jQuery('#event-balloon-'+id+date).fadeOut("fast");
					} else {
						jQuery('#event-balloon-'+id+date).hide();
					}
				});
			});
		}
		
		function changeMonth(page,dir){
			var href = jQuery(page).attr("href");
			var date = href.substr(href.lastIndexOf("/")+1, href.length);
			
			jQuery.getJSON(coe_ajax.path+"/coeajax.php", {"date":date, "postID":coe_ajax.postID, "dir":dir, "func":"getMonthInfo"}, function(data){
				jQuery('#'+containerID).html(data.calendarHTML);
				_initialize();
			});
		}
		
		function _showTodaysEvents(btn){
			
			var href = jQuery(btn).attr("href");
			var date = href.substring(href.lastIndexOf("/")+1,href.lastIndexOf("-"));
			if( !jQuery.browser.msie ) {
				jQuery('.event-balloon').fadeOut("fast");
			} else {
				jQuery('.event-balloon').hide();
			}
			
			if( !jQuery('#events-today').hasClass(date.toString()) ){
				_hideTodaysEvents();
				jQuery.getJSON(coe_ajax.path+"/coeajax.php", {"date":date, "func":"getTodaysEvents"}, function(data){
					jQuery('#events-today').removeClass().addClass(date.toString()).html(data.todayseventsHTML).slideDown("fast");
					jQuery(document).scrollTo(jQuery('#events-today'), 500);
					
					_setUpButtons();
				});
			} else {
				jQuery(document).scrollTo(jQuery('#events-today'), 500);	
			}
		}
		
		function _hideTodaysEvents(){
			jQuery('#events-today').slideUp("fast");			
		}
		
		function _setUpButtons() {						
			jQuery('#events-today .close-btn').show().click(function(){
				_hideTodaysEvents();
				jQuery('#events-today').removeClass();
				return false;
			});
			jQuery('#events-today .viewcalendar-btn').show().click(function(){
				jQuery(document).scrollTo(jQuery('#calendar-of-events'), 500);
				return false;
			});
		}
		
		_initialize();

	};
})(jQuery);