
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp=false;
  }
 }
@else
 xmlhttp=false
 @end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function result_click(div) {
	if(div.expanded == 1) return;
	var img = div.getElementsByTagName('img')[0];
	var timer;
	var n;

	inc = function() {
		var fw = img.width + n;
		if(fw >= 420) {
			clearInterval(timer);
			fw = 420;
			img.style.display = "none"; 
			d = div.getElementsByTagName('div')[1];
			d.style.display = "";
			sendView(img.id);
			div.expanded = 2;
		}
		img.width = fw;
		n = n * 2;
	}
	dec = function() {
		var fw = img.width - n;
		if(fw <= 130) {
			clearInterval(timer);
			fw = 130;
			div.expanded = 0;
		}
		img.width = fw;
		n = n * 2;
	}

	if(div.expanded == 2) {
		d = div.getElementsByTagName('div')[1];
		d.style.display = "none";
		d.innerHTML = d.innerHTML;
		img.style.display = ""; 
		n = 5;
		timer = setInterval("dec()",15);
		div.expanded = 1;
	}
	else if(div.expanded == 0 || div.expanded === undefined) {
		n = 5;
		d = div.getElementsByTagName('div')[1];
		d.innerHTML = d.innerHTML;
		timer = setInterval("inc()",15);
		div.expanded = 1;
	}

}

function retrieveHTMLContent(page,div) {
	var height = div.clientHeight - 190;
	div.innerHTML = "<div style='height: "+height+"px;padding-left: 299px;padding-top: 190px'>\n<img src='img/loader.gif'>\n</div>\n";
	xmlhttp.open("GET",page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			div.innerHTML = xmlhttp.responseText;
			//var allscript = div.getElementsByTagName('script');
			//for(var i=0;i< allscript.length;i++){
			//	eval(allscript[i].text);
			//}
		}
	}
 	xmlhttp.send(null);
}

function selectCat(cat,page) {
	ecrireCookie("selCat",cat);
	window.location = page;
}

function selectSort(cat,num,sort,httpName) {
	ecrireCookie("catSort",sort);
	retrieveHTMLContent('categoryResult.php?cat='+cat+'&page='+num+'&httpName='+httpName+'&sort='+sort,document.getElementById('main'));
}

var scrollY = 0;
var cookie = lireCookie('selCatY');
if(cookie !== null) scrollY = parseInt(cookie);

var scrollMenu;
var scrollTimer,scrollIncY;

function enterScrollBar(e) {
	moveScrollBar(e);

	scrollMenu = document.getElementById('scrollMenu');
	//if(menu.clientHeight + 60 > menu.parentNode.clientHeight)
	scrollTimer = setInterval("moveMenu()",20);
}

function leaveScrollBar(e) {
	clearInterval(scrollTimer);
}

function moveScrollBar(e) {
	var y = e.layerY;
	if(y === undefined)
		y = e.offsetY + 1;
	scrollIncY = (207 - y) / 15; 
}

function moveMenu() {
	scrollY += scrollIncY;
	bottom = scrollMenu.parentNode.clientHeight - scrollMenu.clientHeight;
	if(scrollY < bottom) {
		scrollY = bottom;
		//clearInterval(scrollTimer);
	}
	if(scrollY > 0) {
		scrollY = 0;
		//clearInterval(scrollTimer);
	}
	scrollMenu.style.marginTop = scrollY + "px";
}

function subCatVisibility(cat,img) {
//	if(img.parentNode.id.length == 4)
//		img.parentNode.className = 'selCat';
	div = document.getElementById("subcat"+cat);
	if(div.style.display == "none") {
		div.style.display = "";
		img.className = "minusButton";
		div = div.parentNode;
		hideHeight = 0;
		for(i = 0; i < div.childNodes.length; i++) {
			if(div.childNodes[i].id != 'subcat'+cat) {
				if(div.childNodes[i].id.indexOf('subcat',0) == 0) {
					if(div.childNodes[i].style.display != "none") {
						if(hideHeight != -1)
							hideHeight = div.childNodes[i].offsetHeight;
					}
					div.childNodes[i].style.display = "none";
					div.childNodes[i-1].childNodes[1].className = "plusButton";
					div.childNodes[i-1].className = '';
				}
			}
			else {
				if(hideHeight == 0) hideHeight = -1;
			}
		}
		if(scrollMenu === undefined)
			scrollMenu = document.getElementById('scrollMenu');
		if(hideHeight != -1) {
			scrollY += hideHeight;
			if(scrollY > 0) scrollY = 0;
			scrollMenu.style.marginTop = scrollY + "px";
		}
	}
	else {
		div.style.display = "none";
		img.className = "plusButton";
	}
}

function openCategory(cat) {
	var cat = lireCookie('selCat');
	if(cat != null) {
		var div;
		for(var i = 1; i < cat.length; i++) {
			div = document.getElementById("cat"+cat.substring(0,i));
			subCatVisibility(cat.substring(0,i),div.childNodes[1]);
		}
	}
}

// Stars
var starTimer;
var starDiv;
var starLabels = new Array('Sans intérêt','Bof','Pas mal','Bien','Excellente');
function onStarOver(div,n) {
	clearTimeout(starTimer);
	for(i = 0; i < 5; i++)
		if(i < n)
			div.childNodes[i].className = 'voteStar';
		else
			div.childNodes[i].className = 'voteDarkStar';
	text = div.parentNode.childNodes[0];
	text.innerHTML = starLabels[n-1];

}

function onStarOut(div,n) {
	starDiv = div;
	starTimer = setTimeout('resetStars()',50);
}

function resetStars() {
	for(i = 0; i < 5; i++)
		starDiv.childNodes[i].className = 'voteDarkStar';
	text = starDiv.parentNode.childNodes[0];
	text.innerHTML = 'Notez cette vidéo';
}

function sendStars(id,stars,div) {
	var page = 'vote.php?videoId='+id+'&stars='+stars;
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			div.parentNode.parentNode.innerHTML = xmlhttp.responseText;
		}
	}
 	xmlhttp.send(null);
}

// Views
function sendView(id) {
	var page = 'view.php?videoId='+id;
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
		}
	}
 	xmlhttp.send(null);
}

// Dead
function showSendDead(id) {
	div = document.getElementById('suppDead' + id);
	if(div.style.display == "none")
		div.style.display = '';
	else
		div.style.display = 'none';
}

function sendDead(id) {
	var page = 'dead.php?videoId='+id;
	var timeOut;
	var deadDiv;
		
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			deadDiv = document.getElementById('suppDead' + id);
			deadDiv.innerHTML = xmlhttp.responseText;
			deadDiv.style.display = '';
			hidesuppDead = function(id) {
				var div = document.getElementById('suppDead' + id);
				div.style.display = 'none';
			}
			timeOut = setTimeout('hidesuppDead('+id+')',3000);
		}
	}
 	xmlhttp.send(null);
}


// Send by mail
function showSendByMail(id) {
	div = document.getElementById('suppMail' + id);
	if(div.style.display == "none")
		div.style.display = '';
	else
		div.style.display = 'none';
}

function sendMail(id,div) {
	inputs = div.getElementsByTagName('input');
	var email = inputs[0].value.substr(0,128);
	var name = escape(inputs[1].value.substr(0,128));
	var nl = 'false';
	if(inputs.length == 3) nl = inputs[2].checked;

	var page = 'sendMail.php?videoId='+id+'&email='+email+'&name='+name+'&nl='+nl;
	var timeOut;
	var oldDivContent;
	
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			res = div.getElementsByTagName('div');
			oldDivContent = res[7].innerHTML;
			res[7].innerHTML = xmlhttp.responseText;
			//if(xmlhttp.responseText.indexOf('Erreur') == -1) {
				hideSuppMail = function(id) {
					var div = document.getElementById('suppMail' + id);
					div.style.display = 'none';
					res[7].innerHTML = oldDivContent;
				}
				timeOut = setTimeout('hideSuppMail('+id+')',5000);
			//}
		}
	}
 	xmlhttp.send(null);
}

// Add video or newsletter
var html = {};
function toggleElement(id) {
	adddiv = document.getElementById(id);
	if(adddiv.style.display == "none") {
		adddiv.style.display = '';
	}
	else {
		adddiv.style.display = 'none';
		if(html[id] != '' && html[id] != undefined)
			adddiv.innerHTML = html[id];
	}
}

function addInfo(div,key) {
	inputs = div.getElementsByTagName('input');
	var info = inputs[0].value;
	if(info == '') return;

	var page = 'addInfo.php?info='+info+'&key='+key;
	var timeOut;
	
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			html[div.id] = div.innerHTML;
			div.innerHTML = xmlhttp.responseText;
			timeOut = setTimeout('toggleElement(\''+div.id+'\')',5000);
		}
	}
 	xmlhttp.send(null);
}

function sendNewsLetter(div) {
	inputs = div.parentNode.parentNode.getElementsByTagName('input');
	var info = inputs[0].value;
	var freq = (inputs[1].checked == true? 'H' : 'M');
	if(inputs.length == 5 && inputs[3].checked) freq = 'D';
	if(info == '') return;
	
	var page = 'addInfo.php?info='+info+'&key=NL'+freq;
	
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			div.parentNode.parentNode.innerHTML = xmlhttp.responseText;
		}
	}
 	xmlhttp.send(null);
}

// Send Msg
function showSendMsg(div) {
	senddiv = document.getElementById('sendMsg');
	if(senddiv.style.display == "none") {
		senddiv.style.display = '';
	}
	else
		senddiv.style.display = 'none';
}

function sendMsg(div) {
	inputs = div.getElementsByTagName('input');
	var email = inputs[0].value;
	if(email == '') return;

	inputs = div.getElementsByTagName('textarea');
	var text = escape(inputs[0].value);
	if(text == '') return;

	var page = 'sendMsg.php?email='+email+'&text='+text;
	var timeOut;
	var html;
	
	xmlhttp.open('GET',page,true);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			html = div.innerHTML;
			div.innerHTML = xmlhttp.responseText;
			hideSendMsg = function(id) {
				var div = document.getElementById('sendMsg');
				div.style.display = 'none';
				div.innerHTML = html;
			}
			timeOut = setTimeout('hideSendMsg()',5000);
		}
	}
 	xmlhttp.send(null);
}

// Cookies
function ecrireCookie(nom, valeur)
{
	var argv=ecrireCookie.arguments;
	var argc=ecrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+((expires==null) ? "" : ("; expires="+expires.toGMTString()))+((path==null) ? "" : ("; path="+path))+((domain==null) ? "" : ("; domain="+domain))+((secure==true) ? "; secure" : "");
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function lireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

// Myspace
function mySpace(T,C,U,L)
{
	var targetUrl = 'http://www.myspace.com/Modules/PostTo/Pages/?' + 't=' + encodeURIComponent(T) + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
	window.open(targetUrl);
}
