//Author: Steve Tomlin
//Date: 28/7/2009
//ver: 1

// Global (every page)_________________________________________________________________________________
    function ClassGlobal(){
		this.objTimerHideNav = null;
    //input selections
        this.init = function(){	
			this.fadeInText();
			this.setNav();	
			this.setInputs();			
        }
		this.fadeInText = function(){
			$("#content").fadeIn();
		}
		this.setInputs = function(){
			//onfocus="focusText(this,this.title);HoverSearchBox(this,1);"
            //onblur="blurText(this,this.title);HoverSearchBox(this,0);"
            var objInst = this;
             $("input").each(
                function(intIndex){
                    //set title to value
                    this.title = this.value;
                    //add onfocus
                    $(this).bind("focus",{objInst:objInst},function(e){
                        e.data.objInst.focusText(this,this.title);
                        return false;
                    });
                    //add onblur
                    $(this).bind("blur",{objInst:objInst},function(e){
                        e.data.objInst.blurText(this,this.title);
                        return false;
                    });
                }
            );			
		}
		this.setNav = function(){				
			//mouseover
			var objInst = this;
			$(".nav li a").bind("mouseover",function(e){													 
				 objInst.stopTimer(objInst.objTimerHideNav);
				//remove current visible
				//$(".nav .subnav ul").removeClass("on");
				var objToClear  = $(".nav .subnav ul").stop().css("height","").css("opacity","1").css("display","none").css("z-index","1");
				//$(".nav .subnav ul").css("z-index","1").fadeOut();
				
				//show current
            	//$(this).parent().find(".subnav ul").addClass("on");
				$(this).parent().find(".subnav ul").css("z-index","3").slideDown();
			});
			$(".nav li .subnav a").unbind("mouseover");
			
			 var objMouseOut = new ClassIsMouseOut();
			//mouseout
			$(".nav li").bind("mouseout",function(e){
				//var objTopNavLi	= $(this).parent()[0];
                var isMouseOutA = objMouseOut.isMouseOutObjectArea(e,new Array($(this)[0]));
                if(isMouseOutA){				
            		//$(this).parent().find(".subnav ul").removeClass("on");
					//$(this).parent().find(".subnav ul").slideUp();
					//$(this).parent().find(".subnav ul").css("display","none");
					objInst.stopTimer(objInst.objTimerHideNav);
					var obj = this;
					objInst.objTimerHideNav = setTimeout(function(){$(obj).parent().find(".subnav ul").css("z-index","1").fadeOut()},400,obj);
				}
			});
		}
		this.stopTimer = function(objTimer){
			if(objTimer){clearTimeout(objTimer);objTimer = null};	
		}
        //global functions
        this.focusText = function(obj,strCheck){
            if(obj.type=="text"){
                if(obj.value==strCheck){
                    obj.value='';
                }
            }
        }
        this.blurText = function(obj,strCheck){
            if(obj.type=="text"){
                if(obj.value=='' || obj.value.indexOf(" ")==0){
                    obj.value=strCheck;
                }
            }
        }
    //END input selections
    //browser stuff
        this.isMSIE = (navigator.userAgent.indexOf("MSIE")!=-1);
        this.isMACIE = (this.isMSIE && navigator.userAgent.indexOf("Mac")!=-1);
        this.isSAFARI = (navigator.userAgent.indexOf("Safari")!=-1);

        //msie fix
        this.MSIEActualVersion = function(){
            //MSIE Gets actual version number.
            //NOTE: navigator.appVersion does not get the actual version number for internet explorer
            var intActualVersion=parseFloat(navigator.version);
            if(this.isMSIE){
                var strBrowser=navigator.userAgent;
                var strMSIE="MSIE";
                var intSpace=1;
                intActualVersion=parseFloat(strBrowser.substr(strBrowser.indexOf(strMSIE)+strMSIE.length+intSpace));
                intActualVersion=(isNaN(intActualVersion))?-1:intActualVersion;
            }else{
                intActualVersion = 0;
            }
            return intActualVersion;
        }
        this.getBrowserV = function(){
            if(this.isMSIE){
                return this.MSIEActualVersion();
            }else{
                return navigator.appVersion;
            }
        }
    //END browser stuff
        this.init();
    }
	
//IS MOUSE OUT FUNCTIONS
	function ClassIsMouseOut(){
		this.getParent = function(obj,objThis){
			//obj is objDepartedNode
			while(obj){
				if(obj == objThis){
					return obj;
				}
				obj = obj.parentNode;
			}
			return null
		}
		this.IsChildHovered = function(objThis,objArrivedNode){
			var isChildHovered = false;
			var objParent = this.getParent(objArrivedNode,objThis);
			if(objParent){
				isChildHovered = true;
			}
			return isChildHovered;
		}
		this.isMouseOutObjectArea = function(evt,arrAllowedMouseOut){//,obj2,obj3
			//this is to detect if a user mouseout from one object to another object
			//for example, a user will mouseout from MORE to HIDEMELAYER or vice versa
			//in which case all further mouseout functionality will be void.




			var objArrivedNode = (evt.relatedTarget)?evt.relatedTarget:evt.toElement;
			for(var a = 0; a < arrAllowedMouseOut.length; ++a){
				if(objArrivedNode == arrAllowedMouseOut[a]){
					return false;
				}else if(this.IsChildHovered(arrAllowedMouseOut[a],objArrivedNode)){
					//a mouseout will trigger even if a user does a mouseover onto a node inside the parent node
					//so this detects if the mouseout was triggered by mouseover onto the parents child nodes.
					//and if it does then all further mouseout functionality will be void.
					return false
				}
			}
			return true;
		}
	}
//end isMouseOut	
	
    //onloads
    var objGlobal;
    $(document).ready(function(){							   
        //text onfocus etc...
         objGlobal = new ClassGlobal();
    });

//END global
