// Global Functions

//lava lamp menu

$(function() {
	$(".main-nav").lavaLamp({
		fx: "backout",
		speed: 700
	})
});


//buttons animate

$('.btn').mouseenter(function() {
	$(this).stop().animate({'left':'10px'},300);
});

$('.btn').mouseleave(function(){
	$(this).stop().animate({'left':'0'},300);
});

//logo animate

$('h1#logo2').mouseenter(function(){
		$(this).stop().animate({'top':'0'},300);
	}).mouseleave(function(){
		$(this).stop().animate({'top':'-10px'},300);
});

//Quotes animate
$('#quotes').cycle({ 
	fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	timeout: 5000
}); 

       
//image transition

var canGoLeft = false;

$(document).ready(function() {

	var maxCount= $('#slider > *').length;
	var count=2;
	var previous=1;
	setInterval(imageRotation,5000); //animation speed this is set to 5 seconds at the moment
		
	function imageRotation() {
		$('#image-'+previous).animate({
			"left":"625px"
		}, 1000).fadeOut(500, function(){
			$(this).animate({
				"left": "0px"
			});
		}); //fade out speed this is set to 0.5 seconds at the moment
		$('#image-'+count).fadeIn(1000); //fade in speed this is set to 0.5 seconds at the moment
		previous = count;
		count ++;
		if(count==maxCount+1) {
			count=1;
		}
	}
		
	//events toggle

	//Hide (Collapse) the toggle containers on load
	$(".toggle").hide();
	$(".toggle:first").show();
	
	$(".trigger:first").addClass('active');

	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$(".trigger").click(function(){
		$(this).toggleClass("active").next().slideToggle("300");
	});

	$(".left-arrow img").hide();
	$(".move-left img").hide();
	$(".left-arrow-small img").hide();
	
	if($('#the-calendar-inner').size() == 1) {
		initialiseCal();
	
		$('a.move-right').click(function() {
			movement = 'right';
			checkPosition();
			moveBox();
			return false;
		});
		
		$('a.move-left').click(function() {
			movement = 'left';
			checkPosition();
			moveBox();
			return false;
		});

		$('a.left-arrow').click(function() {
			if (canGoLeft) {
				prevMonth();
			}
			return false;
		});

		$('a.right-arrow').click(function() {
			nextMonth();
			return false;
		});

		$('a.right-arrow-small').click(function() {
			var nextYear = parseInt($("#cal-year-span").html()) + 1;
			$("#cal-year-span").html(nextYear);
			getCal();
			hideLeft();
		});

		$('a.left-arrow-small').click(function() {
			if (canGoLeft) {
				var nextYear = parseInt($("#cal-year-span").html()) - 1;
				$("#cal-year-span").html(nextYear);
				getCal();
				hideLeft();
			}
		});
	}
});

var totalWidth = 0;
var leftPosition = 0;
var calendarWidth = $('#the-calendar').width();
var movement = 0;
// Calendar stuff

function nextMonth() {
	var curMonth = $("#cal-month").html();
	var curYear = parseInt($("#cal-year-span").html());
	var nextMonth;
	var nextYear;

	if (curMonth == 'January') {
		nextMonth = 'February';
		nextYear = curYear;
	}
	else if (curMonth == 'February') {
		nextMonth = 'March';
		nextYear = curYear;
	}
	else if (curMonth == 'March') {
		nextMonth = 'April';
		nextYear = curYear;
	}
	else if (curMonth == 'April') {
		nextMonth = 'May';
		nextYear = curYear;
	}
	else if (curMonth == 'May') {
		nextMonth = 'June';
		nextYear = curYear;
	}
	else if (curMonth == 'June') {
		nextMonth = 'July';
		nextYear = curYear;
	}
	else if (curMonth == 'July') {
		nextMonth = 'August';
		nextYear = curYear;
	}
	else if (curMonth == 'August') {
		nextMonth = 'September';
		nextYear = curYear;
	}
	else if (curMonth == 'September') {
		nextMonth = 'October';
		nextYear = curYear;
	}
	else if (curMonth == 'October') {
		nextMonth = 'November';
		nextYear = curYear;
	}
	else if (curMonth == 'November') {
		nextMonth = 'December';
		nextYear = curYear;
	}
	else if (curMonth == 'December') {
		nextMonth = 'January';
		nextYear = curYear + 1;
	}

	$("#cal-month").html(nextMonth);
	$("#cal-year-span").html(nextYear);

	getCal();
	hideLeft();
}

function prevMonth() {
	var curMonth = $("#cal-month").html();
	var curYear = parseInt($("#cal-year-span").html());
	var nextMonth;
	var nextYear;

	if (curMonth == 'January') {
		nextMonth = 'December';
		nextYear = curYear - 1;
	}
	else if (curMonth == 'February') {
		nextMonth = 'January';
		nextYear = curYear;
	}
	else if (curMonth == 'March') {
		nextMonth = 'February';
		nextYear = curYear;
	}
	else if (curMonth == 'April') {
		nextMonth = 'March';
		nextYear = curYear;
	}
	else if (curMonth == 'May') {
		nextMonth = 'April';
		nextYear = curYear;
	}
	else if (curMonth == 'June') {
		nextMonth = 'May';
		nextYear = curYear;
	}
	else if (curMonth == 'July') {
		nextMonth = 'June';
		nextYear = curYear;
	}
	else if (curMonth == 'August') {
		nextMonth = 'July';
		nextYear = curYear;
	}
	else if (curMonth == 'September') {
		nextMonth = 'August';
		nextYear = curYear;
	}
	else if (curMonth == 'October') {
		nextMonth = 'September';
		nextYear = curYear;
	}
	else if (curMonth == 'November') {
		nextMonth = 'October';
		nextYear = curYear;
	}
	else if (curMonth == 'December') {
		nextMonth = 'November';
		nextYear = curYear;
	}

	$("#cal-month").html(nextMonth);
	$("#cal-year-span").html(nextYear);

	getCal();
	hideLeft();
}

function getCal() {
	var curMonth = $("#cal-month").html();
	var curYear = parseInt($("#cal-year-span").html());

	$.ajax(
		{
			type: "GET",
			url: "/ajax/calendar/?month=" + curMonth + "&year=" + curYear,
			success: function(html) {
				$('#the-calendar').html(html);
				doCufon();
				initialiseCal()
			}
		}
	);
}

function hideLeft() {
	var curMonth = $("#cal-month").html();
	var curYear = parseInt($("#cal-year-span").html());
	var nowMonth = $("#nowMonth").val();
	var nowYear = parseInt($("#nowYear").val());

	if (curMonth == nowMonth && curYear == nowYear) {
		$(".left-arrow img").fadeOut();		
		canGoLeft = false;
	}
	else {
		$(".left-arrow img").fadeIn();		
		canGoLeft = true;
	}

	if (curYear == nowYear) {
		$(".left-arrow-small img").fadeOut();

	}
	else {
		$(".left-arrow-small img").fadeIn();
	}
}

function initialiseCal() {
	totalWidth=0;
	$('#the-calendar-inner > div').each(function(){
		totalWidth = $(this).width() + parseInt($(this).css('margin-right').replace('px','')) + totalWidth;
	});
	$('#the-calendar-inner').width(totalWidth);
	movement="left";
	$('.move-right img').fadeIn();
	checkArrows();
}

function checkPosition() {
	leftPosition = parseInt($('#the-calendar-inner').css('left').replace('px',''));
}

function moveBox() {
	if($('#the-calendar-inner:not(":animated")').size() == 1) {
		if(movement == "right" && leftPosition > (-totalWidth + calendarWidth + 2) ) {
			$('#the-calendar-inner').animate({'left':'-='+calendarWidth},300, function(){
				checkArrows();
			});
		}
		else if(movement == "left" && leftPosition < 0 ) {
			$('#the-calendar-inner').animate({'left':'+='+calendarWidth},300, function(){
				checkArrows();
			});
		}
	}
}

function checkArrows() {
	leftPosition = parseInt($('#the-calendar-inner').css('left').replace('px',''));
	if(movement=="right" && leftPosition <= (-totalWidth + calendarWidth + 2)) {
		$('.move-right img').fadeOut().parent().css('cursor','default');
		$('.move-left img').fadeIn().parent().css('cursor','pointer');
	} else if(movement == "left" && leftPosition == 0 ) {
		$('.move-left img').fadeOut().parent().css('cursor','default');
		$('.move-right img').fadeIn().parent().css('cursor','pointer');
	} else {
		$('.move-left img').fadeIn().parent().css('cursor','pointer');
		$('.move-right img').fadeIn().parent().css('cursor','pointer');
	}

}
