
URLDownload = 'http://www.kopf.com.br/jigsaw/kopfjigsawplugin-setup.exe';
PluginClass = 'KopfJigsawX.KopfJigsaX';
MozillaPluginName = 'Kopf Jigsaw Plug-in';

function CheckBrowser()
{
	// Verifica se o navegador é Internet Explorer e se está rodando no Windows
	if (navigator.userAgent && (navigator.appVersion.indexOf("Win") != -1))
		if  (navigator.userAgent.indexOf("MSIE")>=0)
			return 0;
		else if  (navigator.userAgent.indexOf("Firefox")>=0)
			return 1;
		else
			return -1;
	else
	  return -1;
}

function CheckPluginInstalled(browser)
{
    PluginInstalled = false;

	if (browser == 0)
	{
		// Check Ocx using VB Script
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); 
		document.write('on error resume next \n');
		document.write('PluginInstalled = IsObject(CreateObject("' + PluginClass + '"))\n');
		document.write('</SCR' + 'IPT\> \n');
	}
	else if (browser == 1)
	{
		if (navigator.plugins && navigator.plugins.length)
		{
			for (i = 0; i < navigator.plugins.length; i++)
			{
				PluginInstalled = (navigator.plugins[i].name == MozillaPluginName);
				if (PluginInstalled)
					i = navigator.plugins.length;
			}
		}
	}
	return PluginInstalled;
}

function PublishInvalidBrowser()
{
	document.write('<iframe src="unsupported_browser.php?lang=' + getURLParam('lang') + '" width="100%" height="500" frameborder="0"></iframe>');
}

function PublishPluginNotInstalled()
{
	document.write('<iframe src="install_plugin.php?lang=' + getURLParam('lang') + '" width="100%" height="1000" frameborder="0"></iframe>');
}

function PublishJigsaw(CheckPlugin, imgSrc, width, height)
{
	browser = CheckBrowser();
	if (browser > -1 && CheckPlugin)
	  PlugInInstalled = CheckPluginInstalled(browser)
	else
	  PlugInInstalled = true;

	if (browser == -1)
	{  PublishInvalidBrowser(); }
	else if (!PlugInInstalled)
	{  PublishPluginNotInstalled(); }
	else
	{
		if(document.all)
		{
			availW = document.documentElement.clientWidth;
			availH = document.documentElement.clientHeight;
		}
		else
		{
			availW = innerWidth; 
			availH = innerHeight;
		}
		if (width == null || width == 0)
			width = availW - 135;
		if (height == null || height == 0)
			height = availH  - 65;
		
		document.write('<!-- Avail (' + availW + ',' + availH + '), Game (' + width + ',' + height + ') -->');
		
		var lang = getURLParam('lang');
		
		if (browser == 0)
		{
			s = '<OBJECT classid="clsid:7F9EAF34-6A49-4B3F-91E1-135461C30D2A" ' + 
			  ' codebase="http://www.kopf.com.br/jigsaw/KopfJigsawX.ocx/KopfJigsawX.cab#version=1,0,0,0"' +
			  ' width="' + width + 
			  '" height="' + height + 
			  '" align="center' +
			  '" hspace="0' +
			  '" vspace="0' +
			  '">';
			if (imgSrc != '')
			  s = s + '<PARAM Name="ImageURL" Value="' + imgSrc + '"/>';
			if (lang != '')
				s = s + '<PARAM Name="Locale" Value="' + lang + '"/>';
			s = s + '</OBJECT>';
		}
		else
		{
			s = '<embed type="application/x-jigsaw" pluginspage="http://www.kopf.com.br/jigsaw" pluginurl="http://www.kopf.com.br/jigsaw/NPKopfJigsaw.xpi"' + 
				' width="' + width + 
			    '" height="' + height + '"';
			if (imgSrc != '')
				s = s + ' src="' + imgSrc + '"';
			if (lang != '')
				s = s + ' lang="' + lang + '"';
			s = s + '></embed>';
		}
		document.write(s);
	}
}

function fillIEProperties() {
    // fill missing IE properties
    if (!window.outerWidth) {
        window.outerWidth = document.body.clientWidth + 20
        window.outerHeight = document.body.clientHeight + 160
    }
    // fill missing IE4 properties
    if (!screen.availWidth) {
        screen.availWidth = 640
        screen.availHeight = 480
    }
}

function Maximize()
{
  fillIEProperties();
  if (window.outerWidth < screen.availWidth || window.outerHeight < screen.availHeight)
  { 
    window.resizeTo(screen.availWidth, screen.availHeight);
    window.moveTo(0,0);
  }
  window.focus();
}

function showHint(src, width, height)
{
  if (width == 0)
	width = 320;
  if (height == 0)
	height = 240;
	var lang = getURLParam('lang');
  var wnd = window.open('thumbnail.htm?src=' + src + '&width=' + width + '&height=' + Height + '&lang=' + lang, 'kopf_jigsaw_hint', 'width=' + (width + 40) + ', height=' + (height + 60));
  if (wnd)
  {
	wnd.focus();  
  }
}

function ToggleObjVisibility(objId)
{
	var el = document.getElementById(objId);
	el.style.display = (el.style.display != 'none') ? 'none' : '';
	el = document.getElementById(objId + 'Link');
	if (el)
		el.innerHTML = (el.innerHTML != '+') ? '+' : '-';
}

function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf('?') > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split('&');
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + '=') > -1 ){
        var aParam = aQueryString[iParam].split('=');
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

var window_force_focus = {
	init : function() {
	var opener = window.opener;
	var child_window = self;
	if (opener.addEventListener) {
		if (!opener || !child_window) return;
		if (!opener.blur || !child_window.focus) return;
		opener.addEventListener("focus", 
			function() {
				setTimeout(function(){
					opener.blur();
					child_window.focus();
				}, 0);
			}, false)
		} else if (opener.attachEvent) {
			opener.attachEvent("onfocus", function() {
			opener.blur();
			child_window.focus();
			})
		}
	}
}
