var app = {
	name : navigator.appName.toLowerCase()	
	,
	version : navigator.appVersion.toLowerCase()	
	,
	code :  navigator.appCodeName.toLowerCase()	
	,
	platform : 	navigator.platform.toLowerCase()	
	,
	is_name : function(w) {
		check = this.name;
		if(check.indexOf(w) != -1) {
			return true;	
		}
		else {
			return false;	
		}
	}
	,
	is_version : function(w) {
		check = this.version;
		if(check.indexOf(w) != -1) {
			return true;	
		}
		else {
			return false;	
		}	
	}
	,
	is_code : function(w) {
		check = this.code;
		if(check.indexOf(w) != -1) {
			return true;	
		}	
		else {
			return false;	
		}
	}
	,
	is_platform : function(w) {
		check = this.platform;
		if(check.indexOf(w) != -1) {
			return true;	
		}	
		else {
			return false;	
		}
	}
	,
	is : function() {
		check = this.code + this.platform + this.version + this.name;
		if(check.indexOf(w) != -1) {
			return true;	
		}	
		else {
			return false;	
		}	
	}
}


if( (app.is_version("safari")) && (app.is_platform("mac")) ) {

	custom("safari");
}
if( (app.is_name("opera")) && (app.is_platform("win")) ) {
	custom("opera");	
}


function custom(browser) {
	document.write('<style type="text/css">');
	switch(browser) {
	
	case "safari" : document.write('   form { line-height:16px; } ');
		break;
	case "opera" : document.write(' form { line-height:14px; } ');
		break;
	}
	document.write('</style>');
}

var form = {
	length : "1"
	,
	check : function(target, fields) {
		var check = Array();
		var f = document.forms[target];
		for(i=0; i<fields.length; i++) {
			var field = fields[i];
			var elem = f.elements[field];
			var value = elem.value;
			var defaultValue = elem.defaultValue;
			
		if(field.toLowerCase().indexOf("email") != -1) {
				if(!this.email(target, field)) {
					check.push(field);	
					continue;
				}
			}
		if( (value.length < this.length) || (value == defaultValue) ) {
				check.push(field);	
			}
		
		}
		
		if(check.length == 0) {
			return true;	
		}
		else {
			var out = "Please amend the following before submitting:\n";
			for(i=0; i<check.length; i++) {
				out +=  check[i]+" \n";
			}
			alert(out);
			f.elements[check[0]].focus();
			return false;	
		}
	}	
	,
	email : function(target, field) {
		var email = document.forms[target].elements[field].value;
		var atSym = email.indexOf('@');
		var dot = email.lastIndexOf('.');
		var space = email.indexOf(' ');
		var len = email.length;
		if (atSym < 1 || dot < atSym || len - dot <= 2 || space != -1) {
			return false;
		}
		else { 
			return true; 
		}
	}
	,
	values : function(target) {
		var f = document.forms[$form];
		var out = "";
		for(i=0; i<f.elements.length; i++) {
			var elem = f.elements[i].name;
			var value = f.elements[i].value;
			out += "&"+elem+"="+value;
		}
		return out;	
	}
	,
	prep : function(target) {
		var f = document.forms[target];
		for (var i=0; i<f.elements.length; i++) {
			var element = f.elements[i];
			if (element.type == "reset"||element.type == "submit"||element.type == "radio"||element.type == "checkbox") { continue };
			if (!element.defaultValue) { continue };
			element.onfocus = function() {
				if (this.value == this.defaultValue) {
					this.value = "";
				}
			}
			element.onblur = function() {
				if (this.value == "") {
					this.value = this.defaultValue;
				}
			}
		}
	}
}


function Gallery(target, images) {

	this.target = target;
	this.images = images;
	this.current = 1;
	this.delay = 4000;
	
	//Statics
	this.interval;
	var self;
	
	this.next = function() {
		this.render(this.images[this.current]);
		if(this.current == this.images.length-1) { 
			this.current = 0; 
		}
		else {
			this.current++;	
		}
		
	}
	
	this.previous = function() {
		alert("then "+this.current);
		if(this.current < 0) { 
			this.current = this.images.length; 
		}
		else {
			this.current--;	
		}
		alert("now "+this.current);
		this.render(this.images[this.current]);
	}
	
	this.render = function(imgSource) {
		
		var imgContainer = document.getElementById(this.target);
		if(imgContainer.childNodes.length > 1) {
			imgContainer.removeChild(imgContainer.firstChild);	
		}
		var lastElement = imgContainer.lastChild;
		var newElement = document.createElement("img");
		var id = "image_" + this.current;
		var newImage = new Image();
		newImage.src = imgSource;
		newElement.id = id;
		newElement.src = newImage.src;
		newElement.className = "newImage";
		this.insert(newElement, lastElement);
		this.transition(id);
	}
	
	this.transition = function(target) {
		Effect.Appear(target);
	}
	
	this.insert = function(newElement, targetElement) {
		var parent = targetElement.parentNode;
		if(parent.lastChild == targetElement) {
			parent.appendChild(newElement);
		}
		else {
			parent.insertBefore(newElement, targetElement.nextSibling);	
		}
	}
	
	this.play = function() {
		self = this;
		this.interval = setInterval(handlePlay, this.delay);
	}
	
	function handlePlay() {
		self.next();	
	}
	
	this.stop = function() {
		clearInterval(this.interval);	
	}

}


var imgs = new Array("gfx/banner1.jpg", "gfx/banner2.jpg", "gfx/banner3.jpg", "gfx/banner4.jpg", "gfx/banner5.jpg", "gfx/banner6.jpg");
var myGal1 = new Gallery("holder", imgs);

var Elem = {
	shown : "elemShown"
	,
	hidden : "elemHidden"
	,
	show : function(target) {
		$$(target).className = this.shown;
	}
	,
	hide : function(target) {
		$$(target).className = this.hidden;
	}

}

var Content = {
	target : "target"
	,
	elems : Array("1", "2", "3", "4", "5", "6")
	,
	swap : function(elem) {
		this.change(elem);
		//document.getElementById('target').innerHTML = html;
	}
	,
	change : function(elem) {
		if(elem != 0) {
			for(i=0; i<this.elems.length; i++) {
							
				if(this.elems[i] == elem) {
					document.getElementById(elem).className = "selected";
					document.getElementById("content" + this.elems[i]).className = "elemShown";
				}
				else {
					document.getElementById(this.elems[i]).className = "link";
					document.getElementById("content" + this.elems[i]).className = "elemHidden";
				}
			}
		}
	}

	
}








