if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
	document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
	document.addEventListener( "DOMContentLoaded", function(){ jQuery.ready(); }, false );
}
jQuery.event.remove( window, "load", jQuery.ready );
jQuery.event.add( window, "load", function(){ jQuery.ready(); } );
jQuery.extend({
	includeStates: {},
	include: function(url, callback, dependency){
		if ( typeof callback != 'function' && ! dependency ) {
			dependency = callback;
			callback = null;
		}
		url = url.replace('\n', '');
		jQuery.includeStates[url] = false;
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.onload = function () {
			jQuery.includeStates[url] = true;
			if ( callback )
				callback.call(script);
		};
		script.onreadystatechange = function () {
			if ( this.readyState != "complete" && this.readyState != "loaded" ) return;
			jQuery.includeStates[url] = true;
			if ( callback )
				callback.call(script);
		};
		script.src = url;
		if ( dependency ) {
			if ( dependency.constructor != Array )
				dependency = [dependency];
			setTimeout(function(){
				var valid = true;
				$.each(dependency, function(k, v){
					if (! v() ) {
						valid = false;
						return false;
					}
				})
				if ( valid )
					document.getElementsByTagName('head')[0].appendChild(script);
				else
					setTimeout(arguments.callee, 10);
			}, 10);
		}
		else
			document.getElementsByTagName('head')[0].appendChild(script);
		return function(){
			return jQuery.includeStates[url];
		}
	},
	readyOld: jQuery.ready,
	ready: function () {
		if (jQuery.isReady) return;
		imReady = true;
		$.each(jQuery.includeStates, function(url, state) {
			if (! state)
				return imReady = false;
		});
		if (imReady) {
			jQuery.readyOld.apply(jQuery, arguments);
		} else {
			setTimeout(arguments.callee, 10);
		}
	}
});

function isTouchDevice(){
	try {
		document.createEvent('TouchEvent');
		return true;
	}catch (e){
		return false;
	}
}

$.include('/scripts/js/jQ.Cookie.js');
function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}


function checkMobile(){
	setCookie('askedTouch', true, 7);
	
	var r = confirm('Voulez-vous utiliser notre site mobile optimisé pour les téléphones tactiles ?');
	if(r == true){
		window.location = '/actions/mobile_goto_touch.php';
		return true;
	}
	else{
		//
	}
}

/*
if(isTouchDevice() && !getCookie('askedTouch')){
	checkMobile();
}
*/

jQuery.fn.clearOnClick = function(){
	var $Default = $(this).val();
	
	$(this).focus(function(){
		if(this.value == $Default) {
			this.value = '';
		}
	});

	$(this).blur(function(){
		if(this.value == '') {
			this.value = $Default;
		}
	});
};

function strtoupper(chaine){
	return chaine.toUpperCase();
}

function strtolower(chaine){
	return chaine.toLowerCase();
}

function validPostalCode(Postal){
	return /^([a-ceghj-npr-tv-z]){1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}[a-ceghj-npr-tv-z]{1}[0-9]{1}$/.test(Postal);
}

function popNotification($mainId, $Type, $Text){
	if(!$($mainId + ' .notification').length){
		$($mainId).prepend('<div class="notification n' + $Type + ' png_bg" style="display: none;"><div></div></div>');
		$($mainId + ' .notification').children('div').html($Text);
		$($mainId + ' .notification').slideDown(250);
	} else {
		if($($mainId + ' .notification').css('display') == 'none'){
			$($mainId + ' .notification').children('div').html($Text);
			$($mainId + ' .notification').slideDown(250);
		} else {
			$($mainId + ' .notification').slideUp(250, function(){
				$($mainId + ' .notification').children('div').html($Text);
				$($mainId + ' .notification').slideDown(250);
			});
		}
	}

	return true;
}

function popLogin(){
	alert('need de connexion ! ');
}

function isValidDate($a, $b) {
	var $Split = $b.split($a);
	var $Annee = $Split[0];
	var $Mois = $Split[1];
	var $Jour = $Split[2];
	
	if(!$Annee || !$Mois || !$Jour || $Mois <= 0 || $Mois > 31 || $Jour <= 0 || $Jour > 31){
		return false;
	}
	if($Annee.length != 4 && $Annee <= 2010){
		return false;
	}
	
	return true;
} 

function isValidTime($a, $b) {
	var $Split = $b.split($a);
	var $Heure = $Split[0];
	var $Minute = $Split[1];
	
	if(!$Heure || !$Minute || $Heure < 0 || $Heure > 23 || $Minute < 0 || $Minute > 60){
		return false;
	}
	
	return true;
}

function ulMove(selectedRoute){
	var $Selected = selectedRoute;
	
	var distance = $('[data-aCount='+$Selected+'] a').offset().top;
	
	$('[data-aCount!='+$Selected+'] a').parent('li').css('background-color', '');
	$('[data-aCount!='+$Selected+'] a').css('color', '');
	
	$('[data-aCount='+$Selected+'] a').parent('li').css('background-color', 'yellow');
	$('[data-aCount='+$Selected+'] a').css('color', '#000000');
	
	$('html, body').stop().animate({'paddingTop': ($(window).scrollTop(distance - ($(window).height()/2))) + 'px'}, 1);
}

function timeToSentence($Date, $LongWord){
	var $Secondes = 0;
	
	if($Date > 0){
		var $Secondes = $Date;
	}
	
	if($LongWord == true){
		var $mJour = 'jour';
		var $mHeure = 'heure';
		var $mMinute = 'minute';
		var $mJours = 'jours';
		var $mHeures = 'heures';
		var $mMinutes = 'minutes';
	} else {
		var $mJour = 'j';
		var $mHeure = 'h';
		var $mMinute = 'm';
		var $mJours = 'j';
		var $mHeures = 'h';
		var $mMinutes = 'm';
	}
	
	$Minutes = Math.floor($Secondes / 60);
	$Heures = Math.floor($Minutes / 60);
	$Minutes -= $Heures * 60;
	$Jours = Math.floor($Heures / 24);
	$Heures -= $Jours * 24;
	if($Secondes == 0){
		return 0;
	}
	if($Secondes <= 60){
		return '< 1 minute';
	}
	if($Jours > 0){
		return $Jours+' '+($Jours >= 2 ? $mJours : $mJour) + ($Heures ? ' '+$Heures+' '+($Heures >= 2 ? $mHeures : $mHeure) : '') + ($Minutes > 0 ? ' '+ $Minutes+' '+($Minutes >= 2 ? $mMinutes : $mMinute) : '');
	}
	if($Heures > 0){
		return $Heures+' '+($Heures >= 2 ? $mHeures : $mHeure) + ($Minutes > 0 ? ' '+ $Minutes+' '+($Minutes >= 2 ? $mMinutes : $mMinute) : '');
	}
	if($Minutes > 0){
		return $Minutes+' '+($Minutes >= 2 ? $mMinutes : $mMinute);
	}
	return '< 1 minute';
}

function formatDay($Day){
	switch($Day){
		case 0: return 'Dimanche'; break;
		case 1: return 'Lundi'; break;
		case 2: return 'Mardi'; break;
		case 3: return 'Mercredi'; break;
		case 4: return 'Jeudi'; break;
		case 5: return 'Vendredi'; break;
		case 6: return 'Samedi'; break;
	}
}

tabs = {
	init : function(){
		$currentTab = null;
		$('.tabs').each(function(){
			$(this).find('.tab-content').hide();
			$($(this).find('ul.nav .selected a').attr('href')).fadeIn(300);
			$(this).find('ul.nav a').click(function(event){
				event.preventDefault();
				if($currentTab != $(this).attr('href')){
					$(this).parents('.tabs').find('.tab-content').hide();
					$($(this).attr('href')).fadeIn(300);
					$(this).parent().addClass('selected').siblings().removeClass('selected');
					$currentTab = $(this).attr('href');
				}
			});
		});
	}
}

var $logCount = 0;
jQuery.logThis = function(Message){
	// Plus 1 au log
	$logCount++;
	
	if(!Message){
		Message = 'Erreur - Message non défini.';
	}
		
	// Log ID
	var $logUl = $('#log ul');
		
	// Nouveau ID au LI
	var $liId = 'log_' + $logCount;
		
	$logUl.prepend('<li id="'+$liId+'"></li>');
	$('#' + $liId).html(Message).fadeIn(250);
	
	$('#' + $liId).delay(4500).fadeOut(250, function(){
		$(this).remove();
	});
}

$.include('/scripts/js/jQ.Facebox.js');
$.include('/scripts/js/jQ.Superfish.js');
$.include('/scripts/js/jQ.Tooltip.js');
$.include('/scripts/js/jQ.timeInput.js');
$.include('/scripts/js/jQ.Alphanum.js');
$.include('/scripts/js/jQ.goMap.js');
$.include('/scripts/js/jQ.scrollTo.js');

$(document).ready(function(){
	// On initialise les tabs
	tabs.init();
	
	if($requireSt){
		if(!$.cookie('definedCp') || !$.cookie('definedSt')){
			$.cookie('definedCp', true, { domain: 'tranzit.ca', path: '/', expires: 2 });
			$.facebox({ ajax: '/ajax/define_cp.php' })
		}
	}
	
	$('.slideDown dt').click(function(){
		$(this).toggleClass('active').parent('.slideDown').find('dd').slideToggle()
	});

	$('.top').each(function(){
		$(this).addClass('tooltip').attr('title', 'Remontez en haut de la page.');
	});
	$defaultTop = ($(window).scrollTop() + $(window).height() - 50);
	$('.top').click(function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
	if($('.tooltip').length){
		$('.tooltip').easyTooltip();
		
	}
	
	if($('#countdown_dashboard').length){
		$.include('/scripts/js/jQ.lwtCountdown.js');
	}
	
	$('ul.sf-menu').superfish({
		autoArrows:  true,
		dropShadows: false,
		delay:       50,
		animation:   {opacity:'show',height:'show'}
	});
	

	//$('.list-1 li').hover(function(){$(this).stop().animate({backgroundColor:'#fafafa'})},function(){$(this).stop().animate({backgroundColor:'#fff'})})
	$('.hover').append('<span></span>').hover(function(){$(this).find('>span').stop().animate({opacity:.3})},function(){$(this).find('>span').stop().animate({opacity:0})})
	$('.link-1')
		.hover(function(){
			$(this).stop().animate({
				paddingRight:'21px'},500,'easeOutBounce') 
			},
			function(){
				$(this).stop().animate({
				paddingRight:'12px'},500,'easeOutBounce')}
	);
});
