// globals
var selectedOptions="";
var maxMedFilms=8;
var maxOtherFilms=4;
var codeSelected=false;
var counterMedFilms=0;
var counterOtherFilms=0;
var termsAccepted=false;
var xaxis=0;
var yaxis=0;
var windowStr="width=660,height=300,statusbar=false,menubar=false,titlebar=false,resizeable=false";
var windowStrNetscape="width=670,height=310,statusbar=false,menubar=false,titlebar=false,resizeable=false";
var windowStrSafari="width=685,height=315,statusbar=false,menubar=false,titlebar=false,resizeable=false";


// daten aus Array
function getUrls() {
	var urlArr = document.getElementById('iUrl').value.split(";");
	return urlArr;
}

function getCats() {
	var catArr = document.getElementById('iCat').value.split(";");
	return catArr;
}

function getTitles() {
	var titleArr = document.getElementById('iTitle').value.split(";");
	return titleArr;
}

// prüfen ob urlstring leer ist
function isEmpty() {
	if(document.getElementById('iUrl').value=="") { return true; } else { return false; }
}

// fügt category zu catstr hinzu
function addToCatStr(value) {
document.getElementById('iCat').value+=value+';';
}

// Ersetzt in titeln _ durch Leerzeichen
function replaceUnderline(text) {
	return text.replace(/_/g," ");
}

// Ersetzt in zu erzeugendem Code umlaute durch Entities
function replaceUmlaute(text) {
	text = text.replace(/ä/g,"&auml;");
	text = text.replace(/ü/g,"&uuml;");
	text = text.replace(/ö/g,"&ouml;");
	return text;
}

// Erhöht / Veringert den Filmzähler
function incrementFilmCounter(cat) { 
		if(cat=="sonstige" && counterOtherFilms<maxOtherFilms) { counterOtherFilms++; } 
		if(cat!="sonstige" && counterMedFilms<maxMedFilms) { counterMedFilms++; }
		//alert("so:"+counterOtherFilms+"/"+maxOtherFilms+"; md:"+counterMedFilms+"/"+maxMedFilms+";");
}
function decrementFilmCounter(cat) {
		if(cat=="sonstige") { counterOtherFilms--; }
		if(cat!="sonstige") { counterMedFilms--; }
		//alert("so:"+counterOtherFilms+"/"+maxOtherFilms+"; md:"+counterMedFilms+"/"+maxMedFilms+";");
}

// Prüft die Filmzähler auf max
function isFilmLimit(cat) {
	var ret=false;
	// limit erreicht, message wird ausgegeben
	if(cat=="sonstige" && counterOtherFilms>=maxOtherFilms) { showMaxFilmMessage(cat); ret=true; }
	if(cat!="sonstige" && counterMedFilms>=maxMedFilms) { showMaxFilmMessage(cat); ret=true; } 
	return ret;
}

// Zeigt Message wenn maximale Anzahl an Filmen erreich ist
function showMaxFilmMessage(cat) {
		var max = maxMedFilms;
		var mt1="Sie können aus der Kategorie "+cat+" maximal ";
		var mt2=" Flashfilme auswählen";
		if(cat=="sonstige") { max=maxOtherFilms; }
		alert(mt1+max+mt2);
}

// Zeigt Nutzungsbedingungen-Div an
function TermsOfUsingDiv(doo,accept) {
	window.open("inc/form.asp?email="+document.getElementById('iAdminMail').value,"","menubar=no,titlebar=no,width=680,height=670,resizeable=no");
}

// Markiert den Quellcode im selectierten Feld
function setCodeSelection() {
	document.getElementById('iResult').focus();
	document.getElementById('iResult').select();
	sel = document.selection.createRange();
	window.clipboardData.setData("TEXT",document.getElementById('iResult').value);
}

// Zeigt im Formular Email des Webmasters an / blendet sie aus
function setWebmasterEmail(bool) {
	document.getElementById('iAdminMail').value="true";
}

// erzeugt Code für Vorschau / Generate
function getSource(urls,titles) {
	var urlstr = "";
	for(i=0;i<urls.length;i++) { urlstr+=urls[i]+",";  }
	// img/tv.jpg
	// http://www.media-konrad.de/tv/DesignA/img/tv.jpg
	// http://www.media-konrad.de/tv/DesignA/flash/flash.js
	var tvSource="";
	tvSource = "<link rel='Stylesheet' type='text/css' href='http://www.media-konrad.de/tv/include/test/style.css'>";
	tvSource += "<table border='0' style='border-style:solid; border-width:1px; border-color:#388571;' width='600px' height='190px'>";
	tvSource += "<tr><td valign='top' align='center' colspan='4' height='25px' class='tvHeadline'>Informieren Sie sich über unsere Praxisleistungen und aktuelle Gesundheitsthemen</td></tr>";
	tvSource += "<tr><td colspan='2'></td><th rowspan='2' valign='top' align='left'>";
			tvSource+="<table border='0' width='300px'><tr><td colspan='3' valign='top'>";
			// filme urls
			var titlesPraxisleistungen = "<p><b>Unsere Praxisleistungen:</b><table border='0' width='300px'><tr>";
			var titlesSonstigeleistungen = "<p><b>Sonstige Beiträge:</b><table border='0' width='300px'><tr>";
			var numOfPraxis=0;
			var numOfSonstige=0;
			var catArr = document.getElementById('iCat').value.split(";");
			for(i=0;i<urls.length;i++) {
			  var cat = catArr[i];
			  var title = titles[i];
			  if(titles[i].length>20) { title = title.substring(0,20)+"..."; }

			  if(cat.toLowerCase()!="sonstige") {
			  // category wird hier ausgelesen
			  // wenn ==sonstiges dann schreibe in die zweite Liste
				if(urls[i]!="") { 
						if(numOfPraxis%2==0) { titlesPraxisleistungen+="</tr><tr>"; }
						titlesPraxisleistungen+="\n<td width='148px' valign='top'>&nbsp;<a class='tvLink' onmouseover=\"this.style.color='#308066'\" onmouseout=\"this.style.color='#000000'\" id='"+urls[i]+"' style='cursor:pointer' onclick='embed(this.id);'>"+title+"</a></td>";
						numOfPraxis++;
				}
			  } else {
				if(urls[i]!="") {
						if(numOfSonstige%2==0) { titlesSonstigeleistungen+="</tr><tr>"; }
						titlesSonstigeleistungen+="\n<td width='148px' valign='top'>&nbsp;<a class='tvLink' onmouseover=\"this.style.color='#388571'\" onmouseout=\"this.style.color='#000000'\" id='"+urls[i]+"' style='cursor:pointer' onclick='embed(this.id);'>"+title+"</a></td>";
						numOfSonstige++;
				}
			  }
			}
			titlesPraxisleistungen += "</tr></table></p>";
			titlesSonstigeleistungen += "</tr></table></p>";
			tvSource+=titlesPraxisleistungen+titlesSonstigeleistungen;
			tvSource+="</td></tr></table>\n";
	tvSource += "</th></tr>";
	tvSource += "<tr><td width='3px'></td><td width='235px' height='200px' valign='top'>";
	tvSource += "<table border='0' width='325px' cellspacing='0' cellpadding='0' style=\"background-image:url('http://www.media-konrad.de/tv/include/test/tv.jpg'); background-repeat:no-repeat;\"><tr><td colspan='3' height='"+(18+yaxis)+"px'></td></tr><tr><td width='"+(40+xaxis)+"px'></td><td width='286px' height='160px' align='center'><span id='iVideo'></span></td><td width='42px'></td></tr><tr><td colspan='3' height='25px'></td></tr><tr><td colspan='3' align='center' class='tvText'>Zum Start bitte Beitrag rechts auswählen!</td></tr></table></td><td width='75px'>";
	tvSource += "\n<script language='JavaScript'>flashFiles='"+urlstr+ "'; <#/script><script language='JavaScript' src='http://www.media-konrad.de/tv/include/test/flash.js'><#/script>";

	tvSource+="</td><tr><td colspan='4' align='right'><table border='0' cellspacing='0' cellpadding='0'><tr><td class='tvText'>Ein Service von&nbsp;&nbsp;</td><td><a href='http://www.tv-wartezimmer.de' target='_blank'><img style='border:none;' src='http://www.media-konrad.de/tv/include/test/tv-footer.jpg'></a>&nbsp;</td></tr></table></td></tr></table>";
	tvSource = tvSource.replace(/#/g,"");
	return tvSource;
}

// schreibt bei select onchange preview in tv rechts unten
function getFileInTv(flashFile) {
		if(flashFile!=null) {
		var oeTags = '<table border="0" cellspacing="0" cellpadding="0" bgcolor="#EBA124" width="220" height="125"><tr><td width="220" height="125" align="center" valign="middle">';
		oeTags += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="210" height="115" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">';
		oeTags += '<param name="movie" value="'+flashFile+'" />';
		oeTags += '<param name="quality" value="high" />';
		oeTags += '<param name="bgcolor" value="#EBA124"/>';
		oeTags += '<embed src="'+flashFile+'" quality="high" name="player" width="210" height="115" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
		oeTags += '<\/embed><\/object></table>';
		//alert(oeTags);
		document.getElementById('iPreview').innerHTML=oeTags;
		}
}

// fügt eine Url zur Liste hinzu
function addUrl(id,cat) {
 if(document.getElementById(id).selectedIndex>=0) {
	if(isFilmLimit(cat)==false) {
	 incrementFilmCounter(cat);
	 var value = document.getElementById(id).value;
	 var text = document.getElementById(id).options[document.getElementById(id).selectedIndex].text;
	 text = replaceUnderline(text);
	 document.getElementById('iUrl').value+=value+';';	// list.options[list.selectedIndex].text
	 document.getElementById('iTitle').value+=text+';';
	 document.getElementById('iCat').value+=cat+';';
	 selectedOptions += "<option value='"+value+"'>"+text+"</option>";
	 document.getElementById('iFlashList').innerHTML="<select onchange='getFileInTv(this.value);' style='width:200px' id='iSelect' size='6'>"+selectedOptions+"</select>";
	}
 } else { alert("Bitte wählen Sie einen Film aus der Liste aus!"); }
}

// entfernt eine Url aus der Liste 
function dropUrl(id) {
if(isEmpty()==false) {
  if(document.getElementById('iSelect')!=null && document.getElementById('iSelect').value!="") {

	var url = document.getElementById('iSelect').value
	var value = document.getElementById('iSelect').options[document.getElementById('iSelect').selectedIndex].text;
	// aus catArr element an jetzigem Index holen
	var catArr = getCats();
	decrementFilmCounter(catArr[document.getElementById('iSelect').selectedIndex]);
	catArr[document.getElementById('iSelect').selectedIndex]="";
	// catstr neu schreiben
	document.getElementById('iCat').value="";
	for (i=0;i<catArr.length;i++) {
		if(catArr[i]!="") {
		document.getElementById('iCat').value+=catArr[i]+";";
		}
	}
	// replacen des Option-Strings
	selectedOptions = selectedOptions.replace("<option value='"+url+"'>"+value+"</option>","");
	document.getElementById('iFlashList').innerHTML="<select style='width:200px' id='iSelect' size='6'>"+selectedOptions+"</select>";
	document.getElementById('iUrl').value=document.getElementById('iUrl').value.replace(url+";","");
	document.getElementById('iTitle').value=document.getElementById('iTitle').value.replace(value+";","");
	
  }
 }
}

// löscht Url-Liste komplett
function dropAllUrls() {
selectedOptions="";
document.getElementById('iFlashList').innerHTML="<select style='width:200px' id='iSelect' size='6'>"+"<option>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>"+"</select>";
document.getElementById('iUrl').value="";
document.getElementById('iTitle').value="";
counterMedFilms=0;
counterOtherFilms=0;
}

//	***	***	***	GENERATE FUNCTION Z	***	***	***	***

// Erstellt Vorschau Code in textarea
function generateCode(bool) {
	if(isEmpty()==false) {
		if(termsAccepted==true) {
		 //var scriptcode = "<script language='JavaScript'>embed(files[0]);<#/script>";
		 var tmp = getSource(getUrls(),getTitles(),xaxis);//+scriptcode.replace("#","");
		 // REPLACE flash.js mit http://www.media-konrad.de/tv/DesignA/flash/flash.js
		 tmp = tmp.replace("inc/flash.js","http://www.media-konrad.de/tv/include/test/flash.js");
		 tmp = replaceUmlaute(tmp);
		 document.getElementById('iResult').value=tmp;
		} else { setWebmasterEmail(bool); TermsOfUsingDiv(true,false);  }
	} else { alert("bitte wählen sie mindestens einen Film aus!"); }
}

// Ruft previewFunktion für jeweiligen Browser auf
function previewWindow() {
	if(isEmpty()==false) {
		switch(navigator.appName) {
			case "Microsoft Internet Explorer" : previewIE(); break;
			case "Opera" : previewOpera(); break;
			default : checkNetscape(); break;
		}

	} else { alert("bitte wählen sie mindestens einen Film aus!"); }
}

// Ermittelt unterteilungen von Netscape
function checkNetscape() {
	var version = navigator.appVersion;
	if(version.indexOf("Safari")>0) {
		previewSafari();
	} else {
		previewFirefox();
	}
}

//	####	####	BROWSER-VORSCHAU	####	####	####

// Erstellt Vorschau für IE
function previewIE() {
			var win = window.open("","FlashVorschau",windowStr);
			win.document.open();
			var urls = getUrls();
			var scriptcode = "";
			xaxis=0;
			win.document.write("<div style='position:absolute; left:1; top:1;'><body id="+urls[0]+"><table align='center'><tr><td>"+getSource(getUrls(),getTitles())+"</td></tr></table></body></div>");
			win.document.close();
}
// Erstellt Vorschau für Firefox
function previewFirefox() {
			var win = window.open("","FlashVorschau",windowStrNetscape);
			win.document.open();
			var urls = getUrls();
			var scriptcode = "";
			xaxis=-1;
			yaxis=0;
			win.document.write("<body id="+urls[0]+"><table align='center'><tr><td>"+getSource(getUrls(),getTitles())+"</td></tr></table></body>");
			win.document.close();
}
// Erstellt Vorschau für Opera
function previewOpera() {
			var win = window.open("","FlashVorschau",windowStr);
			win.document.open();
			var urls = getUrls();
			var scriptcode = "";
			xaxis=0;
			win.document.write("<div style='position:absolute; left:1; top:1;'><body id="+urls[0]+"><table align='center'><tr><td>"+getSource(getUrls(),getTitles())+"</td></tr></table></body></div>");
			win.document.close();
}
// Erstellt Vorschau für Safari
function previewSafari() {
			var win = window.open("","FlashVorschau",windowStrSafari);
			win.document.open();
			var urls = getUrls();
			var scriptcode = "";
			xaxis=2;
			yaxis=0;
			win.document.write("<body id="+urls[0]+"><table align='center'><tr><td>"+getSource(getUrls(),getTitles())+"</td></tr></table></body>");
			win.document.close();
}

//	####	####	ENDE-VORSCHAU		####	####	####

// Schreibt für IE&Opera Iframe, ansonsten nichts
function writeIFrame(){
		var retstr="";
		var frstr="<iframe name='iIframe' scrolling='yes' style='width:562px; height:300px; border-style:solid; border-width:2px; border-color:#006567;'></iframe>";
		/*switch(navigator.appName) {
			case "Microsoft Internet Explorer" : retstr = frstr; break;
			case "Opera" : retstr = frstr; break;
			//default : reststr=""; break;
		}*/
		document.write(retstr);
}

// schreibt erzeugten Code in die Zwischenablage
function codeToClipboard(bool) {
  if(isEmpty()==false) {
	codeSelected=true;
	if(termsAccepted==true) {
		 var tmp = getSource(getUrls(),getTitles(),xaxis);//+scriptcode.replace("#","");
		 tmp = tmp.replace("inc/flash.js","http://www.media-konrad.de/tv/include/test/flash.js");
		 tmp = replaceUmlaute(tmp);
		 document.getElementById('iResult').value=tmp;
		 // selectiert Code und versucht, diesen zu kopieren
		 setCodeSelection();
			   //sel = document.selection.createRange();
			   //window.clipboardData.setData("TEXT",tmp);
		} else { setWebmasterEmail(bool); TermsOfUsingDiv(true,false); }
  } else { alert("bitte wählen sie mindestens einen Film aus!"); }
}
