//Written By sébastien Boyart for Novacom Hitec - 2009

WebTV = function(channel,maindiv,dart,dliste){
	this.CHANNEL=channel;
	this.CATEGORIES=null;
	this.CURMODE="";
	//this.URLBASE="http://192.168.0.248/BRanko/webtv-prodrenault";
	this.URLBASE="http://www.greenext.eu/webtv";
	this.URLDBRQ=this.URLBASE+"/scripts/DbRequest.php";
	this.DIVARTICLE=document.getElementById(dart);
	this.DIVLISTE=document.getElementById(dliste);
	this.MAINDIV=document.getElementById(maindiv);
	this.LOADER=document.createElement("IMG");
	this.LOADER.src="loader.gif";
	this.LOADER.className="loader";
	this.getListe();
	}

WebTV.prototype.getListe=function(){
	var xhr=this.createReqObj();
	xhr.onreadystatechange = function(){ 
	 var xmlDoc=null;
     if(xhr.readyState == 4){
        if(xhr.status == 200){ 
          	try{ 
			    var parser=new DOMParser();
			    xmlDoc=parser.parseFromString(xhr.responseText,"text/xml");
				wtv.writeListe(xmlDoc);
			}
			catch(e) {
				alert(e.message);
			}
			
        } 
       else { 
			alert("Error: returned status code " + xhr.status + " " + xhr.statusText);
	   } 
	   //xhr.wtv.hideLoader();
    }
	}
	var url=this.URLDBRQ+"?rand="+new Date().getTime();
	url+="&qmode=init";
	url+="&channel="+this.CHANNEL;
	url+="&sortby=VIDEOS.V_ORDER&sortmode=desc";
	xhr.open("GET", url, true); 
	xhr.send(null); 	
}
WebTV.prototype.parseCategories=function(categs){
	this.CATEGORIES=new Array();
	for(var i=0;i<categs.length;i++){
		this.CATEGORIES[categs[i].getAttribute("id")]=categs[i];
	}
}
WebTV.prototype.writeListe=function(xmldoc){
	/*this.DIVLISTE.innerHTML="&nbsp;";*/
	this.DIVLISTE.innerHTML=" ";
	var flvs=this.xGetElementsByTagName("flv",xmldoc);
	this.parseCategories(this.xGetElementsByTagName("c",xmldoc));
	for(i=0;i<flvs.length;i++){
		vig=document.createElement("DIV");
		vig.wtv=this;
		vig.uuid=this.getNodeValue("uuid",flvs[i]);
		vig.onclick=function(){
			this.wtv.getArticle(this.uuid);
		}
		vig.id="vig"+i;
		vig.className="vignette";
		
		
		l1=document.createElement("DIV");
		l1.className="vline";
		
		d1=document.createElement("DIV");
		im=document.createElement("IMG");
		im.src=this.URLBASE+this.getNodeValue("thumb",flvs[i]);
		im.className="vig_thumb";
		d1.className="dvig_thumb";
		d1.appendChild(im);
		
		d2=document.createElement("DIV");
		tt=document.createTextNode(this.getNodeValue("titre",flvs[i]));
		d2.className="vig_title bleu";
		d2.appendChild(tt);
		
		l1.appendChild(d1);
		l1.appendChild(d2);
		
		l2=document.createElement("DIV");
		l2.className="vline";
		
		d3=document.createElement("DIV");
		imcateg=document.createElement("IMG");
		imcateg.src=this.URLBASE+this.CATEGORIES[this.getNodeValue("categorie",flvs[i])].getAttribute("thumb");
		imcateg.className="vig_imgcateg";
		d3.className="dvig_imgcateg";
		d3.appendChild(imcateg);
		
		d4=document.createElement("DIV");
		dd=document.createTextNode(this.getNodeValue("description",flvs[i]));
		d4.className="vig_desc";
		d4.appendChild(dd);
		
		l2.appendChild(d3);
		l2.appendChild(d4);

		vig.appendChild(l1);
		vig.appendChild(l2);
		this.DIVLISTE.appendChild(vig);
		if(i==0) this.getArticle(vig.uuid);
	}
}
WebTV.prototype.writeArticle=function(xmldoc){
	this.DIVARTICLE.innerHTML="";
	var flvs=this.xGetElementsByTagName("flv",xmldoc);
	for(i=0;i<flvs.length;i++){
	switch(this.getNodeValue("type",flvs[i])){
		case "image":
			this.DIVARTICLE.innerHTML+="<div class='ar_title'>"+this.getNodeValue("titre",flvs[i])+"</div><br>";
			this.DIVARTICLE.innerHTML+="<img class='imarticle' src=\""+this.URLBASE+flvs[i].getAttribute("name")+"\"><br>";
			this.DIVARTICLE.innerHTML+=this.getNodeValue("description",flvs[i]);
			break;
		case "vipi":
			var msize=this.getNodeValue("mediasize",flvs[i]).split("x");
			var d=document.createElement("DIV");
			d.name="flashcontent";
			d.id="flashcontent";
			d.style.width=msize[0]+"px";
			d.style.height=msize[1]+"px";
			this.DIVARTICLE.appendChild(d);
			var params = {
  					menu: "false",
  					allowFullScreen: "true",
     				scale: "noScale",
					salign: "tl",
					allowScriptAcces: "always",
					wmode: "opaque"
                  };
			swfobject.embedSWF(flvs[i].getAttribute("name"), "flashcontent", msize[0], msize[1], "9.0.115","expressInstall.swf",null, params);
			break;
		case "anim":
			var msize=this.getNodeValue("mediasize",flvs[i]).split("x");
			var d=document.createElement("DIV");
			d.name="flashcontent";
			d.id="flashcontent";
			d.style.width="498px";
			d.style.height="330px";
			this.DIVARTICLE.appendChild(d);
			var params = {
  					menu: "false",
  					allowFullScreen: "true",
     				scale: "showAll",
					salign: "tl",
					allowScriptAcces: "always",
					wmode: "opaque"
                  };
			swfobject.embedSWF(this.URLBASE+flvs[i].getAttribute("name"), "flashcontent", 498, 330, "9.0.115","expressInstall.swf",null, params);
			break;
		case "video":
			//alert(this.getNodeValue("uuid",flvs[i]));
			//if(this.CURMODE!="video"){
				var msize=this.getNodeValue("mediasize",flvs[i]).split("x");
				var dt=document.createElement("DIV");
				dt.className="ar_title";
				var tt=document.createTextNode(this.getNodeValue("titre",flvs[i]));
				dt.appendChild(tt);
				this.DIVARTICLE.appendChild(dt);
				
				var d=document.createElement("DIV");
				d.name="flashcontent";
				d.id="flashcontent";
				d.style.width="498px";
				d.style.height="330px";
				this.DIVARTICLE.appendChild(d);
				
				var ddesc=document.createElement("DIV");
				var tdesc=document.createTextNode(this.getNodeValue("description",flvs[i]));
				ddesc.appendChild(tdesc);
				ddesc.className="ar_desc";
				this.DIVARTICLE.appendChild(ddesc);
				
				this.CURMODE="video";
				var flashvars = {
						DBURL: this.URLDBRQ,
						kelChannel: this.CHANNEL,
						uuid: this.getNodeValue("uuid",flvs[i]),
						myWidth: 498,
						myHeight: 330,
						mySkin: "swf/skin_dubra.swf",
						enableSimilars: "none",    //onlyFullScreen //both //none
						nextVid: "2",
						precVid: "3"
						};
						
				var params = {
						menu: "false",
						allowFullScreen: "true",
						scale: "noScale",
						salign: "tl",
						allowScriptAcces: "always",
						wmode: "opaque"
					  };

				swfobject.embedSWF("swf/dubra.swf", "flashcontent", "498", "330", "9.0.115","expressInstall.swf",flashvars, params);
			//}
			//else{
				//document.getElementById("flashcontent").JS_play_video(this.getNodeValue("uuid",flvs[i]));
			//}
			break;
		default:
			break;
		}
	}
	
}
WebTV.prototype.showLoader=function(uuid){
	this.DIVARTICLE.innerHTML="&nbsp;";
	this.DIVARTICLE.appendChild(this.LOADER);
	this.MAINDIV.scrollTop=0;
}
WebTV.prototype.getArticle=function(uuid){
	var xhr=this.createReqObj();
	//xhr.wtv=this;
	xhr.onreadystatechange = function(){ 
	 var xmlDoc=null;
     if(xhr.readyState == 4){
        if(xhr.status == 200){ 
          	try{ 
			    var parser=new DOMParser();
				//alert(xhr.responseText);
			    xmlDoc=parser.parseFromString(xhr.responseText,"text/xml");
				wtv.writeArticle(xmlDoc);
			}
			catch(e) {
				alert(e.message);
			}
			
        } 
       else { 
			alert("Error: returned status code " + xhr.status + " " + xhr.statusText);
	   } 
	   //xhr.wtv.hideLoader();
    }
	}
	var url=this.URLDBRQ+"?rand="+new Date().getTime();
	url+="&qmode=vid";
	url+="&channel="+this.CHANNEL;
	url+="&qstr="+uuid;
	url+="&format=html";
	xhr.open("GET", url, true); 
	this.showLoader();
	xhr.send(null); 
}

WebTV.prototype.createReqObj=function(){
	var xhr=null;
	try{
     xhr = new XMLHttpRequest(); 
   } catch(e){ 
     try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } 
     catch (e2)
    { 
       try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } 
       catch (e) {
		alert("IMPOSSIBLE DE CREER LE XMLHttpRequest");
		return;
	   }
    }
  }
  return xhr;
}

WebTV.prototype.getNodeValue=function(t,p){
	if(this.xGetElementsByTagName(t,p)[0]==undefined && t.indexOf(":")==-1){
		return null;
	}
	if(document.all){
		if(this.xGetElementsByTagName(t,p)[0].childNodes.length>0)
				return this.xGetElementsByTagName(t,p)[0].childNodes[0].nodeValue;
			else 
				return null;
		}
	else{
		if(t.indexOf(":")!=-1){
			var splitted=t.split(":");
			if(this.xGetElementsByTagName(splitted[1],p)[0].childNodes.length>0)
				return this.xGetElementsByTagName(splitted[1],p)[0].childNodes[0].nodeValue;
			else
				return null;
		}
		else{
			if(this.xGetElementsByTagName(t,p)[0].childNodes.length>0)
				return this.xGetElementsByTagName(t,p)[0].childNodes[0].nodeValue;
			else 
				return null;
		}
	}
}

// xGetElementsByTagName r5, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

WebTV.prototype.xGetElementsByTagName=function(t,p)
{
  var list = null;
  t = t || '*';
  p = this.xGetElementById(p) || document;
  if (typeof p.getElementsByTagName != 'undefined') { // DOM1
    list = p.getElementsByTagName(t);
    if (t=='*' && (!list || !list.length)) list = p.all; // IE5 '*' bug
  }
  else { // IE4 object model
    if (t=='*') list = p.all;
    else if (p.all && p.all.tags) list = p.all.tags(t);
  }
  return list || [];
}
// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

WebTV.prototype.xGetElementById=function(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

if (typeof DOMParser == "undefined") {
	   DOMParser = function () {}
	   DOMParser.prototype.parseFromString = function (str, contentType) {
		  if (typeof ActiveXObject != "undefined") {
			 var d = new ActiveXObject("MSXML2.DomDocument");
			 d.loadXML(str);
			 return d;
		  } else if (typeof XMLHttpRequest != "undefined") {
			 var req = new XMLHttpRequest;
			 req.open("GET", "data:" + (contentType || "application/xml")+";charset=utf-8," + encodeURIComponent(str), false);
			 if (req.overrideMimeType) {
				req.overrideMimeType(contentType);
			 }
			 req.send(null);
			 return req.responseXML;
		  }
	   }
	}


