////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// prepareLinks()
	//
	//
	//
function prepareLinks() {
	var links = document.getElementsByTagName("a");
	for(var i=0; i<links.length; i++) {
		var rel = links[i].getAttribute("rel");
		if(rel == "external") {
			links[i].setAttribute("target", "_blank");
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// addClass()
	//
	//
	//
function addClass(element, value) {
	if(!element.className) {
		element.className = value;	
	}else{
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// smartIndividualInput( theField )
	//
	//
	//
function smartIndividualInput(theField) {
	var searchField = $(theField);
	if(!searchField) return false;
	searchField.onfocus = function() {
		if(this.value == this.defaultValue) {
			this.value = "";
		}
	}
	searchField.onblur = function() {
		if(this.value == "") {
			this.value = this.defaultValue;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	//smartInputs()
	//
	//
	//
function smartInputs(theForm) {
	var form = $(theForm);
	if(!form) return false;

	var errorClass;

	for(var i=0; i < form.elements.length; i++) {
		var element = form.elements[i];
		if (element.type == 'submit') continue;
		if (!element.defaultValue) continue;
		element.onfocus = function() {
			if(this.type == 'text') {
				
				if(this.value == this.defaultValue) {
					this.value = "";
				}
				
				if(this.className != '') {
					errorClass = this.className;
					this.className = '';
				}
			}else{
				if(this.value == this.defaultValue && this.value.indexOf("message") != -1) {
					this.value = "";
				}
			}
		}
		element.onblur = function() {
			if(this.value == "") {
				this.value = this.defaultValue;
				this.className = errorClass;
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	// fadeElem()
	//
	//
function fadeElem(element, speed, start, end) {
	 new Effect.Opacity(element, {duration:speed, from:start, to:end});
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	//displayLoading()
	//
	//
	//
function displayLoading(element, value) {
	var button = document.getElementById(element);
	button.setAttribute("value", value);
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// removeConfirmations();
	//
	//
	//
function removeConfirmations() {
	var paras = document.getElementsByTagName("p");
	for(var i=0; i<paras.length; i++){
		if(paras[i].className == "confirm" || paras[i].className == "removed"){
			var feedback = paras[i];
			setTimeout(function() {new Effect.Fade(feedback);}, 6000);
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// ajaxifyForm(form_id, url, loadingElem)
	//
	//
	//
function ajaxifyForm(form_id, url, loadingElem, loadingMsg, optCallback) {
	if(document.getElementById(form_id)) {
		var theForm = document.getElementById(form_id);
		var xhr = new Hijax();
		xhr.setContainer(theForm);
		xhr.setUrl(base_path + url);
		xhr.setCanvas(theForm.parentNode);
		if(loadingElem != '') {
			xhr.setLoading(function() {
				displayLoading(loadingElem, loadingMsg);
			});
		}
		xhr.setCallback(function() {
			ajaxifyForm(form_id, url, loadingElem, loadingMsg, optCallback);
			removeConfirmations();
			
			if(optCallback) {
				optCallback.each(function(item) {
				  item();
				});
				
			}
		});
		xhr.captureData();
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// initPageScroll( htmlelem [container], classname [top_class]);
	// Grabs all links in [container], sets their onclick to scroll to an internal anchor
	// 
	// Grabs all a tags with [top_class], sets their onclick to scroll to an internal anchor
	//
	//
	//
function initPageScroll(container, top_class) {
	var container = $(container);
	if(!container) return false;
	
	
	// Prepare main links
	//
	var links = container.getElementsByTagName('a');
	bindScrollLinks(links);
	
	
	// Prepare links to scroll back to top, or werever
	//
	var top_links = $$('a.' + top_class);
	bindScrollLinks(top_links);
}

// This is probably a misnomer, since I'm pretty sure this wouldn't be considered binding; but whatever I don't care.
//
function bindScrollLinks(collection) {
	for(var i=0; i < collection.length; i++) {
		collection[i].destination = collection[i].getAttribute('href').split('#')[1];
		collection[i].onclick = function() {
			new Effect.ScrollTo(this.destination);
			return false;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	//writeFlashContent()
	//
	//
	//
function writeFlashContent(swfPath, embedId, version, backgroundColor, containerId, swfWidth, swfHeight, argName, argValue) {	
	var swfPath = swfPath;
	var embedId = embedId;
	var version = version;
	var backgroundColor = backgroundColor;
	var containerId = containerId;
	var swfWidth = swfWidth;
	var swfHeight = swfHeight;

	if(!$(containerId)) return false;
	var swfContainer = $(containerId);
	
	
	var so = new SWFObject(swfPath, embedId, swfWidth, swfHeight, version, backgroundColor);
	so.addParam("scale", "noscale");
	so.addParam("menu","false");
	so.addParam("salign", "tl");
	so.addParam("allowScriptAccess","always");
	if(backgroundColor == 'transparent') {
		so.addParam("wmode", "transparent");
	}
	if(argName && argValue) {
		so.addVariable(argName, argValue);
	}
	so.write(swfContainer);	
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	// pageTitleTextReplace()
	//
	//
	//
function pageTitleTextReplace(elem_id, swf_width, swf_height, swf_path) {
	
	/*
	 * You know anyone who uses IE 6? Smack them in the eyeball
	 *
	*/
	var browser = navigator.userAgent;
	if(browser.indexOf('MSIE 6.0') != -1) return false;
	
	var header = $(elem_id);
	if(!header) return false;
	
	header.style.textIndent = '-5000em';
	header.style.position   = 'relative';
    
	var header_value = header.firstChild.nodeValue;
	var embed_id     = "swfPageTitle";
	var holder       = document.createElement('span');
	
	holder_id               = 'swf_holder';
	holder.style.position   = 'absolute';
	holder.style.textIndent = '0em';
	holder.style.left       = '0em';
	holder.style.top        = '10px';
	holder.style.display    = 'block';
	holder.setAttribute('id', holder_id);
	header.appendChild(holder);
	
	writeFlashContent(swf_path, embed_id, '9', 'transparent', holder_id, '0', '0', 'heading_text', header_value);

	var embedded_swf          = $(embed_id);
	embedded_swf.style.width  = swf_width + "px";
	embedded_swf.style.height = swf_height + "px";
}


function init_mini_gallery() 
{
	var container = $('sidebar_gallery');
	if(!container) return false;
	var url = 'home_gallery.php?image=';
	
	var control_links = $$('a.direction_control');
	for(var i = 0; i < control_links.length; i++)
	{
		var t = control_links[i];
		var query = t.getAttribute('href').split(':');
		t.destination = query[query.length - 1];
		t.onclick = function()
		{
			new Ajax.Updater(container, url + this.destination, { method: 'get', onComplete: mini_gal_callback });
			return false;
		}
	}
}

function mini_gal_callback() 
{
	init_mini_gallery();
}


////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//
	//
	//addLoadEvent()
	//
	//
	//
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	}else{
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function init_contact_form() {
	smartInputs('contact_form');
}

////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//call functions on page load with addLoadEvent
	//
addLoadEvent(function(){
	prepareLinks();
	ajaxifyForm('contact_form', 'contact_form.php', 'send_btn', 'Sending...', new Array(init_contact_form));
	smartIndividualInput('login_email');
	smartIndividualInput('login_pass');
	smartInputs('contact_form');
	writeFlashContent('swf/video_player.swf', 'video_player', '8', '#000000', 'home_video', '441', '255');
	writeFlashContent('swf/imageGallery.swf', 'image_gallery', '8', '#76A9D1', 'sidebar_gallery_holder', '284', '329','url',window.location);
	pageTitleTextReplace('page_title', '580', '35', 'swf/page_heading.swf');
	init_mini_gallery();
});


////////////////////////////////////////////////////////////////////////////////////////////////
	//
	//open Lightox from flash
	//
	
function openSlimbox(image,desc){
	if(!desc) desc = "&nbsp;";
	Slimbox.open("http://www.metropolis-productions.com/"+image, desc);
}
