/**
*
* COPYRIGHT JOHN SHERLOCK 2008
*
*/

var divContents;
var currentContent;
var galleryPath;
var galleryWidth;
var galleryHeight;

var ie = BrowserDetect.browser == 'Explorer';
var ff = BrowserDetect.browser == 'Firefox';
var ch = BrowserDetect.browser == 'Chrome';
var saf = BrowserDetect.browser == 'Safari';

var ayla = {
	id : 'ayla',
	password : 'lowe',
	galleryPath : 'images/galleries/ayla/gallery.xml'
}	

var gwenAndTrevor = {
	id : 'gwenandtrevor',
	password : 'rome',
	galleryPath : 'images/galleries/gwenandtrevor/gallery.xml'
}	
	
var siobhanAndJames = {
	id : 'siobhanandjames',
	password : 'sligo',
	galleryPath : 'images/galleries/siobhanandjames/gallery.xml'
}	
	
var tanzania = {
	id : 'tanzania',
	password : 'tanzania',
	galleryPath : 'images/galleries/tanzania/gallery.xml'
}

var roAndMyles = {
	id : 'roandmyles',
	password : 'boston',
	galleryPath : 'images/galleries/roandmyles/gallery.xml'
}

var lornaAndErik = {
	id : 'lornaanderik',
	password : 'newtown',
	galleryPath : 'images/galleries/lornaanderik/gallery.xml'
}

var claireAndDamien = {
	id : 'claireanddamien',
	password : 'owens',
	galleryPath : 'images/galleries/claireanddamien/gallery.xml'
}

var annAndSean = {
	id : 'annandsean',
	password : 'greystones',
	galleryPath : 'images/galleries/annandsean/gallery.xml'
}

var pat = {
	id : 'pat',
	password : 'surprise',
	galleryPath : 'images/galleries/patsbirthday/gallery.xml'
}

var claireAndBrian = {
	id : 'claireandbrian',
	password : 'bray',
	galleryPath : 'images/galleries/claireandbrian/gallery.xml'
}

var emmaAndSeanWicklow = {
	id : 'emmaandseanwicklow',
	password : 'wicklow',
	galleryPath : 'images/galleries/emmaandseanwicklow/gallery.xml'
}

var emmaAndSeanVegas = {
	id : 'emmaandseanvegas',
	password : 'lasvegas',
	galleryPath : 'images/galleries/emmaandseanvegas/gallery.xml'
}

var eileenAndTom = {
	id : 'eileenandtom',
	password : 'ennis',
	galleryPath : 'images/galleries/eileenandtom/gallery.xml'
}

var clientGalleries = $H();
clientGalleries.set(ayla.id, ayla);
clientGalleries.set(gwenAndTrevor.id, gwenAndTrevor);
clientGalleries.set(siobhanAndJames.id, siobhanAndJames);
clientGalleries.set(tanzania.id, tanzania);
clientGalleries.set(roAndMyles.id, roAndMyles);
clientGalleries.set(lornaAndErik.id, lornaAndErik);
clientGalleries.set(claireAndDamien.id, claireAndDamien);
clientGalleries.set(annAndSean.id, annAndSean);
clientGalleries.set(pat.id, pat);
clientGalleries.set(claireAndBrian.id, claireAndBrian);
clientGalleries.set(emmaAndSeanWicklow.id, emmaAndSeanWicklow);
clientGalleries.set(emmaAndSeanVegas.id, emmaAndSeanVegas);
clientGalleries.set(eileenAndTom.id, eileenAndTom);


var screens = new Array();

function addScreen(screen)
{
	screens.push(screen);
}

function changeOpacity1(divId, goal, increment, nextFunctionCondition, nextFunction)
{
	var div = $(divId);
	var finished = false;

	if(div.style.visibility == 'hidden')
	{
		div.style.opacity = 0;
		div.style.filter = 'alpha(opacity=0)';
	}
	var currentOpacity = getOpacity(div);

	var opacDiff = goal - currentOpacity;

	if ((opacDiff >= 0 && opacDiff-increment< 0) ||
		(opacDiff <= 0 && widthDiff+increment > 0))
	{
		div.style.opacity = goal/100;
		div.style.filter = 'alpha(goal=' + goal + ')';
		finished = true;

		if(currentOpacity == 0)
			div.style.visibility = 'hidden'
		else
			div.style.visibility = 'visible'
	}
	else
	{
		if(currentOpacity < goal)
		{
			div.style.opacity = (currentOpacity+increment)/100;
			div.style.filter = 'alpha(opacity=' + (currentOpacity+increment) + ')';
		}
		else
		{
			div.style.opacity = (currentOpacity-increment)/100;
			div.style.filter = 'alpha(opacity=' + (currentOpacity-increment) + ')';
		}
		if(currentOpacity+increment <= 0)
			div.style.visibility = 'hidden'
		else
			div.style.visibility = 'visible'

		setTimeout(function(){changeOpacity(divId, goal, increment, nextFunctionCondition, nextFunction);},100);
	}

	if((nextFunctionCondition!=null && eval(nextFunctionCondition)==true) ||
		(nextFunctionCondition == null && finished))
	{
		callNextFunction(nextFunction);
	}
}

var highlighted = null;
var working = false;
var next = null;

function highlightBox(num, newReq)
{
	var divs = new Array('','music','landscape','wedding','nature','portrait');

	var goAgain = false;

	if((working && !newReq) || (!working && newReq && highlighted != num) || (!working && newReq && highlighted == null))
	{
		if(highlighted == null)
			highlighted = num;

		if (next == num)
			next = null;
						
		working = true;
		
		for (i=1;i<divs.length;i++)
		{
			var div = $(divs[i]);

			var width = getIntValue(div.style.width);

			if(num == i && width <260)
			{
				setDivOpacity(div, 100);
				if(width + 32>=260)
				{
					div.style.width = 260;
					
				}
				else
				{
					div.style.width = width + 32;
					goAgain = true;
				}
			}
			else if(width > 80)
			{
				setDivOpacity(div, 40);

				if(highlighted == i)
				{
					if((width-32) <= 80)
					{
						div.style.width = (ie)?80:78
					}
					else
					{
						div.style.width = width - 32;
						goAgain = true;
					}
				}
				else
				{
					if((width-8) <= 80)
					{
						div.style.width = (ie)?80:78
					}
					else
						div.style.width = width - 8;			
				}
			}
		}
		if(goAgain)
		{
			setTimeout(function(){highlightBox(num);},20);		
		}
		else
		{
			working = false;
			highlighted = num;
			if(next != null)
			{
				setTimeout(function(){highlightBox(next, true);},20);				
			}
		}
	}
	else if(highlighted != num)		
		next = num;
		
	//status = 'next: ' + next + 'working: ' + working;
}

function fadeInLogo(step, nextFunction)
{
	var logostub = $('logostub');
	var logotail = $('logotail');
	
	logostub.style.visibility = 'visible';
	logotail.style.visibility = 'visible';

	var steps = new Array(
		-607, -502, -475, -425, -385, -355, -335, -320, -312, -300,
		7, 12, 22, 37, 57, 87, 127, 177, 232, 314
	);

	if(!step)
		step = 0;

	if(step < 10)
	{
		logostub.style.left = steps[step]+'px';
		logotail.style.left = steps[steps.length-(step+1)]+'px';

		step = step+1;
		setDivOpacity(logostub, step*10);
		setDivOpacity(logotail, step*10);
		setTimeout(function(){fadeInLogo(step, nextFunction)},20);
	}
	else if(nextFunction)
	{
		callNextFunction(nextFunction);
	}
}


function revealMenuItem(menuDivId, nextFunction)
{
	var div = $(menuDivId);
	div.style.visibility = 'visible';

	move(menuDivId, 0,null,1, 'div.offsetTop == 10', nextFunction);
}

function triggerNextMenuCondition(div)
{
	return div.offsetTop == -10;
}

function goBack()
{
	screens.pop();
	var screen = screens.pop();
	if(screen == 'gallery' || screen == 'slideshow')
	{
		showGallery();
		fadeOut('back');
	}
	else if(screen == 'about')
		showAbout();
	else if(screen == 'clientArea')
		showClientArea();
	else if(screen == 'clientGallery')
		showClientGallery();
		
}

function goHome()
{
	fadeOut(currentContent,null,'finished == true');
	showGallery();
	fadeOut('back');	
}


function showAbout()
{
	if(currentContent != 'about')
	{
		var functions = new Array(
			function(){fadeOut(currentContent,null,'finished == true')},
			function(){resizeHeight('contentWindow', 15, 400, 'finished==true')},
			function(){resizeWidth('contentWindow', 15, 600, 'finished==true')},
			function(){fadeIn('about',100,'finished == true')},
			function(){setCurrentContent('about')},
			function(){fadeIn('back',100,null)}			
		);
		executeFunctions(functions);
	}
}

function showClientArea()
{
	if(currentContent != 'clientArea')
	{
		var functions = new Array(
			function(){fadeOut(currentContent,null,'finished == true')},
			function(){resizeHeight('contentWindow', 15, 400, 'finished==true')},
			function(){resizeWidth('contentWindow', 15, 600, 'finished==true')},
			function(){fadeIn('clientArea',100,'finished == true')},
			function(){setCurrentContent('clientArea')},
			function(){fadeIn('back',100,null)}
		);
		executeFunctions(functions);
	}
}

function setGallerySource(galleryPath)
{
	var clientGalleryDiv = $('clientGallery');
		
	var fo = new SWFObject("viewer.swf", "viewer", galleryWidth, galleryHeight, "7", "#000000");	
	fo.addVariable("preloaderColor", "0xffffff");
	fo.addVariable("xmlDataPath", galleryPath);	
	fo.write("clientGallery");
}

function setOptimalGallerySize()
{
	galleryWidth = 1000;
	galleryHeight = 700;
	
	var bufferedWidth = galleryWidth + 50;
	var bufferedHeight = galleryHeight + 70;
	
	var windowW = document.body.clientWidth;
	var windowH = document.body.clientHeight;
	
	if(windowW >= bufferedWidth && windowH >= bufferedHeight)
	{
		return;
	}
	else
	{
		galleryWidth = windowW-50;
		galleryHeight = (galleryWidth/100)*70;
		
		if(galleryHeight + 70 <= windowH)
			return
		else
		{
			galleryHeight = windowH - 70;
			galleryWidth = (galleryHeight/70)*100;
		}
	}

}


function showClientGallery(gallery)
{
	if(!gallery)
	{
		var user = $('user').value;
		var pass = $('pass').value;
		
		gallery = clientGalleries.get(user);
		
		if(pass != gallery.password)
			gallery = null;
	}
	
	if(gallery)
	{
		setOptimalGallerySize();
	
		galleryPath = gallery.galleryPath;
	
		if(currentContent != 'clientGallery')
		{
			var functions = new Array(
				function(){fadeOut(currentContent,null,'finished == true')},
				function(){resizeWidth('contentWindow', 15, galleryWidth, 'finished==true')},
				function(){resizeHeight('contentWindow', 15, galleryHeight, 'finished==true')},
				function(){fadeIn('clientGallery',100,'finished == true')},
				function(){setCurrentContent('clientGallery')},
				function(){setGallerySource(galleryPath)}
			);
			executeFunctions(functions);
			
			var pageTracker = _gat._getTracker("UA-12345-1");
			pageTracker._trackPageview(gallery.id);
		}
	}
	else
		alert('Username and/or password invalid');
}

function showServices()
{
	if(currentContent != 'services')
	{
		hideDiv('servicesTextBox');
		hideDiv('servicesText');

		var functions = new Array(
			function(){fadeOut(currentContent,null,'finished == true')},
			function(){resizeHeight('contentWindow', 15, 400, 'finished==true')},
			function(){fadeIn('services',100,null)},
			function(){setCurrentContent('services')},
			function(){setDivContents('servicesText',true)},
			function(){fadeIn('servicesTextBox',30,'finished == true')},
			function(){writeDivContents('servicesText', divContents, true)}			
		);

		if(currentContent != null)
		{
			functions.reverse();
			functions.push(function(){fadeOut(currentContent,null,'finished == true')});
			functions.reverse();
		}
		executeFunctions(functions);
	}
}

function showDownload()
{
	if(currentContent != 'download')
	{
		var functions = new Array(
			function(){fadeOut(currentContent,null,'finished == true')},
			function(){resizeHeight('contentWindow', 15, 600, 'finished==true')},			
			function(){fadeIn('download',100,null)},
			function(){setCurrentContent('download')}
		);

		if(currentContent != null)
		{
			functions.reverse();
			functions.push(function(){fadeOut(currentContent,null,'finished == true')});
			functions.reverse();
		}
		executeFunctions(functions);
	}
}

function showGallery()
{
	// reset the div starting positions
	
	setDivTop('music',-400);
	setDivTop('landscape',400);
	setDivTop('wedding',-400);
	setDivTop('nature',400);
	setDivTop('portrait',-400);
	
	setDivWidth('music',(ie)?116:115);
	setDivWidth('landscape',(ie)?116:115);
	setDivWidth('wedding',(ie)?116:114);
	setDivWidth('nature',(ie)?116:114);
	setDivWidth('portrait',(ie)?116:114);

	//$('container').style.width = '600px';

	var functions = new Array(
		function(){fadeOut(currentContent,null,'finished == true')},
		function(){resizeWidth('contentWindow', 15, 600, 'finished==true')},
		function(){resizeHeight('contentWindow', 15, 400, 'finished==true')},
		function(){fadeIn('gallery',100,null)},
		function(){setCurrentContent('gallery')},
		function(){fadeSlide('music',-1,null,0,null,10,null,'count==7')},
		function(){fadeSlide('landscape',-1,null,0,null,10,null,'count==7')},
		function(){fadeSlide('wedding',-1,null,0,null,10,null,'count==7')},
		function(){fadeSlide('nature',-1,null,0,null,10,null,'count==7')},
		function(){fadeSlide('portrait',-1,null,0,null,10, null,'count==7')},
		//function(){fadeIn('contact',100,null)},
		function(){fadeIn('login',100,null)}
	);
	
	highlighted = null;
	
	executeFunctions(functions);
}

function pickGallery(num)
{
	loadGallery(num);
	
	var divs = new Array('','music','landscape','wedding','nature','portrait');

	var functions = new Array();
	
	for(i=1;i<divs.length;i++)
	{
		if(i!=num)
		{
			var divId = divs[i];
			var top = (i%2==0)?400:-400;
			var f = 'function () {\n fadeSlide(\''+divId+'\','+top+',null,1,null,10,null,\'count==10\');\n}';
			functions.push(f);
		}
	}
	var divId = divs[num];
	var top = (num%2==0)?400:-400;
	var f = 'function () {\n fadeSlide(\''+divId+'\','+top+',null,1,null,10,null,\'count==10\');\n}';
	functions.push(f);
	functions.push(function(){resizeHeight('contentWindow', 15, 600, 'finished==true')});
	functions.push(function(){fadeOut(currentContent,null,'finished == true')});
	functions.push(function(){fadeIn('slideshow',100,null)});
	functions.push(function(){setCurrentContent('slideshow')});
	functions.push(function(){fadeIn('back',100,null)});
	executeFunctions(functions);
	
	var pageTracker = _gat._getTracker("UA-12345-1");
	pageTracker._trackPageview(divId);
}

function loadGallery(id)
{
	$('slideshow').innerHTML = '';
	$('slideshow').style.display = 'none';

	var so = new SWFObject("monoslideshow.swf", "SOmonoSlideshow", "600.00", "600.00", "7", "#000000");
	so.addVariable("showLogo", "false");
	so.addVariable("startWithImageID", id);
	so.write("slideshow");
}


function doOnload()
{
	//preloadImages();
}


