$(document).ready(function() {
	
	$('<div id="dialog-event" class="dialog-event">').css("display","none").appendTo("body");
	
	var $calendarModal = $("#dialog-event");
	var targets = window.location.hash.replace(/#/,"").split("|");
	var validSlugTypes = new Array("listing","details");
	
	var slug = targets[0];
	var slugType = targets[1];
	
	var onPageLoad = targets.length == 2 && !!slug.length && !!slugType.length && (($.inArray(slugType, validSlugTypes) != -1) );
	
	$calendarModal.dialog({ 
		autoOpen: false,
		width: 540,
		modal: true,
		show:{
			effect:"scale",
			options:{
				direction:"both",
				percent:"100"
			},
			speed:800
		},
		hide:{
			effect:"scale",
			options:{
				direction:"both",
				percent:"100"
			},
			speed:800
		},
		resizable: false,
		draggable: false,
		position: ['center','center']
	});
	
	if(onPageLoad && $calendarModal.length) {
		$.post("/bluhq/modules/metroplan/async/calendarAsync.php", { 
				slug: slug, 
				slugType: slugType 
			},
			function(response){
				$calendarModal.html(response);
				if( !!(isNaN(Number($.trim(response))) || Boolean(Number($.trim(response)))) ) {
					$calendarModal.dialog('open');
				}
			}
		);
	}
	
	$("a[rel=details], a[rel=listing]").live('click',function(e) {
		e.preventDefault();
		if( !$('body').find('#minical').length ) {
			CalendarLoadingAnimation.load('page','/img/ajax-loader.gif');
		} else {
			CalendarLoadingAnimation.load('container','/img/ajax-loader2.gif',"#minical > .inner > #dayContents","#dayContents");
		}
				
		if( $calendarModal.dialog("isOpen") ) {
			window.noTransition = false;
			$calendarModal.dialog('close');
		}
		$.post("/bluhq/modules/metroplan/async/calendarAsync.php", { 
				slug: $(this).attr('href').replace(/#/,''), 
				slugType: $(this).attr('rel') 
			},
			function(response){						
				$(function() {
					window.setTimeout(function () {
						$calendarModal.html(response);
						$calendarModal.dialog('open');
					}, 600);
				});
			}
		);
	});
	
	/* MINI CALENDAR */
	$("#minical p").each(function(i) {
		if( $(this).find('a').length ) {
			$(this).css('cursor','pointer');
		}
	});
	
	var date = new Date();
	var currentMonth = (date.getFullYear()+"-"+pad((date.getMonth()+1),2));
	var currentDate = (date.getFullYear()+"-"+pad((date.getMonth()+1),2)+"-"+(date.getDate()));
	
	$("#minical p").live('click',function(e) {
		e.preventDefault();
		CalendarLoadingAnimation.load('container','/img/ajax-loader2.gif',"#minical > .inner > #dayContents","#dayContents");
		if( $(this).find('a').length ) {
			$("#minical .dayselected").each(function() {
				$(this).removeClass("dayselected");
			});
			$(this).parent().addClass("dayselected");
			
			var inputDate = $(this).find('a').attr('href').replace(/#/,'');
		
			$.post("/bluhq/modules/metroplan/async/calendarAsync.php", { 
					slug: inputDate, 
					slugType: $(this).find('a').attr('rel')
				},
				function(response){
					if(inputDate == currentDate) {
						$("#minical #dayContent").hide();
						$("#minical #dayContentToday").show();
					} else {
						$("#minical #dayContentToday").hide();
						$("#minical #dayContent").show().html(response);
					}
					
				}
			);
		}
	});
	
	$("#minical .header .entrydate a").live('click',function(e) {
		e.preventDefault();
		CalendarLoadingAnimation.load('container','/img/ajax-loader2.gif',"#minical > .inner > #dayContents","#dayContents");
		var $anchor = $(this);
		
		$("#minical .dayselected").each(function() {
			$(this).removeClass("dayselected");
		});
		$(this).parent().addClass("dayselected");
		
		var slugTypeTmpArr = $(this).attr('rel').split(':');
		var inputMonth = $(this).attr('href').replace(/#/,'');
		
		var calendarHref = $("#minical > .header .more").attr('href').replace(/\d{4}\-\d{2}/,"");
		$("#minical > .header .more").attr('href',(calendarHref+inputMonth));
		
		$.post("/bluhq/modules/metroplan/async/calendarAsync.php", { 
				slug: $(this).attr('href').replace(/#/,''), 
				slugType: $(this).attr('rel')
			},
			function(response){
			
				if(inputMonth == currentMonth) {
					$("#minical #dayContent").hide();
					$("#minical #dayContentToday").show();
				} else {
					$("#minical #dayContent").hide();
					$("#minical #dayContentToday").hide();
				}
				
				$("#minical > .inner > .content").html(response);
				
				/* 
				this is duplicated here (from above) because of the month switch 
				jquery's .live() doesn't work with .each()
				*/
				$("#minical p").each(function(i) {
					if( $(this).find('a').length ) {
						$(this).css('cursor','pointer');
					}
				});
			}
		);
	});
		
	
	
	/* END MINI CALENDAR */
});

CalendarLoadingAnimation = {
	
	load: function(type,img,container,toggleContainer) {
		$('#loaderImg').hide();
		
		switch(type) {
			case 'page':
				$('#loaderImg img').attr('src',img);
				
				$('#loaderImg')
				.ajaxStart(function() {
					$(this)
					.css("position","absolute")
					.css("width","100%")
					.css("z-index","2000")
					.css("top",(posTop()+pageHeight()/2))
					.show();
				})
				.ajaxComplete(function() {
					$this = $(this);
					if(window.noTransition) {
						$this.hide();
					} else {
						window.setTimeout(function () {
							$this.hide();
						}, 600);
					}
				});
				break;
				
			case 'container':
				$('#loaderImg img').attr('src',img);
				
				var orgLoc = $('#loaderImg').parent().attr('id');

				$('#loaderImg')
				.ajaxStart(function() {
					$(toggleContainer).hide();
					
					$(this)
					.css("text-align","center")
					.css("margin","20px 0")
					.insertBefore(container)
					.show();
				})
				.ajaxComplete(function() {
					window.setTimeout(function () {
						$(toggleContainer).show("slow");
						$(container).parent().find("#loaderImg")
						.attr("style","display: none;")
						.appendTo("#"+orgLoc)
						.hide();
					}, 200);
				});
				break;
		}
	}
}
