// Main JavaScript initialisation functions
// (C) Optimalworks Ltd - http://www.optimalworks.net/

// startup function
function Main() {
	StartTicker();
	MapInit();
}

// show Google map
function MapInit() {
	var map = $("map");

	if (map && GBrowserIsCompatible()) {
		window.onunload = GUnload;

		map.style.backgroundImage = "none";
		var mapControl = new GMap2(map);
		mapControl.addControl(new GLargeMapControl());
		mapControl.addControl(new GScaleControl());
		mapControl.setCenter(new GLatLng(50.790921,-3.509595), 13);

		var marker = new GMarker(mapControl.getCenter());
		var overlay = function() { marker.openInfoWindowHtml("<div class=\"address\"><p>Awlwood Joinery</p></div>"); }
		GEvent.addListener(marker, "click", overlay);
		mapControl.addOverlay(marker);
		overlay();
	}
}

// ticker start
var ticker, tickertext, tickerhover = null, tpos = 0;
var TickerPause = 60;
var TickerIncDefault = -6;
var TickerIncMax = 20;
var TickerSensitivity = 0.1;
var TickerInc = TickerIncDefault;
function StartTicker() {
	ticker = $("ticker");
	if (ticker) {
		// find ticker content
		tickertext = $c(ticker);
		if (tickertext.length == 1) tickertext = tickertext[0]; else tickertext = null;
		if (tickertext) {
			// setup and styling
			tickertext.style.position = "absolute";
			tickertext.style.whiteSpace = "nowrap";
			tickertext.style.padding = "10px 0px";
			ticker.style.height = tickertext.offsetHeight+"px";
			ticker.style.cursor = "e-resize";
			tpos = ticker.offsetWidth;
			// faded edges
			for (var i = 2; i <= 8; i=i+2) {
				var dwidth = 5;
				var pos = (i-2)/2 * dwidth;
				var n = document.createElement("div");
				n.style.position = "absolute";
				n.style.width = dwidth+"px";
				n.style.height = tickertext.offsetHeight+"px";
				n.style.backgroundColor = "#ffffff";
				Graphic.Opacity(n, (10-i)*10);
				n.style.top = "0px";
				var n1 = n.cloneNode(true);
				n1.style.left = pos+"px";
				ticker.appendChild(n1);
				var n2 = n.cloneNode(true);
				n2.style.right = pos+"px";
				ticker.appendChild(n2);
			}
			// event handlers
			ticker.onmouseover = TickerControl;
			ticker.onmouseout = TickerControl;
			ticker.onmousemove = TickerControl;
			setInterval(ScrollTicker, TickerPause);
		}
	}
}

// scroll the ticker
function ScrollTicker() {
	tpos += TickerInc;
	var ow = ticker.offsetWidth;
	var tw = -tickertext.offsetWidth;
	if (tpos < tw) tpos = ow;
	if (tpos > ow) tpos = tw;
	tickertext.style.left = tpos+"px";
}

// mouse over ticker
function TickerControl(evt) {
	var e = new EventObject(evt);
	switch (e.Type) {
		case "mouseover": tickerhover = e.MouseX; TickerInc = 0; break;
		case "mouseout": tickerhover = null; TickerInc = TickerIncDefault; break;
		case "mousemove":
			if (tickerhover !== null) {
				TickerInc = Math.floor((tickerhover - e.MouseX) * TickerSensitivity);
				if (Math.abs(TickerInc) > TickerIncMax) TickerInc = TickerIncMax * (TickerInc < 0 ? -1 : 1);
			}
			break;
	}
}

// string trimming
String.prototype.Trim = function() { return this.replace(/^\s*|\s*$/g, ""); }

// array stack push (if unsupported)
if (!Array.prototype.push) { Array.prototype.push = function(element) { this[this.length] = element; } }

// getElementById
function $(id) { return (document.getElementById ? document.getElementById(id) : null); }

// getElementsByTagName
function $t(tag, root) {
	if (typeof root == 'string') root = $(root);
	if (!root) root = document;
	return (document.getElementsByTagName ? root.getElementsByTagName(tag) : null);
}

// get child elements (ignoring whitespace and comments)
function $c(element) {
	var ce = [];
	if (typeof element == 'string') element = $(element);
	if (element) {
		for (var i=0; i<element.childNodes.length; i++) {
			if (element.childNodes[i].nodeType == 1 && element.childNodes[i].nodeName != "!") ce.push(element.childNodes[i]);
		}
	}
	return ce;
}

// event object
function EventObject(event) {
	this.Event = (event ? event : window.event);
	this.Element = (this.Event.target ? this.Event.target : this.Event.srcElement);
	this.Type = String(this.Event.type).toLowerCase();
	this.MouseX = 0;
	this.MouseY = 0;
	var mre = /mouse|click/i;
	if (mre.test(this.Type)) {
		this.MouseX = (this.Event.pageX ? this.Event.pageX : this.Event.clientX + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft));
		this.MouseY = (this.Event.pageY ? this.Event.pageY : this.Event.clientY + Math.max(document.documentElement.scrollTop, document.body.scrollTop));
	}
}

// graphic routines
var Graphic = new function() {
	// set element opacity (0 to 100).
	this.Opacity = function(element, oVal, autoHide) {
		if (typeof element == 'string') element = $(element);
		if (element) {
			oVal = (oVal < 0 ? 0 : (oVal > 100 ? 100 : oVal));
			if (autoHide) {
				var visibility = element.style.visibility;
				if (oVal == 0 && visibility != "hidden") element.style.visibility = "hidden";
				if (oVal > 0 && visibility != "visible") element.style.visibility = "visible";
			}
			if (oVal == 100) oVal = 99.999; // fix Mozilla flicker
			var oVal1 = oVal / 100;
			element.style.opacity = oVal1;
			element.style.MozOpacity = oVal1;
			element.style.filter = "alpha(opacity:"+oVal+")";
			element.style.KHTMLOpacity = oVal1;
		}
	}
}

// startup event
window.onload = Main;
