// ajax variables
var currentTime = new Date();
var myRequest = currentTime.getTime();

// tab content
var mytabContents;

// map vars
var mLa1;
var mLo1;

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

if (!Array.prototype.forEach) {
 Array.prototype.forEach = function(fun /*, thisp*/) {
 var len = this.length;
 if (typeof fun != "function")
 throw new TypeError();

 var thisp = arguments[1];
 for (var i = 0; i < len; i++) {
 if (i in this)
	fun.call(thisp, this[i], i, this);
 }
 };
}


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 makeBool(s) {
	var bool = 1;
	var s = s.toString();
	s = s.toLowerCase();
	bool = (s === "false") ? 0 : 1;
	return bool;
}

function stringContains(s,contains) {
 if (s.indexOf(contains) > -1) {
	 return true;
 }
	return false;
}

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

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

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 logOut() {
	question = "Are you sure you wish to logout?";
	if (confirm(question)) {
		URL = "logout.php";
		window.location.href= URL;
	}
}

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

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 showMap(mapHolder,lat,lon) {
	if (document.getElementById(mapHolder)) {
		if (GBrowserIsCompatible()) {
			var new_icon = new GIcon();
			new_icon.image = "/images/icons/map.png";
			new_icon.size = new GSize(100,58);
			new_icon.iconAnchor = new GPoint(100,58);
			new_icon.infoWindowAnchor = new GPoint(100,58);
			
			var opt;
			opt = {};
			opt.icon = new_icon;
			opt.draggable = false;
			opt.clickable = false;
			opt.dragCrossMove = true;
            var map = new GMap2(document.getElementById(mapHolder));
            map.setCenter(new GLatLng(lat, lon), 15);
            map.setUIToDefault();
			point = new GLatLng(lat, lon);
			map.addOverlay(new GMarker(point,opt)); 
		}
	}
}

function loadMap() {		
	if (document.getElementById("Map")) {
		showMap("Map",mLa1, mLo1);
		}
}

function goBack(steps) {
	if (!steps) {
		steps = -1;
	}
	backURL = histor.back(steps);
	window.location.replace = backURL;
}

if (isDefined(window.addEventListener)) {
	window.addEventListener("load", RunFunctions, false);
}

else if (isDefined(window.attachEvent)) {
	window.attachEvent("onload", RunFunctions);
}

function RunFunctions() {
	//loadMap();
	externalLinks();
	blurLinks();
	formControls();
	tabContentInt();
	hookMceEditorsByRel();
	if ((window.attachEvent) && (!isDefined(document.body.style.maxHeight))) {
		correctPNG();
	}
}


