/*
 * ipla Detector
 * --------------------------------
 * version: 2.5
 */
var blankImg = "http://detector.ipla.pl/2_5/gfx/detector_blank.gif";

if (!iplabrowser) {
	var iplabrowser = {
		isModern: (document.getElementById && document.getElementsByTagName)?true:false,
		isWindows: (navigator.platform == 'Win32')?true:false,
		isSafari: (navigator.vendor && navigator.vendor.indexOf('Apple') >= 0)?true:false,
		isWebkit: (navigator.userAgent.indexOf('WebKit/') >= 0)?true:false,
		isOpera: (window.opera)?true:false
	};
	iplabrowser.isIE = (document.all && iplabrowser.isModern && !iplabrowser.isOpera)?true:false;
	iplabrowser.isUnsupportedBrowser = (iplabrowser.isSafari || iplabrowser.isWebkit)?true:false;
}

if (!ipla)
var ipla = {
	done: false,
    languages: {
        "en": {
			background: "http://detector.ipla.pl/2_5/gfx/detector_bg.png",
            installPrompt: "You need to install the free ipla player to view this movie. Do you want to download ipla?",
			iplaDesc: "interactive multimedia platform",
			needIpla: "To view this movie you need to have ipla installed.",
			downloadInstallIpla: "Download and install ipla.",
			whatIsIpla: "Don't know what's ipla?",
            demoIpla: "View a demonstration",
            haveIpla: "Have ipla installed? Click",
            clickHere: "here",
            legalese: "With ipla, you can view online videos legally and safe over the Internet."
        },
        "pl": {
			background: "http://detector.ipla.pl/2_5/gfx/detector_bg.png",
            installPrompt: "Do obejrzenia tego filmu wymagany jest player ipla. Czy chcesz pobrać ipla?",
			iplaDesc: "interaktywna platforma rozrywkowa",
			needIpla: "Do obejrzenia materiału wideo potrzebna jest ipla.",
			downloadInstallIpla: "Pobierz i zainstaluj ipla.",
			whatIsIpla: "Nie wiesz czym jest ipla?",
            demoIpla: "Zobacz demo",
            haveIpla: "Masz już ipla? Kliknij",
            clickHere: "tutaj",
            legalese: "Platforma ipla zapewnia legalny i bezpieczny dostęp do materiałów wideo w internecie."
        }
    },
    installUrl: "http://download.ipla.redefine.pl/iplasetup.exe",
	liteUrl: "http://www.ipla.pl/Ipla_Lite,3387/index.html",
    downloadUrl: "http://ipla.pl/pobierz.php",
	demoUrl: "http://www.ipla.pl",
	vbscript: [
		'Function isIplaInstalled()',
		'on error resume next',
		'Set oIpla = CreateObject("IPLAMK.iplamkCtrl")',
		'isIplaInstalled = IsObject(oIpla)',
		'Set oIpla = nothing',
		'End Function'].join("\n"),
	detected : false,
    language : "pl",

	checkMIME: function() {
		if (typeof(navigator.mimeTypes['application/x-ipla']) == 'object')
			return true;
		return false;
	},
        isDone: function() {
            return (ipla.done) ? true : false;
        },
	checkActiveX: function() {
		if(iplabrowser.isIE) {
			var code = document.createElement('script');
			code.setAttribute('language', 'VBscript');
			code.text = ipla.vbscript;
			var body = document.getElementsByTagName('body');
			if(!body)
				return true;
			try {
				document.getElementsByTagName('body')[0].appendChild(code);
			    return isIplaInstalled();
			}
			catch (e) {};
		} else
			return false;
	},
	
	/** 
	 * instalacja ipla
	 */
	install: function(e, aObject){
		var popupInstall=false;
		var fastInstall=false;
		var lightboxInstall=true;
		
		if(!(typeof window.iplaPopup == 'undefined') && !window.iplaPopup)
			popupInstall=false;

		if(!(typeof window.iplaFastInstall == 'undefined') && !window.iplaFastInstall)
			fastInstall=false;

		if(window.installIplaPage)
			ipla.downloadUrl = window.installIplaPage;
		else if(window.iplaInstallURL) 
			ipla.downloadUrl = window.iplaInstallURL;

		if ( lightboxInstall ) {
			iplaPopupForce(aObject.href);
			
			if (e) {
				e.cancelBubble = true;
				e.returnValue = false;
				if (e.stopPropagation) {
					e.stopPropagation();
					e.preventDefault();
				}
			}
		}
		else if (fastInstall && iplabrowser.isWindows) {
			var bInstall = confirm(ipla.languages[ ipla.language ].installPrompt);
			if (bInstall) {
				window.location = ipla.installUrl;
			}
		}
		else if (popupInstall) 
			w = window.open(ipla.downloadUrl);
		else
			document.location.href = ipla.downloadUrl;
		return true;
	},
	
	getContainterPos: function()
	{
		//(parseInt(ipla.getHeight / 2, 10)) + "px";
		var win_h;
		if(window.pageYOffset)
			win_h = (window.pageYOffset);
		else if(document.documentElement.scrollTop)
			win_h = (document.documentElement.scrollTop);
		else if(document.body.scrollTop)
			win_h = (document.body.scrollTop);
		else
			win_h = 0;
			
		win_h = parseInt(win_h, 10);
			
		win_h += 100;

		return win_h + 'px';
	},
	
	/** 
	 * detekcja lub propozycja instalacji ipla
	 */
	detect: function(e) {
		if(!e) var e = window.event;
		
		if ( e.srcElement ) {
			var target = e.srcElement;
		} else {
			var target = this;
		}
		if(target.tagName != 'A' && target.tagName != 'a')
			target = target.parentNode;

		if (!ipla.detected)
			ipla.detected = ipla.check();
		if (ipla.detected == false) {
			ipla.install(e, target);
			return false;
		}
		return true;
	},
	
	/**
	 * Sprawdzenie, czy ipla jest zainstalowana. Do samodzielnego wywołania
	 * @return true / false / undefined
	 */
	check: function() {
		if (!iplabrowser.isWindows)
			return false;
		if(iplabrowser.isUnsupportedBrowser)
			return false; //'undefined';
		if(iplabrowser.isIE)
			return ipla.checkActiveX();
		else
			return ipla.checkMIME();
	},

	/**
	 * skanuje wszystkie linki, znajduje te pasujace do maski 'ipla:' i podpina pod nie zdarzenie detekcji ipla
	 */ 
	assign: function(){
		if(iplabrowser.isModern) {
			var a = document.getElementsByTagName('a');
			for (var i = 0, l = a.length; i < l; i++) {
				if (a[i].href.length && a[i].href.indexOf('ipla:') != -1) {
					if(a[i].href.lastIndexOf('/')==6) a[i].href = a[i].href+'/';
					a[i].href = a[i].href+encodeURI(document.location.href);
					if (a[i].addEventListener) a[i].addEventListener('click', ipla.detect, false);
				    else if (a[i].attachEvent) a[i].attachEvent('onclick', ipla.detect);
				}
				
			}
			ipla.done= true;
		}
	},
	
	/**
	 * system
	 */
	os: function(){
		var ua = navigator.userAgent;
		if(ua.indexOf('Mac OS') > 0)
			return 'Mac';
		if(ua.indexOf('Linux') > 0)
			return 'Linux';
		if(ua.indexOf('Unix') > 0)
			return 'Linux';
		return 'Windows';
	},
	
	/**
	 * inicjalizacja skryptu - po zaladowaniu dokumentu wywoluje ipla.assign
	 */ 
	init: function(){
		if ( navigator.language ) {
			var navigatorLanguage = navigator.language.substr(0, navigator.language.indexOf("-"));
		} else if ( navigator.browserLanguage ){
			var navigatorLanguage = navigator.browserLanguage.substr(0, navigator.browserLanguage.indexOf("-"));
		} else {
			var navigatorLanguage = "pl";
		}
		
        if ( window.iplaLanguage != null && typeof(ipla.languages[ window.iplaLanguage ]) == "object" ) {
            ipla.language = window.iplaLanguage
        } else if ( typeof(ipla.languages[ navigatorLanguage ]) == "object" ) {
            ipla.language = navigatorLanguage
        }
        
		if(typeof window.addEventListener != 'undefined')
			window.addEventListener('load', ipla.assign, false);
		else if(typeof document.addEventListener != 'undefined')
			document.addEventListener('load', ipla.assign, false);
		else if(typeof window.attachEvent != 'undefined')
			window.attachEvent('onload', ipla.assign);
	}
};
ipla.init();

// compatibility
function iplaInit(){
	ipla.init();
};
function iplaLoad(){
	ipla.assign();
};
function iplaCheck(){
	var res = ipla.check();
	if(res == false)
		return false;
	return true;
}
function iplaCheckAndForce(movieId)
{
	var res = iplaCheck();
	if(res)
		document.location.href = movieId;
	else
		iplaPopupForce(movieId);
}

function iplaPopupForce(movieId)
{
		var body = document.getElementsByTagName("body")[0];
		var overlay = document.createElement("div");
		overlay.style.background = "#000";
		overlay.style.position = "fixed";
		overlay.style.width = "100%";
		overlay.style.height = "100%";
		overlay.style.top = "0";
		overlay.style.left = "0";
		overlay.style.opacity = "0.6";
		overlay.style.filter = "alpha(opacity=60)";
		overlay.style.zIndex = "999";
		overlay.onclick = function() {
			body.removeChild(messageContainer);
			if (window.opera) {
				overlay.style.left = "10000px";
				overlay.style.top = "10000px";
				overlay.style.display = "none";
			}
			else
				body.removeChild(overlay);
		}
		
		if ( navigator.userAgent.indexOf('MSIE 6.') != -1 ) {
			overlay.style.position = "absolute";
			overlay.style.setExpression("width", "document.documentElement.clientWidth + 'px'");
			overlay.style.setExpression("height", "document.documentElement.clientHeight + 'px'");
		}
		
		var messageContainer = document.createElement("div");
		messageContainer.style.background = "url(" + ipla.languages[ ipla.language ].background + ") center center no-repeat";
		messageContainer.style.position = "absolute";
		//messageContainer.style.top = "50%";
		messageContainer.style.top = ipla.getContainterPos();
		messageContainer.style.left = "50%";
		messageContainer.style.margin = "0 0 0 -300px";		// -200px 0 0 -300px
		messageContainer.style.border = "0";
		messageContainer.style.zIndex = "1000";
		messageContainer.style.width = "621px";
		messageContainer.style.height = "385px";			
		
		if ( navigator.userAgent.indexOf('MSIE 6.') != -1 ) {
			messageContainer.style.background = "none";

			var fakeIEobj = document.createElement("div");
			fakeIEobj.style.zIndex = "-1";	
			fakeIEobj.style.width = "621px";
			fakeIEobj.style.height = "385px";
			fakeIEobj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ ipla.languages[ ipla.language ].background +"', sizingMethod='scale')";
			messageContainer.appendChild(fakeIEobj);
		}
		
		var messageText = document.createTextNode(" "+ ipla.languages[ ipla.language ].iplaDesc);
		
		/* Pobierz i zainstaluj ipla */
		var getIplaLink = document.createElement("a");
		getIplaLink.style.position = "absolute";
		getIplaLink.style.left = "40px";
		getIplaLink.style.top = "180px";
		getIplaLink.style.display = "block";
		getIplaLink.style.padding = "0";
		getIplaLink.style.border = "0";
		if(ipla.os() == 'Windows')
			getIplaLink.href = ipla.installUrl;
		else
		{
			getIplaLink.href = ipla.liteUrl;
			getIplaLink.target = "_blank";
		}
		getIplaLink.innerHTML = '<img src="' + blankImg + '" alt="" width="345" height="25" border="0" />';
		
		var demoLink = document.createElement("a");
		demoLink.style.position = "absolute";
		demoLink.style.left = "200px";
		demoLink.style.top = "250px";
		demoLink.style.display = "block";
		demoLink.style.padding = "0";
		demoLink.style.border = "0";
		demoLink.href = ipla.demoUrl;
		demoLink.target = "_blank";
		demoLink.innerHTML = '<img src="' + blankImg + '" alt="" width="90" height="20" border="0" />';
			
		if(ipla.os() == 'Windows')
		{
			var haveiplaLink = document.createElement("a");
			haveiplaLink.style.position = "absolute";
			haveiplaLink.style.left = "173px";
			haveiplaLink.style.top = "275px";
			haveiplaLink.style.display = "block";
			haveiplaLink.style.padding = "0";
			haveiplaLink.style.border = "0";
			haveiplaLink.href = movieId;
			haveiplaLink.innerHTML = '<img src="' + blankImg + '" alt="" width="30" height="20" border="0" />';
		}
		else
		{
			var haveiplaLink = document.createElement("div");
			haveiplaLink.style.position = "absolute";
			haveiplaLink.style.left = "173px";
			haveiplaLink.style.top = "275px";
			haveiplaLink.style.padding = "0";
			haveiplaLink.style.width = "220px";
			haveiplaLink.style.height = "140px";

			var flashDetectMovie = 'http://detector.ipla.pl/2_5/swf/ipla_lite_detector.swf';
			haveiplaLink.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="220" height="140" id="flashIplaDetector" align="middle">'+
					'<param name="allowScriptAccess" value="sameDomain" />'+
					'<param name="allowFullScreen" value="true" />'+
					'<param name="flashvars" value="vod_url='+movieId+'"/>'+
					'<param name="movie" value="'+flashDetectMovie+'" />'+
					'<param name="quality" value="high" /><param name="wmode" value="transparent" />'+
					'<embed src="'+flashDetectMovie+'" flashvars="vod_url='+movieId+'" quality="high" wmode="transparent" width="220" height="140" name="flashIplaDetector" align="middle" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'+
					'</object>';
		}
		
		messageContainer.appendChild(getIplaLink);
		messageContainer.appendChild(demoLink);
		messageContainer.appendChild(haveiplaLink);
		            
		var messageClose = document.createElement("a");
		messageClose.style.background = "";
		messageClose.style.width = "22px";
		messageClose.style.height = "22px"
		messageClose.style.position = "absolute";
		messageClose.style.top = "1px";
		messageClose.style.right = "5px";
		messageClose.style.display = "block";
		messageClose.href = "javascript:void(null)";
		messageClose.onclick = function() {
			body.removeChild(messageContainer);
			if (window.opera) {
				overlay.style.left = "10000px";
				overlay.style.top = "10000px";
				overlay.style.display = "none";
			}
			else
				body.removeChild(overlay);
		}
		if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
			messageClose.style.top = "10px";
			messageClose.style.right = "20px";
		};

		messageContainer.appendChild(messageClose);

		body.appendChild(overlay);
		body.appendChild(messageContainer);
}

function detectionDone() {
    return ipla.isDone();
}
