// www.guschu.cz/javascripts/general.js

// Detect language of page
var metas = document.getElementsByTagName("meta");
for (i=0; i<metas.length; i++) {
  //if (metas[i].getAttribute("http-equiv")=="Content-Language") {
    if (metas[i].getAttribute("content")=="cs") lang="cs";
    if (metas[i].getAttribute("content")=="en") lang="en";
    if (metas[i].getAttribute("content")=="de") lang="de";
  //}
}

// created by Peter-Paul Koch http://www.quirksmode.org/
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// created by Peter-Paul Koch http://www.quirksmode.org/
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// created by Honza Petr, admin@jaknaweb.com, www.jaknaweb.com
var sdiak = "áäčďéěíĺľňóô öŕšťúů üýřžÁÄČĎÉĚÍĹĽŇÓÔ ÖŔŠŤÚŮ ÜÝŘŽ";
var bdiak = "aacdeeillnoo orstuu uyrzAACDEEILLNOO ORSTUU UYRZ";
function bezdiak(txt) {
  var tx = "";
  for(p = 0; p < txt.length; p++) {
    if (sdiak.indexOf(txt.charAt(p)) != -1) {
      tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
    }
    else tx += txt.charAt(p);
  }
  return tx;
}


// Open external links in new window and add information about it to title
function externalLink(state) {
  var zapnout_vypnout_externallink = document.getElementById('zapnout_vypnout_externallink');
  if (!state) state=readCookie("extlink");
  if (!state) state="on";
  if (state=="on") {
    createCookie("extlink","on",DAYS);
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.className == "external_link") {
        anchor.target = "_blank";
        if (lang=="cs") anchor.title += "\n(stránka se otevře v novém okně)";
        if (lang=="en") anchor.title += "\n(page will be opened in new window)";
        if (lang=="de") anchor.title += "\n(page will be opened in new window)";
      }
    }
    if (zapnout_vypnout_externallink) zapnout_vypnout_externallink.innerHTML = '&nbsp;Tato funkce je nyní zapnuta, můžete ji <a class="move" href="javascript:externalLink(\'off\');" title="Vypnout otevírání externích odkazů v novém okně">vypnout</a>.';
  }
  if (state=="off") {
    createCookie("extlink","off",DAYS);
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.className == "external_link") {
        anchor.target = "";
        if (lang=="cs") anchor.title = anchor.title.replace("\n(stránka se otevře v novém okně)","");
        if (lang=="en") anchor.title = anchor.title.replace("\n(page will be opened in new window)","");
        if (lang=="de") anchor.title = anchor.title.replace("\n(page will be opened in new window)","");
      }
    }
    if (zapnout_vypnout_externallink) zapnout_vypnout_externallink.innerHTML = '&nbsp;Tato funkce je nyní vypnuta, můžete ji <a class="move" href="javascript:externalLink(\'on\');" title="Zapnout otevírání externích odkazů v novém okně">zapnout</a>.';
  }
}

// Add to Favorites
var browser = navigator.userAgent.toLowerCase();
var version = navigator.appVersion.toLowerCase();
var isIE = ((browser.indexOf("msie") != -1) && (browser.indexOf("opera") == -1) && (browser.indexOf("webtv") == -1));
//var isGecko = (browser.indexOf("gecko") != -1);
//var isOpera = (browser.indexOf("opera") != -1);
//var isFirefox = (browser.indexOf("firefox") != -1);
var ie = (version.indexOf('msie')>-1);
var win = (version.indexOf('windows')>-1);
var opera = (browser.indexOf('opera')>-1);

function writeAddFavorite() {
  if ((opera) && (win)) return false;
  else {
    if ((ie) && (win) && (lang=="cs")) document.write('\074a id="favorites" href="#" onclick="javascript:doAddFavorite(\'cs\');" title="Zobrazí dialog pro přidání stránky do Oblíbených položek"\076\074span\076Přidat k&nbsp;oblíbeným\074/span\076\074/a\076 \074span class="hidden"\076| \074/span\076');
    if ((ie) && (win) && (lang=="en")) document.write('\074a id="favorites" href="#" onclick="javascript:doAddFavorite(\'en\');" title="Shows dialog for addition page to Favorites"\076\074span\076Add to Favorites\074/span\076\074/a\076 \074span class="hidden"\076| \074/span\076');
    if ((ie) && (win) && (lang=="de")) document.write('\074a id="favorites" href="#" onclick="javascript:doAddFavorite(\'de\');" title="Shows dialog for addition page to Favorites"\076\074span\076Add to Favorites\074/span\076\074/a\076 \074span class="hidden"\076| \074/span\076');
  }
}

function doAddFavorite(lang) {
  if ((isIE) && (lang=="cs")) {
    window.external.addFavorite('http://www.guschu.cz/','Güschu s.r.o.');
    return false;
  }
  if ((isIE) && (lang=="en")) {
    window.external.addFavorite('http://www.guschu.cz/en/','Güschu s.r.o.');
    return false;
  }
  if ((isIE) && (lang=="de")) {
    window.external.addFavorite('http://www.guschu.cz/de/','Güschu s.r.o.');
    return false;
  }
  else
    return true;
}

// Skryje nebo zobrazí element
function show_hide_element(id) {
  var item = document.getElementById(id);
  if (item) {
    if (item.className == "hide") {
      item.className="show";
    }
    else {
      item.className="hide";
    }
  }
}

// OnMouseOver
function onmo() {
/*  address_converter(null,'onmo');*/
}

window.onload=function() {
  /*address_converter();*/
  externalLink();
  stlswtch(null,'onload'); // require stlswtch.js
}

window.onclose=function() {
  stlswtch(null,'onclose'); // require stlswtch.js
}

window.onunload=function() {
  stlswtch(null,'onunload'); // require stlswtch.js
}

/*
 * automatic logout counter
 * @author: Filip Hlouch
 */
function autoLogOutCounter() {
	var $logoutTimeInput = document.getElementById('autoLogoutTime');
	if( !$logoutTimeInput ) return;
	
	var $logoutTime = $logoutTimeInput.value;
	var $autologoutCounter = document.getElementById('autologoutCounter');
	
	var $currentTime   = Math.round(( new Date().getTime() ) / 1000);
	var $expireTime    = $logoutTime - $currentTime;
	var $expireMinutes = Math.floor($expireTime / 60);
	var $expireSeconds = $expireTime % 60;
	$autologoutCounter.textContent =  ( $expireMinutes ? $expireMinutes + ' min ' : $expireSeconds + ' sec' );
	
	if( $expireTime <= 0 ) {
		window.location = 'login.php';
	} else if( $expireTime <= 60 ) {
		var $answer = window.confirm( 'Již delší dobu nedošlo k Vašemu spojení se serverem.' + "\n" + 'Chcete zůstat nadále přihlášeni?' );
		if( $answer ) {
			var $myRequest = new Request( {
				 url: 'loginContinue.php'
				,onComplete: function(response) {
					var $newExpireTime = response.trim();
					if( !$newExpireTime ) window.location = 'login.php';
					document.getElementById('autoLogoutTime').value = $newExpireTime;
					}
				} );
			$myRequest.send();
		} else {
			window.location = 'login.php';
		}
	}
	
	window.setTimeout( 'autoLogOutCounter()', 500 );
}

// kontrola nacteni obsahu HTML
function CheckDocumentBody() {
	var $html_page = document.documentElement.innerHTML.toLowerCase();
	if($html_page.indexOf("</body>") > -1) {
		// uzaviraci tag </body> je soucasti obsahu dokumentu
		jsLoad();
	} else {
		// uzaviraci tag </body> neni soucasti obsahu dokumentu
		// -> kontrola dokumentu po pul vterine 
		window.setTimeout("CheckDocumentBody()",500);
	}
}

function jsLoad() { // spusteni funkci po nacteni obsahu HTML
	autoLogOutCounter();
}

document.onload = CheckDocumentBody(); // spusteni kontroly nacteni obsahu HTML
