﻿/*
	This file was developed for StGeorgeHall.com by Roman Dmitri © 2011
	Last update: 10/3/2011 2:09 PM
*/

/* ======= UTILITY ============================================================= */

function loadFileTXT(fileNameTXT, callFunction){
	
	if (window.XMLHttpRequest) { var xmlhttp = new XMLHttpRequest(); } // Modern Browsers
	else { var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } // Internet Explorer 5/6

	xmlhttp.open('GET', fileNameTXT, false);
	xmlhttp.send('');
	
	callFunction(xmlhttp.responseText);
}

// asynchronous version of loadFileTXT ... execute callFunction upon completion
function asyncFileTXT(fileNameTXT, callFunction){
	
	if (window.XMLHttpRequest) { var xmlhttp = new XMLHttpRequest(); } // Modern Browsers
	else { var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } // Internet Explorer 5/6

	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{ callFunction(xmlhttp.responseText); }
	}
	
	xmlhttp.open('GET', fileNameTXT, true);
	xmlhttp.send('');
}

// change innerHTML of given element with asyncFileTXT
function dumpScript(scriptLink, divName){
	
	var divHandle = document.getElementById(divName);
		divHandle.innerHTML = 'loading ' + divName + '...';
		
	asyncFileTXT(scriptLink, function(divString) { divHandle.innerHTML = divString; });
}

// above followed by callFunction
// NOTE** use Function('callFunction(\'variable\')'); to pass with variables!
function dumpScriptCall(scriptLink, divName, callFunction){
	
	var divHandle = document.getElementById(divName);
		divHandle.innerHTML = 'loading ' + divName + '...';
		
	asyncFileTXT(scriptLink, function(divString) { divHandle.innerHTML = divString; callFunction(); });
}

/* ======= GALLERY ============================================================= */

var imageRotate_id = 0;

function imageInit(){
	setImage(1); // photoIndex 1 is activeIndex 0
	image_startRotation();
}

function setImage(photoIndex){
	
	var photoCount = parseInt($('div#image div#properties div#photoCount').text());
	
	var photoFile = $('div#image div#photoDetails_'+photoIndex+' div#photoFile').text();
	var photoNote = $('div#image div#photoDetails_'+photoIndex+' div#photoNote').html();
	var photoNote_show = $('div#image div#photoDetails_'+photoIndex+' div#photoNote_show').text();
	
	$('div#image div#image_back').css({'background-image':'URL("media/photos/'+photoFile+'")'});
	$('div#image img#image_src').attr('src','media/photos/'+photoFile);
	
	$('div#noteBlock span#photoNote').html(photoNote);
	$('div#noteBlock_container').css({'display':photoNote_show});
	
	$('div#image div#properties div#photoIndex').text(photoIndex);
	$('div#image span#photoIndex_status').text(photoIndex+' of '+photoCount);
}

function image_startRotation(){ imageRotate_id = setInterval('navClick(\'next\')', 3000); }

function navRoll_over(navDirection){ clearInterval(imageRotate_id); $('div#nav_'+navDirection).css({'background-image':'URL("media/layout/ornate/arrow_'+navDirection+'_roll.png")'}); }
function navRoll_out(navDirection){ $('div#nav_'+navDirection).css({'background-image':'URL("media/layout/ornate/arrow_'+navDirection+'.png")'}); image_startRotation(); }

function navClick(navDirection){
	
	var photoIndex = parseInt($('div#image div#properties div#photoIndex').text());
	var photoCount = parseInt($('div#image div#properties div#photoCount').text());
	
	var newIndex = photoIndex;

	switch (navDirection) {
		case 'prev': newIndex = (photoIndex > 1) ? photoIndex - 1 : photoCount; break;
		case 'next': newIndex = (photoIndex < photoCount) ? photoIndex + 1 : 1; break;
	}

	setImage(newIndex);
}

function photoRoll_over(){ clearInterval(imageRotate_id); show_noteBlock();  }
function photoRoll_out(){ hide_noteBlock(); image_startRotation(); }

function show_noteBlock() { $('div#noteBlock').stop().show(); }
function hide_noteBlock() { $('div#noteBlock').stop().hide(); }

/* ======= TOURS =============================================================== */

var tourAnimate = new Array();
//var tourDisplay = new Array();

function initTour(tourName) {
	tourAnimate[tourName] = true; animateTour(tourName, 1);
	//tourDisplay[tourName] = $('div#tour_'+tourName+' div.tourDisplay');
}

function animateTour(tourName, tourDirection){
	if (tourAnimate[tourName]) {
		var tourDisplay = $('div#tour_'+tourName+' div.tourDisplay');
		tourDisplay.animate({backgroundPosition: '-='+tourDirection*300+'px'}, 10000, 'linear', function(){ animateTour(tourName, 1); });
	}
}

function tourEvent_over(tourName){
	tourAnimate[tourName] = false;
	$('div#tour_'+tourName+' div#noteBlock').stop().show();
	$('div#tour_'+tourName+' div.tourDisplay').stop(true);
}

function tourEvent_out(tourName){
	$('div#tour_'+tourName+' div#noteBlock').stop().hide();
	tourAnimate[tourName] = true;
	animateTour(tourName, 1);
}

function show_descBlock() { $('div#descBlock').stop().show(); $('div#tourBlock').stop().hide(); }
function show_tourBlock() { $('div#descBlock').stop().hide(); $('div#tourBlock').stop().show(); }















