function iHtmlEncode(text) {
	text=text.replace(/\&/g,'&amp;').replace(/\"/g,'&quot;').replace(/\</g,'&lt;').replace(/\>/g,'&gt;');
	return(text);
}

function iHtmlDecode(text) {
	text=text.replace(/\&lt;/g,'<').replace(/\&gt;/g,'>').replace(/\&quot;/g,'"').replace(/\&amp;/g,'&');
	return(text);
}

iImpressorInstallAllow=false;
function iImpressorCommand(command,value){
	switch(command) {
		case 'ColorPaletteShow':
			var s=document.getElementById('ColorPalette');
			if (typeof(s._x)=='undefined')
				s._x=420;
			if (!s._v)
				s.style.left=s._x+'px';
			s._v=true;						
			break;
		case 'ColorPaletteHide':
			var s=document.getElementById('ColorPalette');
			s._x=parseInt(s.style.left);
			s._v=false;
			s.style.left='-139px';
			break;
		case 'ColorKitShow':
			var p=iPosition(value);
		    var s=document.getElementById('ColorKit');
		    s.style.left=mouseX;//(p.X+value.clientWidth+(iBrowser.Family=='msie'?271+(document.skyscrapper?27:114):-12))+'px';
		    s.style.top=mouseY;//(p.Y+20)+'px';
		    break;
		case 'ColorKitHide':
		    var s=document.getElementById('ColorKit');
			s.style.left='-198px';
		    break;
		case 'InstallDone':
			iImpressorInstallDone();
			break;
	}
}
function iImpressorBrowser() {
	if (iBrowser.Family=='msie') {
		if (iBrowser.Version<5) return(false)
	} else if (iBrowser.Family=='mozilla') {
		if (iBrowser.Version<2) return(false)
	} else
		return(false);
	return(true);
}
function iImpressorCheck(vmin) {
	if (!iImpressorBrowser()) return(false);
	if (typeof(vmin)=='undefined')
		vmin='1.0.0.1';
	vmin=vmin.split('.');
	var v=document.iImpressorVersion;
	if (typeof(v)=='undefined') {
		if (navigator.plugins.length) {
			if(p=navigator.plugins['Impressor'])
				if (r=/(\d+)\.(\d+)\.(\d+)\.(\d+)/i.exec(p.description)) {
					r.shift();
					v=r;
				}
	  	} else {
	  		try {
	  			if(x=new ActiveXObject("Impressor.iPluginX"))
		  			v=(x.Version).split('.');
		  	} catch(e) {}
		}
		document.iImpressorVersion=v;
	}
	if (!v) return(false);
	for(var i=0;i<4;i++) {
		if (((n=parseInt(v[i]))>(m=parseInt(vmin[i]))))
			return(true);
		if (!(n>=m))
			return(false);
	}
	return(true);
}
function iImpressorGenerate(id,dx,dy,parameters,html_container,id_container) {
	var ok=(id=='Install')||(iImpressorCheck());
	if (!ok) return(false);
	parameters.Base=document.base;
	var html='<OBJECT type="application/x-impressor" width="'+dx+'" height="'+dy+'" codebase="http://www.impressor.pl/install/'+(iBrowser.Family=='msie'?'impressor.ocx#version=1,0,0,1':'Impressor')+'">';
	for(n in parameters)
		html+='<PARAM NAME="'+iHtmlEncode(n)+'" VALUE="'+iHtmlEncode(parameters[n])+'">';
	html+='</OBJECT>';
	if (html_container)
		html=html_container.replace(/\<OBJECT\/\>/g,html);
	if (!document.iImpressorObjects)
		document.iImpressorObjects=[];
	document.iImpressorObjects.push(parameters);
	if (typeof(id_container)=='undefined')
		document.write(html)
	else {
		var div=document.getElementById(id_container);
		if (div)
			div.innerHTML=html;
	}
	return(true);
}
function iImpressorInstall() {
	if (!iImpressorBrowser()) return(false);
	var ok=iImpressorCheck();
	if (ok) return(false);
	
	var html='<div id="plugin-install"><b>Pokoloruj ten projekt domu</b><p>Zainstaluj rozszerzenie, dzięki któremu będziesz mógł nadawać swoje kolory projektom domów z naszej oferty.</p>';
	html+='<div id="plugin-object" style="position:"><iframe id="plugin-frame"></iframe></div>';
	// przed
	html+='<div id="plugin-ask"><div onclick="iImpressorInstallIcon()" class="cancel">zrobię to później</div><div onclick="iImpressorInstallDo();" class="ok">rozpocznij instalację</div><div class="icon">zawsze możesz powrócić do instalacji klikając ikonę pędzla</div></div>';
	// podczas
	html+='<div style="display:none;" id="plugin-process">Trwa instalacja programu Impressor<div class="loading"><div></div></div></div>'; // Problem? <a>Skorzystaj z pomocy</a>.
	// przerwa
	html+='<div style="display:none;" id="plugin-break"><div onclick="iImpressorInstallDo()" class="ok">kontynuuj instalację</div><b>Instalacja przerwana</b><div class="error"><div></div></div>Problem? <a>Skorzystaj z pomocy</a>.</div>';
	// po
	html+='<div style="display:none;" id="plugin-done"><b>Instalacja zakończona pomyślnie</b><div class="loaded"><div></div></div>Przeładowanie strony za <span id="plugin-counter">5</span> sekund ...</div>';
	if ((iBrowser.Family=='msie')&&(iCookieGet('iInstall')=='1')) {
		html+='<script>iImpressorInstallDo()</script>';
	}
	html+='</div>';
	document.write(html);
	
	return(true);
}
function iImpressorInstallIcon() {
	var p=$('plugin-install');
	if (p.style.display!='block') {
		p.style.display='block';
		$('plugin-ask').style.display='block';
		$('plugin-break').style.display='none';
	    $('plugin-process').style.display='none';
	    $('plugin-done').style.display='none';
		//p.style.left=mouseX-235;
		//p.style.top=mouseY+15;
	} else {
		p.style.display='none';
		iCookieSet('iInstall','0');
	}
}
function iImpressorInstallDo() {
	$('plugin-ask').style.display='none';
	$('plugin-process').style.display='block';
	$('plugin-break').style.display='none';
	if (iBrowser.Family=='mozilla') {
		document.iImpressorDY=window.outerHeight-window.innerHeight;
		$('plugin-frame').src='';
		$('plugin-frame').src='http://www.impressor.pl/install/Impressor';
		if (document.iImpressorTimer) window.clearInterval(document.iImpressorTimer);
		document.iImpressorTimer=window.setInterval('iImpressorInstallTimerMozilla()',1000);
		return;
	} else if (iBrowser.Family=='msie') {
		//document.iImpressorDY=document.body.clientHeight;
		//if (document.iImpressorTimer) window.clearInterval(document.iImpressorTimer);
		//document.iImpressorTimer=window.setInterval('iImpressorInstallTimerMsie()',1000);
	}
	// IE
	iCookieSet('iInstall',iCookieGet('iInstall')=='1'?'':'1');
	iImpressorGenerate('Install',1,1,{Scene:'Install',Command:'iImpressorCommand'},false,'plugin-object');
}
function iImpressorInstallTimerMozilla() {
	if (document.iImpressorDY!=window.outerHeight-window.innerHeight) {
		window.clearInterval(document.iImpressorTimer);
		$('plugin-break').style.display='block';
		$('plugin-process').style.display='none';
		$('plugin-frame').src='';
		$('plugin-frame').src='http://www.impressor.pl/install/Impressor';
	}
}
function iImpressorInstallTimerMsie() {
	if (document.iImpressorDY>document.body.clientHeight) {
		var div=$('plugin-install-help');
		if (div)
			div.style.display='block';
		else {
			div=document.createElement('div');
			div.id='plugin-install-help';
			div.innerHTML='to jest tekst pomocy';
			document.body.appendChild(div);
		}		
		document.iImpressorDY=document.body.clientHeight;
	} else if (document.iImpressorDY<document.body.clientHeight) {
		$('plugin-install-help').style.display='none';
		document.iImpressorDY=document.body.clientHeight;
	}
}
function iImpressorInstallDone() {
	$('plugin-ask').style.display='none';
	$('plugin-process').style.display='none';
	$('plugin-break').style.display='none';
	$('plugin-done').style.display='block';
	if (document.iImpressorTimer) window.clearInterval(document.iImpressorTimer);
	//window.setTimeout('window.location.reload();',3000);
	window.iImpressorCounter=5;
	document.iImpressorTimer=window.setInterval('iImpressorInstallDoneTimer()',1000);
}
function iImpressorInstallDoneTimer() {
	window.iImpressorCounter--;
	$('plugin-counter').innerHTML=window.iImpressorCounter;
	if (window.iImpressorCounter<0) {
		window.clearInterval(document.iImpressorTimer);
		window.location.reload();
	}
}
function iImpressorOnLoad() {
	var p=$('plugin-install');
	if ((p)&&(iImpressorInstallAllow)) {
		var u=gc(iImpressorInstallAllow);
		if (u) {
			u=iPosition(u[0]);
			//iDump(u);
			//p.style.left=u.X-220;
			//p.style.top=u.Y+15;
		}
		if (iImpressorInstallAllow && (iCookieGet('iInstall')!='0'))
			p.style.display='block';
	}
}
function iImpressorPanels() {
	var ok=iImpressorCheck();
	if (!ok) return(false);
	var obj=document.iImpressorObjects;
	if (!obj) return;
	for(var i=0;i<obj.length;i++)
		if (obj[i].Scene=='View') {
			iImpressorGenerate('ColorPalette',140,156,{
				Application:obj[i].Application,
				Scene:"ColorPalette",
				Transparent:"On",
				Command:"iImpressorCommand"
			},
			'<div class="draggable" id="ColorPalette" style="position:absolute;left:-139px;top:640px;width:140px;height:156px;"><div class="mover" id="ColorPaletteMover" style="position:absolute;left:81px;top:4px;width:40px;height:14px;cursor:pointer;background:#FFF;opacity:0;-moz-opacity:0;-webkit-opacity:0;filter:alpha(opacity=0);"></div><OBJECT/></div>');
			break;
		}
	for(var i=0;i<obj.length;i++)
		if (obj[i].Scene=='ViewList') {
			iImpressorGenerate('ColorKit',199,288,{
				Application:obj[i].Application,
				Scene:"ColorKit",
				Transparent:"On",
				Command:"iImpressorCommand"
			},
			'<div class="draggable" id="ColorKit" style="position:absolute;left:-198px;top:730px;width:199px;height:288px;display:block;"><div class="mover" id="ColorKitMover" style="position:absolute;left:4px;top:2px;width:174px;height:17px;cursor:pointer;background:#FFF;opacity:0;-moz-opacity:0;-webkit-opacity:0;filter:alpha(opacity=0);"></div><OBJECT/></div>');
			break;
		}	
}
function iImpressorViewList(id) {
	if (!iImpressorBrowser()) return;
	var d=g(id);
	var b=document.createElement("a");
	b.className='_tooltip color_icon';
	b.title='ten projekt możesz pokolorować';
	b.href=d.firstChild.href;
	d.insertBefore(b,d.firstChild);
}
