/* 
HTML to XHTML
(c) 2008 Donato Furlani
Javascript, regular expressions, DOM

Usage:
1) create a container div XHTML element with an id attribute, e.g. <div id='id1'></div>
2) create a new hToX object, passing 1 parameter:
	- the id of the container div 
	 e.g. var htox1 = new hToX('id1');
*/

//GLOBAL VARS
var hToX_Objects = new Object(); // like an associative array

function hToX(divId) {
	if (hToX_Objects[divId]) { //one instance already exists in the given DIV, destroy all its contents and take its place
			hToX_removeContents(hToX_Objects[divId].hToXdiv); //remove EVERY element inside the container DIV
			delete hToX_Objects[divId]; //destroy previous instance
	}
	
	hToX_Objects[divId] = this;	//put the object into the global array
	this.divId = divId;
	this.hToXdiv = document.getElementById(this.divId);
	
	
	//object methods:
	this.clearArea = funcClearArea;
	this.convertArea = funcConvertArea;	
	this.checkArea = funcCheckArea;
	
	
	//create DIV elements, and other elements for later use
	this.hToXcontainerDiv = document.createElement('div');
	this.hToXcontainerDiv.style.backgroundColor = "#aaaaaa";
	this.hToXcontainerDiv.style.padding = "1em";
        this.hToXcontainerDiv.style.textAlign = "center";
        this.hToXcontainerDiv.style.border = "1px dotted #222222";
        this.hToXdiv.appendChild(this.hToXcontainerDiv);

	
	this.tarea = document.createElement('textarea');
	this.tarea.setAttribute('cols','40');
	this.tarea.setAttribute('rows','10');
	this.tarea.setAttribute('onfocus','hToX_check(\''+this.divId+'\');');
	this.tarea.onfocus = new Function('hToX_check(\''+this.divId+'\');');
	this.tarea.style.margin = "0.5em auto";
        this.tarea.style.width = "90%";
	this.tarea.value = 'Paste here your HTML code.\n\nSupported code:\n\nYouTube, GoogleVideo, Hulu, Veoh, DailyMotion, Yahoo, Revver, Metacafe, Brightcove, MegaVideo, Libero, Deezer';
	this.hToXcontainerDiv.appendChild(this.tarea);
	
	this.buttDiv = document.createElement('div');
	this.buttDiv.style.margin = "0 auto";
	this.hToXcontainerDiv.appendChild(this.buttDiv);
	
	this.convButton = document.createElement('input');
	this.convButton.setAttribute('type','button');
	this.convButton.style.marginRight = "0.5em";
	this.convButton.setAttribute('value','Convert to XHTML');
	this.convButton.setAttribute('onclick','hToX_convert(\''+this.divId+'\');');
	this.convButton.onclick = new Function('hToX_convert(\''+this.divId+'\');');
	this.buttDiv.appendChild(this.convButton);
	
	this.clearButton = document.createElement('input');
	this.clearButton.setAttribute('type','button');
	this.clearButton.setAttribute('value','Clear');
	this.clearButton.setAttribute('onclick','hToX_clear(\''+this.divId+'\');');
	this.clearButton.onclick = new Function('hToX_clear(\''+this.divId+'\');');
	this.buttDiv.appendChild(this.clearButton);
	
}


// FUNCTIONS CALLED FROM OBJECT
function funcClearArea() {
	this.tarea.value = "";
}

function funcCheckArea() {
	if (this.tarea.value.indexOf('Supported code') > 0) this.clearArea();
}

function funcConvertArea() {
	var toConvert = this.tarea.value;
	var codeType = "none";
	if(toConvert.indexOf("deezer.com") != -1) { codeType = "deezer";}
	if(toConvert.indexOf("youtube.com") != -1) { codeType = "youtube";}
	if(toConvert.indexOf("video.google") != -1) { codeType = "google";}
	if(toConvert.indexOf("dailymotion.") != -1) { codeType = "dailymotion";}
	if(toConvert.indexOf("metacafe.com") != -1) { codeType = "metacafe";}
	if(toConvert.indexOf("veoh.com") != -1) { codeType = "veoh";}
	if(toConvert.indexOf("revver.com") != -1) { codeType = "revver";}
	if(toConvert.indexOf("brightcove.tv") != -1) { codeType = "brightcove";}
	if(toConvert.indexOf("yahoo.com/") != -1) { codeType = "yahoo";}
	if(toConvert.indexOf("hulu.com") != -1) { codeType = "hulu";}
	if(toConvert.indexOf("megavideo.com") != -1) { codeType = "megavideo";}
	if(toConvert.indexOf("video.libero") != -1) { codeType = "libero";}
	
	var xhtmlcode = "Sorry, \nHTML code could not be recognized";
	
	//IN ALL CASES, TRANSFORM & AND ADD OBJECT TYPE
toConvert = toConvert.replace(/&/g,"&amp;");


	
	switch(codeType){
	
		case 'deezer':
			xhtmlcode = rule1(toConvert);
			xhtmlcode = xhtmlcode.replace(/border="0"/g,'');
		break;

		case 'youtube':
			xhtmlcode = rule1(toConvert);
		break;

		case 'dailymotion':
			xhtmlcode = rule1(toConvert);
		break;


		case 'google':
			var valuepatt = /src="[^\s]*"/;
			var valueres = valuepatt.exec(toConvert);
			var objdata = valueres[0].replace(/src/,'data');
			var heipatt = /height:[0-9]*/;
			var widpatt = /width:[0-9]*/;
			var heires = heipatt.exec(toConvert);
			var widres = widpatt.exec(toConvert);
			var newh = heires[0].replace(/height:/,'height="');
			var neww = widres[0].replace(/width:/,'width="');
			var dimensions = newh + '" ' + neww + '" ';
			xhtmlcode = '<object type="application/x-shockwave-flash" ' + dimensions + objdata + ' >';
			var pardata = objdata.replace(/data/,'value');
			xhtmlcode += '<param name="movie" ' + pardata + ' />';
			xhtmlcode += '<param name="flashvars" value="" /> </object>';
		break;


		case 'metacafe':
			var valuepatt = /src="[^\s]*"/;
			var valueres = valuepatt.exec(toConvert);
			var objdata = valueres[0].replace(/src/,'data');
			var heipatt = /height="[0-9]*"/;
			var widpatt = /width="[0-9]*"/;
			var heires = heipatt.exec(toConvert);
			var widres = widpatt.exec(toConvert);
			xhtmlcode = '<object type="application/x-shockwave-flash" ' + heires[0] + ' ' + widres[0] + ' ' + objdata + ' >';
			var pardata = objdata.replace(/data/,'value');
			xhtmlcode += '<param name="movie" ' + pardata + ' /> ';
			xhtmlcode += '<param name="wmode" value="transparent" /> ';
			var flashpatt = /flashVars="[^\s]*"/;
			var flashres = flashpatt.exec(toConvert);
			if (flashres != null) {
			var flashvars = flashres[0].replace(/flashVars/,'value');
			xhtmlcode += '<param name="flashvars" ' + flashvars + ' />'; }
			xhtmlcode += ' </object>';
			toConvert = toConvert.replace(/<embed[\s\S]*embed>/,'');
			toConvert = toConvert.replace(/<br>/g,'<br />');
			toConvert = toConvert.replace(/font>/g,'span>');
			toConvert = toConvert.replace(/<font [^>]*>/m,'<span style="font-size: x-small;">');
			xhtmlcode += toConvert;
		break;
		
		case 'veoh':
			var valuepatt = /src="[^\s]*"/;
			var valueres = valuepatt.exec(toConvert);
			var objdata = valueres[0].replace(/src/,'data');
			var heipatt = /height="[0-9]*"/;
			var widpatt = /width="[0-9]*"/;
			var heires = heipatt.exec(toConvert);
			var widres = widpatt.exec(toConvert);
			xhtmlcode = '<object type="application/x-shockwave-flash" ' + heires[0] + ' ' + widres[0] + ' ' + objdata + ' >';
			var pardata = objdata.replace(/data/,'value');
			xhtmlcode += '<param name="movie" ' + pardata + ' /> ';
			xhtmlcode += '<param name="allowFullScreen" value="true" /> ';
			xhtmlcode += ' </object>';
			toConvert = toConvert.replace(/<embed[\s\S]*embed>/,'');
			xhtmlcode += toConvert;
		break;

		case 'revver':
			toConvert = toConvert.replace(/<embed[\s\S]*embed>/,'');
			xhtmlcode = toConvert;
		break;
		
		case 'brightcove':
			var valuepatt = /src='[^']*/;
			var valueres = valuepatt.exec(toConvert);
			var objdata = valueres[0].replace(/src='/,'data="');
			
			var flashpatt = /flashVars='[^']*/;
			var flashres = flashpatt.exec(toConvert);
			var flashvars = flashres[0].replace(/flashVars='/,'?');
			
			objdata += flashvars + '"';
			
			var heipatt = /height='[0-9]*/;
			var widpatt = /width='[0-9]*/;
			var heires = heipatt.exec(toConvert);
			var widres = widpatt.exec(toConvert);
			var newh = heires[0].replace(/height='/,'height="');
			var neww = widres[0].replace(/width='/,'width="');
			var dimensions = newh + '" ' + neww + '" ';
			xhtmlcode = '<object type="application/x-shockwave-flash" ' + dimensions + objdata + ' >';
			var pardata = objdata.replace(/data/,'value');
			xhtmlcode += '<param name="movie" ' + pardata + ' />';	
			xhtmlcode += '<param name="base" value="http://admin.brightcove.com" />';
			xhtmlcode += '<param name="name" value="bcPlayer" />';
			xhtmlcode += '<param name="allowFullScreen" value="true" />';
			xhtmlcode += '<param name="allowScriptAccess" value="always" />';
			xhtmlcode += '<param name="seamlesstabbing" value="false" />';
			xhtmlcode += '<param name="swLiveConnect" value="true" />';
			xhtmlcode += '<param name="swLiveConnect" value="true" />';
			xhtmlcode += '<param name="bgcolor" value="#FFFFFF" />';		
			xhtmlcode += '</object>';
		break;

case 'yahoo':
xhtmlcode = rule1(toConvert);
break;


		case 'hulu':
			xhtmlcode = rule1(toConvert);
		break;

		case 'megavideo':
			xhtmlcode = rule1(toConvert);
		break;

		case 'libero':
			xhtmlcode = rule1(toConvert);
		break;

		default:
			//nothing
	}
		
	
	this.tarea.value = xhtmlcode ;
	if(codeType != "none") this.tarea.select();
}	

//COMMON CODE
function rule1(htmlinput) {
var htmlcode = htmlinput;
var valuepatt = /movie" value="[^"]*"/;
var valueres = valuepatt.exec(htmlcode);
var objdata = valueres[0].replace(/movie" value/,'data');
var objrepl = 'object type="application/x-shockwave-flash" ' + objdata + ' ';
htmlcode = htmlcode.replace(/object /, objrepl);
htmlcode = htmlcode.replace(/<embed[\s\S]*embed>/,'');
var xhtmloutput = htmlcode;
return xhtmloutput;
}

	
// FUNCTIONS CALLED FROM OUTSIDE
function hToX_shutdown(whichone) {	//detroy
	if(hToX_Objects[whichone]) {
		hToX_removeContents(hToX_Objects[whichone].hToXdiv);
		delete hToX_Objects[whichone];
	}
}

function hToX_check(whichone) {
	if (hToX_Objects[whichone]) {
		hToX_Objects[whichone].checkArea();
	}
}

function hToX_clear(whichone) {
	if (hToX_Objects[whichone]) {
		hToX_Objects[whichone].clearArea();
	}
}

function hToX_convert(whichone) {
	if (hToX_Objects[whichone]) {
		hToX_Objects[whichone].convertArea();
	}
}
	
function hToX_removeContents(divx) {	//clean DIV, delete all content
	while(divx.hasChildNodes() ) { 
		divx.removeChild( divx.firstChild ); 
	}
}
                                                                        
