/*  ________________________________
   | _link_externe
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function _link_externe(anchor) { 
	_linkexterne = window.open(anchor.href);
	setTimeout('_linkexterne.focus();',250);
	return false;
}

/*  ________________________________
   | _link_meteo
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function _link_meteo(anchor) { 
	_linkmeteo = window.open(anchor.href,null,'height=260,width=420,status=no,toolbar=no,menubar=no,location=no');
	setTimeout('_linkmeteo.focus();',250);
	return false;
}

/*  ________________________________
   | _link_print
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function _link_print(anchor) { 
	_linkprint = window.open(anchor.href,'imprimez','height=560,width=770,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
	setTimeout('_linkprint.focus();',250);
	return false;
}


/*  ________________________________
   | _link_send
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function _link_send(anchor) { 
	_linksend = window.open(anchor.href +'?url='+escape(location),'envoyer','height=550,width=400,status=no,toolbar=no,menubar=no,location=no,scrollbars=no');
	setTimeout('_linksend.focus();',250);
	return false;
}


/*  ________________________________
   | POPUP 
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
	
function popup(popupName,url,popupWidth,popupHeight){
	win = window.open(url, popupName,"width=" + popupWidth + ",height=" + popupHeight + ",resizable=no,scrollbars=no,toolbar=no,status=no,location=no,directories=no");
	//bug de NN4 quand focus trop vite, attend 0,25 sec
	setTimeout('win.focus();',250);
	return false;
}

function popupScroll(popupName,url,popupWidth,popupHeight){
	win = window.open(url, popupName,"width=" + popupWidth + ",height=" + popupHeight + ",resizable=no,scrollbars=yes,toolbar=no,status=no,location=no,directories=no");
	//bug de NN4 quand focus trop vite, attend 0,25 sec
	setTimeout('win.focus();',250);
	return false;
}


/*  ________________________________
   | _link_popstreamlcn
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function _link_popstreamlcn(url, player) { 
	window.open(url,"playerstream_"+player,"menubar=0,resizable=0,width=650,height=420"); 
	return false;
}



/*  ________________________________
   | playerlcn
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
	
function playerlcn(anchor){
	location.replace(anchor.href);
}

/*  ________________________________
| CRAWLER
 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */

 function crawl(){
	for (var i=0; i<document.getElementsByTagName('a').length; ++i) {
		var link = document.getElementsByTagName('a')[i];
		
		if ( link.rel == 'externe') {
			link.onclick = function(){ return _link_externe(this); }

		} /*else
		if ( link.rel == 'playerlcn') {
			link.onclick = function(){ return playerlcn(this); }

		}*/ else
		if ( link.rel == 'streaming_lca') {
			link.onclick = function(){ return popup('argent',this.href,650,503); }
			
		} else
		if ( link.rel == 'streaming_lcn') {
			link.onclick = function(){ return _link_popstreamlcn(this.href, 'lcn'); }
			
		} else
		if ( link.rel == 'print') {
			link.onclick = function(){ return _link_print(this); }
			
		} else
		if ( link.rel == 'send') {
			link.onclick = function(){ return _link_send(this); }
			
		} else
		if ( link.rel == 'poptxtvideo') {
			link.onclick = function(){ return popup('video',this.href,400,454); }
			
		} else
		if ( /^feedroom_([a-z])+$/.test(link.rel)) {
			link.onclick = function(){ return _feedroom(this); }

		}
	}
}

$(document).ready(function(){
	/* Checkup pour la boite video dans les textes complets */
	if($(".boiteVideo")){$(".boiteVideo div:last").addClass("last");}
	if($(".lastchronique")){$(".lastchronique div:last").addClass("last");}
	if($(".lequipe")){$(".lequipe ul li:last").addClass("last");}
	if($(".c_archives")){$(".c_archives dl:last").addClass("last");}
	if($(".c_annees")){$(".row div.c_annees:last").addClass("last");}
});


/*  ________________________________
   | Chroniqueurs
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function checkRadio(){
	/* Détermine le param passé dans l'url */
	maliste = ['sallenouvelle','claudepoirier','denislevesque','richardmartineau','ronfournier','jocelynecazin','larocquelapierre','helicopteretva','denisebombardier'];
	chroniqueur = document.location.search.toString().substring(1);
	for(i=0;i<9;++i){
		if(chroniqueur == maliste[i]){
			document.getElementById(chroniqueur).checked = 1;
			break;
		}else{
			document.getElementById('sallenouvelle').checked = 1;
		}
	}
}


/*  ________________________________
   | Valide Form
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
function validForm(formulaire){

	
	
	// Validation des adresses de courriel
	var emailto = formulaire.adresse_electronique_destinataire;
	var emailfrom = formulaire.adresse_electronique;
	
	// Courriel du destinataire
	if(emailto.value == ""){
		alert("Vous devez entrer l'adresse électronique du destinataire");
		emailto.focus();
		return false;
	} else if((emailto.value.indexOf('.', 0) == -1) || (emailto.value.indexOf('@', 0) == -1)){
		alert("L'adresse électronique du destinataire doit être valide");
		emailto.focus();
		return false;
	}
	
	
	// Validation du nom
	if(formulaire.nom.value == ""){
		alert("Vous devez entrer votre nom");
		formulaire.nom.focus();
		return false;
	}
	
	// Courriel de l'envoyeur
	if(emailfrom.value == ""){
		alert("Vous devez entrer votre adresse électronique");
		emailfrom.focus();
		return false;
	} else if((emailfrom.value.indexOf('.', 0) == -1) || (emailfrom.value.indexOf('@', 0) == -1)){
		alert("Votre adresse électronique doit être valide");
		emailfrom.focus();
		return false;
	}
	
}

function validEmail(form_bulletin){
	if(form_bulletin.emailaddr.value == ""){
		alert("Vous devez entrer votre adresse électronique");
		form_bulletin.emailaddr.focus();
		return false;
	} else if((form_bulletin.emailaddr.value.indexOf('.', 0) == -1) || (form_bulletin.emailaddr.value.indexOf('@', 0) == -1)){
		alert("L'adresse électronique doit être valide");
		form_bulletin.emailaddr.focus();
		return false;
	}
}



/*  ________________________________
   | Feedroom
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ */
var channels = {
	nationales:		'fee9a4f5fafebbf0d3ef5cb874d34276e45c088a',
	entrevues:		'32eff8553840bace0b1e345caf98e99089982e5e',
	accommodements:	'880b92acc59ef36247ada1f68bc99764581186ea'
};

/*function feedroom_player(channel){*/
function feedroom_player(channel){	
	
	document.writeln("<EMBED src=\"http://downloads.canoe.com.edgesuite.net/feedroom/lcn/embed/Player.swf\" Flashvars= skin=embed&site=lcn&fr_chl="+channels[channel]+" quality=high menu=false WIDTH=\"193\" HEIGHT=\"250\" allowFullScreen=\"true\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\" scale=\"\"></EMBED>");
	
}


function writeIframe(){
	
	var params = getparams();
	var url;

	if (params['story']) {
		url = 'fr_story='+params['story'];
	} else {
		monchannel = (document.location.search.toString().substring(1)) ? document.location.search.toString().substring(1) : 'nationales';
		url = 'fr_chl='+channels[monchannel];
	}
	
	document.write('<iframe src="http://lcn.feedroom.com?'+url+'" height="1200" width="978" scrolling="no" frameborder="0"><'+'/iframe>');

	//monchannel = (document.location.search.toString().substring(1)) ? document.location.search.toString().substring(1) : 'nationales';
	//document.write('<iframe src="http://lcn.feedroom.com?fr_chl='+channels[monchannel]+'" height="1200" width="978" scrolling="no" frameborder="0"><'+'/iframe>');
}


function _feedroom(anchor) {
	var channel = anchor.rel.substring(9);
	var target  = anchor.id.substring(10);
	var source  = (target == 1) ? 2 : 1;
	

	document.getElementById('videos_tab'+source).className = 'tabswitcher_off';
	document.getElementById('videos_tab'+target).className = 'tabswitcher_on';
	
	document.getElementById('feedroom_video').innerHTML = "<EMBED src=\"http://downloads.canoe.com.edgesuite.net/feedroom/lcn/embed/Player.swf\" Flashvars= skin=embed&site=lcn&fr_chl="+channels[channel]+" quality=high menu=false WIDTH=\"193\" HEIGHT=\"250\" allowFullScreen=\"true\" TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\" scale=\"\"></EMBED>"+'<a href="http://lcn.canoe.ca/videos/?'+channel+'" class="allvideo">CLIQUEZ pour plus de vidéos! </a>';

	return false;
}



function getparams() {
	var elements = window.location.search.substring(1).split('&');
	var items    = {};

	for (var i in elements) {
		if (elements[i].split('=')[0] != '') {
			items[elements[i].split('=')[0]] = unescape(elements[i].split('=')[1]);
		}
	}
	
	return items;
};






/*-------------------------------------------*\
                 DYNAMIC LEADS
\*-------------------------------------------*/
var currentTab=1;		//Keeps track of the current tab for rotating.
var dlRotate;			//Stores the interval for tab switching so it can be stopped.
var dlRunning = false;	//Keeps track of whether or not the auto scroll is running.
var dlNumTabs;			//Counts the number of tabs once the DOM is ready.
var firstLoad = false;	//Modifies the Toggle function on the first page load.
var dlIsFade = false	//Checks if a fading tranistion is required.
var dlIsTabs = false	//Checks if tab switch is required.
var dlIsCount = false	//Checks if a counter needs to be displayed.
var dlFade = false;	//Keeps track of fading well it is occuring.
var dlFadeTime = 20;
if(readCookie("dlTime") != null)
	var dlTime = (readCookie("dlTime") * 1000) + (dlFadeTime * 2); //Keeps track of the flipping interval
else
	var dlTime = 7000 + (dlFadeTime * 2); //Keeps track of the flipping interval

function dlToggle() { //Starts/stops the auto switching
	if(dlRunning) {
		clearInterval(dlRotate);
		dlRunning = false;
		$(".rotateNav a.pause").removeClass("pause").addClass("play");
	} else {
		firstLoad?firstLoad=false:dlCycle("next");
		dlRotate = setInterval('dlCycle("'+'next'+'")', dlTime);
		dlRunning = true;
		$(".rotateNav a.play").removeClass("play").addClass("pause");
	}
}

function dlCycle(direction) { //Goes to next or previous tab
	if(direction == "next") {
		dlSwitch(currentTab>=dlNumTabs?1:currentTab+1);
	} else {
		dlSwitch(currentTab==1?dlNumTabs:currentTab-1);
	}
}

function dlSwitch(tabNum) { //Goes to the requested tab
	if(dlIsTabs) {
		$("#dl .tabs-nav .tabswitcher_on").removeClass("tabswitcher_on");
		$("#dl .tabs-nav a").eq(tabNum-1).addClass("tabswitcher_on");
	}
	
	if(dlIsCount) $(".rotateCount span").html(tabNum);
	
	if(dlIsFade) {
		if(!dlFade) {
			dlFade = true;
			$(".rotateItem").eq(currentTab-1).fadeOut(dlFadeTime,function(){
				$(".rotateItem").eq(tabNum-1).fadeIn(dlFadeTime,function(){dlFade = false;});
			});
			dlFade = false;		
		}
	} else {
		$(".rotateItem").eq(currentTab-1).hide();
		$(".rotateItem").eq(tabNum-1).show();
	}

currentTab=parseInt(tabNum);
		
}

var isCTRL = false;
var isFocus = false;
function arrowSwitch(Event) {
	if(Event==null) Event=event; //IE doesn't pass the event!
	if(Event.keyCode == 37 && isCTRL) { //left 37
		if(dlRunning) dlToggle();
		dlCycle("previous");
	} else if(Event.keyCode == 39 && isCTRL) { //right 39
		if(dlRunning) dlToggle();
		dlCycle("next");
	} else if(Event.keyCode == 17) {
		isCTRL = true;
	}
	
	if(Event.keyCode == 9 && !isFocus) { //outlines are turned off for firefox until the user hits tab
		$("a").addClass("withFocus");
		isFocus = true;
	}
	
}

function arrowCancel(Event) {
	if(Event==null) Event=event; //IE doesn't pass the event!
	if(Event.keyCode == 17) {
		isCTRL = false;
	}	
}

function dlTimeChange(newTime) {
	if(dlRunning) {
		dlToggle();
		$(".rotateNav a.play").removeClass("play").addClass("pause"); //toggle removes this class, but since we aren't really toggling lets put it back.
	}
	dlTime = (newTime * 1000) + (dlFadeTime * 2);
	setTimeout("dlToggle()",dlTime);
	createCookie("dlTime",newTime,1000);
}

function dlSlideCookie() { //Changes the default slider value for the DLs if a cookie is set
	if(readCookie("dlTime") != null) {
		var current = $("ul.dlTimeChange li.default");
		$("ul.dlTimeChange li").filter(function(index) {
			return parseInt($(this).html()) == parseInt(readCookie("dlTime"));
		}).addClass("default");
		if($("ul.dlTimeChange li.default").size() == 2) { //Checks to see if a new default was found, if not nothing changes.
			current.removeClass("default");
			return true;
		} else {
			return false;	
		}
	} else {
		return false;	
	}
}

/*-------------------------------------------*\
                 TAB SWITCH
\*-------------------------------------------*/
var tabFade = false;	//Keeps track of fading well it is occuring.
function tabSwitch(linkElement) { //Goes to the requested tab
	
	var tabContainer = linkElement.parents(".tabsContainer").eq(0);
	var oldTab = tabContainer.children(".active").eq(0);
	var newTab = $(linkElement.attr("href"));
	//if(!tabFade && !oldTab.is(linkElement.attr("href"))) {
	
		tabFade = true;
		switchActiveTab(linkElement);
		oldTab.fadeOut(200,function(){
			newTab.fadeIn(200,function(){
				oldTab.removeClass("active");
				newTab.addClass("active");
				tabFade = false;
			});
		});
	//}
}

function switchActiveTab(linkElement) { //Applys the class of active to whatever link element is passed and removes it from all its siblings
	var currentLI = linkElement.parent();
	currentLI.siblings().removeClass("active");
	currentLI.addClass("active");
}


/*-------------------------------------------*\
                 COOKIES
\*-------------------------------------------*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}