//SET NOCONFLICT TO WORK WITH OTHER LIBRARIES
jQuery.noConflict();

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){

	//Styles
	jQuery('ul li:last-child, ol li:last-child').addClass('last');
	jQuery('#leftSidebar ul#sideNews li:last-child').addClass('last');
	jQuery('#newsList li:last-child').addClass('last');
	jQuery('#news div:last-child').addClass('last');
	jQuery('ul#childnav li ul li.here').parent('ul').addClass('here');
	
	//Navigation Dropdowns
	jQuery('ul.sf-menu').supersubs({ 
        minWidth:    14, 
        maxWidth:    27,
        extraWidth:  1
	}).superfish({ 
	    delay:       1000,
	    animation:   {opacity:'show'},
	    speed:       '1000',
	    autoArrows:  false,
	    dropShadows: false
	});
	
	jQuery('form#newsletter input').clearInput();

	//Remove titles on top navigation
	jQuery('ul.sf-menu li a').removeAttr('title');


	//ChildNav show & hide nav functions
	var childNavConfig = {    
	     sensitivity: 3, //sensitivity threshold (must be 1 or higher)    
	     interval: 200, //milliseconds for onMouseOver polling interval    
	     over: showChildNav,  
	     timeout: 800, //milliseconds delay before onMouseOut    
	     out: hideChildNav 
	};
	
	//Enable childnav expand/collapse
	jQuery('ul#childnav li').hoverIntent(childNavConfig);
	
});


//Show child navigation function
function showChildNav(){
		
	//Check if hovered item contains a second level
	if (jQuery(this).children('ul').length > 0){
		
		//Check if second level is visible before hiding others
		if(jQuery(this).children('ul').is(':visible')){
		
			//Do nothing
		
		}
		else {
			
			jQuery(this).addClass('expanded');
			
			//Slide up all other visible child navs
			jQuery('ul#childnav li ul').slideUp('slow');
			
			//Slide down current child nav
			jQuery(this).children('ul').slideDown('slow');
		}
		
	}

}


//Hide child navigation function
function hideChildNav(){
	
	//Check if hovered item is currently active
	if(jQuery(this).hasClass('here') || jQuery(this).hasClass('parent-here')){
	
		//Do nothing
	
	}
	else {
		
		jQuery(this).removeClass('expanded');
		
		//Slide up current child nav
		jQuery(this).children('ul').slideUp('slow');
		
	}
	
}

//Load url from selectbox
function loadUrl(url){

	//Check if its an internal or external link
	var isInternal = function(u) {
		return ((u.indexOf("://") == -1 && u.indexOf("www.") == -1) || u.indexOf(window.location.host) > -1);
	}

	if (url == "#"){
	
		//Do nothing
	
	}
	else {	
	
		if (isInternal(url)){
			
			window.location = url;
			
		}
		else if (window.confirm("Are you sure you want to leave our site?")){
			
			window.location = url;
			
		}

	}

}

//Load popup from selectbox
function loadPopup(url){

	//Check if its an internal or external link
	var isInternal = function(u) {
		return ((u.indexOf("://") == -1 && u.indexOf("www.") == -1) || u.indexOf(window.location.host) > -1);
	}
	
	if (url == "#"){
	
		//Do nothing
	
	}
	else {
	
		if (isInternal(url)){
			
			window.open(url,'nbif','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,width=800,height=600');
			
		}
		else if (window.confirm("Are you sure you want to leave our site?")){
			
			window.open(url,'nbif','resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,width=800,height=600');
			
		}

	}
	

}

//Clear input fields function
jQuery.fn.clearInput = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};