/*
 * KING HARRYS CORNWALL
 *
 * Subject to copyright.
 *
 * Web Development - LOOKsystems Limited
 * mailto:info@looksystems.ltd.uk
 * http://www.looksystems.ltd.uk
 *
 */

// COLUMN SUPPORT

function columns_balance(columns) {
	if (!columns || !columns.length) return;
	var bottom = 0;
	var cols = columns.length;
	for (var c = 0; c < cols; ++c) {
		var colObject = document.getElementById(columns[c]);
		columns[c] = colObject;
		if (!colObject) continue;
		colObject.style.overflow = 'visible';
		colObject.style.height = 'auto';
		var by = colObject.offsetTop + colObject.offsetHeight;
		if (by > bottom) bottom = by;
	}
	if (!bottom) return;
	for (var c = 0; c < cols; ++c) {
		var colObject = columns[c];
		if (!colObject) continue;
		var grow = 11 + bottom - colObject.offsetTop - colObject.offsetHeight;
		if (grow <= 0) continue;
		var child = colObject.lastChild;
		while (child != undefined && !child.style) child = child.previousSibling;
		if (!child) continue;
		child = child.lastChild;
		while (child != undefined && child.className != 'boxmain') child = child.previousSibling;
		if (!child) continue;
		child.style.paddingBottom =grow+'px'; // child.offsetHeight
	}
	var heightObject =  document.getElementById('height');
	if (heightObject != undefined && heightObject.value != undefined) heightObject.value = bottom/2;
}

// POPUP SUPPORT

var popups = new Object;
var popups_child;

function popups_initialise() {
	if (!document.links.length) return;
	var a = 0;
	while (a < document.links.length) {
		var id = document.links[a].id;
		if (!popups[id]) id = document.links[a].id;
		if (!popups[id] && id.substring(0, 5) == 'popup') id = 'popup';
		if (popups[id] != null || id == 'popup') {
			var href = document.links[a].href;
			document.links[a].href = "javascript:popup('"+id+"','"+href+"')";
			document.links[a].target = "_self";
		}
		a++;
	}
}

function popup(id, url) {
	var name, features;
	if (popups != null) features = popups[id];
	if (features != null) {
		name = features[0];
		var x = features[1];
		var y = features[2];
		var w = features[3];
		var h = features[4];
		if (x < 0) x = (window.screen.availWidth - w) / 2;
		else if (window.screenX != null) x += window.screenX;
		if (y < 0) y = (window.screen.availHeight - h) / 2;
		else if (window.screenY != null) y += window.screenY;
		features = features[5];
		if (features != "") features += ",";
		features += "width="+w+",height="+h;
		features += ",screenX="+x+",screenY="+y;
		features += ",left="+x+",top="+y;
	}
	if (popups_child != null) {
		if (popups_child.close != null) popups_child.close();
		else name = '';
	}
	popups_child = window.open(url, name, features);
	if (popups_child != null && popups_child.focus != null) popups_child.focus();
}

// MENUS

var mcTimer = false;

function mi(item) {

	var navigation = document.getElementById('skip_navigation');
	if (navigation != null && navigation.style.visibility != 'hidden') return;

	var children = item.parentNode.parentNode.getElementsByTagName('ul');
	for (c = 0; c < children.length; ++c) {
		if (children[c] != item) children[c].style.visibility = 'hidden';
	}
	children = item.parentNode.getElementsByTagName('ul');
	if (children.length) children[0].style.visibility = 'visible';

	if (!mcTimer) return;
	clearTimeout(mcTimer);
	mcTimer = false;

}

function mo() {
	var navigation = document.getElementById('skip_navigation');
	if (navigation != null && navigation.style.visibility != 'hidden') return;
	mcTimer = setTimeout("mc()", 250);
}

function mc() {

	mcTimer = false;
	var menu = document.getElementById('menu');
	if (!menu) return;

	var children = menu.getElementsByTagName('ul');
	for (c = 1; c < children.length; ++c) {
		children[c].style.visibility = 'hidden';
	}

}

// ACCESSIBLE FORMS

var waiForms = new Object;

function waiforms() {
	if (!document.forms.length) return;
	for (var f = 0; f < document.forms.length; ++f) {
		if (document.forms[f].className == "waiform") waiform(document.forms[f]);
	}
}

function waiform(objForm) {
	if (typeof(objForm) != 'object') objForm = document.getElementById(objForm);
	if (!objForm || !objForm.id || !waiForms[objForm.id]) return;
	objForm.onsubmit = function() { return waiform_onsubmit(this); }
	for (var e = 0; e < objForm.elements.length; ++e) {
		var element = objForm.elements[e];
		if (element.type != "text" && element.type != "textarea") continue;
		if (!waiForms[objForm.id][element.id]) continue;
		element.onfocus = function() { return waiform_onfocus(this); }
		element.onblur = function() { return waiform_onblur(this); }
		if (element.value == '') element.value = waiForms[objForm.id][element.id];
	}
}

function waiform_onfocus(objInput) {
	if (!objInput) return;
	var objForm = objInput.form;
	if (!objForm || !waiForms[objForm.id]) return;
	if (objInput.value == waiForms[objForm.id][objInput.id]) objInput.value = '';
}

function waiform_onblur(objInput) {
	if (!objInput) return;
	var objForm = objInput.form;
	if (!objForm || !waiForms[objForm.id]) return;
	if (objInput.value == '') objInput.value = waiForms[objForm.id][objInput.id];
}

function waiform_onsubmit(objForm) {
	if (!objForm || !waiForms[objForm.id]) return;
	for (var e = 0; e < objForm.elements.length; ++e) {
		var element = objForm.elements[e];
		if (element.type != "text" && element.type != "textarea") continue;
		if (!waiForms[objForm.id][element.id]) continue;
		if (element.value == waiForms[objForm.id][element.id]) element.value = '';
	}
	return true;
}

// SLIDESHOW

var slideshow = new Object;

function slideshow_initialise(images, id) {
	if (images == null || document.getElementById == null) return;
	slideshow.images = images;
	var i = 0;
	slideshow.cells = new Array;
	if (id == null) img = 'slide';

	var object = document.getElementById(id);
	if (!object) return;

	var links = object.getElementsByTagName('a');
	if (!links.length) return;

	slideshow.linkObj = links[0];

	var spans = object.getElementsByTagName('span');
	if (spans.length < 6) return;

	slideshow.imageObj = spans[0];
	slideshow.textObj = spans[7];

	slideshow.logoClass = spans[2].className;
	if (!slideshow.logoClass) slideshow.logoClass = "logo";

	var bolds = object.getElementsByTagName('b');
	if (bolds.length < 2) return;

	slideshow.frontObj = document.getElementById('stage-front');

	slideshow.headingObj = bolds[0];
	slideshow.titleObj = bolds[2];

	slideshow.cache = new Image;
	slideshow.timerId = 0;
	if (slideshow.index == null) slideshow.index = 0;
	if (slideshow.wait == null) slideshow.wait = 6000;
	if (slideshow.delay == null) slideshow.delay = 6000;
	slideshow_preload();
}

function slideshow_preload() {
	if (++slideshow.index >= slideshow.images.length) slideshow.index = 0;
	var time = new Date;
	var wait = time.getTime();
	var index = slideshow.index;
	var delay = slideshow.wait;
	slideshow.wait = slideshow.delay;
	slideshow.cache.src = slideshow.images[index][0];
	if (++index >= slideshow.images.length) index = 0;
	wait = time.getTime() - wait;
	if (wait > slideshow.wait) slideshow_crossfade();
	else {
		slideshow.timerId = setTimeout(slideshow_crossfade, delay - wait);
	}
}

function slideshow_crossfade() {
	slideshow.timerId = 0;
	if (!slideshow.cache.complete) {
		slideshow.timerId = setTimeout(slideshow_play, slideshow.wait);
		return;
	}
	slideshow.frontObj.style.background = slideshow.imageObj.style.background;
	slideshow.frontObj.innerHTML = slideshow.imageObj.innerHTML;
	$('stage-front').setOpacity(1.0);
	new Effect.Opacity('stage-front', { from: 1.0, to: 0.0, duration: 2.0 });
	slideshow_play();

}

function slideshow_play() {
	var index = slideshow.index;
	slideshow.imageObj.style.background = 'url('+slideshow.cache.src+') 0 0 no-repeat';
	slideshow.headingObj.innerHTML = '<span class="'+slideshow.logoClass+'"><!-- KHC LOGO --></span>'+slideshow.images[index][2]+'<br>';
	slideshow.titleObj.innerHTML = slideshow.images[index][3]+'<br>';
	slideshow.textObj.innerHTML = slideshow.images[index][4];
	slideshow.linkObj.href = slideshow.images[index][5];
	slideshow_preload();
}

function slideshow_stop() {
	clearTimeout(slideshow.timerId);
	slideshow.timerId = 0;
}

// SWFUPLOAD

// DOCUMENT FOCUS

function sf() {
	var f = 0;
	while (f < document.forms.length && document.forms[f].className == 'waiform') ++f;
	if (f >= document.forms.length) return;
	var e = 0;
	var myform = document.forms[f];
	while (e < myform.elements.length) {
		if (myform.elements[e].type == "text" || myform.elements[e].type == "textarea") {
			myform.elements[e].focus();
			break;
		}
		e++;
	}
}

// DOCUMENT INITIALISE

function init(so) {
	if (so) init_submitonce();
	if (typeof upload == 'object') uploader = new SWFUpload(upload);
	columns_balance(['colLeft', 'colCenter', 'colRight']);
	popups_initialise();
	waiforms();
	sf();
}

