//Page titles for dynamic galleries...
ssTitle = "Slideshow Stories";
docTitle = " - Wedding Photography | Gillespie Photography";

//RESIZE THE PAGE TO FIT THE BROWSER...

//set on page... so this is kind of redundant
var min_height = 550;

//this is the aspect ratio that we want to use... (width/height)
var max_aspect = 2.0;

var height_header=115;
var height_footer=55+2;
var height_pad=16;

//is it loaded yet?
var flash_load=false;
var flash_stall=true;

//================================
// FUNCTIONS TO RUN BEFORE LOAD...
//================================

//only if a height is set...
if(typeof(set_height)=="number"){
	window.onload=function(){ resizeFlash();setTimeout("checkStall();",10000); }
	window.onresize=function(){ resizeFlash(); }
}

//if there's a hash that's a url, redirect to that url
if(window.location.hash && window.location.hash.indexOf("/")>-1) {
	document.location = "http://www.gillespiephoto.com"+window.location.hash.substr(1);
}

//================================
// NORMAL FUNCTIONS
//================================

function checkStall(){
	if(flash_stall && typeof(flashvars)=="object"){
		document.getElementById("loading").innerHTML="<h2>Sorry, an error has occurred.</h2><br /><h4>We're so sorry our website could not be loaded.  We'd love to show you our photos, but <br />unfortunately a technical glitch is temporarily preventing our galleries from loading.</h4><br />Please <a href=\"http://get.adobe.com/flashplayer/\">install Flash&reg;</a> and turn on Javascript.  If you continue to experience this problem, please contact us.";
		//alert('Movie is stalled… please refresh page.\n\nPlease notify your administrator.');
	}
}

function resizeFlash(){
	var new_height=get_height()-(height_header+height_footer+height_pad);
	
	//if we have a better minheight, use that
	if(typeof(set_height)=="number")
		min_height=set_height;
	//if we have a better max_aspect, use that
	if(typeof(set_aspect)=="number")
		max_aspect=set_aspect;
	
	//we need the height to be at least 600px...
	if(new_height<min_height)
		new_height=min_height;
	if(new_height>2000)
		new_height=min_height;
		
	//only if there is flash...
	if(typeof(flashvars)=="object"){	
		//lets figure out how wide the browser window is...
		c_width = document.getElementById("photocont").offsetWidth;
		
		//calculate the aspect ratio...
		c_aspect = c_width/new_height;
		
		//if the width is too big, let's make it a bit taller...
		if(c_aspect>max_aspect)
			new_height = c_width/max_aspect;
		
	}else{
	//text page...
		var min_height=document.getElementById("contentcont").offsetHeight;
		if(new_height<min_height)
			new_height = min_height;
	}
	
	if(typeof(flashvars)=="object"){
		document.getElementById("photo").style.height=new_height+'px';
		document.getElementById("slideshow").style.height=new_height+'px';
		document.getElementById("photocont").style.height=new_height+'px';
	}else{
		document.getElementById("content").style.height=new_height+'px';
	}
}

function get_height() {
	return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}

function initializeSlideshow(){	
	//not stalled!
	flash_stall=false;
	
	var slideshowElem = document.getElementById("slideshow");
	slideshowElem.addListener("itemLoaderComplete", "onItemLoaderComplete");
}
		
function onItemLoaderComplete(){
	//let everyone know it loaded!
	flash_load=true;
	
	var slideshowElem = document.getElementById("slideshow");
	var loadingElem = document.getElementById("loading");
	slideshowElem.removeListener("itemLoaderComplete", "onItemLoaderComplete");
	
	//first object is loaded...
	loadingElem.style.display="none";
}

function changeGallery(gal,size,obj){
	if(flash_load && typeof(set_height)=="number" && set_height==size){
		var slideshowElem = document.getElementById("slideshow");
		var loadingElem = document.getElementById("loading");
		
		//load xml
		if(gal=="VIDEO")
			slideshowElem.loadDataFile("xml/video_xml.php?d="+gal);
		else
			slideshowElem.loadDataFile("xml/gallery_xml.php?d="+gal);
			
		//show the loading thingy...
		loadingElem.style.display="block";
		//take away the loader after first image loads...
		slideshowElem.addListener("itemLoaderComplete", "onItemLoaderComplete");
		
		//highlight the object...
		unHighlight();
		obj.className="selected";
		//put the page url to the right one...
		if(gal=="VIDEO"){
			document.title = ssTitle + docTitle;
			window.location.hash="/videos/";
		}else{
			document.title = ucfirst(gal) + docTitle;
			window.location.hash="/gallery/"+gal+"/";
		}
			
		
		//don't follow the link
		return false;
	}else{
		return true;
	}
}

function ucfirst (str) {
    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}

function unHighlight(){
	var arr = new Array();
	arr = document.getElementsByTagName( "a" );
	for(var i=0; i < arr.length; i++)
	{
	    var tagObj = arr.item(i);
	    if(tagObj.className=="selected")
			tagObj.className="";           
	}
}

function writeEmail(user){

	var domain = "gillespiephoto";
	var suffix = "com";
	
	document.write('<a href="' + 'mailto:' + user + '@' + domain + '.' + suffix + '">' + user + '@' + domain + '.' + suffix + '</a>');

}


function checkEmail(str){
 var filter=/^.+@.+\..{2,4}$/

 if (filter.test(str))
    return true;
 else 
 	return false;
}

function checkForm(theform){
	if(theform.name.value.length<=0){
		alert("Please enter your full name.");
		theform.name.focus();
		return false;
	}
	if(theform.email.value.length<=0){
		alert("Please enter your e-mail address.");
		theform.email.focus();
		return false;
	}
	if(!checkEmail(theform.email.value)){
		alert("Please enter a valid e-mail address.");
		theform.email.focus();
		return false;
	}
	if(theform.subject.value.length<=0){
		alert("Please enter a subject for your message.");
		theform.subject.focus();
		return false;
	}
	if(theform.message.value.length<=0){
		alert("Please enter a message or details.");
		theform.message.focus();
		return false;
	}
	theform.action.value=unescape("%73%65%6E%64");
	return true;
}
