/*******************************************************
  Modjula site.js
	
	(c) Copyright 2009 Ripple Vision Pty Ltd
	All rights Reserved.
*******************************************************/

var OnLoadLoader = {
	functions: new Array(0),
	loaded: false,
	addFunction: function($function) {
		if (!this.loaded) {
			if (typeof($function) == 'function') {
				this.functions.push($function);
			}
		}	else {
			if (typeof($function) == 'function') {
				try {
					$function();
				} finally {
				}
			}
		}
	},
	init: function() {
		window.onload = OnLoadLoader.onLoad;
	},
	onLoad: function() {
		OnLoadLoader.loaded = true;
		for (var i = 0; i < OnLoadLoader.functions.length; i++) {
			if (typeof(OnLoadLoader.functions[i]) == 'function') {
				try {
					OnLoadLoader.functions[i]();
				} finally {
				}
			}
		}
	}
}

function loadJSFile($url) {
	var e = document.createElement('script');
	e.setAttribute("type", "text/javascript");
	e.setAttribute("src", $url.replace(/(&amp;)/gi,'&'));
  document.getElementsByTagName("head")[0].appendChild(e);
}

function anchorAdjust(a, n) {
	if (typeof(a.style) != 'undefined') {
		a.style.backgroundPosition = '0px '+n+'px';
	}
}

function getObjectX(obj,middle) {
	var x = 0;
	if (middle && (typeof(obj.offsetWidth) != 'undefined')) { x += (obj.offsetWidth / 2); }
	if (typeof(obj.offsetParent) != 'undefined') {
		do {
			x += obj.offsetLeft;
		} while (obj = obj.offsetParent);
		return x;
	} 
	return 0;
}

function getObjectY(obj,middle) {
	var y = 0;
	if (middle && (typeof(obj.offsetHeight) != 'undefined')) { y += (obj.offsetHeight / 2); }
	if (typeof(obj.offsetParent) != 'undefined') {
		do {
			y += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return y;
	} 
	return 0;
}

function getScrollX() {
  if(typeof(window.pageXOffset) == 'number') {
    return window.pageXOffset;
  } else if(document.body && document.body.scrollLeft) {
    return document.body.scrollLeft;
  } else if(document.documentElement && document.documentElement.scrollLeft) {
    return document.documentElement.scrollLeft;
  } else {
		return 0;
	}
}

function getScrollY() {
  if(typeof(window.pageYOffset) == 'number') {
    return window.pageYOffset;
  } else if(document.body && document.body.scrollTop) {
    return document.body.scrollTop;
  } else if(document.documentElement && document.documentElement.scrollTop) {
    return document.documentElement.scrollTop;
  } else {
		return 0;
	}
}

function getPageWidth() {
	if (document.getElementById('page')) {
		return document.body.offsetWidth;
	} else {
		return 0;
	}
}

function getPageHeight() {
	var h = 0;
	if (document.getElementById('page')) {
		return document.body.offsetHeight;
	}
	return h;
}

function getWindowWidth() {
  var w = 0;
	if (document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
	} else if (document.body != null) {
		w = document.body.clientWidth;
	} else if (typeof(window.innerWidth) == 'number') {
		w = window.innerWidth;
	}
	
	return w;
} 

function getWindowHeight() {
  var h = 0;
	if (typeof(window.innerHeight) == 'number') {
		h = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		h = document.documentElement.clientHeight;
	} else if (document.body != null) {
		h = document.body.clientHeight;
	}
	return h;
}

function getTextDate() {
  var $d = new Date();
  var $month = $d.getMonth() + "";
  var $day = $d.getDate() + "";
  var $hours = $d.getHours() + "";
  var $mins = $d.getMinutes() + "";
  var $secs = $d.getSeconds() + "";
  
  if ($month.length < 2) { $month = "0" + $month; }
  if ($day.length < 2) { $day = "0" + $day; }
  if ($hours.length < 2) { $hours = "0" + $hours; }
  if ($mins.length < 2) { $mins = "0" + $mins; }
  if ($secs.length < 2) { $secs = "0" + $secs; }
  return $d.getFullYear()+$month+$day+$hours+$mins+$secs;
}

function externalLinks() {
  var anchors = document.getElementsByTagName("a");
  for (var i = 0; i < anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "external")) {
      anchor.target = "_blank";
    }
  }
}

function reloadScrolls() {
	tags = document.getElementsByTagName("*");
	for (var i = 0; i < tags.length; i++) {
		if ((tags[i].onscroll != null) && (typeof(tags[i].onscroll) != 'undefined')) {
			target = tags[i];
			if (typeof(target) != 'undefined') {
				cookieStart = document.cookie.indexOf("scrolltrack_"+target.id+"=");
				if (cookieStart > -1) {
					cookieStart += String(target.id).length + 13;
					cookieEnd = document.cookie.indexOf(";", cookieStart);
					if (cookieEnd == -1) { cookieEnd = document.cookie.length; }
					target.scrollTop = document.cookie.substring(cookieStart, cookieEnd);
				}
			}
		}
	}
	if (typeof(window.trackname) == "string") {
		sx = sy = 0;
		cookieStart = document.cookie.indexOf("windowscrolltrack_x_"+window.trackname);
		if (cookieStart > -1) {
			cookieStart += String(window.trackname).length + 21;
			cookieEnd = document.cookie.indexOf(";", cookieStart);
			if (cookieEnd == -1) { cookieEnd = document.cookie.length; }
			sx = document.cookie.substring(cookieStart, cookieEnd);
		}
		cookieStart = document.cookie.indexOf("windowscrolltrack_y_"+window.trackname);
		if (cookieStart > -1) {
			cookieStart += String(window.trackname).length + 21;
			cookieEnd = document.cookie.indexOf(";", cookieStart);
			if (cookieEnd == -1) { cookieEnd = document.cookie.length; }
			sy = document.cookie.substring(cookieStart, cookieEnd);
		}
		window.scroll(sx, sy);
	}
}

function trackScroll($id) {
	target = document.getElementById($id);
	if (typeof(target) != 'undefined') {
		target.onscroll = function() {
			document.cookie="scrolltrack_"+this.id+"="+String(this.scrollTop);
		}
	}
}

function trackWindowScroll($id) {
	if (typeof(window.onscroll) == "function") {
		var trackWindowOldScrollEvent = window.onscroll;
	}
	window.trackname = $id;
	window.onscroll = function() {
		if (typeof(trackWindowOldScrollEvent) == "function") {
			trackWindowOldScrollEvent();
		}
		document.cookie="windowscrolltrack_x_"+$id+"="+String(getScrollX());
		document.cookie="windowscrolltrack_y_"+$id+"="+String(getScrollY());
	}
}

function setObjectHeight($id, $height) {
	var o = document.getElementById($id);
	if (typeof(o) != 'undefined') {
		o.style.height = ($height + "px");
		if (typeof(ModjulControls) != 'undefined') {
			ModjulControls.readjust();
		}
	}
}

function init() {
  if (document.getElementsByTagName) {
		externalLinks();
		reloadScrolls();
	}
}


OnLoadLoader.init();
OnLoadLoader.addFunction(init);
