var d = document;

function g(id) {
	return d.getElementById(id);
}

function gt(o, tag) {
	return o.getElementsByTagName(tag);
}

JJ = {

	"flash" : false,

	"ua" : "N/A",

	"imgCache" : [],

	"imgLoad" : function() {
		var i, j = this.imgCache.length;
		for (i=0;i<arguments.length;i++) {
			JJ.imgCache[j] = new Image();
			JJ.imgCache[j++].src = "/images/buttons/" + arguments[i] + "_hover.jpg";
      JJ.imgCache[j] = new Image();
			JJ.imgCache[j++].src = "/images/buttons/" + arguments[i] + "_down.jpg";
		}
	},

	"addEvent" : function(o, ev, h, uc) {
		var i, j;
		if (typeof(o) == "string") o = [g(o)];
		if (typeof(ev) == "string") {
			ev = [ev];
			h = [h];
		}
		for (i=0;i<o.length;i++) {
			for (j=0;j<ev.length;j++) {
				try {
					o[i].addEventListener(ev[j], h[j], (uc ? uc : false));
				}
				catch(err) {
					o[i]["on" + ev[j]] = h[j];
				}
			}
		}
	},

	"cancelEvent" : function(e) {
		try {
			e.preventDefault();
		}
		catch(err) {
			return false;
		}
	},

	"getWinDim" : function() {
		var w, h;
		if (window.innerWidth) {
			w = window.innerWidth;
			h = window.innerHeight;
		}
		else {
			w = JJ.doc.offsetWidth;
			h = JJ.doc.offsetHeight;
		}
		return { "width" : w, "height" : h };
	},

	"tooltips" : [],

  "Tooltip" : function(o) {
		this.bop = o;
		this.bop.box = g("tt_" + o.id.split("_")[1]);

		JJ.addEvent([this.bop], "mouseover", function(e) {
			var x, y, wd, vC, hC;
			var bH = this.box.offsetHeight;
			var evt = (e ? e : event);
			x = evt.clientX;
			y = evt.clientY;
			wd = JJ.getWinDim();
			vC = ((x + 240 > wd.width) ? -208 : 0);
			hC = ((y + 50 + bH > wd.height) ? (-bH - 20) : 35);
			x += JJ.doc.scrollLeft + vC;
			y += JJ.doc.scrollTop + hC;
			with(this.box.style){
				left = x + "px";
				top = y + "px";
				visibility = "visible";
			}
			with (JJ.tts) {
				height = bH + "px";
				left = (x + 3) + "px";
				top = (y + 3) + "px";
				visibility = "visible";
			}
		});

		JJ.addEvent([this.bop], "mouseout", function() { JJ.tts.visibility = "hidden"; this.box.style.visibility = "hidden"; });
	}

};

JJ.imgLoad("accommodation", "contact", "facilities", "home", "links", "maps", "mtuba", "photos", "tours", "videos");

(function () {
	var i, j, v = 0, p = navigator.plugins.length, ua = navigator.userAgent;
	for (i=0;i<p;i++) {
		if (navigator.plugins[i].name.indexOf("Flash") != -1) {
			for (j=6;j<21;j++) {
				if (navigator.plugins[i].description.indexOf(j + ".") != -1) {
					v = j;
					break;
				}
			}
		}
		if (v > 0) break;
	}
	if (v == 0) {
		for (i=20;i>5;i--) {
			try {
				x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				v = i;
			}
			catch(e) {}
			if (v > 0) break;
		}
	}
	JJ.flashVersion = v;
	JJ.flash = (v >= 6);

	if (d.selection && this.clientInformation) {
		if (this.XMLHttpRequest) {
			if (this.JSON) {
				JJ.ua = "ie8";
			}
			else {
				JJ.ua = "ie7";
			}
		}
		else {
			JJ.ua = "ie6";
		}
	}
	else if (this.clientInformation && ua.indexOf("Chrome") != -1) {
		JJ.ua = "sa-ch";
	}
	else if (this.clientInformation) {
		JJ.ua = "sa";
	}
	else if (d.selection) {
		JJ.ua = "op";
	}
	else if (ua.indexOf("Firefox") != -1) {
		JJ.ua = "fx" + parseFloat(ua.substr(ua.indexOf("Firefox/") + 8, 3));
	}
})();

function heading(n, h) {
	if (JJ.flash) {
		g("heading").innerHTML = '<embed src="/flash/head-' + n + '.swf" loop="false" menu="false" quality="best" wmode="transparent" bgcolor="#550000" width="100%" height="' + h + '" type="application/x-shockwave-flash" />';
	}
}

function init() {
	var i, j, f, arr, str;
	if (screen.width < 1000 && JJ.ua == "ie6") {
		if (g("top-images")) g("top-images").style.width = "auto";
		if (g("bottom-images")) g("bottom-images").style.width = "auto";
		if (g("sent-info")) g("sent-info").style.width = "auto";
	}
	if (!JJ.flash) {
    g("banner").innerHTML = '<img src="/images/banner.png" alt="Wendys Country Lodge - Bed &amp; Breakfast Accommodation in Mtubatuba/St. Lucia, South Africa" title="" />';
		g("text-banner").innerHTML = '<img src="/images/text.png" alt="They told that paradise is in the sky...It&#39;s not true, I found it here by Wendy!! Angela - France" title="" />';
		g("zulu-adv").innerHTML = '<a href="http://www.zululand-adventures.com" target="_blank"><img src="/images/zulu-adv.png" alt="Zululand Aquatic and Animal Adventures - Game Reserve Safaris, Deep-Sea Fishing and Much More" title="" /></a>';
		arr = gt(g("main-content"), "div");
		for (i=0;i<arr.length;i++) {
			if (arr[i].id.indexOf("pic_") == 0) {
				arr[i].innerHTML = '<img class="photo" src="/images/' + arr[i].id.split("_")[1] + '.jpg" alt="" />';
			}
			else if (arr[i].id.indexOf("photo_") == 0) {
				str = arr[i].id.split("_")[1];
				arr[i].innerHTML = '<a href="/images/photos/' + str + '.jpg" target="_blank"><img class="photo" src="/images/photos/thumb_' + str + '.jpg" alt="" /></a>';
				JJ.addEvent([arr[i].firstChild], ["click", "focus"], [JJ.cancelEvent, function() { this.blur() }]);
				JJ.addEvent([arr[i].firstChild.firstChild], "click", function() { JJ.imgEnlarger.load(this, this.src.replace(/thumb_/, "")) });
			}
		}
	}
	else if (JJ.ua == "op") {
		arr = gt(g("main-content"), "div");
		for (i=0;i<arr.length;i++) {
			if (arr[i].id.indexOf("photo_") == 0) {
				arr[i].innerHTML = arr[i].innerHTML; // Activates the interactive flash movies in Opera
			}
		}
	}

	f = function() {
		var arr = this.firstChild.src.split("/");
		this.firstChild.src = '/images/buttons/' + arr[arr.length - 1].split("_")[0] + '_hover.jpg';
	}

	JJ.addEvent(gt(g("navigation"), "a"), ["mouseover", "mousedown", "mouseup", "mouseout", "drag", "focus"], [

		f,

	  function(e) {
			var arr = this.firstChild.src.split("/");
			this.firstChild.src = '/images/buttons/' + arr[arr.length - 1].split("_")[0] + '_down.jpg';
		  try {
				e.preventDefault();
			}
			catch(err) {
				return false;
			}
		},

		f,

	  function() {
			var arr = this.firstChild.src.split("/");
			this.firstChild.src = '/images/buttons/' + arr[arr.length - 1].split("_")[0] + '_up.jpg';
		},

		JJ.cancelEvent,

	  function() {
			this.blur();
		}

	]);

	JJ.doc = (JJ.ua.indexOf("sa") == 0 ? d.body : d.documentElement);
	arr = gt(d, "span");
	j = 0;
	for (i=0;i<arr.length;i++) {
		if (arr[i].id.indexOf("t_") == 0) {
			JJ.tooltips[j++] = new JJ.Tooltip(arr[i]);
		}
	}
	if (g("tt-s")) {
		JJ.tts = g("tt-s").style;
	}

}

JJ.addEvent([window], "load", init);
