function updateFeature (featureH, overlayH, infoLeft) {
	$("#feature").animate({
		'height': featureH
	});
	
	$("#featureOverlay").animate({
		'height': overlayH
	});
	
	$("#feature #info").animate({
		'left': infoLeft
	});

	closeVideo ();
}
	
function hideAll () {
	$("#feature #overview").fadeOut(500);
	$("#feature #photos").hide();
	$("#feature #videos").hide();
}

function showOverview() {
	hideAll();
	updateFeature("350px","350px", "0px");
	$("#feature #overview").fadeIn(500, function(){
		$('#feature #photos img').eq(0).click();	
	});
	
}

function showPhoto() {
	hideAll();
	updateFeature("525px","0px", "-350px");
	$("#feature #photos").show();
	$('#feature #photos img').eq(0).click();
}

function showVideos() {
	hideAll();
	updateFeature("525px","525px", "-350px");
	$("#feature #videos").delay(250).fadeIn(250,function () {
		$('#feature #videos img').eq(0).click();	
	});
}

function closeVideo () {
	$("#featureModal").html("").fadeOut(200);
}

// ++++++++++++++++++++++++++++++++++++++++
// MODAL FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++

var prevEvent = 0;
var currEvent = 0;
var timer;
var timeOut = 6000;

var modalOpen;

function closeModal () {
	$("#modalInner").html("");
	$("#eventModal").fadeOut(500, function (){
		initTimer();
	});
	
	$(window).unbind("keydown");
}

function openModal (theMovie, isYoutube) {
		
	$("#modalClose").css({
		'position' : 'absolute',
		'top' : '5px',
		'right' : '5px',
		'color' : '#eeeeee',
		'font-size' : '1.3em'
	});
	
	$("#mainEvents #eventContainer").unbind('mouseover').unbind('mouseout');
	
	modalOpen = true;	
			
	$("#eventModal").fadeIn(500, function () {	
		if (isYoutube) {
			loadYoutubeContent(theMovie);
		} else {
			loadFlashContent (theMovie);	
		}
	});
	
	$(window).bind("keydown", function (event) {
		if (event.keyCode === 27) {
			closeModal();	
		}
	});
	
}

function loadFlashContent (theMovie) {
	
	var theHTML = '<object width="640" height="388">'
	theHTML += '<param name="movie" value="media/MainPlayer.swf?myVideo=' + theMovie + '" />'
	theHTML += '<param name="play" value="true"/>'
	theHTML += '<param name="quality" value="high" />'
	
	theHTML += '<embed src="media/MainPlayer.swf?myVideo=' + theMovie + '" width="640" height="388" play="true"></embed>'
	theHTML += '</object>';
	
	$("#modalInner").html(theHTML);
}

function loadYoutubeContent (theMovie) {
	
	var theHTML = '<iframe width="640" height="388" src="http://www.youtube.com/embed/'+ theMovie +'?autoplay=1" frameborder="0" allowfullscreen></iframe>'
	
	$("#modalInner").html(theHTML);
}

// ++++++++++++++++++++++++++++++++++++++++
// EVENTS FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++
function nextEvent () {
	var newEvent = currEvent + 1;
	
	var eventCount = $("#mainEvents .eventsNav a").length;
		
	if (newEvent > eventCount - 1) {
		newEvent = 0;
	}
	
	moveEvents(newEvent);
			
	timer = setTimeout(nextEvent, timeOut);
}	

// MOVE THE EVENTS CONTAINER
function moveEvents(event){
	$("#mainEvents #eventContainer").animate({
		'left' : -307 * event
	});
	
	$("#mainEvents .eventsNav a").eq(currEvent).html('<img src="./source/images/circleOff.jpg"/>');
	$("#mainEvents .eventsNav a").eq(event).html('<img src="./source/images/circleOn.jpg"/>');
	
	currEvent = event;
}

function initTimer () {
	// HOVER EVENTS
	$("#mainEvents #eventContainer").bind('mouseover', function(){
		clearTimeout(timer);
	}).bind('mouseout', function(){
		timer = setTimeout(nextEvent, timeOut);
	});	
	
	// INIT THE TIMER
	timer = setTimeout(nextEvent, timeOut);
}

function initFeatures(){
	$('#featureNav a').bind('click', function(e){
		$('#featureNav a').fadeTo(1,1);
		$(this).fadeTo(200 ,0.5);	
	});	

	
	$('#feature #videos img').bind('mouseenter', function(e){
		$(this).stop().fadeTo(200, 1.0);
	}).bind('mouseleave', function(){
		if (!$(this).hasClass("selected")){
			$(this).stop().fadeTo(200, 0.5);
		}
	}).bind('click', function(){
		$('#feature #videos img').stop().fadeTo(200, 0.3);
		$('#feature #photos img').removeClass('selected');
		$(this).stop().fadeTo(200, 1.0);
		$(this).addClass('selected');
		var theSRC = $(this).attr('alt');
		var content = '<iframe width="520" height="415" src="http://www.youtube.com/embed/' + theSRC + '" frameborder="0" allowfullscreen></iframe>';
		$("#featureModal").delay(100).html(content).fadeIn(250);
	});
	
	$('#feature #photos img').bind('mouseenter', function(e){
		$(this).stop().fadeTo(200, 1.0);
	}).bind('mouseleave', function(){
		if (!$(this).hasClass("selected")){
			$(this).fadeTo(200, 0.5);
		}
	}).bind('click', function(){
		$('#feature #photos img').stop().fadeTo(200, 0.3);		
		$('#feature #photos img').removeClass('selected');
		$(this).addClass('selected');
		$(this).stop().fadeTo(200, 1.0);
		
		var theSRC = $(this).attr('src');
		$('#featureBackground').attr('src', theSRC);
	});	
}

// +++++++++++++++++++++++++++++++++++++++++++++
// FINAL
// +++++++++++++++++++++++++++++++++++++++++++++
$(document).bind('ready', function (){

	// CLICK EVENTS
	$("#mainEvents .eventsNav a").bind('click', function (e){
		clearTimeout(timer);
		num = $("#mainEvents .eventsNav a").index(this);
		moveEvents(num);
		timer = setTimeout(nextEvent, timeOut * 2);
		
		return false;
	});	

	initTimer();
});


