var Calendar = {
	_holder : $('#calendar'),
	_months : ['','January','February','March','April','May','June','July','August','September','October','November','December'],
	_url : $.themeURL+'inc/scripts/calendar.php',
	_next : false,
	_prev : false,
	_curMonth : false,
	_curYear : 0,
	_cat : false,
	_loader : false,
	
	init : function() {
		
		var prevDate_ = this._holder.find('a.prev').text().match(/^(\d+)\s(\d+)$/);
		
		this._curYear = parseInt(this._holder.find('span.year').text());
		this._curMonth = (prevDate_[1] == "12") ? 1 : prevDate_[1]*1+1;
		
		this._next = this._holder.find('a.next');
		this._prev = this._holder.find('a.prev');
		
		this._cat = this._next.attr('href');
		
		this.addListeners();
		this._loader = new ShowLoader(this._holder,1);
		
		if($.browser.msie && parseInt($.browser.version) == 7) { this.fixIE7(); }
	},
	
	addListeners : function() {
		var scope = this;
		this._holder.find('a.next, a.prev').bind('click',function(e) {
			e.preventDefault();
			
			if(scope._loadingCalendar) { return; }
			
			scope._loader.Start();
			
			if($(this).hasClass('next')) {
				scope._curMonth++;
				if(scope._curMonth == 13) { scope._curMonth = 1; scope._curYear++; }
			} else {
				scope._curMonth--;
				if(scope._curMonth == 0) { scope._curMonth = 12; scope._curYear--; }		
			}
			
			scope.getCalendar();
			scope.updateLinks();
		});
		
		//add .live listeners to the coloured bars
		$('#calendar ul.body li span.event').live('mouseenter',function() {
			if(!$(this).data('leftpos')) { var left_ = $(this).position().left; $(this).data('leftpos',left_); }
			
			var top_ = 0;
			var left_ = -5;
			var width_ = 182;
			var height_ = 112;
			
			if($(this).parent().parent().width() - $(this).parent().position().left <= ($(this).parent().outerWidth()+20)) {
				left_ = -98;
			}
			
			if($(this).parent().parent().height() - $(this).parent().position().top <= $(this).parent().outerHeight()) {
				top_ = -61;
			}
			
			
			if($.browser.msie && parseInt($.browser.version) == 7) {
				$('#calendar ul.body').css('margin-left','-1px');
				$('#calendar ul.body li').css('zIndex',0);
				$(this).parent().css('zIndex',1);
			}
			
			$(this).css('z-index','10000').stop().animate({top:top_,left:left_,width:width_,height:height_},200,function() {
				$(this).children().stop().animate({opacity:1},75);
				$(this).find('.bar-logo').css('visibility','visible');
			});
			
			//get the proper marginTop on the span
			var span_ = $(this).children('span');
			span_.css('width','150px');
			var spanHeight_ = span_.height();
			span_.css('margin-top',(height_-spanHeight_)/2+'px');
			
		});
		
		$('#calendar ul.body li span.event').live('mouseleave',function() {
			var left_ = $(this).data('leftpos');
			
			var elem_ = $(this);
			$(this).children().stop().animate({opacity:0},75,function() {
				$(this).find('.bar-logo').css('visibility','hidden');
				elem_.stop().animate({top:50,width:8,height:0,left:left_},200,function() {
					$(this).css('z-index','1');
				});
			})
		});
		
		$('#calendar ul.body li a').live('click',function(e) {
			e.preventDefault();
			if($(this).attr('href') != '#') {
				if($(document).scrollTop() > 220) { $("html:not(:animated),body:not(:animated)").animate({ scrollTop: 145}, 200 ); }
				scope.loadEvent($(this).attr('href'));
			}
		});
		
		$('#nav a').each(function() {
			if($(this).text().toLowerCase() == 'calendar') {
				$(this).bind('click',function(e) {
					e.preventDefault();
					$('#nav a').removeClass('selected');
					$(this).addClass('selected');
					scope.showCalendar();
				});
			}
		});
	},
	
	loadEvent : function(href_) {
		//get the specified event and load it onto the page
		//hide #calendar and #featuredbanner_side
		Calendar.hideCalendar();
		BannerSlider.hide();
		FeaturedEvent.loadEvent(href_);
		PromoSlider.hide();
	},
	
	hideCalendar : function(cb_) {
		$('#calendar .handle').removeClass('expanded');
		if($('#calendar:animated').length || !parseInt($('#calendar').css('left'))) {
			this._holder.stop().animate({left:-682},500,'easeInOutQuint',cb_);
			//BannerSlider.hide();
		} else {
			if(cb_) { cb_.call(); }
		}
	},
	
	showCalendar : function() {
		BarHighlight._panels.removeClass('selected');
		$('#calendar').stop().animate({left:0},500,'easeInOutQuint');
		$('#calendar .handle').addClass('expanded');
		BannerSlider.collapse(function() { BannerSlider.show(); });
	},
	
	updateLinks : function() {
		var scope = this;
		
		var nextMonth_ = this._curMonth+1;
		var nextYear_ = this._curYear;
		var prevMonth_ = this._curMonth-1;
		var prevYear_ = this._curYear;
		
		if(nextMonth_ == 13) {
			nextMonth_ = 1;
			nextYear_ += 1;
		}
		
		if(prevMonth_ == 0) {
			prevMonth_ = 12;
			prevYear_ -= 1;
		}
		
		this._next.html(nextMonth_ + ' ' + nextYear_).attr('title',scope._months[nextMonth_]+ ' ' + nextYear_);
		this._prev.html(prevMonth_ + ' ' + prevYear_).attr('title',scope._months[prevMonth_]+ ' ' + prevYear_);
		
		
	},
	
	getCalendar : function() {
		var scope = this;
		this._loadingCalendar= true;
		$.get(
			scope._url,
			{ ajax : 'true', month : scope._curMonth, year : scope._curYear, cat : scope._cat },
			function(data) { scope.replaceCalendar(data); },
			'html'
		);	
	},
	
	fixIE7 : function() {
		var zIndex_ = 9999;
		$('#calendar ul.body li').css('zIndex',zIndex_);
		$('#calendar ul.body li').each(function() {
			$(this).css('zIndex',zIndex_);
			zIndex_ -= 10;
		});
	},
	
	replaceCalendar : function(data) {
		var scope = this;
		
		this._holder.find('ul.body').stop().animate({opacity:0},function() {
			$(this).html($(data).html()).animate({opacity:1});
			scope._loadingCalendar = false;
			if($.browser.msie && parseInt($.browser.version) == 7) { scope.fixIE7(); }
		});	
		
		this._holder.find('span.month').stop().animate({opacity:0},function() { $(this).html(scope._months[scope._curMonth]).animate({opacity:1}) });
		this._holder.find('span.year').stop().animate({opacity:0},function() { $(this).html(scope._curYear).animate({opacity:1}); scope._loader.Stop(); });
	}
}


if($('#calendar').length) {
	Calendar.init();
}

$('#event a.next, #event a.prev').live('click',function(e) {
	e.preventDefault();
	//$('#event').animate({opacity:0},400,function() {
		FeaturedEvent.loadEvent($(this).attr('href'));
	//});
});
