// 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(){
				_showEventContainer(jQuery('a:first',this));
			});
			_setUpButtons();
			jQuery('#'+containerID+' .dots-list li').each(function(index, element) {
																   
				jQuery('a:first',this).click(function(){
					_showEventContainer(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("-"));
					
					element.hasLeft = false;
					
					jQuery(this).animate({height:40},100);
					jQuery('a:first',this).animate({height:38},100);
					
					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();							
						}
	
						jQuery('#event-balloon-'+id+date+' .event-balloon-content').removeClass("loading").css({width:266});
						jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').show();
						jQuery('#event-balloon-'+id+date+',#event-balloon-'+id+date+' .event-balloon-container').css({width:276});
						jQuery('#event-balloon-'+id+date+' .event-balloon-strip-u, #event-balloon-'+id+date+' .event-balloon-strip-b').css({width:244});
						var h = jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').innerHeight();
						jQuery('#event-balloon-'+id+date).css({top:-((h/2)-14),height:(h+10)});
						jQuery('#event-balloon-'+id+date+' .event-balloon-content').css({height:h});
						jQuery('#event-balloon-'+id+date+' .event-balloon-container').css({height:(h+10)});
						jQuery('#event-balloon-'+id+date+' .event-balloon-strip-l, #event-balloon-'+id+date+' .event-balloon-strip-r').css({height:(h-20)});
	
					} else {
						element.isLoading = true;
						var bubbleHTML = '<div id="event-balloon-'+id+date+'" class="event-balloon">';
							bubbleHTML += '<div class="event-balloon-container">';
								bubbleHTML += '<div class="event-balloon-corner-ul">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-corner-ur">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-corner-bl">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-corner-br">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-strip-l">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-strip-r">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-strip-u">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-strip-b">&nbsp;</div>';
								bubbleHTML += '<div class="event-balloon-content loading"></div>';
							bubbleHTML += '</div>';
						bubbleHTML += '</div>';
						jQuery(this).append(bubbleHTML);
							
						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+"/ajax/coeajax.php", {"eventID":id, "postID":coe_ajax.postID, "date":date, "func":"getEventInfo"}, function(data){
							var newHTML = '<div class="event-balloon-content-container" style="display:none;">';
							//newHTML += '<img title="'+data.title+'" src="'+data.image+'" width="244" height="170" />';
							newHTML += '<h3>'+data.title+'</h3>';
							newHTML += '<p>'+data.time+'</p>';
							newHTML += '</div>';
							
							if(!element.hasLeft){
								jQuery('#event-balloon-'+id+date+' .event-balloon-content').removeClass("loading").prepend(newHTML).animate({width:266},200);
								jQuery('#event-balloon-'+id+date+',#event-balloon-'+id+date+' .event-balloon-container').animate({width:276},200);
								jQuery('#event-balloon-'+id+date+' .event-balloon-strip-u, #event-balloon-'+id+date+' .event-balloon-strip-b').animate({width:244},200);
								jQuery('#event-balloon-'+id+date).animate({width:276},200, function(e){
										var h = jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').innerHeight();
										if(!element.hasLeft){
											jQuery('#event-balloon-'+id+date+' .event-balloon-content').animate({height:h},200, function(e){
												if(!element.hasLeft){
													jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').fadeIn('slow');
												} else {
													jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').show();
												}
											});
											jQuery('#event-balloon-'+id+date).animate({top:-((h/2)-14),height:(h+10)},200);
											jQuery('#event-balloon-'+id+date+' .event-balloon-container').animate({height:(h+10)},200);
											jQuery('#event-balloon-'+id+date+' .event-balloon-strip-l, #event-balloon-'+id+date+' .event-balloon-strip-r').animate({height:(h-20)},200);
										} else {
											jQuery('#event-balloon-'+id+date).hide();
											jQuery('#event-balloon-'+id+date).css({top:-((h/2)-14),height:(h+10)});
											jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').show();
											jQuery('#event-balloon-'+id+date+' .event-balloon-content').css({height:h});
											jQuery('#event-balloon-'+id+date+' .event-balloon-container').css({height:(h+10)});
											jQuery('#event-balloon-'+id+date+' .event-balloon-strip-l, #event-balloon-'+id+date+' .event-balloon-strip-r').css({height:(h-20)});
										}
										element.isLoading = false;
								});
							} else {
								jQuery('#event-balloon-'+id+date+' .event-balloon-content').removeClass("loading").prepend(newHTML).css({width:266});
								jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').show();
								jQuery('#event-balloon-'+id+date+',#event-balloon-'+id+date+' .event-balloon-container').css({width:276});
								jQuery('#event-balloon-'+id+date+' .event-balloon-strip-u, #event-balloon-'+id+date+' .event-balloon-strip-b').css({width:244});
								var h = jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').innerHeight();
								jQuery('#event-balloon-'+id+date).css({top:-((h/2)-14),height:(h+10)});
								jQuery('#event-balloon-'+id+date+' .event-balloon-content').css({height:h});
								jQuery('#event-balloon-'+id+date+' .event-balloon-container').css({height:(h+10)});
								jQuery('#event-balloon-'+id+date+' .event-balloon-strip-l, #event-balloon-'+id+date+' .event-balloon-strip-r').css({height:(h-20)});
								element.isLoading = false;
							}
							
							jQuery('#event-balloon-'+id+date+' .event-fullday a').click(function(){
								_showEventContainer(this);
								return false;
							});
						});
					}
				})
				.bind( 'mouseleave', function(event) {
					// lowlight day
					jQuery(this).closest('td.hasevent').removeClass('active');
					
					jQuery(this).animate({height:17},100);
					jQuery('a:first',this).animate({height:15},100);
					
					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("-"));
	
					jQuery('#event-balloon-'+id+date+' .event-balloon-content').removeClass("loading").css({width:266});
					jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').show();
					jQuery('#event-balloon-'+id+date+',#event-balloon-'+id+date+' .event-balloon-container').css({width:276});
					jQuery('#event-balloon-'+id+date+' .event-balloon-strip-u, #event-balloon-'+id+date+' .event-balloon-strip-b').css({width:244});
					var h = jQuery('#event-balloon-'+id+date+' .event-balloon-content-container').innerHeight();
					jQuery('#event-balloon-'+id+date).css({top:-((h/2)-14),height:(h+10)});
					jQuery('#event-balloon-'+id+date+' .event-balloon-content').css({height:h});
					jQuery('#event-balloon-'+id+date+' .event-balloon-container').css({height:(h+10)});
					jQuery('#event-balloon-'+id+date+' .event-balloon-strip-l, #event-balloon-'+id+date+' .event-balloon-strip-r').css({height:(h-20)});
					
					element.hasLeft = true;
					
					if( !jQuery.browser.msie && !element.isLoading ) {
						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+"/ajax/coeajax.php", {"date":date, "postID":coe_ajax.postID, "dir":dir, "func":"getMonthInfo"}, function(data){
				jQuery('#'+containerID).html(data.calendarHTML);
				_initialize();
			});
		}
		
		function _showEventContainer(btn){
			
			var href = jQuery(btn).attr("href");
			var date = href.substring(href.lastIndexOf("/")+1,href.lastIndexOf("-"));
			var id = href.substring(href.lastIndexOf("-")+1,href.length);
					
			if( !jQuery.browser.msie ) {
				jQuery('.event-balloon').fadeOut("fast");
			} else {
				jQuery('.event-balloon').hide();
			}
			
			if( !jQuery('#event-container').hasClass(date.toString()) ){
				_hideEventContainer();
				jQuery.getJSON(coe_ajax.path+"/ajax/coeajax.php", {"eventID":id, "func":"getEventContainer"}, function(data){
					jQuery('#event-container').removeClass().addClass(date.toString()).html(data.eventHTML).slideDown("fast");
					jQuery(document).scrollTo(jQuery('#event-container'), 500);
					
					_setUpButtons();
				});
			} else {
				jQuery(document).scrollTo(jQuery('#event-container'), 500);	
			}
		}
		
		function _hideEventContainer(){
			jQuery('#event-container').slideUp("fast");			
		}
		
		function _setUpButtons() {						
			/*jQuery('#events-today .close-btn').show().click(function(){
				_hideEventContainer();
				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);