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

//CHECK DOWNLOADFORM REGISTRATION
var downloadRegistration = jQuery.cookie('downloadRegistration');

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){
		
	// Add class to odd lines of a table
	jQuery("tr:even").addClass('even');
		
	//Clear the input fields
	jQuery('input, textarea').clearInput();

	//Set hover timeouts for the forms
	jQuery('form#partnerLogin').hover(function(){
	    
	    //Clear the timeout on hover
	    clearTimeout(jQuery(this).data('timeout'));
	
	},function(){
	    
	    var t = setTimeout(function() {
	       	
	    	jQuery('form#partnerLogin').fadeOut('fast');
		    jQuery('header#siteHeader ul#topTools li.login').removeClass('active');
    
	    }, 4000);
	    
	    jQuery(this).data('timeout', t);
	
	});
	
	//Set hover timeouts for the forms
	jQuery('form#search').hover(function(){
	    
	    //Clear the timeout on hover
	    clearTimeout(jQuery(this).data('timeout'));
	
	},function(){
	    
	    var t = setTimeout(function() {
	       	
	    	jQuery('form#search').fadeOut('fast');
	       	jQuery('header#siteHeader ul#topTools li.search').removeClass('active');
	    
	    }, 4000);
	    
	    jQuery(this).data('timeout', t);
	
	});

	//Set hover timeouts for the forms
	jQuery('ul#quickTools').hover(function(){
	    
	    //Clear the timeout on hover
	    clearTimeout(jQuery(this).data('timeout'));
	
	},function(){
	    
	    var t = setTimeout(function() {
	       	
	    	jQuery('ul#quickTools').fadeOut('fast');
	       	jQuery('header#siteHeader ul#topTools li.quickTools').removeClass('active');
	    
	    }, 4000);
	    
	    jQuery(this).data('timeout', t);
	
	});
	
});

//Toggle the visibility of the search form
function toggleSearch(){
	
	//Check if the container is visible
	if(jQuery('form#search').is(':visible')){
		
		//It's visible
		jQuery('form#search').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.search').removeClass('active');

		
	} else {

		jQuery('form#partnerLogin').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.login').removeClass('active');
		jQuery('ul#quickTools').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.quickTools').removeClass('active');
		
		//It's not visable
		jQuery('header#siteHeader ul#topTools li.search').addClass('active');
		jQuery('form#search').fadeIn('fast');
		
	}

}

//Toggle the visibility of the login form
function toggleLogin(){

	//Check if the container is visible
	if(jQuery('form#partnerLogin').is(':visible')){
		
		//It's visible
		jQuery('form#partnerLogin').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.login').removeClass('active');

		
	} else {

		jQuery('form#search').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.search').removeClass('active');
		jQuery('ul#quickTools').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.quickTools').removeClass('active');
		
		//It's not visable
		jQuery('header#siteHeader ul#topTools li.login').addClass('active');
		jQuery('form#partnerLogin').fadeIn('fast');
		
	}

}

//Toggle the visibility of the quickTools
function toggleQuickTools(){

	//Check if the container is visible
	if(jQuery('ul#quickTools').is(':visible')){
		
		//It's visible
		jQuery('ul#quickTools').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.quickTools').removeClass('active');

		
	} else {

		jQuery('form#search').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.search').removeClass('active');
		jQuery('form#partnerLogin').fadeOut('fast');
		jQuery('header#siteHeader ul#topTools li.login').removeClass('active');
		
		//It's not visable
		jQuery('header#siteHeader ul#topTools li.quickTools').addClass('active');
		jQuery('ul#quickTools').fadeIn('fast');
		
	}

}

//Switch the language based on the value
function switchLanguage(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;
			
		}

	}

}

//clearInput 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;
		}
	});
};

//clearForm function
jQuery.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return jQuery(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};
