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

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){
	
	//Enable cufon font replacement
	//Cufon.replace('h1, h2, h3, ul#nav li a, #applynow, .nivo-caption h3, .nivo-caption p, #splash', { fontFamily: 'Transit' });
	
	if (jQuery.cookie('lang') != "1")
	{
		//Show the splash page
		jQuery.blockUI({ message: jQuery('#splash'), css: { width: '268px' } });
	}
	
	//Clear the input fields
	jQuery('input, textarea').clearInput();
	
	//Enable the homepage slideshow
	jQuery('#slideshow').nivoSlider({
		effect: 'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices: 1,
		animSpeed: 500,
		pauseTime: 6000,
		startSlide: 0, //Set starting Slide (0 index)
		directionNav: true, //Next & Prev
		directionNavHide: false, //Only show on hover
		controlNav: false, //1,2,3...
		controlNavThumbs: false, //Use thumbnails for Control Nav
		keyboardNav: true, //Use left & right arrows
		pauseOnHover: true, //Stop animation while hovering
		manualAdvance: false //Force manual transitions
	});

	//Enable the innerpages slideshow
	jQuery('#banner').nivoSlider({
		effect: 'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices: 1,
		animSpeed: 500,
		pauseTime: 6000,
		startSlide: 0, //Set starting Slide (0 index)
		directionNav: false, //Next & Prev
		directionNavHide: false, //Only show on hover
		controlNav: false, //1,2,3...
		controlNavThumbs: false, //Use thumbnails for Control Nav
		keyboardNav: false, //Use left & right arrows
		pauseOnHover: false, //Stop animation while hovering
		manualAdvance: false //Force manual transitions
	});

	
	//Set hover timeouts for the forms
	jQuery('form#login').hover(function(){
	    
	    //Clear the timeout on hover
	    clearTimeout(jQuery(this).data('timeout'));
	
	},function(){
	    
	    var t = setTimeout(function() {
	       	
	       	jQuery('#header #topTools a.login').removeClass('active');
	    	jQuery('form#login').fadeOut('fast');
	    
	    }, 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('#header #topTools a.search').removeClass('active');
	    	jQuery('form#search').fadeOut('fast');
	    
	    }, 4000);
	    
	    jQuery(this).data('timeout', t);
	
	});

});

//Set the language cookie
function setLanguage(lang, currentLang){
		
	jQuery.cookie('lang', "1", { expires: 365, path: '/'});
	
	if (lang == currentLang)
	{
		//en or fr 
		jQuery.unblockUI();
	}
	else
	{
		window.location = "/" + lang + "/";
	}
}

//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('#header #topTools a.search').removeClass('active');
		jQuery('form#search').fadeOut();

		
	} else {
		
		//It's not visable
		jQuery('#header #topTools a.search').addClass('active');
		jQuery('form#search').fadeIn();
		
	}

}

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

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

		
	} else {
		
		//It's not visable
		jQuery('#header #topTools a.login').addClass('active');
		jQuery('form#login').fadeIn();
		
	}

}


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