var d=document, imgs = new Array(), thumbs = new Array(), zInterval = null, current=0, pause=false, fading=false;

var anim;
var anim2;

function so_init() {
		//set up images
		if(d.getElementById("imageContainer")){
			imgs = d.getElementById("imageContainer").getElementsByTagName("img");
			for(i=1;i<imgs.length;i++){
				imgs[i].style.display = 'none';
				imgs[i].xOpacity = 0;
			}
			imgs[0].style.display = "block";
			imgs[0].xOpacity = .99;
			imgs[0].style.zIndex = 88;

			if(d.getElementById("thumbs")){
				// Thumbs take you to the appropriate slide
				thumbs = d.getElementById("thumbs").getElementsByTagName("a");
				for (var i=0; i < thumbs.length; i++) {
					thumbs[i].onclick = function(){
						selection = parseInt(this.id.replace('t',''));							
						if (fading != true){
							clearTimeout(anim);
							setTimeout('so_xfade(selection)',300);
						} else {
							anim2 = setTimeout('queueNext(selection)',300);
						}
						return false;
					};			
				};
				d.getElementById("thumbs").style.display = 'block';				
			}			
			
			// Big images click through to next one
			big_links = d.getElementById("imageContainer").getElementsByTagName('a');
			for (var i=0; i < big_links.length; i++) {
				big_links[i].onclick = function(){
					clearTimeout(anim);
					setTimeout('so_xfade("false")',300);
					return false;			
				};
			};						

			// On homepage wait for second image before continuing
			if(document.body.id == 'home'){
				it = 0;
				function isComplete(){
					if(imgs[1].complete || imgs[1].complete == undefined || it == 40){				
							setTimeout("so_xfade('false')",5000);
							clearTimeout(com);
					} else {
						it += 1;
					}
				};
				com = setInterval(isComplete,10);				
			} else {
				anim = setTimeout("so_xfade('false')",5000);	
			}
		}

		if(d.getElementById("quotes")){
			qs = d.getElementById("quotes").getElementsByTagName("li");
			for(i=1;i<qs.length;i++){
				qs[i].style.display = 'none';
				qs[i].xOpacity = 0;
			}
			qs[0].style.display = "block";
			qs[0].xOpacity = .99;
			qs[0].style.zIndex = 88;		

			anim = setTimeout("so_qfade()",8000);
		}		
};

// Function to hold off while another fade finishes
function queueNext(selection){
 clearTimeout(anim);
 so_xfade(selection);
}

// this is called multiple times until fully faded)
function so_xfade() {
	fading = true;
	
	if(so_xfade.arguments[0] != 'false'){
		nIndex = so_xfade.arguments[0];
		sel = so_xfade.arguments[0];
	} else {
		nIndex = imgs[current+1]?current+1:0;
		sel = 'false';
	}

	cOpacity = imgs[current].xOpacity;
	nOpacity = imgs[nIndex].xOpacity;
		
	cOpacity-=.10; 
	nOpacity+=.10;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current],'c'); 
	setOpacity(imgs[nIndex],'n');

	// If current image is fully faded out, display it as none and reset the step through gallery
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
		sel = (sel == current ? 'false' : sel);
		anim = setTimeout("so_xfade(sel)",5000);
	// else keep doing this function until faded
	} else {
		anim = setTimeout("so_xfade(sel)",25);	
	}
	
	// ----- does the fading
	function setOpacity(obj,type) {
		if(obj.xOpacity==1) {
			obj.xOpacity = 1;
			return;
		}
		// set the caption change
		if(type == 'n'){
			if(document.getElementById('caption')){
				document.getElementById('caption').innerHTML = obj.alt;
				setThumb(obj);
				// alert(fading);				
			}
		}				
		
		// x-browser fading
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";		
	};
	
	// set the selected thumb
	function setThumb(obj){
		var lis = document.getElementById('thumbs').getElementsByTagName('li');
		for (var i=0; i < lis.length; i++) {
			var n = parseInt(obj.id.replace('img',''));			
			i==n ? lis[i].className = 'selected' : lis[i].className = '';
		};		
	};	
};

// Center portrait shots automatically without having to set classes for them
function centerImages(){
	var ic = document.getElementById('imageContainer');
	var id = document.body.id;
	if(ic && id != 'home'){
		var imgs = ic.getElementsByTagName('img');
		for (var i=0; i < imgs.length; i++) {
			var id = document.getElementById(imgs[i].id);
			 if(id.width < 472){
			 	id.style.marginLeft = -(id.width/2) + "px";
			}
		};
	}
}

// Quote fading
function so_qfade() {
	cOpacity = qs[current].xOpacity;
	nIndex = qs[current+1]?current+1:0;

	nOpacity = qs[nIndex].xOpacity;
		
	cOpacity-=.05; 
	nOpacity+=.05;
	
	qs[nIndex].style.display = "block";
	qs[current].xOpacity = cOpacity;
	qs[nIndex].xOpacity = nOpacity;
	
	setOpacity(qs[current]); 
	setOpacity(qs[nIndex]);

	// If current image is fully faded out, display it as none and reset the step through gallery
	if(cOpacity<=0) {
		qs[current].style.display = "none";
		current = nIndex;
		anim = setTimeout("so_qfade()",8000);		
	// else keep doing this function until faded
	} else {
		anim = setTimeout("so_qfade()",25);	
	}
	
	// ----- does the fading
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		// x-browser fading
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";		
	};

};

// Check subscribe form for errors
function validateSubscribeForm(){
	var form = document.getElementById('subscription');
	var email = document.getElementById('zikjk-zikjk');
	if(form && email){
		email.value = "";		
		form.onsubmit = function(){
			var error = document.getElementById('email_error');
			if(email.value == ''){
				email.parentNode.className = 'error';
				return false;
			} else {
				email.parentNode.className = '';
				return true;
			}
		};
	}				
};

// Widont
String.prototype.widont = function() {
	return this.replace(/([^\s])\s+([^\s]+)\s*$/, '$1&nbsp;$2');
};

function widont(){
	var p  = document.getElementsByTagName('p');
	if(p.length > 0){
		for (var i=0; i < p.length; i++) {
			var txt = p[i].innerHTML.widont();
			p[i].innerHTML = txt;
		};
	}
};
// End




// DOM Loaded
function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if (_timer) clearInterval(_timer);
	
	// List functions to run on DOM Load
	centerImages();
	so_init();
	validateSubscribeForm();
	widont();
	// indentParagraphs();
	// 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
		}
	};
}
