/* ==================================== */
/* == Homepage Display Stuff ========== */
/* ==================================== */

// Globalise variables
var a_feedDisplays;
var a_feeds;

var currentFeed = 0;
var i_totalFeeds;
var i_currentPage = 0;
var i_highestPage = 0;

var t;
var tStandard = 5;
var b_cyclePause = 0;
var b_videoPlaying = 0;


// Show next display item
nextFeed = function(){
	currentFeed++;
	if ( currentFeed > (i_totalFeeds-1) ){
		currentFeed = 0;
	}
	displayFeed(currentFeed);
}

// Show a display item
function displayFeed(x) {

	hideAllDisplays();
	closeDisplayVideo();

	a_feedDisplays[x].appear({duration: .5});

	a_feeds.each( function(elt) {
		if ( elt.retrieve("id") == x ){
			new Effect.Opacity(elt, { to: 1, duration: 0.4 });
		} else {
			new Effect.Opacity(elt, { to: .7, duration: 0.4 });
		}
	});

	currentFeed = x;

	var i_currentDisplayFeed_Page = Math.floor(x/4);

	if ( i_currentDisplayFeed_Page != i_currentPage ) {
		changePage(i_currentDisplayFeed_Page);
	}

	runCycleTimer(tStandard);

}

// Hide all the display items
function hideAllDisplays() {
	a_feedDisplays.each( function(elt) {
		elt.hide();
	});
}

// Display the video player and load a youtube video
function loadDisplayVideo(x) {
	b_videoPlaying = 1;
	stopCycleTimer();
	hideAllDisplays();
	$('homeDisplayVideo').appear({duration: .5});
	new Ajax.Updater('homeDisplayVideoLoader', "/data/theocca/scripts/ajax_youtubevideo.php?v=" + x);
}

// Close the video display
function closeDisplayVideo() {
	$('homeDisplayVideo').hide();
	b_videoPlaying = 0;
}


// Timer functions
function runCycleTimer(i_seconds){
	clearTimeout(t);
	if ( !b_videoPlaying && !b_cyclePause ){
		t = setTimeout("nextFeed()",i_seconds*1000);
	}
}

function stopCycleTimer(){
	clearTimeout(t);
}


// Paging functions
function changePage(x){

	var pageIncrement = -405;

	if ( x >= 0 && x <= i_highestPage ){
		$('homeDisplayFeedSlider').morph('margin-top: ' + x*pageIncrement + 'px;', { duration: 0.5 } );
		i_currentPage = x;
	}

	displayFeed(x*4);

	updatePageButtons();
}

function updatePageButtons(){
	if ( i_currentPage == 0 ){
		$('homeDisplayFeedNavPrev').addClassName("buttonInactive");
	} else {
		$('homeDisplayFeedNavPrev').removeClassName("buttonInactive");
	}

	if ( i_currentPage == i_highestPage ){
		$('homeDisplayFeedNavNext').addClassName("buttonInactive");
	} else {
		$('homeDisplayFeedNavNext').removeClassName("buttonInactive");
	}

	var i_currentRangeLower = ((i_currentPage+1)*4)-3;
	var i_currentRangeUpper = Math.min((i_currentPage+1)*4,i_totalFeeds);

	$('homeDisplayFeedCurrentRange').update("Showing " + i_currentRangeLower + "-" + i_currentRangeUpper + " of " + i_totalFeeds );
}


document.observe("dom:loaded", function() {

	// Get all display items and feed items
	a_feedDisplays = $('homeDisplay').select('div.displayItem');
	a_feeds = $('homeDisplayFeedHolder').select('dl');

	i_totalFeeds = a_feeds.length;
	i_highestPage = Math.ceil(a_feeds.length/4)-1;

	// Observes for feed items
	var i = 0;
	a_feeds.each( function(elt) {
		elt.store("id",i);

		// Make entire DL element activate display item on click
		elt.observe("click", function(evt){	
			displayFeed(elt.retrieve("id"));
		});

		// Stop hyperlink in thumbnail hyperlink
		elt.select('dd')[0].select('a')[0].observe("click", function(evt){
			displayFeed(elt.retrieve("id"));
			Event.stop(evt);
		});

		// Catch all YouTube links and load directly into display
		elt.select('a.keyLink').each( function(elt_a){
			if ( elt_a.getAttribute("href").include("youtube") ){
				elt_a.observe("click", function(evt){
					loadDisplayVideo(elt_a.readAttribute("href"));
					Event.stop(evt);
				});
			}
		});

		i++;
	});


	// Observes for display items
	var i = 0;
	a_feedDisplays.each( function(elt) {
		elt.store("id",i);

		var a = elt.select('a')[0];

		a.observe("click", function(evt){
			if ( a.getAttribute("href").include("youtube") ){
				loadDisplayVideo(a.readAttribute("href"));
				Event.stop(evt);
			}
		});
		i++;
	});


	// Video close button
	$('homeDisplayVideoClose').observe("click", function(evt){
		closeDisplayVideo();
		displayFeed(currentFeed);
		Event.stop(evt);
	});


	// Pause functionality when hovering feed or display area
	$('homeDisplayFeed').observe("mouseover", function(evt){
		b_cyclePause = 1;
		stopCycleTimer();
	});
	$('homeDisplayFeed').observe("mouseout", function(evt){
		b_cyclePause = 0;
		runCycleTimer(tStandard*.7);
	});


	// Paging links
	$('homeDisplayFeedNavPrev').observe("click", function(evt){
		changePage(i_currentPage-1);
		Event.stop(evt);
	});
	$('homeDisplayFeedNavNext').observe("click", function(evt){
		changePage(i_currentPage+1);
		Event.stop(evt);
	});

	// Initiate setup
	closeDisplayVideo();
	displayFeed(0);
	runCycleTimer(tStandard);
	updatePageButtons();

});


/* ==================================== */


/* ==================================== */
/* == Student Stories Stuff =========== */
/* ==================================== */

var a_firstnames = new Array("Diana, MJ, Sandy & Mark","Howard","Mark");
var a_lastnames = new Array("","Abe","Harris");
var a_quotes = new Array("I feel like we operate as a family","This is a new chapter in my life","I wanted something that would equip me");
var a_videolinks = new Array("http://www.youtube.com/v/bq8dI2YIxyE","http://www.youtube.com/v/CVReUyKhlXc","http://www.youtube.com/v/LEx1A4NOlBU");
var a_images = new Array("diana_large.png","howardabe_large.png","markharris_large.png");


function changeStudent(i){
	$('studentStoryQuote').update("&quot;" + a_quotes[i] + "&quot;");
	$('studentStoryName').update(a_firstnames[i] + " " + a_lastnames[i]);
	$('studentStoryWatchVideoLink').setAttribute("href", a_videolinks[i] + "?autoplay=1&hd=1&rel=0");
	$('studentStoryWatchVideoLink').update("Watch " + a_firstnames[i] + "'s video");

	$('studentStoryWatchVideoLink').stopObserving("click");
	$('studentStoryWatchVideoLink').observe("click", function(evt){
		Lightview.show({
			href: $('studentStoryWatchVideoLink').href,
			rel: 'flash',
			options: {
				autosize: true,
				topclose: true,
				width: 640,
				height: 385
			}
		});
		Event.stop(evt);
	});

	$('studentStoryImage').setAttribute("src", "/data/theocca/uploads/student_stories/" + a_images[i]);
}

document.observe("dom:loaded", function() {

	var i_student = 0;
	$$('a.studentStoryLink').each( function(e){
		e.store("myId",i_student);
		e.observe("click", function(evt){
			changeStudent(this.retrieve("myId"));
			Event.stop(evt);
		});
		i_student++;
	});

	changeStudent(2);

});




