// Utilities
function show(elem){
	elem.style.display = 'block';
};

function hide(elem){
	elem.style.display = 'none';
};

function toggle(elem){
	elem.style.display = (elem.style.display == 'none' ? 'block' : 'none');
};

function reset(elem){
	for (var i=0; i < elem.length; i++) {
	if(!elem.elements[i].type == 'submit'){
				elem.elements[i].value = '';	
		}
	};
};

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function debug(txt){
	document.getElementById('debug').innerHTML += txt;
};

function getSize(which) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return (which=='height' ? myHeight : myWidth);
};

// Stop page going off the top of the screen when resized
function topBlocker(){
	var h = getSize('height');
	var wr = document.getElementById('wrapper');
	if(h <= 570){
		addClass(wr,'halt');
	} else {
		removeClass(wr,'halt');
	}
};

// End Utilities

// Deals with showing and hiding subscribe form
function hideSubscribeForm(){
	if(!document.getElementById('details_form')){return true;}
	var url = document.location.search;
	
	reset(document.getElementById('cm_form'));
	
	if(url){
		document.getElementById('name').focus();
	} else {		
		hide(document.getElementById('details_form'));
		var link = document.createElement('a');
		link.href = '?d=1';
		link.id = 'add_details';
		var txt = document.createTextNode('add your details');
		link.appendChild(txt);
		link.onclick = function (e){
			show(document.getElementById('details_form'));
			document.getElementById('name').focus();
			return false;
		};

		var container = document.getElementById('updates');
		var span = document.getElementById('add_details');
		container.replaceChild(link,span);		
	}	
};

// Prepares onsubmit validation
function prepareCmForm(){
	if(!document.getElementById('cm_form')){return true;}
	
	document.getElementById('cm_form').onsubmit = function(e){
		var allGood = validateCmForm();
		if(allGood == true){
		 return true;
		} else {
			return false;	
		}
	};
};

// the validation
function validateCmForm(){
	var errors = false;
	if(document.getElementById('l265096-265096').value == ''){
			document.getElementById('email_error').innerHTML = 'Please enter your email address';		
			show(document.getElementById('email_error'));
			errors = true;
	} else {
		hide(document.getElementById('email_error'));		
	}
	
	if(document.getElementById('name').value == ''){
		document.getElementById('name_error').innerHTML = 'Please enter your name';
		show(document.getElementById('name_error'));
		errors = true;
	} else {
		hide(document.getElementById('name_error'));
	}
		
	if(errors == true){
		return false;
	} else {		
		return true;		
	}		
};

// External links, opening in new window
function externalLinks(){
    var c = document.getElementById('copy');
    if(c){
        var ls = c.getElementsByTagName('a');
        for(var i=0; i<ls.length; i++){
            if(ls[i].getAttribute('rel') == 'external')
            {
                ls[i].className += (ls[i].className ? ' extlink': 'extlink');
                ls[i].title += '(opens in a new window)';
                ls[i].onclick = function(){
									window.open(this.href);
									return false;
								};
            }
        };
    }
};

// Data required for preloading each section
function preparePreloading(){
	var currentpage = document.body.className;
	var images = new Array();
	
	var recent  = ['recent','rh','jw','hs','tto','uv','ko','hmy'];
	var archive = ['archive','gf','vn','gg','eg','tt','ze','eu'];
	
	var sections = [recent,archive];
	
	for (var i=0; i < sections.length; i++) {
		if(sections[i][0]==currentpage){
			var item = i;
			for (var j=1; j < sections[item].length; j++) {
				images.push(sections[item][j]);
			};
		}
	};	
	preloadImages(currentpage,images);
};

// The preloader
function preloadImages(section,images){
	var src = '/img/projects/'+section+'/';
	var preload = new Array;
	
	for (var i=0; i < images.length; i++) {
		for (var j=1; j < 4; j++) {
			preload[j] = new Image();
			preload[j].src = src+images[i]+'-lrg-'+(j+1)+'.jpg';
		};
	};	
};

// prepare the galleries, links and external links as soon as the dom is available
// preload images when the page has loaded
// window.onload = function(){
// 	preparePreloading();
// };
// Window & Loading Events
window.onresize = function(){
	topBlocker();
};

// DOM Loaded
function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if (_timer) clearInterval(_timer);
	
	// List functions to run on DOM Load
	// prepareGalleries();
	topBlocker();
	externalLinks();
	hideSubscribeForm();
	prepareCmForm();	
	so_init();
	// end
};

// DOM Loaded do not edit
/* for Mozilla/Opera9 and Safari */
if (document.addEventListener) {
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)){
		var _timer = setInterval(function(){if(/loaded|complete/.test(document.readyState)){ init(); }}, 0);
		window.onload(init);
	} else {
		document.addEventListener("DOMContentLoaded", init, false);
	}
} else {
	document.write("<script id=__ie_onload defer src=//:><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			this.onreadystatechange = null;
			init(); // call the onload handler
		}
	};
}

