(function($) {

	/*-------------------------------
		CENTER OBJECT ON SCREEN
	---------------------------------*/
	$.fn.centerOnScreen = function(){
		if(!this[0]){ return false; }
		var $this = this[0];
		var yScroll = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
		var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		var windowWidth  = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		$this.style.top  = (yScroll+((windowHeight - $this.offsetHeight)/2)) + "px";
		$this.style.left = ((windowWidth/2) - ($this.offsetWidth/2)) + "px";
	}

	/*-------------------------------
		GET OBJECT'S XY COORDINATES
	---------------------------------*/
	$.fn.getCoordinates = function () {
		if(!this[0]){ return false; }
		var obj = this[0];
		x = y = 0;
		if (obj.offsetParent) {
			x = obj.offsetLeft;
			y = obj.offsetTop;
			while (obj = obj.offsetParent) {
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
		}
		return {x:x,y:y};
	}
	
	/*-------------------------------
		NBOX OVERLAY
	---------------------------------*/
	$.fn.nBox = function(){
		if(!this){ return false; }
		var $this = this;
		$("body").append("<div id='coverMe'></div>");
		var wHeight = window.innerHeight || document.documentElement.clientWidth || document.body.clientWidth;
		var windowHeight = (document.height > wHeight ) ? document.height : wHeight;
		var windowWidth = "100%";
		$("#coverMe").height(windowHeight).width(windowWidth).fadeIn();
		$($this).fadeIn();
		$($this).centerOnScreen();
		function hideNBox() {
			$("#coverMe").fadeOut();
			$(".nBox").fadeOut();
			window.nBoxTimeout = setTimeout( function() { $("#coverMe").remove(); }, 1500);
		}
		$($this).children("a[href='#close']").click(function() {
			hideNBox();
			return false;
		});
		$("#coverMe").bgIframe(); // Add iframe for IE need bgiframe plugin
		$("#coverMe").click(function() {
			hideNBox();
		});
		
	}
	
	/*-------------------------------
		Reset Text Field
	---------------------------------*/
	$.fn.resetTxtField = function(){
		if(!this){ return false; }
		var $this = this;
		$this.rem = $(this).val();
		$(this).focus(function(){
			if($(this).val()==$this.rem){ $(this).val("") }
		});
		$(this).blur(function(){
			if($(this).val()==""){ $(this).val($this.rem) }
		});
	}
	
	/*-------------------------------
		Make Tabs Control
	---------------------------------*/
	$.fn.makeTabsController = function(o){
		if(!this){return false};
		var $this = this;
		$this.whichTabContent = o.tabViewId;
		$this.fadeSpeed = o.fadeSpeed || "slow";
		$("#"+$this.whichTabContent+" :first-child").fadeIn();
		$this.children("li a").click(function(){
			var thisID = this.href.split("#")[1];
			var current = $this.attr("class");
			if(current==thisID){return false};
			$("#"+current).fadeOut($this.fadeSpeed);
			$this.attr("class",thisID);
			$("#"+thisID).fadeIn($this.fadeSpeed);
			return false;
		});
	}
	
	/*-------------------------------
		Set Indicator
	---------------------------------*/
	$.fn.setIndicator = function(o){
		if(!this || !this[0]){return false};
		var o 		= o 		|| {};
		var objSel 	= o.objSel 	|| this;
		var indc 	= o.indc 	|| ".indicator";
		var offsetX = o.offsetX || "";
		var offsetY = o.offsetY || "";
		// first check which button is selected
		var thisX = $(objSel).getCoordinates().x + ($(objSel).width()/2) + offsetX;
		var thisY = $(objSel).getCoordinates().y +  $(objSel).height()   + offsetY;
		$(indc).css({top:thisY,left:thisX});
		$(this).unbind("click.setIndicator");
		$(this).bind("click.setIndicator",function(){
			//$(".loading").loadingAnimation();
			var thisX = $(this).getCoordinates().x + ($(this).width()/2) + offsetX;
			var thisY = $(this).getCoordinates().y +  $(this).height()   + offsetY;
			var page  = $(this).attr("href");
			var color = $(this).attr("color") || false;
			$(indc).animate({top:thisY,left:thisX});
			if(color){
				$(indc).attr("src","/img/backgrounds/indicator_"+color+".png");
			} else {
				$(indc).attr("src","/img/backgrounds/indicator.png");
			}
			$(".mainMenu li").removeClass("selected");
			$(this).parent("li").addClass("selected");
			/*$("#Content").load(page,{ajax:true},function(){
				$(".side .img").fadeIn(500);
				eventHandler.onload();
			});
			$(this).ajaxError(function(){
				$(".errorMsg").errorMessage();
			})*/
			//return false;
		})
	}
	
	/*-------------------------------
		Loading Animation
	---------------------------------*/
	$.fn.loadingAnimation = function(o){
		if(!this || !this[0]){return false};
		var o 		= o 		|| {};
		var objSel 	= o.objSel 	|| this;
		var offsetX = o.offsetX || false;
		var offsetY = o.offsetY || false;
		$(objSel).hide();
		if(offsetX || offsetY){
			$(objSel).css({top:offsetY,left:offsetX});
		} else {
			$(objSel).centerOnScreen();
		}
		$(objSel).fadeIn();
		$(objSel).ajaxStop(function(){
			$(objSel).fadeOut();
		});
		$(objSel).ajaxError(function(){
			$(objSel).fadeOut();
		});
	}
	
	/*-------------------------------
		Error Message
	---------------------------------*/
	$.fn.errorMessage = function(o){
		if(!this || !this[0]){return false};
		var o 		= o 		|| {};
		var objSel 	= o.objSel 	|| this;
		var text 	= o.txt 	|| "A network error occurred, please try again.";
		var offsetY = o.offsetY || false;
		var offsetY = o.offsetY || false;
		$(objSel).hide();
		if(offsetX || offsetY){
			$(objSel).css({top:offsetY,left:offsetX});
		} else {
			$(objSel).centerOnScreen();
		}
		$(objSel).html(text);
		$(objSel).fadeIn();
		window.setTimeout = setTimeout(function(){
			$(objSel).fadeOut();
		},1000)
	}
	
	/*-------------------------------
		Send Email
	---------------------------------*/
	$.fn.sendMail = function(o){
		if(!this || !this[0]){return false};
		$(this).unbind("click.sendMail");
		$(this).bind("click.sendMail",function(){
			$(o.formId).submit(function(){
				$.post($(this).attr("action"),$(this).serializeArray(),function(data){
					if(data=="success"){
						$(o.formId+" .msg").fadeIn().html("Thank you for your message! We'll get back to you shortly.");
						$(o.formId+" div").hide();
						$(o.formId+" legend").hide();
						return false;
					} else if(data=="failed"){
						$(o.formId+" .msg").fadeIn().html("Failed to send. Please try again.");
						return false;
					} else if(data!=="success" || data!=="failed"){
						$(o.formId+" .msg").fadeIn().html(data);
						return false;
					}
				})
			return false;
			});
		})
	}
	
	/*-------------------------------
		Expand Map
	---------------------------------*/
	$.fn.expandMe = function(o){
		if(!this || !this[0]){return false};
		$(this).unbind("click.expandMe");
		$(this).bind("click.expandMe",function(){
			var me = $(this);
			$(".map").animate({width:'700px'},function(){
				me.html('&lt; Close Map');
				me.unbind("click.expandMe");
				me.bind("click.expandMe",function(){
					$(".map").animate({width:'210px'},function(){
						me.html('Expand Map &gt;');
						eventHandler.expandMe();
					});
					return false;
				})
			});
			return false;
		})
	}
	
})(jQuery);

/*------------
	AJAX EVENT HANDLERS
--------------*/
var eventHandler = {	// WHERE THE MAGIC HAPPENS
	bind:function(o) {
		var o = o || {};
		var method = o.method || "";
		var arrMethods = method.split(",");
		for (var i in arrMethods) {			
			this[$.trim(arrMethods[i])]();
		}
	},
	// BIND EVENTS TO ELEMENTS ON THE INITIAL PAGE LOAD
	onload:function() {
		for (var i in this) {
			if (i != 'bind' && i != 'onload') {
				this[i]();
			}
		}
	},
	setIndicator:function(){
		if(!$(".pgAdmin").length){
			$(".mainMenu a").setIndicator({indc:".indicator",objSel:".mainMenu li[class='selected'] a",offsetX:-20,offsetY:29});
			$(".side .img").fadeIn(500);
		}
	},
	sendMail:function(){
		$("button.sendMail").sendMail({formId:"#sendMail"});
	},
	expandMe:function(){
		$(".expandMap").expandMe();
	}
}

/*------------
	ONLOAD
--------------*/
$(function(){ eventHandler.onload(); });