// Extended RTE 1.2 for SharePoint
// Created By Boris Gomiunik 
// Please visit my Blog to find more tricks for SharePoint: http://boris.gomiunik.net
// More info about ERTE on http://boris.gomiunik.net/erte
// Project posted on CodePlex -- http://www.codeplex.com/erte

///Settings:
//imagePath - enter the RTE Toolbar button icon if location is different than the one below
var imagePath="/_catalogs/masterpage/erte.gif"

//Localization arrays. To localize, copy the line below and change the LCID.
var erte1033 = new Array("Please input details","Alt Text","This will be displayed in RSS feeds","Embed Code","Paste your Embed code here","Insert","Close"); //english
var erte1060 = new Array("Vnesite parametre","Besedilo","Besedilo bo izpisano v RSS-viru","Embed koda","Prilepite Embed kodo v polje","Vstavi","Zapri"); //english
var erte1036 = new Array("Parametres de configuration","Texte de remplacement","Ce texte sera affiche dans le flux RSS a la place de l'objet insere.","Code Embed","Coller le code HTML de l'object ici.","Inserer","Fermer"); //French - thanks to Slash71

/*-------------No need to edit below this line------------------*/

//set the language. If there is no localization array available, use english as default
/*if (window['erte'+L_Menu_LCID]) {
	erte_lang = window['erte'+L_Menu_LCID];
} 
else {
	erte_lang = window['erte1033']  
}
*/
erte_lang = window['erte1033']  ;
videosEmbedded = false;
function getSize(embedString){
}

//this is the function that generates the "Insert Flash" dialog.
function embedFlash(gb_whichElement) {
  while (gb_whichElement.id.indexOf('_toolbar') == -1) {
  	gb_whichElement = gb_whichElement.parentNode;
  }
  fieldID = gb_whichElement.id;
  fieldID = fieldID.substring(0,fieldID.indexOf('_toolbar'));
  RTE_SaveSelection(fieldID);
  var generator=window.open('','question','height=300,width=300,scrollbar=no,menu=no,toolbar=no,status=no,location=no');
  generator.document.write('<html><head><title>'+erte_lang[0]+'</title>' + 
  '<script type="text/javascript">function insertFlash() { \n' + 
  'var embedCode = document.getElementById("embed").innerText; \n' + 
  'var finalString = "<div class=erte_embed " + getSize(embedCode) + " style=background:white style=display:block; id=" \n' + 
  'if(document.getElementById("dostopplayer").checked) {\n' +
  'finalString += escape(dostopplayer(embedCode)); \n'+
  '} else {\n' + 
  'finalString += escape(embedCode) }\n' + 
  'finalString += ">" \n' + 
  'finalString += document.getElementById("alt").value\n' + 
  'finalString += "</div>"\n' +
  'window.opener.document.getElementById("' + fieldID + '_iframe").focus();' + 
  'window.opener.RTE_GetSelection(document.getElementById("field").value).pasteHTML(finalString)\n' + 
  'window.close()\n;' +
  '}'+
  'function getSize(embedString) {\n' + 
  'var parts = embedString.split("||");\n' + 
  'if(document.getElementById("dostopplayer").checked==false) return " style=width:620px;"; \n' + 
  'return "style=height:390px style=width:620;"\n ' + 
  '}\n' + 
'function dostopplayer(embedCode){\n'+
'	return \'<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="620" height="390">\'+\n'+
'		  \'<param name="source" value="/smooth/Dostop.SmoothPlayer.xap"/>\'+\n'+
'		  \'<param name="onError" value="onSilverlightError" />\'+\n'+
'		  \'<param name="background" value="white" />\'+\n'+
'		  \'<param name="minRuntimeVersion" value="3.0.40818.0" />\'+\n'+
'		  \'<param name="autoUpgrade" value="true" />\'+\n'+
'		  \'<param name="initParams" value=\\\'source=\'+embedCode+\'\\\' />\'+\n'+
'		  \'<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">\'+\n'+
' 		  \'  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>\'+\n'+
'		  \'</a>\'+\n'+
'	      \'</object>\';\n'+
'}\n'  +
  "<\/script>\n" +
  '</head><body style="margin:10px; font-family: verdana; font-size: 10px;">\n' + 
  '<strong>'+erte_lang[1]+':</strong> <input id="alt" type="text" style="border: 1px black solid; width: 200px; font-family: verdana; font-size: 10px;"/><br/><span style="color: gray">('+erte_lang[2]+')</span>\n' + 
  '<br/> <br/><strong>'+erte_lang[3]+':</strong><br/>\n' + 
  '<textarea id="embed" style="width: 250px; height: 100px; border: 1px black solid; font-family: verdana; font-size: 10px;"></textarea>\n' + 
  '<br/><span style="color: gray">('+erte_lang[4]+')</span><br/>\n' +
  '<input type="hidden" id="field" value="'+fieldID+'"\n' + 
  '<br/> <br/><button style="font-family: verdana; font-size: 10px;" onclick="insertFlash()">'+erte_lang[5]+'</button>\n' +
  '&nbsp;&nbsp;<button style="font-family: verdana; font-size: 10px;" onclick="window.close()">'+erte_lang[6]+'</button>\n' + 
  '&nbsp;&nbsp;<input type="checkbox" id="dostopplayer" />\n'+
  '</body></html>\n');
  generator.document.close();
}

function embedVideos() 
{
	if(videosEmbedded){
		return;
	} else {
		videosEmbedded = true;
	}
/* --- This part here adds the button --- */
	objekty = document.getElementsByTagName('table');
	for (i=0;i<objekty.length;i++) {
		if (objekty[i].className == 'ms-rtetoolbarmenu ms-long' && objekty[i].childNodes[0].childNodes.length == 2) {
			var newCell = objekty[i].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].insertCell();
			newCell.setAttribute('unselectable', 'on');
			newCell.setAttribute('class', 'ms-rtetoolbarmenu');
			newCell.innerHTML = '<a href="javascript:" unselectable="on" onclick="embedFlash(this); return false"><img unselectable="on" border="0" src="' + imagePath + '"/></a>'
		}
	}
	setTimeout(renderDisplay(), 200);
	//alert(document.getElementById("vir").innerHTML);
}

function renderDisplay(){
/* --- This part will render the display --- */
	var new_embed = false;
	objekty2 = document.getElementsByTagName('div');
	for (i=0;i<objekty2.length;i++) {
		if (objekty2[i].className == 'erte_embed') {
			var embed_code = unescape(objekty2[i].id);
			var split_code = embed_code.split("||");
			
			if(split_code[0]=="dostop.si"){
			
				objekty2[i].innerHTML =  split_code[1] + prikaziVideoplayer();
			} else {
				objekty2[i].innerHTML = embed_code;
			}
			new_embed = true;
			//alert(objekty2[i].innerHTML);
		}
	}
	if(!new_embed){
		objekty2 = document.getElementsByTagName('span');
		for (i=0;i<objekty2.length;i++) {
			if (objekty2[i].className == 'erte_embed') {
				var embed_code = unescape(objekty2[i].id);
				objekty2[i].innerHTML = embed_code;
			}
		}
	}
	

}

function dostopplayer(embedCode){
	return '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="570" height="390">'+
		  '<param name="source" value="/smooth/Dostop.SmoothPlayer.xap"/>'+
		  '<param name="onError" value="onSilverlightError" />'+
		  '<param name="background" value="white" />'+
		  '<param name="minRuntimeVersion" value="3.0.40818.0" />'+
		  '<param name="autoUpgrade" value="true" />'+
		  '<param name="initParams" value=\''+embedCode+'\' />'+
		  '<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">'+
 		  '  <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>'+
		  '</a>'+
	      '</object>';
}

function embedDostopVideoPlayer(embed_code){
	var scriptContent = '<object type="text/xml" id="vir"><?xml version="1.0" encoding="utf-8" ?> ' +
		'<data startalbumindex="0" transition="CrossFadeTransition"> ' +
		 '<album title="Novice" description="" source="http://www.dostop.si/galerije_uredniki/Sourock08/21.jpg" ><slide title="3DPrasci" description="www.rockheads.si" source="http://storage.dostop.si/video/dostop_si/3dprasci.wmv" thumbnail="http://www.dostop.si/galerije_uredniki/Sourock08/6.jpg" link="http://www.youtube.com/watch?v=CaP2KglyBHs"></slide></album> ' +
		'</data>' + 
		'</object>';
	return scriptContent;
	
}
function prikaziVideoplayer(container){

  return '<object type="application/x-silverlight-2" data="data:application/x-silverlight-2," width="540" height="360"><param name="background" value="#000" /><param name="source" value="Vertigo.SlideShow.xap" /><param name="initParams" value="ConfigurationProvider=DarkTheme,DataProvider=XmlDataProvider;Path=Data.xml" /><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /></a>	</object>';

}
function prikaziVideoplayer(container){

  return '<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="570" height="390">'+
		  '<param name="source" value="/smooth/Dostop.SmoothPlayer.xap"/>'+
		  '<param name="onError" value="onSilverlightError" />'+
		  '<param name="background" value="white" />'+
		  '<param name="minRuntimeVersion" value="3.0.40818.0" />'+
		  '<param name="autoUpgrade" value="true" />'+
		  '<param name="initParams" value="commentID=source=http://storage.dostop.si/video/smooth/kam zvečer po nakupe/kam zvečer po nakupe.ism/Manifest, relatedTitle1=Informativni dnevi - Kam naprej?, relatedLink1=http://www.dostop.si/novica.aspx?ID=678, relatedImage1=http://www.dostop.si/Slike/informativni_010WEB.jpg, relatedTitle2=Avantura na motornih saneh, relatedLink2=http://www.dostop.si/novica.aspx?ID=675, relatedImage2=http://www.dostop.si/Slike/motorne_sani_pohorjeWEB.jpg" />'+
		  '<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">'+
 		  '<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>'+
		  '</a>'+
	    '</object>';

}

if (window['_spBodyOnLoadFunctionNames']) {
	_spBodyOnLoadFunctionNames.push("embedVideos");
}
else { embedVideos() }

