// trieda `strazca dostupnosti` pre MojParfem.sk
// Created by All4net www.all4net.sk 
// Copyright 2009



function Guardian( cesta, key ) {
	this.verzia = "1.0.1";
	this.cesta = cesta;
	this.skey = key;	
		
}

Guardian.prototype.setTovar = function( tovar ) {
	this.tovar = tovar;
}

Guardian.prototype.getVerzia = function() {
	alert(this.verzia);
}

Guardian.prototype.vytvorDiv = function() {
	
	var objBody = document.getElementsByTagName("body").item(0);	
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','GuardOver');
	objOverlay.style.display = 'none';
		
	try {		
		objBody.appendChild(objOverlay);
	} catch(e) {
		alert('Volanie funkcie musi byt vnutri tela stranky !');
	}
	
	var objGuard = document.createElement("div");
	objGuard.setAttribute('id','guardian');
	objGuard.style.display = 'none';
	objBody.appendChild(objGuard);
	
}

Guardian.prototype.ukazOverlay = function(id_tovar) {
	
	document.onkeyup = this.keyReact;
	
	window.scrollTo(0,0);			
	
	var t = setTimeout( function(e){
		document.getElementById('GuardOver').style.opacity = "0.8";
	}, 100);
	
	this.setTovar(id_tovar);
	var divOver  = document.getElementById('GuardOver');
	var divGuard = document.getElementById('guardian');
	
	var rozmery = this.getPageSize();
			
	divOver.style.height =  rozmery[1]+"px";
	divOver.style.display = "block";
	// html na stred
	divGuard.style.left = rozmery[2]/2-250 + "px";
	divGuard.style.top = rozmery[3]/2-190 + "px";
	
	this.vratHTML();
	divGuard.style.display = "block";
			
}

	
Guardian.prototype.keyReactBCP = function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}
		key = String.fromCharCode(keycode).toLowerCase();
		
		if ( keyCode == escapeKey ) {
			gd.koniec();
		}
}

Guardian.prototype.testa = function() {
	alert('ahoj');
}
Guardian.prototype.keyReact = function(e) {
		var KeyID = (window.event) ? event.keyCode : e.keyCode;
		if (KeyID == 27) { // esc    
			gd.koniec();
		}
}

Guardian.prototype.koniec = function( delay ) {
	if ( delay == true ) {
		var _self = this;
		setTimeout("_self.koniec();", 1000);
	}	
	document.getElementById('GuardOver').style.opacity = "0.7";
	var t = setTimeout( function(e){
												document.getElementById('GuardOver').style.display = "none";
												document.getElementById('guardian').style.display = "none" 
											}, 100);
}

Guardian.prototype.vratHTML = function( objMail, objKod ) {
		if (xmlHttpGuard.readyState == 4 || xmlHttpGuard.readyState == 0)	{ //ak nie je objekt zaneprazdneny...			
			document.getElementById('guardian').innerHTML = '<img alt="loading..." class="loadBig" src="'+ this.cesta +'guardian/img/loadBig.gif" />';
			if ( objMail )
				var mail = "mail="+objMail.value+"&";
			if ( objKod )
				var kod = "kod="+objKod.value+"&";
			
			var key = "key="+this.skey;	
				
			xmlHttpGuard.open("GET", this.cesta+"guard.php?tovar="+this.tovar+"&"+ mail+kod, true); // spustenie stranky na serveri
			akcia = "guard";
			xmlHttpGuard.onreadystatechange = handleServerResponse;	// definuje metodu pre spracovanie odpovedi zo serveru
			xmlHttpGuard.send(null);	// posle ziadost na server      										
		} else {                                                                               
			setTimeout(function(e){ this.vratHTML() }, 1000); // ak je spojenie zaneprazdnene, tak skusim o sekundu neskor
		}
		
}

// Core code from - quirksmode.org
// Edit for Firefox by pHaez
Guardian.prototype.getPageSize = function() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
	
}

// funkcia pre antiSpam
Guardian.prototype.reloadAntiSpam = function() {
		datum = new Date();										
		document.getElementById('antiSpam').src = this.cesta+'antispam/generuj_antispam_obrazok.php?cas='+datum.getTime();		
}
