// Vertically center the website
(function ($) {
$.fn.vAlign = function() {
	return this.each(function() {
		var site_height = 468; // Roughly 90% of the total content area
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = Math.round((ph - ah) / 2);
		$('#bg').css('height', mh + site_height - 45);
		$('#bg-image').css('height', mh + site_height - 45);
		$(this).css('paddingTop', mh);
	});
};
})(jQuery);


$(function() {
	
	$('#registration-form input[type=text], #registration-form textarea').each(function() {
		$(this).attr('rel', $(this).attr('value'));
	});

	$('#registration-form input[type=text], #registration-form textarea').focus(function() {
		if ($(this).attr('value') == $(this).attr('rel')) {
			$(this).attr('value', '');
		}
	});
	
	$('#registration-form input[type=text], #registration-form textarea').blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', $(this).attr('rel'));
		}
	});
	
});


// Image animations
function slideSwitch() {
	// Get the current image
    var $active = $('#slideshow img.active');

	// Select the next image to display
    if ( $active.length == 0 ) $active = $('#slideshow img:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

	// Test if we need to crossfade greyscale images
	var crossfade = $active.hasClass('greyscale');
	
	if (crossfade == true) {
	    $next.animate({opacity: 1}, 500, function() {
	    	$active.animate({opacity: 0}, 500).removeClass('active');
	    }).addClass('active');

	    delay = display_duration;
	} else {
	    $active.animate({opacity: 0}, 500, function() {
	    	$next.animate({opacity: 1}, 500).addClass('active');
	    }).removeClass('active');
	    
	    if (greyscale == true) {
	    	delay = 2500;
	    } else {
	    	delay = display_duration;
	    }
    }
    
    // Update description or selected navigation item
    if (show_image_descriptions == true) {
    	$('#slideshow-description').html($next.attr('alt'));
    } else {
	    current = $next.attr('id');
	    nextnav = $('#slideshow-nav [rel='+current+']');
	    if (nextnav.length) {
		    $('#slideshow-nav a').removeClass('selected-image');
		    nextnav.addClass('selected-image');	
	    }
    }

    // Create loop
    slideswitch = setTimeout( "slideSwitch()", delay );
}

$(function() {
	
	// Vertically centre website
    $('#wrapper-site').vAlign();
    $(window).bind('resize', function(){
		$('#wrapper-site').vAlign();
	});


	// Set up slideshow
	var images = $('#slideshow img');
	if (images.length == 1) {

		// Set up fake navigation for consistency
		if ($('body').hasClass('history')) {
			$('#slideshow').after('<div id="slideshow-description">'+$('#slideshow img:first').attr('alt')+'</div>');
		} else {
			$('#slideshow').after('<ul id="slideshow-nav"><li><a href="#" class="selected-image">x</a></li></ul>');
		}
		
	} else if (images.length > 1) {
	
		// Check if greyscale images are shown (philosophy page)
		greyscale = $('#slideshow').hasClass('greyscale');

		// Set delays between image transitions
		if ( $('body').hasClass('history') ) {
			display_duration = 2500;
		} else {
			display_duration = 3000;
		}

		
		// Check if image descriptions should be shown instead of navigation (history pages)
		show_image_descriptions = $('body').hasClass('history');

		// Hide images by default
		images.css({opacity: 0});
		
		// Begin slideshow
		slideSwitch();

		// Create buttons for image navigation UNLESS we're on the history page
		if (show_image_descriptions) {
			$('#slideshow').after('<div id="slideshow-description">'+$('#slideshow img:first').attr('alt')+'</div>');
		} else {
			$('#slideshow').after('<ul id="slideshow-nav"></ul>');
			images.each(function(index) {
				$(this).attr('id', 'slideshow-img-'+index);
			});
			
			nav_html = '';
			for (i=0; i < images.length; i++){  
				if (greyscale == true) {
					if (i % 2 == 0) {
						nav_html += '<li><a href="#" rel="slideshow-img-'+i+'"></a></li>';
					}
				} else {
				    nav_html += '<li><a href="#" rel="slideshow-img-'+i+'"></a></li>';
			    }
			}  
			$('#slideshow-nav').append(nav_html);
			$('#slideshow-nav a:first').addClass('selected-image');

			// Set up click functions			
			$('#slideshow-nav a').click(function() {

				// Stop auto animation
				clearTimeout(slideswitch);
			
				// Switch images
				$active = $('#slideshow img.active');
				$next = $('#'+$(this).attr('rel'));			
			    $active.animate({opacity: 0}, 500, function() {
			    	$next.animate({opacity: 1}, 500).addClass('active');
			    }).removeClass('active');
			    
			    // Update thumbnail display
			    $('#slideshow-nav a').removeClass('selected-image');
			    $(this).addClass('selected-image');
			    
				// Resume slideshow
				if (greyscale) {
					slideswitch = setTimeout( "slideSwitch()", 2500 );
				} else {
					slideswitch = setTimeout( "slideSwitch()", display_duration );
				}
				

			});
		}
	}
	
	
	
	// Set up multi-column layout
	var columns = $('#columns .column').length;
	if (columns > 2) {
		// Create navigation and prepare columns
		$('.content-area').after('<div id="slideshow-nav"><a href="#" id="prev-col" class="disabled">&lt; Back</a> &nbsp; <a href="#" id="next-col">More &gt;</a></div>');
		var num_cols = $('#columns .column').length -2;
		var col_width = 265;
		var max_margin = 0 - (num_cols * col_width);
		
		// Set up click functions			
		$('#next-col').click(function() {
			if ($(this).hasClass('disabled')) {
				return false;
			}
			
			$('#prev-col').removeClass('disabled');
			
			cur_margin = parseInt($('#columns').css('margin-left'));
			new_margin = cur_margin - col_width;
			
			$('#columns').animate({'margin-left': new_margin+'px'});
			
			if (new_margin == max_margin) {
				$(this).addClass('disabled');
			}
		});
		
		$('#prev-col').click(function() {
			if ($(this).hasClass('disabled')) {
				return false;
			}
			
			$('#next-col').removeClass('disabled');
			
			cur_margin = parseInt($('#columns').css('margin-left'));
			new_margin = cur_margin + col_width;
			
			$('#columns').animate({'margin-left': new_margin+'px'});
			
			if (new_margin == 0) {
				$(this).addClass('disabled');
			}
		});
	
	}
	
});


/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "322";


/* No need to change anything after this */


//document.write('<style type="text/css"></style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onmouseup = Custom.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;

var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.slider=function(){
	function slide(n,p){this.n=n; this.init(p)}
	slide.prototype.init=function(p){
		var s=this.x=T$(p.id), u=this.u=T$$('ul',s)[0], c=this.m=T$$('li',u), l=c.length, i=this.l=this.c=0; this.b=1;
		if(p.navid&&p.activeclass){this.g=T$$('li',T$(p.navid)); this.s=p.activeclass}
		this.a=p.auto||0; this.p=p.resume||0; this.r=p.rewind||0; this.e=p.elastic||false; this.v=p.vertical||0; s.style.overflow='hidden';
		for(i;i<l;i++){if(c[i].parentNode==u){this.l++}}
		if(this.v){;
			u.style.top=0; this.h=p.height||c[0].offsetHeight; u.style.height=(this.l*this.h)+'px'
		}else{
			u.style.left=0; this.w=p.width||c[0].offsetWidth; u.style.width=(this.l*this.w)+'px'
		}
		this.nav(p.position||0);
		if(p.position){this.pos(p.position||0,this.a?1:0,1)}else if(this.a){this.auto()}
		if(p.left){this.sel(p.left)}
		if(p.right){this.sel(p.right)}
	},
	slide.prototype.auto=function(){
		this.x.ai=setInterval(new Function(this.n+'.move(1,1,1)'),this.a*1000)
	},
	slide.prototype.move=function(d,a){
		var n=this.c+d;
		if(this.r){n=d==1?n==this.l?0:n:n<0?this.l-1:n}
		this.pos(n,a,1)
	},
	slide.prototype.pos=function(p,a,m){
		var v=p; clearInterval(this.x.ai); clearInterval(this.x.si);
		if(!this.r){
			if(m){
				if(p==-1||(p!=0&&Math.abs(p)%this.l==0)){
					this.b++;
					for(var i=0;i<this.l;i++){this.u.appendChild(this.m[i].cloneNode(1))}
					this.v?this.u.style.height=(this.l*this.h*this.b)+'px':this.u.style.width=(this.l*this.w*this.b)+'px';
				}
				if(p==-1||(p<0&&Math.abs(p)%this.l==0)){
					this.v?this.u.style.top=(this.l*this.h*-1)+'px':this.u.style.left=(this.l*this.w*-1)+'px'; v=this.l-1
				}
			}else if(this.c>this.l&&this.b>1){
				v=(this.l*(this.b-1))+p; p=v
			}
		}
		var t=this.v?v*this.h*-1:v*this.w*-1, d=p<this.c?-1:1; this.c=v; var n=this.c%this.l; this.nav(n);
		if(this.e){t=t-(8*d)}
		this.x.si=setInterval(new Function(this.n+'.slide('+t+','+d+',1,'+a+')'),10)
	},
	slide.prototype.nav=function(n){
		if(this.g){for(var i=0;i<this.l;i++){this.g[i].className=i==n?this.s:''}}
	},
	slide.prototype.slide=function(t,d,i,a){
		var o=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left);
		if(o==t){
			clearInterval(this.x.si);
			if(this.e&&i<3){
				this.x.si=setInterval(new Function(this.n+'.slide('+(i==1?t+(12*d):t+(4*d))+','+(i==1?(-1*d):(-1*d))+','+(i==1?2:3)+','+a+')'),10)
			}else{
				if(a||(this.a&&this.p)){this.auto()}
				if(this.b>1&&this.c%this.l==0){this.clear()}
			}
		}else{
			var v=o-Math.ceil(Math.abs(t-o)*.1)*d+'px';
			this.v?this.u.style.top=v:this.u.style.left=v
		}
	},
	slide.prototype.clear=function(){
		var c=T$$('li',this.u), t=i=c.length; this.v?this.u.style.top=0:this.u.style.left=0; this.b=1; this.c=0;
		for(i;i>0;i--){
			var e=c[i-1];
			if(t>this.l&&e.parentNode==this.u){this.u.removeChild(e); t--}
		}
	},
	slide.prototype.sel=function(i){
		var e=T$(i); e.onselectstart=e.onmousedown=function(){return false}
	}
	return{slide:slide}
}();
