// scrolling
var scrollSpeed	= 50;
var scrollWidth	= 20;
var scrollTime = 10;

function isDefined(property) {
	return (typeof property != 'undefined');
}

function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	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 blurLinks() {
	lnks = document.getElementsByTagName("a");
	inpts = document.getElementsByName("input");
	for(i=0;i<lnks.length;i++) {
		if ((lnks[i].getAttribute("rel") == null) || (lnks[i].getAttribute("rel") == "")) {
			lnks[i].onfocus = new Function("this.blur()");
		}
	}
	for(i=0;i<inpts.length;i++) {
		inpts[i].onfocus= new Function("this.blur()");
	}
}

function correctPNG() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			img.style.visibility = "hidden";
			var src = img.src;
			var div = document.createElement("DIV");
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			if (img.parentElement.href) {
				div.style.cursor = "hand";
			}
			img.replaceNode(div);
			img.style.visibility = "visible";
		}
	}
}

function featuredStatus(type,ID) {
	requestURL = "/ajax/featuredStatus.php";
	requestQuery = "type=" + type;
	requestQuery += "&id=" + ID;
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestQuery, "POST");
}

function liveStatus(type,ID) {
	requestURL = "/ajax/liveStatus.php";
	requestQuery = "type=" + type;
	requestQuery += "&id=" + ID;
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestQuery, "POST");
}

function hookMceEditorsByRel() {
	var textAreaArray = getElementsByRel("tinyMCE",null,"textarea")
	for(var j=0;j<textAreaArray.length;j++) {
		tinyMCE.execCommand('mceAddControl', true, textAreaArray[j].id); 
	}
}


function getElementsByRel(searchRel,node,tag) {
	var relElements = new Array();
	if (node == null) {
		node = document;
	}
	if (tag == null) {
		tag = "*";
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchRel+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if (pattern.test(els[i].getAttribute("rel"))) {
			relElements[j] = els[i];
			j++;
		}
	}
	return relElements;
}

function tabContentInt() {
	if (document.getElementById("tabContent")) {
		mytabContents = new ddtabcontent("tabContent");
		mytabContents.setpersist(false);
		mytabContents.setselectedClassTarget("link");
		mytabContents.init();
	}
}

function deleteRecord(ID,title,page,PageA) {
	question = "Are you sure you want to remove " + title + "?";
	if (confirm(question)) {
		URL = page + ".php?Action=delete&ID=" + ID + PageA;
		window.location.href= URL;
	}
}

function logOut() {
	question = "Are you sure you wish to logout?";
	if (confirm(question)) {
		URL = "logout.php";
		window.location.href= URL;
	}
}

function scroller(holder,el,scrollerTime) {
	if (!el) {
		el = "span";
	}
	if (!scrollerTime) {
		scrollerTime = scrollTime;
	}
	this.el = el;
	this.automaticInterval = 0;
	this.automaticTimeout = 0;
	this.manualInterval = 0;
	this.endPos = 0;
	this.scrollTime = (scrollerTime * 1000);
	if ((holder) && (document.getElementById(holder))) {
		this.scrollHolder = document.getElementById(holder);
		this.scrollElements = document.getElementById(holder).getElementsByTagName(el);
		this.childWidth = parseFloat(this.scrollElements[0].offsetWidth);
		this.scrollHolder.style.width = (this.scrollElements.length * this.childWidth) + "px";
		this.startAutoScroll();
	}
}

scroller.prototype.autoScroll = function() {
	this.scrollHolder.style.left = (!this.scrollHolder.style.left) ? 0 : parseFloat(this.scrollHolder.style.left);
	if (parseFloat(this.scrollHolder.style.left) > ((0-parseFloat(this.scrollHolder.style.width)) + this.childWidth)) {
		this.endPos = Math.abs(parseFloat(this.scrollHolder.style.left) - this.childWidth);
		var self = this;
		this.automaticInterval = setInterval(function() { self.scrollLeft(this.endPos); }, scrollSpeed);
	} else {
		this.scrollHolder.style.left = parseInt(this.childWidth) + "px";
		this.clearAuto();
		this.autoScroll();		
	}
}

scroller.prototype.clearAuto = function() {
	clearInterval(this.automaticInterval);
	clearTimeout(this.automaticTimeout);
}

scroller.prototype.startAutoScroll = function() {
	var self = this;
	this.automaticTimeout = setTimeout(function() { self.autoScroll(); }, self.scrollTime);
}

scroller.prototype.scrollEnd = function() {
	if (this.manualInterval != 0) {
		clearInterval(this.manualInterval);
	}
	if (this.automaticInterval != 0) {
		this.clearAuto();
	}
	this.startAutoScroll();
}

scroller.prototype.scrollLeft = function() {
	if (parseFloat(this.scrollHolder.style.left) > (0-this.endPos)) {
		this.scrollHolder.style.left = parseFloat(this.scrollHolder.style.left) - parseInt(scrollWidth) + "px";
	} else {
		this.scrollEnd();
		this.scrollHolder.style.left = (0 - parseInt(this.endPos)) + "px";
	}
}

scroller.prototype.scrollRight = function() {
	if (parseFloat(this.scrollHolder.style.left) < parseInt(this.endPos)) {
		this.scrollHolder.style.left = parseFloat(this.scrollHolder.style.left) + parseInt(scrollWidth) + "px";
	} else {
		this.scrollEnd();
		this.scrollHolder.style.left = parseInt(this.endPos) + "px";
	}
}

function runFunctions() {
	externalLinks();
	blurLinks();
	formControls();
	tabContentInt();
	hookMceEditorsByRel();
	var newsTicker = new scroller("NewsTicker");
	var shareTicker = new scroller("ShareTicker","",7);
	if ((window.attachEvent) && (!isDefined(document.body.style.maxHeight))) {
		correctPNG();
	}
}

if (isDefined(window.addEventListener)) {
	window.addEventListener("load", runFunctions, false);
} else if (isDefined(window.attachEvent)) {
	window.attachEvent("onload", runFunctions);
}
