


function ShowIt(playerId, x, y, width, height, autostart, launchButtonId, version){
	this.serverdomain = "";
	this.bandwidthPath	= "";
	this.baseUrl = "";
	this.specialParams = "?r=r";
	this.skinName = 'defaultPlayerLiveSkin';
	this.applicationId = '155779072';
	
	this.swfBase = "";
	this.id = null;
	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.launcher = null;
	this.debug = false;
	this.allowedWmodes = ["window", "opaque", "transparent"];
	this.initialize = false;
	this.config = null;
	this.parentElement = null;
	this.embedMode = null;
	this.version = "AS3";
	this.mouseOverState= "";
	this.forcedWidth = -1;
	this.forcedHeight = -1;
	this.keepVideoAspectRatio = null;
	this.autoplay= null;
	this.playlistTemplate = null;
	this.tokens = null;
	this.customParams = null;
	this.category = null;
	this.zIndex = 100;
	this.elementsToHide = null;
	var isValid = true;
	
	this.initialize = initializeShowIt;
	this.setWmode = showItSetWmode;
	this.setContent = showItSetContent;
	this.setParams = showItSetParams;
	this.insertIntoElement = insertShowItIntoElement;

	this.contentId = null;
	this.singleton = null;
	
	this.initialize(playerId, x, y, width, height, autostart, launchButtonId, version);
};

function showItSetWmode(wmode){
	if(this.initialized){
		alert("ERROR: cannot set wmode after initialization");
		return;
	}
	var found = false;
	for(var i = 0; i < this.allowedWmodes.length; i++){
		if(this.allowedWmodes[i] == wmode){
			found = true;
			break;
		}
	}
	if(!found){
		alert("ERROR: invalid wmode");
		return;
	}
	this.wmode = wmode;
}

function showItSetContent(id, singleton){
	if(singleton == undefined){
		this.singleton = true;
 		this.specialParams += '$singleton=true';
	}
	else if(singleton){
		this.singleton = singleton;
		this.specialParams += '$singleton=' + singleton;
	}
	this.contentId = id;
	this.specialParams = this.specialParams + '$contentId=' + id;
}

function showItSetParams (){
	var urlPath = document.location.href;
	var idx = urlPath.indexOf("?");
	if(idx > 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];
						//this.specialParams += '$category=' + paramProperties[1];
						break;
					case 'singleton':
						this.singleton = paramProperties[1];
						this.specialParams += '$singleton=' + paramProperties[1];
						break;
					case 'content':
						this.contentId = paramProperties[1];
						this.specialParams += '$contentId=' + paramProperties[1];
						break;
					case 'startPosition':
						this.specialParams += '$startPosition=' + paramProperties[1];
						break;
					case 'idx':
						this.specialParams += '$idx=' + paramProperties[1];
						break;
		 		}
	 		}
	 	}
	 	
	}
	if(this.forcedWidth > 0 && this.forcedHeight > 0){
		
		this.specialParams += '$forcedWidth=' + this.forcedWidth;
		this.specialParams += '$forcedHeight=' + this.forcedHeight;
		if(this.version == "AS3" || this.version == "AS3_10"){
			this.specialParams += '$keepVideoAspectRatio=' + this.keepVideoAspectRatio;
		}
		else{
			this.specialParams += '$keepFormat=' + !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, autostart, launchButtonId, version){
	if(document.getElementById(playerId) != undefined){
		alert("ERROR: element " + playerId + " already exists");
		return;
	}
	if(!autostart && document.getElementById(launchButtonId) == undefined){
		alert("ERROR: launch button is not defined");
		return;
	}
	this.serverdomain = "http://" + getDomainFromUrl('http://iptv.cdn.tv1.de/player/macros/_s_defaultPlayerLiveSkin/_x_s-155779072/');
	var baseLink = '/player/macros/_s_defaultPlayerLiveSkin/_x_s-155779072/';
	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/';
	if(version != undefined){
		this.version = version;
	}
	this.id = playerId;
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	this.autostart = autostart;
	if(! autostart){
		this.launcher = document.getElementById(launchButtonId);
		this.launcher.style.cursor = "pointer";
	}
}

function autoInit(showIt, ctrl){
	if(ctrl != null){
		ctrl.onclick = function(){
			load(showIt);
		}
	}
	else{
		addEvent(window,'load',function(){
			load(showIt);
		});
	}
	addEvent(window,'unload',function(){
		unload(showIt);
	});
}

function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent) 
		obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
}

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.zIndex = showIt.zIndex;
	player.style.display = "block";
	showIt.player = player;
	showIt.parentElement.appendChild(player);
	
	if(showIt.version == "AS3"){
		embedAS3Version(showIt);
	}
	else if(showIt.version == "AS3_10"){
		embedAS3Flash10Version(showIt);
	}
	else{
		embedAS2Version(showIt);
	}
	
}

function unload(showIt){
	if(showIt.parentElement != null){
		showIt.parentElement.parentNode.removeChild(showIt.parentElement);
	}
}


function embedAS2Version(showIt){
	var configPath = showIt.serverdomain + '/player/macros/_s_defaultPlayerLiveSkin/_x_s-155779072/xflv/config.xml' + showIt.specialParams;
	var flashvars = "plugin_version=9.0.115&amp;baseUrl=" + showIt.baseUrl + "&amp;configXml=" + configPath +
	"&amp;homePage=" + window.location.href + "&amp;domain=" + document.domain +
	"&amp;debug=" + showIt.debug + "&amp;swfBase=" + showIt.swfBase;
	if(showIt.bandwidthPath != undefined){
		flashvars = flashvars + "&amp;bandwidthPath=" + showIt.bandwidthPath;
	}
	var embedCode = '<object id="si_'+ showIt.id + '" type="application/x-shockwave-flash" data="' + showIt.serverdomain + '/iptv/swf/xflv/showIt.swf" ' +
	'width="' + showIt.width + '" height="' + showIt.height + '">' +
	'<param name="movie" value="' + showIt.serverdomain + '/iptv/swf/xflv/showIt.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 embedAS3Flash10Version(showIt){
	var flashvars = "plugin_version=10.0&amp;jsEnabled=true&amp;baseUrl=" + showIt.baseUrl + 
	"&amp;skin=" + showIt.skinName +
	"&amp;application=" + showIt.applicationId +
	"&amp;params=" + showIt.specialParams +
	"&amp;homePage=" + window.location.href + "&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;xasxTemplate=" + showIt.playlistTemplate;
	}
	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];
		}
	}
	
	
	var embedCode = '<object id="si_'+ showIt.id + '" type="application/x-shockwave-flash" data="' + showIt.serverdomain + '/iptv/swf/xflv/showIt3.1.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/showIt3.1.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 embedAS3Version(showIt){
	var flashvars = "plugin_version=9.0.115&amp;jsEnabled=true&amp;baseUrl=" + showIt.baseUrl + 
	"&amp;skin=" + showIt.skinName +
	"&amp;application=" + showIt.applicationId +
	"&amp;params=" + showIt.specialParams +
	"&amp;homePage=" + window.location.href + "&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;xasxTemplate=" + showIt.playlistTemplate;
	}
	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];
		}
	}
	
	var embedCode = '<object id="si_'+ showIt.id + '" type="application/x-shockwave-flash" data="' + showIt.serverdomain + '/iptv/swf/xflv/showIt3.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/showIt3.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;
}

function debug(message){
	if(window.console){
		window.console.log(message);
	}
}
