var v_fixDblKey = 0;
function fixDblKey() {
if (v_fixDblKey != 0) {
return true;
} else {
v_fixDblKey = setTimeout('v_fixDblKey = 0;', 10);
return false;
}} 

var curBG, boxBG, boxL, curL, next, blinked, blinker, window_loaded, interface_disabled;
var blink=new Array();
window.onload = function() {
if(window_loaded==true) return;
window_loaded=true;
boxL = document.getElementsByClassName("boxL");
if(boxL.length>0){
boxL.each(function(node){
node.onfocus = function() { boxBG_over(this.previousSibling); curL=this; }
node.onmouseover = function() { if(interface_disabled !=true){ this.focus(); }}
});
boxBG = document.getElementsByClassName("boxBG");
boxBG.each(function(node){
node.onmouseover = function() { if(interface_disabled !=true){ this.nextSibling.focus(); }}
if(node.hasClassName('blink')) blink.push(node); 
});
if(boxL[7]) boxL[7].focus();
if(blink.length>0){
blinker_start();
blinker = new PeriodicalExecuter(blinker_start, 1);
}
Event.observe(document, 'keydown', function(event){
if(fixDblKey() || (interface_disabled==true)) return;
if(event.keyCode == 38){ //up
next=boxL.indexOf(curL)-5; 
if(next==0-5) next=14; 
else if(next<0) next=next+14; 
if(boxL[next]) boxL[next].focus(); 
} else if(event.keyCode == 40){ //down
next=boxL.indexOf(curL)+5; 
if(next==14+5) next=0; 
else if(next>14) next=next-14; 
if(boxL[next]) boxL[next].focus(); 
} else if(event.keyCode == 37){ //left
next=boxL.indexOf(curL)-1; 
if(next<0) next=14; 
if(boxL[next]) boxL[next].focus(); 
} else if(event.keyCode == 39){ //right
next=boxL.indexOf(curL)+1; 
if(next>14) next=0; 
if(boxL[next]) boxL[next].focus(); 
}});
}}

function blinker_start(){
if(blinked !=true){
blink.each(function(node){
node.style.background='transparent';
})
blinked=true;
} else {
blink.each(function(node){
node.style.background='#000';
})
blinked=false;
}}
function blinker_stop(){
if(blink.length>0){ if(blinker) blinker.stop();
blink.each(function(node){
node.style.background='#000';
})
blink=new Array();
}}
function boxBG_over(el){
if(curBG !=el){
//blinker_stop();
if(curBG) curBG.style.background='#000';
if(el) el.style.background='transparent';
curBG=el;
}}

function player(file,CDN){
interface_disabled=true;
Element.show('splash');
$('splash').innerHTML='<a href="#" onclick="return hide_splash()" style="float:right">fermer [x]</a><div id="lab_'+file+'" style="width:360px;height:270px"><object height="100%" width="100%"><param name="movie" value="/player/flash/?skin=mini&file='+file+'&amp;CDN='+CDN+'&amp;autostart=1"><param name="allowFullScreen" value="true"/><embed src="/player/flash/?skin=mini&file='+file+'&amp;CDN='+CDN+'&amp;autostart=1" type="application/x-shockwave-flash" width="100%" height="100%" allowFullScreen="true"></embed></object></div><div style="padding:50px"><h1><a href="#" onclick="return dailyVideo()">Cliquez-ici pour regarder la vidéo du jour...</a></h1></div>';
$('splash').firstChild.focus();
return false;
}
function hide_splash(){
$('splash').innerHTML='';
Element.hide('splash');
interface_disabled=false;
return false;
}
function dailyVideo(){
var dailyVid=$('cat_10');
if(dailyVid) window.location=dailyVid.href;
return false;
}

function labPostRoll(vid){
$('lab_'+vid).style.visibility='hidden';
//Element.show('labPostRoll');
}
function postRoll_hide(vid){
$('lab_'+vid).style.visibility='visible';
return false;
}

function overlay(a){
goInto(a,'overlay2');
if($('chat')) $('chat').style.visibility="hidden";
Element.show('overlay1');
return false;
}
function overlay_close(){
Element.hide('overlay1');
if($('chat')) $('chat').style.visibility="";
return false;
}
function require_login(){
overlay('/signup');
return false;
}
function require_login_flash(){ //used by Flash chat
require_login();
}

function goInto(a,div){
if(isString(a)) l=a;
else l=a.href;
new Ajax.Updater(div, l, {parameters:'mode=ajax', evalScripts:true, method: 'get', asynchronous:true});
return false;
}

function rate(el){
rdiv=$('rate');
new Ajax.Updater(rdiv, '/ajax/', {parameters:'Video_ID='+rdiv.getAttribute('name')+'&rate='+el.id, evalScripts:true, asynchronous:true});
return false;
}

function collapseThread( theId ) {
var comment = $(theId);
var theBody = findBody(comment);
if(comment.className.indexOf("collapsed") > -1) {
comment.className = comment.className.replace(" collapsed", "");;
} else {
comment.className += " collapsed";
}
}

function expandThread( theId ) {
var comment = $(theId);
var theBody = findBody(comment);
if(comment.className.indexOf("collapsed") > -1) {
comment.className = comment.className.replace(" collapsed", "");;
} 
}

function findBody(el){
var divs = el.getElementsByTagName("div");
var ret;
for(var i = 0; i < divs.length; ++i) {
if(divs.item(i).className.indexOf("c_body") > -1)
return divs.item(i);
}
return false;
}

function onAddComment(form) {
hideReply();
new Ajax.Updater($('addcomment'), '/ajax/', {onLoading:function(request){$('addcomment').innerHTML='Enregistrement en cours...';}, parameters:'do=comment&'+Form.serialize(form), evalScripts:true, asynchronous:true});
return false;
}

function hideReply() {
var replies = document.getElementsByClassName("reply");
for (i = 0; i < replies.length; i++){
Element.hide(replies[i]);
}}

function moveAddCommentBelow(theId, threadId, collapse){
expandThread( theId );
var addComment = $("addcomment");
var comment = $(theId);
if(collapse)
{
for(var i = 0; i < comment.childNodes.length; ++i) {
var c = comment.childNodes.item(i);
if(typeof(c.className) == "string" && c.className.indexOf("collapsed")<0) {
c.className += " collapsed";
}
}
}
addComment.parentNode.removeChild(addComment);
comment.appendChild(addComment);
if(comment.className.indexOf("alt")>-1) {
addComment.className = addComment.className.replace(" alt", ""); 
} else {
addComment.className += " alt";
}
$("Reply_ID").value = threadId;
$("reroot").style.display = "block";
//var aTags = comment.getElementsByTagName("A");
//var anc = aTags.item(0).id;
//document.location.href = "#"+anc;
$("Comment").focus();
return false; 
}

function reRoot(){
var addComment = $("addcomment"); 
var reRootElement = $("reroot");
reRootElement.style.display = "none";
var content = $("content_more");
addComment.parentNode.removeChild(addComment);
content.appendChild(addComment);
addComment.className = addComment.className.replace(" alt", "");
$("Reply_ID").value = "0";
return false; 
}

function vid_info(title,desc,date,views,comments){
$('vid_title').innerHTML=title;
$('vid_desc').innerHTML=desc;
$('vid_date').innerHTML=date;
$('vid_views').innerHTML=views;
$('vid_comments').innerHTML=comments;
}

function reset_i(el,s){
if(el.value==s){
el.value="";
} }

function switch_vids(el) {
var sel = el.selectedIndex;
goInto('/?param='+el.options[sel].value, $('vid_list'))
}

function switch_page_vids(page,el) {
var sel = el.selectedIndex;
top.window.location.href = page+el.options[sel].value;
}

function tab(current){
var allT = document.getElementsByClassName("tab_active");
allT.each(function(node){
if(node !=current) node.className='tab';
});
current.className='tab_active'; 
return goInto(current,$('tab_contents').firstChild);
}

function isString(v) { return typeof v == 'string'; }

function labInfoFlash(el){
new Effect.Highlight(el, {startcolor:'#ffd929', endcolor:'#000000'});
}

function validateSignup(theform) {
for(i = 0; i < theform.elements.length; i++){
if(theform.elements[i].name=='Email') {
chk_email=checkEmail(theform.elements[i].value);
if(chk_email !='') {
alert(chk_email);
return false; 
}} else if(theform.elements[i].name=='password1'){
if(theform.elements[i].value.length<6){
alert("Votre mot de passe doit faire au moins 6 caracteres...");
return false; 
}
password1=theform.elements[i].value;
} else if(theform.elements[i].name=='password2'){
if(password1 !=theform.elements[i].value){
alert("Vous devez saisir deux fois le meme mot de passe...");
return false; 
}}
if(theform.elements[i].value==undefined || theform.elements[i].value =="") {
alert("Merci de bien vouloir renseigner tous les champs...");
return false; 
}}
new Ajax.Updater(theform, theform.action+'?mode=ajax', {asynchronous:true, parameters:Form.serialize(theform)}); 
return false;
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "Merci de saisir votre adresse email.\n";
}
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Merci de saisir une adresse email valide.\n";
    }
    else {
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
       error = "Merci de saisir une adresse email valide.\n";
       }
    }
return error;    
}

function buzz_video(url,title,text,tags){ 
$('social').innerHTML='<a href="#" onclick="Element.hide(\'social\');return false" style="float:right">[x]</a>Vous pouvez faire buzzer cette vidéo en l\'envoyant à ces sites : <p><a href="http://del.icio.us/post?url='+url+'&title='+title+'&notes='+text+'&tags='+tags+'" target="_blank"><img src="/img/icons/delicious.gif">del.icio.us</a> <a href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+url+'&t='+title+'&d='+text+'&tag='+tags+'" target="_blank"><img src="/img/icons/yahoo.gif">Yahoo</a> <a href="http://www.stumbleupon.com/submit?language=FR&url='+url+'&title='+title+'&newcomment='+text+'&tagnames='+tags+'" target="_blank"><img src="/img/icons/stumbleupon.png">StumbleUpon</a> <a href="http://www.scoopeo.com/scoop/new?newurl='+url+'&title='+title+'" target="_blank"><img src="/img/icons/scoopeo.png">Scoopeo</a> <a href="http://www.pioche.fr/submit_news.php?url='+url+'&title='+title+'" target="_blank"><img src="/img/icons/pioche.gif">Pioche</a> <a href="http://blogmarks.net/my/new.php?mini=1&simple=1&url='+url+'&title='+title+'&content='+text+'" target="_blank"><img src="/img/icons/blogmarks.png">Blogmarks</a> <a href="http://ma.gnolia.com/beta/bookmarklet/add?url='+url+'&title='+title+'&description='+text+'&tags='+tags+'" target="_blank"><img src="/img/icons/magnolia.gif">Magnolia</a> <a href="http://furl.net/storeIt.jsp?t='+title+'&u='+url+'" target="_blank"><img src="/img/icons/furl.gif">Furl</a> <a href="http://digg.com/submit?phase=2&title='+title+'&bodytext='+text+'&url='+url+'" target="_blank"><img src="/img/icons/digg.gif">Digg</a> <a href="http://reddit.com/submit?url='+url+'&title='+title+'" target="_blank"><img src="/img/icons/reddit.gif">Reddit</a> <a href="http://www.newsvine.com/_tools/seed&save?u='+url+'" target="_blank"><img src="/img/icons/newsvine.gif">Newsvine</a>';
Element.toggle('social');
return false;
}