
//Heallo
function ShowItPlus(playerId, x, y, width, height){
	this.serverdomain = "";
	this.bandwidthPath	= "";
	this.baseUrl = "";
	this.specialParams = "?r=r";
	this.skinName = 'KIM';
	this.applicationId = '374308865';
	this.swfBase = "";
	this.id = null;
	this.rootCategory = -1;
	this.content = -1;
	this.width = 0;
	this.height = 0;
	this.x = 0;
	this.y = 0;
	this.targetName = 0;
	this.player = null;
	this.wmode = "window";
	this.autostart = true;
	this.debug = false;
	this.allowedWmodes = ["window", "opaque", "transparent"];
	this.initialize = false;
	this.config = null;
	this.parentElement = null;
	this.embedMode = null;
	this.forcedWidth = -1;
	this.forcedHeight = -1;
	this.keepVideoAspectRatio = null;
	this.autoplay= null;
	this.playlistTemplate = "xflv/plus/mxasx.xml";
	this.configTemplate = "xflv/plus/config.xml";
	this.tokens = null;
	this.customParams = null;
	this.category = null;
	this.initialize = initializeShowIt;
	this.setWmode = showItSetWmode;
	this.setParams = showItSetParams;
	this.insertIntoElement = insertShowItIntoElement;
	this.initialize(playerId, x, y, width, height);
	this.html5Skin = null;
};

function showItSetWmode(wmode){
	if(this.initialized){
		alert("ERROR: cannot set wmode after initialization");
		return;
	}
	if(this.allowedWmodes.indexOf(wmode) < 0){
		alert("ERROR: invalid wmode");
		return;
	}
	this.wmode = wmode;
}

function showItSetParams (){
	var urlPath = document.location.href;
	this.homePage = urlPath;
	var idx = urlPath.indexOf("?");
	if(idx > 0){
		this.homePage = urlPath.split("?")[0];
	 	urlPath = urlPath.substring(idx + 1, urlPath.length);
	 	var params = urlPath.split("&");
	 	for(var  i = 0; i < params.length; i++){
	 		var param = params[i];
	 		if(param != ""){
	 			var paramProperties = param.split("=");
	 			switch(paramProperties[0]){
	 				case 'category':
	 					this.category = paramProperties[1];
						break;
					case 'content':
	 					this.content = paramProperties[1];
						break;
					default:{
						if(this.homePage.indexOf("?") < 0){
							this.homePage = this.homePage + "?";
						}
						else{
							this.homePage = this.homePage + "&";
						}
						this.homePage = this.homePage + paramProperties[0] + "=" + paramProperties[1];
						break;
					}
		 		}
	 		}
	 	}
	}
	if(this.forcedWidth > 0 && this.forcedHeight > 0){
		this.specialParams += '$forcedWidth=' + this.forcedWidth;
		this.specialParams += '$forcedHeight=' + this.forcedHeight;
		this.specialParams += '$keepVideoAspectRatio=' + this.keepVideoAspectRatio;
	}
	if(this.category != null){
		this.specialParams += '$category=' + this.category;
 	}
}

function insertShowItIntoElement(elementId, videoWidth, videoHeight, keepVideoAspectRatio){
	if(elementId == undefined || document.getElementById(elementId) == undefined ){
		alert("ERROR: target element not defined");
		return;
	}
	if(videoWidth != undefined && videoHeight != undefined){
		this.forcedWidth = videoWidth;
		this.forcedHeight = videoHeight;
		if(keepVideoAspectRatio != undefined){
			this.keepVideoAspectRatio = keepVideoAspectRatio;
		}
	}
	this.setParams();
	this.embedMode = "embedded";
	this.parentElement = document.getElementById(elementId);
	if(this.autostart){
		autoInit(this);
	}
	else{
		autoInit(this, this.launcher);
	}
}

function initializeShowIt(playerId, x, y, width, height){
	if(document.getElementById(playerId) != undefined){
		alert("ERROR: element " + playerId + " already exists");
		return;
	}
	this.serverdomain = "http://" + getDomainFromUrl('http://iptv.cdn.tv1.de/iptv/player/macros/_v_f_500_de/_s_KIM/_x_s-374308865/');
	var baseLink = '/iptv/player/macros/_v_f_500_de/_s_KIM/_x_s-374308865/'
	var first = baseLink.indexOf("/_");
	this.baseUrl = this.serverdomain + baseLink.substring(0, first);
	this.bandwidthPath = this.serverdomain + '/iptv/img/test.txt';
	this.swfBase = this.serverdomain +  '/iptv/swf/xflv/';
	this.id = playerId;
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
}

function autoInit(showIt, ctrl){
	if(ctrl != null){
		ctrl.onclick = function(){
			load(showIt);
		}
	}
	else{
		window.onload = function(){
			load(showIt);
		}
	}
	window.onunload =  function(){
		unload(showIt);
	}
}

function load(showIt){
	if(showIt.launcher != null){
		showIt.launcher.removeEvents();
		showIt.launcher.style.cursor = "default";
		showIt.launcher.style.visibility = "hidden";
	}
	var holder = showIt.parentElement;
	var player = document.createElement("div");
	player.id = showIt.id;
	player.style.position = "relative";
	player.style.padding = 0;
	player.style.margin = 0;
	player.style.border = "none";
	player.style.width = showIt.width + "px";
	player.style.height = showIt.height + "px";
	player.style.display = "block";
	showIt.player = player;
	showIt.parentElement.appendChild(player);
	var useHTML5 = false;
	
		
		
	if(useHTML5){
		embedHtml5(showIt);
	}
	else{
		embed(showIt);
	}
}

function unload(showIt){
	if(showIt.parentElement != null){
		showIt.parentElement.parentNode.removeChild(showIt.parentElement);
	}
}

function embedHtml5(showIt){
	
	var frameUrl = 'http://iptv.cdn.tv1.de/iptv/player/macros/_v_f_500_de/_s_KIM/_x_s-374308865/xflv/plus/html5/HTML5.html';
	if(showIt.html5Skin != null){
		var idx = frameUrl.indexOf("_s_");
		if(idx < 0){
			//insert before extra info
			idx = frameUrl.indexOf("_x_");
			
			var url = frameUrl.substring(0, idx);
			var url2 = frameUrl.substring(idx, frameUrl.length);
			frameUrl = url + "_s_" + showIt.html5Skin + "/" + url2;
		}
		else{
			var url = frameUrl.substring(0, idx);
			var nextPartIdx = frameUrl.indexOf("/", idx);
			var url2 = frameUrl.substring(nextPartIdx, frameUrl.length);
			frameUrl = url + "_s_" + showIt.html5Skin + url2;
		}
	}
	var idx = frameUrl.indexOf("_v_");
	if(idx > 0){
		var url = frameUrl.substring(0, idx);
		var nextPartIdx = frameUrl.indexOf("/", idx);
		var url2 = frameUrl.substring(nextPartIdx, frameUrl.length);
		frameUrl = url + "_v_q_1028_de" + url2;
	}
	var frame = document.createElement("iframe");
	frame.src = frameUrl;
	frame.width = showIt.width;
	frame.height = showIt.height;
	frame.scrolling = "no";
	var attr = document.createAttribute("frameborder");
	attr.nodeValue = "0";
	frame.setAttributeNode(attr);
	attr = document.createAttribute("allowtransparency");
	attr.nodeValue = "true";
	frame.setAttributeNode(attr);
	document.getElementById(showIt.id).appendChild(frame);
}

function embed(showIt){
	var flashvars = "plugin_version=9.0.115&amp;baseUrl=" + showIt.baseUrl + 
	"&amp;skin=" + showIt.skinName +
	"&amp;application=" + showIt.applicationId +
	"&amp;params=" + showIt.specialParams +
	"&amp;homePage=" + escape(showIt.homePage) + "&amp;domain=" + document.domain +
	"&amp;debug=" + showIt.debug + "&amp;swfBase=" + showIt.swfBase;
	if(showIt.webcastId != undefined){
		flashvars = flashvars + "&amp;webcastId=" + showIt.webcastId;
	}
	if(showIt.bandwidth > 0){
		flashvars = flashvars + "&amp;bandwidth=" + showIt.bandwidth;
	}
	else if(showIt.bandwidthPath != undefined){
		flashvars = flashvars + "&amp;bandwidthPath=" + showIt.bandwidthPath;
	}
	if(showIt.autoPlay != null){
		flashvars = flashvars + "&amp;autoPlay=" + showIt.autoplay;
	}
	if(showIt.playlistTemplate != null){
		flashvars = flashvars + "&amp;playlistTemplate=" + showIt.playlistTemplate;
	}
	if(showIt.configTemplate != null){
		flashvars = flashvars + "&amp;configTemplate=" + showIt.configTemplate;
	}
	if(showIt.rootCategory > 0){
		flashvars = flashvars + "&amp;rootCategory=" + showIt.rootCategory;
	}
	if(showIt.content > 0){
		flashvars = flashvars + "&amp;content=" + showIt.content;
	}
	if(showIt.enhancedStreaming == true){
		flashvars = flashvars + "&amp;fmsConnectionMode=enhanced";
		if(showIt.tokens != null && showIt.tokens > 0){
			flashvars = flashvars + "&amp;urlTokens=" + showIt.tokens;
		}
	}
	if(showIt.customParams != null){
		for(var i = 0; i < showIt.customParams.length; i++){
			var paramParts = showIt.customParams[i].split("=");
			if(paramParts.length != 2){
				continue;
			}
			flashvars = flashvars + "&amp;" + paramParts[0] + "=" + paramParts[1];
		}
	}
	flashvars = flashvars + '&amp;language=de';
	var embedCode = '<object type="application/x-shockwave-flash" data="' + showIt.serverdomain + '/iptv/swf/xflv/showItPlus.swf" ' +
	'width="' + showIt.width + '" ' + 
	'height="' + showIt.height + '" ' +
	'style="width:' + showIt.width + ' px;height:' + showIt.height + ' px">' +
	'<param name="movie" value="' + showIt.serverdomain + '/iptv/swf/xflv/showItPlus.swf"/>' +
	'<param name="wmode" value="' + showIt.wmode + '" />' +
	'<param name="quality" value="high" />' +
	'<param name="swliveconnect" value="true" />' +
	'<param name="allowscriptaccess" value="always" />' +
	'<param name="scalemode" value="showall" />' +
	'<param name="allowfullscreen" value="true" />' +
	'<param name="flashvars" value="' +  flashvars + '"/>' +
	'</object>';
	document.getElementById(showIt.id).innerHTML = embedCode;
}

function getDomainFromUrl(url){
	if(url == null){
		return null;
	}
	var afterHTTP = url.substring(7,url.length);
	var nextSlashIndex = afterHTTP.indexOf("/");
	var domain = afterHTTP.substring(0, nextSlashIndex);
	return domain;
}