/*---------------------------------------------------------------------------------------------- = LEXIQUE = -----------------------------------------------------------------------------------------------------*/
/*
var lexique=new Array;
lexique['fr']=new Array;
*/
/*---------------------------------------------------------------------------------------------- = GENERIQUE = -----------------------------------------------------------------------------------------------------*/

var nb_onw8=0;
function w8(on) {
	/*if(on) {
		nb_onw8++;
		Style.show("w8");
	}else {
		nb_onw8--;
		if(nb_onw8<=0) {
			Style.hide("w8");
		}
	}*/
}

function set_lang(select_lang) {
	user_lang=select_lang;
}

function cap_lock(e){
	kc = e.keyCode?e.keyCode:e.which;
	sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
	if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk)) {
		Dom.write(lexique[user_lang]['cap_lock'],'info');
	}else {
		Dom.write("",'info');
	}
}

function E(obj) {
	if (typeof obj=='string') {
		obj=document.getElementById(obj);
	}
	return obj;
}

/*---------------------------------------------------------------------------------------------- = OBJET = -----------------------------------------------------------------------------------------------------*/

var Dom = {
	write: function(val,obj) {
		E(obj).innerHTML=val;
		if (arguments.length == 3) {
			window.setTimeout("Dom.write('','"+obj+"')",arguments[2]);
		}
	},

	get_nb_opt: function(obj) {
		return E(obj).getElementsByTagName('option').length;
	},

	get_nb_gpopt: function(obj) {
		return E(obj).getElementsByTagName('optgroup').length;
	},

	is_selected: function(obj) {
		return E(obj).selected;
	},

	is_checked: function(obj) {
		return E(obj).checked;
	},

	add: function(type,parent,txt,atts) {
		new_elemt=E(parent).appendChild(document.createElement(type));
		new_elemt.appendChild(document.createTextNode(txt));
		for(att in atts) {
			new_elemt.setAttribute(att,atts[att]);
		}
		return new_elemt;
	},

	remove: function(obj) {
		E(obj).parentNode.removeChild(E(obj));
	},

	get_val: function(obj) {
		return E(obj).value;
	}
};

var Style = {
	hide: function(obj) {
		E(obj).style.display="none";
	},

	show: function(obj) {
		E(obj).style.display="block";
	},

	bg_clr: function(obj,clr) {
		E(obj).style.background=clr;
	},

	txt_clr: function(obj,clr) {
		E(obj).style.color=clr;
	}
};

A = function(url,query) {
	this.url = url;
	this.query = query;
	this.init = false;
	this.xhr = this.getXhr();
}

A.prototype.getXhr=function() {
	if(window.XMLHttpRequest) {// FF
		this.xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject) { // IE
		try {
			this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else {
		//alert("ajax non supporté !");
		this.xhr = false;
	}
	if(this.xhr) {
		this.init=true;
		w8(true);
	}
    return this.xhr;
};

A.prototype.call=function(method) {
	if (!this.init) return false;
	var objA = this;
	try	{
		this.xhr.onreadystatechange = function() { A.getFile(objA); }
		if (method == "GET")	{
			this.url=this.url+"?"+this.query;
			this.xhr.open("GET", this.url, true);
			this.xhr.send(null);
		}else if (method == "POST") {
			this.xhr.open("POST", this.url, true);
			this.xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.xhr.send(this.query);
		}
	}catch(e) {
		this.init = false;
		this.get_a();
	}
};

A.getFile = function(objA) {
	if (objA.xhr.readyState == 4) {
		objA.txt = objA.xhr.responseText;
		objA.xml = objA.xhr.responseXML;
		objA.readyState = objA.xhr.readyState;
		objA.status	= objA.xhr.status;
		objA.get_a();
	}
}

A.prototype.to_write=function() {
	w8(false);
	if (arguments.length == 6) {
		ok_obj=arguments[0];
		ok_txt=arguments[1];
		eq=arguments[2];
		val=arguments[3];
		nok_obj=arguments[4];
		nok_txt=arguments[5];
		if( (eq && this.txt == val) || (!eq && this.txt != val) ) {
			if(ok_obj=="info") {
				Dom.write(ok_txt,ok_obj,5000);
			}else {
				Dom.write(ok_txt,ok_obj);
			}
			return true;
		}else {
			if(nok_txt!="") {
				if(nok_obj=="info") {
					Dom.write(nok_txt,nok_obj,5000);
				}else {
					Dom.write(nok_txt,nok_obj);
				}
			}
			return false;
		}
	}
}

A.prototype.get_a=function() {}

/*---------------------------------------------------------------------------------------------- = PAGINATION = -----------------------------------------------------------------------------------------------------*/

var rslt_page=40; // variable a changer aussi dans class_video.php
function goto_page(page) {
	from=rslt_page*page;
	list_videos(from);
}

var wait_defile_timer = false;
var defile_timer = false;
var down;
function defile_page(depart,sens,max) {
	count=false;
	if (arguments.length == 4) {
		count=arguments[3];
	}
	if(!count) {
		wait_defile_timer=true;
		defile_timer = false;
		down=window.setTimeout("defile_page('"+depart+"','"+sens+"','"+max+"',"+true+")","1600");
		return;
	}else {
		if(sens=="suiv") {
			depart++;
			if(depart>max) {
				depart=max;
			}
		}else {
			depart--;
			if(depart<1) {
				depart=1;
			}
		}
		wait_defile_timer=false;
		defile_timer = true;
		Style.txt_clr("current_page","#F00");
		Dom.write(depart,"current_page");
		down=window.setTimeout("defile_page('"+depart+"','"+sens+"','"+max+"',"+true+")","400");
		return;
	}
}

function stop_defile(cat_id,init,go,sens,max) {
	Style.txt_clr("current_page","#000");
	if(wait_defile_timer) {
		window.clearTimeout(down);
		if(go=="true") {
			if(sens=="suiv") {
				max--;
				page=E("current_page").innerHTML * 1;
				if(page>max) page=max;
			}else {
				page=E("current_page").innerHTML - 2;
				if(page<0) page=0;
			}
			goto_page(page);
		}
	}else if(defile_timer) {
		window.clearTimeout(down);
		if(go=="true") {
			page=E("current_page").innerHTML-1;
			goto_page(page);
		}else {
			E("current_page").innerHTML=init;
		}
	}
	wait_defile_timer=false;
	defile_timer = false;
}

/*---------------------------------------------------------------------------------------------- = COMMENTAIRES = -----------------------------------------------------------------------------------------------------*/

function form_comments() {
	if(E("form_comment").style.display!="block") {
		Dom.write("annuler","show_form_comments");
		Style.show("form_comment");
	}else {
		Dom.write("ajouter un commentaire","show_form_comments");
		Style.hide("form_comment");
	}
}

function add_comment(video_id) {
	var auteur=Dom.get_val("new_auteur");
	var texte=Dom.get_val("new_comment");
	var a=new A("./scripts/ajax_add_comment.php","video_id="+video_id+"&auteur="+auteur+"&texte="+texte);
	a.get_a=function() {
		if(a.init && a.status=="200") {
			a.to_write("cadre_comments",a.txt,false,"0","","");
		}
	}
	a.call("POST");
}

function show_comments() {
	if(E("hide_comments").style.display!="block") {
		Dom.write("masquer les commentaires","show_all_comments");
		Style.show("all_comments");
		Style.show("hide_comments");
	}else {
		Dom.write("afficher tous les commentaires","show_all_comments");
		Style.hide("all_comments");
		Style.hide("hide_comments");
	}
}

/*---------------------------------------------------------------------------------------------- = FUNCTIONS = -----------------------------------------------------------------------------------------------------*/

function list_videos(from) {
	var theme=E("select_theme").innerHTML;
	var stheme=E("select_stheme").innerHTML;
	var mc=E("select_mc").innerHTML;
	var order=E("select_order").innerHTML;
	var a=new A("./scripts/ajax_select_videos.php","theme="+theme+"&stheme="+stheme+"&mc="+mc+"&from="+from+"&order="+order);
	a.get_a=function() {
		if(a.init && a.status=="200") {
			a.to_write("cadre_videos",a.txt,false,"0","","");
		}
	}
	a.call("POST");
}

function show_video(video_id) {
	var a=new A("./scripts/ajax_show_video.php","video_id="+video_id);
	a.get_a=function() {
		if(a.init && a.status=="200") {
			a.to_write("cadre_video",a.txt,false,"0","","");
		}
	}
	a.call("POST");
}

function select_by(by,search) {
	var reset="false";
	if(arguments.length==3) {
		reset=arguments[2];
	}
	if(reset=="true") {
		Dom.write("","select_theme");
		Dom.write("","select_stheme");
		Dom.write("","select_mc");
	}
	switch(by) {
		default:
		case "all" :
			Dom.write("","select_theme");
			Dom.write("","select_stheme");
			Dom.write("","select_mc");
			select_theme("","");
			break;
		case "theme":
			Dom.write(search,"select_theme");
			break;
		case "stheme":
			Dom.write(search,"select_stheme");
			break;
		case "mc":
			search=(search==null)?Dom.get_val("mc_search"):search;
			Dom.write(search,"select_mc");
			break;
		case "order":
			Dom.write(search,"select_order");
			break;
		}
	list_videos(0);
}

function select_theme (idObj,search) {
	var reset="false";
	if(arguments.length==3) {
		reset=arguments[2];
	}
	theme_sel=E("theme_selection").childNodes;
	for(cpt=0;cpt<theme_sel.length;cpt++) {
		if(theme_sel[cpt].className == "themes txt_clic theme_selected") {
			theme_sel[cpt].className="themes txt_clic";
		}
	}
	if(idObj!="") {
		E(idObj).className="themes txt_clic theme_selected";
		select_by("theme",search,reset);
	}
}

function select_order(idObj,order) {
	order_sel=E("order_selection").childNodes;
	for(cpt=0;cpt<order_sel.length;cpt++) {
		if(order_sel[cpt].className == "order_desc order_selected") {
			order_sel[cpt].className="order_desc";
		}else if(order_sel[cpt].className == "order_asc order_selected") {
			order_sel[cpt].className="order_asc";
		}
	}
	if(idObj.className=="order_desc") {
		idObj.className="order_asc order_selected";
		order+=" ASC";
	}else {
		idObj.className="order_desc order_selected";
		order+=" DESC";
	}
	select_by("order",order);
}

function key_send_search(key) {
	if(key==13) {
		select_by("mc",null);
	}
}

function vote(item,vote) {
	var votes_values="";
	var expires=new Date();
	expires.setTime(expires.getTime()+ 24*3600*1000);
	vote++;
	cookie_value=GetCookie("vote_video_ipercast");
	votes_values=cookie_value+"#"+item+"#("+vote+")";
	document.cookie="vote_video_ipercast"+"="+votes_values+((expires==null)?"":("; expires="+expires.toGMTString()));
	// enregistrer les cookies avec une variable en fonction du site dans lequel ce script est integre
	var a=new A("./scripts/ajax_vote.php","item="+item+"&vote="+vote);
	a.get_a=function() {
		if(a.init && a.status=="200") {
			a.to_write("cadre_vote",a.txt,false,"0","","");
		}
	}
	a.call("POST");
}

function GetCookie (name) {
	if ( document.cookie) {
		index = document.cookie.indexOf( name);
		if ( index != -1) {
			nDeb = (document.cookie.indexOf( "=", index) + 1);
			nFin = document.cookie.indexOf( ";", index);
			if (nFin == -1) {
				nFin = document.cookie.length;
			}
			return unescape(document.cookie.substring(nDeb, nFin));
		}
	}
	return null;
}

function envois(video_id) {
	url=document.location;
	var popup_envois=window.open('./scripts/envois.php?video_id='+video_id+'&url='+url+'','Envoyer à un ami','toolbar=0,menubar=0,location=0,scrollbars=0,resizable=1,directories=0,width=500,height=500,top=0');
}