/* (c) David Konrad 2010, 2011 - info@4horizons.com - rev 04.12.2011 */

var ieAlertCookie = 'ieAlert';

var _4h = {
	init : function() {
		$("input:button, input:submit").button();
		this.corners();
		this.alertIE();
	},
	waitUrl : function(url) {
		var html = '<h3>Processing <a href="'+url+'">'+url+'</a> please wait..</h3>';
		html = html+'<img src="images/exec.gif" style="width:770px;height:16px;"/>';
		return html;
	},
	wait : function(msg) {
		var html = '<h3>'+msg+' - please wait..</h3>';
		html = html+'<img src="images/exec.gif" style="width:770px;height:16px;"/>';
		return html;
	},
	flash : function(button) {
		$(button).effect("pulsate", { times:1 }, 200);
		//hex instead of "gray", due to IE8 start.0 error
		$(button).effect('highlight', { color: '#848484' }, 2000);
	},
	corners : function () {
		if ($(".leftmenu").find(".selected").index()==0) {
			$(".leftmenu").find(".selected").corner("round br");
		} else {
			$(".leftmenu").find(".selected").corner("round right");
		}
		$(".pagemenu").find(".selected").corner("round bottom");
	},
	getCookie : function(cookie) {
		var src = cookie + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(src) == 0) return c.substring(src.length,c.length);
		}
		return null;
	},
	hideComments : function() {
		$("#comments").hide();
	},
	showComments : function() {
		$("#comments").show();
	},
	writeLog : function(tool, action) {
		var a=action.substring(0,100);
		var b=encodeURIComponent(a);
		var url='classes/ajax_log.php?t='+tool+'&a='+b;
		$.ajax({
			url: url,
			cache: false,
			async: true,
			timeout : 5000,
			success: function(html) {
				//
			}
		});
	},
	isIE : function() {
		return (navigator.appName == 'Microsoft Internet Explorer');
	},
	showIEAlert : function() {
		return this.getCookie(ieAlertCookie)!='false';
	},
	alertIEHide : function() {
		document.cookie=ieAlertCookie+'=false';
		$("#ieAlert").hide();
	},
	alertIE : function() {
		var p=document.location.pathname.search(/note-about-browsers.html/i);
		var idx=document.location.pathname.search(/.html/i);
		if ((this.isIE()) && (this.showIEAlert())) {
			if ((idx>-1) && (p==-1)) {
				var html='<div id="ieAlert" onClick="_4h.alertIEHide();" ';
				html=html+'style="padding:15px;margin:15px;background-color:yellow;color:red;font-size:120%;border:1px dotted red;cursor:pointer;" title="Click to remove message">';
				html=html+'<span style="font-size:130%">Note</span>: You seem too use Internet Explorer as browser. ';
				html=html+'Read the <a href="note-about-browsers.html"><b>Note about browsers</b></a> before you continue. ';
				html=html+'</div>';
				$(html).insertAfter('h2:first');
			}
		}
	},
	removeScheme : function(url) {
		if (url.indexOf('http://')>-1) {
			url = url.substr(7, url.length);
		}
		return url;
	},
	removeWww : function(url) {
		if (url.indexOf('www.')>-1) {
			url = url.substr(4, url.length);
		}
		return url;
	},
	getDomainName : function(url) {
		try {
			//from http://bytes.com/topic/javascript/answers/835342-get-domain-name-url
			return url.match(/:\/\/(.[^/]+)/)[1];
		} catch (e) {
			return "undefined";
		}
	},
	getSubDomain : function(url) {
		// if there, remove white space from both ends
		url = url.replace(new RegExp(/^\s+/),""); // START
		url = url.replace(new RegExp(/\s+$/),""); // END
 
		// if found, convert back slashes to forward slashes
		url = url.replace(new RegExp(/\\/g),"/");
 
		// if there, removes \'http://\', \'https://\' or \'ftp://\' from the start
		url = url.replace(new RegExp(/^http\:\/\/|^https\:\/\/|^ftp\:\/\//i),"");
 
		// if there, removes \'www.\' from the start of the string
		url = url.replace(new RegExp(/^www\./i),"");
 
		// remove complete string from first forward slash on
		url = url.replace(new RegExp(/\/(.*)/),"");
	 
		// removes \'.??.??\' or \'.???.??\' from end - e.g. \'.co.uk\', \'.com.au\'
		if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
			url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i),"");
 
		// removes \'.??\' or \'.???\' or \'.????\' from end - e.g. \'.us\', \'.com\', \'.info\'
		} else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
		      url = url.replace(new RegExp(/\.[a-z]{2,4}$/i),"");
		}
 
		// check to see if there is a dot \'.\' left in the string
		var subDomain = (url.match(new RegExp(/\./g))) ? true : false;
 
		return subDomain;
	},
 	trim : function(s) {
		var l=0; 
		var r=s.length -1;
		while (l < s.length && s[l] == ' ') { l++; }
		while (r > l && s[r] == ' ') { r-=1; }
		return s.substring(l, r+1);
	},
	urlLink : function(url) {
		return '<a href="'+url+'">'+url+'</a>';
	},
	buttonEnable : function(button, enable) {
		if (enable) {
			$(button).removeAttr("disabled");
			//fix jquery UI bug
			$(button).removeClass('ui-state-disabled');
		} else {
			$(button).attr("disabled", "disabled");
			$(button).addClass('ui-state-disabled');
		}
	},
	sc_set_wait : function(element) {
		$(element).html('<img src="images/sc.gif"/>');
	},
	sc_set_ok : function(element) {
		$(element).html('<img src="images/sc_ok.png"/>');
	},
	sc_set_fail : function(element) {
		$(element).html('<img src="images/sc_fail.png"/>');
	},
	sc_is_ok : function(element) {
		return ($(element).html().toLowerCase() == '<img src="images/sc_ok.png">');
	}
};

