$(function() {
	$('.listingsday_container a').toggle(showDay, hideDay);
	$('.eventInfo a.readMore').click(readMore);
	$('.listingPopup a.top-right').click(readNoMore);

	$('.listingPopup .left-arrow').click(prevReadMore);
	$('.listingPopup .right-arrow').click(nextReadMore);

	//$(window).scroll(movePopup).resize(movePopup);
});

var oldLeft;
var oldText;

function getDays(atag) {
	var day = atag.href.substr(atag.href.indexOf('#')+1);
	return $('.listing-day-titles .' + day + ' .dayInner:not(:animated), .dayboxes .' + day + ' .dayboxInner:not(:animated)');
}


var topx = 0;
function updateTopValues(days) {

	var update = true;
	days.each(function() {
		if ($(this).hasClass('expanded') == false) {
			update = false;
		}
	});

	// Animate the divs down by changing their top
	if (update) {

		topx = 0;
		var margin = 5;

		days.each(function(key) {
			if (key==0) return;
			$(this).animate({top:topx}, 300, 'linear', function() {
				// Done
			});

			var height = parseInt();
			topx += ($(this).height() + margin); // Extra for margin

		});
	
		$('.dayboxInner:not(.expanded)').hide();
		$('#whats-on-left-column').animate({height:topx+200}, 300, 'linear', function() {
		});

	}
}
function showDay() {
	var days = getDays(this);
	if(days.length == 0) return;

	oldLeft = days.position().left;
	oldText = this.innerHTML;
	var linkThis = this;

	var heightSet = false;

	$('.dayInner:not(.expanded)').hide();
	
	days.each(function(key) {

		//if (key==0) return;

		var day = $(this);

		day
			.find('.moreInfo')
				.fadeIn()
			.end()
			.find('.readMore')
				.slideUp()
			.end()
			.find('.event-title')
				.slideUp()
			.end()
			.css({position:'absolute', left:oldLeft})
			//.animate({left:4, width:758}, 600, 'easeOutCubic', function() {
			.animate({left:4, width:656}, 300, 'linear', function() {
				linkThis.innerHTML = "< Back";

				//if(heightSet) return;
				//heightSet = true;

				//$(this).find('.dayInfo').show();

				// Change background colour of text container div
				$('.daycontent').css('background-image','url("../images/dates/date-wide-bg.gif")');
				$('.listing-day-titles').animate({'height': 50}, 300, 'linear', function() {

				});

				// Remove height constraints
				$('.daycontent').css({'height':'auto','width':'600px'});
				$('.dayboxInner').css({'height':'auto','margin-bottom':'10px'});

				// Mark this div as finished expanding, and check if they have
				// all been expanded. If they have then update their top values
				day.addClass('expanded');
				updateTopValues(days);

			});
		//console.log($(this).find('.events-container'));
		//top = $(this).find('.events-container').height()+top;

	});

}

function hideDay(day) {
	var days = getDays(this);
	if(days.length == 0) return;

	var linkThis = this;
	linkThis.innerHTML = oldText;

	// Remove expanded class
	days.removeClass('expanded');

	//$('.listing-day-titles').animate({'height': 42}, 500, 'easeOutCubic', function() {
	$('.listing-day-titles').animate({'height': 42}, 300, 'linear', function() {

		// Change background colour of text container div
		$('.daycontent').css('background-image','url("../images/dates/date-bg.png")');
		days
			/*.find('.dayInfo')
				.hide()
			.end()*/
			.find('.moreInfo')
				.fadeOut()
			.end()
			.find('.readMore')
				.slideDown()
			.end()
			.find('.event-title')
				.slideDown()
			.end()
			//.animate({left:oldLeft, width:90}, 600, 'easeOutCubic', function() {
			.animate({left:oldLeft, width:90, height:70}, 300, 'linear', function() {
				$(this).css({position: ''});

			});

			// Fade day name titles back in
			$('.dayInner:not(.expanded)').fadeIn();

			// Add height constraints
			$('.daycontent').css({'height':'60px','width':'58px'});
			$('.dayboxInner').css({'height':'70px','margin-bottom':'0px'});
			$('.daybox').css({'height':'70px'});

			// Reset height of outer container pushing footer down
			$('.dayboxInner').show();
			$('#whats-on-left-column').css('height','auto');

	});
}

function movePopup() {
	var maskOffset = $(document).scrollTop();

	$(".listingPopupBg").css({
		width: $(window).width(),
		height: $(window).height(),
		top: maskOffset,
		opacity:0.5
	});

	$(".listingPopup").css({
		top:maskOffset
	});
}

var currentEvent;
var enableRight;
var enableLeft;

function readMore(e) {
	e.preventDefault();
	currentEvent = this.href.substr(this.href.indexOf('#')+7);

	movePopup();
	$(".listingPopup .content").html($(this).parents('.daybox').html());

	$(".listingPopup")
		.fadeIn()
		.find(".readMore")
			.hide()
		.end()
		.find(".moreInfo")
			.show()
		.end()
		.find(".dayboxInner")
			.css("width", "")

	$(".listingPopupBg").fadeIn();

	displayArrows();
}

function readNoMore(e) {
	e.preventDefault();

	$(".listingPopup").fadeOut();
	$(".listingPopupBg").fadeOut();

	$("body").css('overflow', '');
}

function prevReadMore(e) {
	e.preventDefault();
	if(!enableLeft)
		return;

	var el = getPrevEvent(currentEvent);
	currentEvent = el.attr('id').substr(6);

	$(".listingPopup .content").html(el.html());
	displayArrows();
}

function nextReadMore(e) {
	e.preventDefault();
	if(!enableRight)
		return;

	var el = getNextEvent(currentEvent);
	currentEvent = el.attr('id').substr(6);

	$(".listingPopup .content").html(el.html());
	displayArrows();
}

function displayArrows() {
	if(!getNextEvent(currentEvent).length) {
		$('.listingPopup .right-arrow').fadeOut();
		enableRight = false;
	} else {
		$('.listingPopup .right-arrow').fadeIn();
		enableRight = true;
	}

	if(!getPrevEvent(currentEvent).length) {
		$('.listingPopup .left-arrow').fadeOut();
		enableLeft = false;
	} else {
		$('.listingPopup .left-arrow').fadeIn();
		enableLeft = true;
	}
}

function getNextEvent(eventId) {
	return $('#event-' + eventId).nextAll("div.daybox:not(.noevents):not(.inactive)");
}

function getPrevEvent(eventId) {
	return $('#event-' + eventId).prevAll("div.daybox:not(.noevents):not(.inactive)");
}

