/**
 * PGweb 2006
*/

Page = function () {};

Page.showPicture = function (imgNode, src, w, h) 
{
   var ImgDiv = document.getElementById('page-orig-image');
   
   if (ImgDiv) {
      if (ImgDiv.parentNode) ImgDiv.parentNode.removeChild(ImgDiv);
      var Img = ImgDiv.firstChild;
   } else {
      ImgDiv = document.createElement('div');
      ImgDiv.id = 'page-orig-image';
      ImgDiv.style.position = 'fixed';
      if (document.all && !window.Opera) ImgDiv.style.position = 'absolute';
      ImgDiv.style.zIndex = '2';
      ImgDiv.style.left = '0';
      ImgDiv.style.textAlign = 'center';
      ImgDiv.style.width = '100%';
      
      
      ImgDiv.onclick = function () {this.parentNode.removeChild(this)};
      
      var Img = document.createElement('img');
      ImgDiv.appendChild(Img);
   };
   
   var vp = Page.getViewport();
   
   Img.src = '';
   Img.src = src;
   
   var pomerX = w/(vp.x-4);
   var pomerY = h/(vp.y-4);
   
   if (pomerX>1 || pomerY>1) {
      var pomerMax = pomerX > pomerY ? pomerX : pomerY;
      w = Math.round(w/pomerMax);
      h = Math.round(h/pomerMax);
   };
   Img.width = w;
   Img.height = h;
   
   var scrollTop = 0;
   if (document.all && !window.Opera) {
      scrollTop = document.body.scrollTop;
   };
   
   ImgDiv.style.top = ((vp.y/2)-(h/2)-2+scrollTop)+'px';
   Img.style.backgroundColor = 'white';
   
   Img.style.border = '2px solid black';
   
   
   document.body.appendChild(ImgDiv);
};

Page.getViewport = function () {
   var x,y;
   if (self.innerHeight) {
   	x = self.innerWidth;
   	y = self.innerHeight;
   } else if (document.documentElement && document.documentElement.clientHeight) {
   	x = document.documentElement.clientWidth;
   	y = document.documentElement.clientHeight;
   } else if (document.body) {
   	x = document.body.clientWidth;
   	y = document.body.clientHeight;
   };
   return {'x' : x, 'y' : y};
};

Page.regFileCombo = function (idCombo, elCombo, elAddButton, itemClassName, hiddenClassName) {
   if (!Page.FileCombo) Page.FileCombo = {};
   Page.FileCombo[idCombo]
   
   var els = elCombo.getElementsByTagName('*');
   var elHiddenBuffer = [];
   for (var i=0; i<els.length; i++) {
      if (Lib.containsClass(els[i], itemClassName) && Lib.containsClass(els[i], hiddenClassName)) {
         elHiddenBuffer[elHiddenBuffer.length] = els[i];
      };
   };
   
   elAddButton.onclick = function () {
      var itemEl = elHiddenBuffer.shift();
      Lib.removeClass(itemEl, hiddenClassName);
      if (elHiddenBuffer.length==0) elAddButton.parentNode.removeChild(elAddButton);
      return false;
   }
};

Page.showFlights = function (el, show) {
   var tbl=document.getElementsByTagName('table'); for (var i=0; i < tbl.length; i++) if (tbl[i].className=='flights') try {tbl[i].style.display = show ? 'table' : 'none';} catch (e) {tbl[i].style.display = show ? 'block' : 'none';};
   el.className = 'hide';
   document.getElementById(el.id=='show-flights' ? 'hide-flights' : 'show-flights').className = '';
   return false;
};

Page.showFlightsRanked = function (el, url) {
   
   el.className = 'for-flights wait';
   
   new Request.HTML({url : url}).addEvents({
		'success' : function (T, E, HTML, J) {
			el.className = 'for-flights-fill';
			el.innerHTML = HTML;
			el.onclick = null;
			FlightThumb();
		},
		'failure' : function () {
			el.className = 'for-flights';
		}
	}).get();
};


Page.createModalLayer = function (id)
{
   var el = document.getElementById(id);
   if (!el) return;
   var elLayer = document.createElement('div');
   
   var ls = elLayer.style;
   ls.width = '100%';
   ls.height = '1000%';
   ls.position = 'absolute';
   ls.zIndex = '10';
   ls.backgroundColor = 'gainsboro';
   ls.MozOpacity = '0.5';
   ls.opacity = '0.5';
   ls.textAlign = 'middle';
   
   var es = el.style;
   es.margin = '200px auto';
   es.position = 'absolute';
   es.zIndex = '11';
   
   document.body.insertBefore(elLayer, document.body.firstChild);
};


Page.validationPrompt = function (txt, self) {
   var out = prompt(txt);
   if (out==null) {
      return false;
   };
   if (out) {
      self.href += '&msg='+out;
   };
   return true;
};

Page.sDefense = function ($fEl) {
   var $fc = document.createElement('input');
   $fc.type = 'hidden';
   $fc.name = 'dfns';
   $fc.value = 'snfd';
   $fEl.appendChild($fc);
};

Page.visit_status = {};
Page.loadVisitStatus = function (ident) {
   var id_user = Page.readCookie('id_user_'+ident);
   if (!id_user) return;
	document.write('<script type="text/javascript" src="/js/visit.status.php?var=visit_status&ident='+ident+'&id_user='+id_user+'"></script>');
	/*
	var value = Page.readCookie('visit_status_'+ident);
   if (!value) return;
   eval("Page.visit_status[ident] ="+decodeURIComponent(value));
   */
};

Page.isFirstNew = true;
Page.viewVisitStatus = function (ident, id, timestamp, threadUrl) {

   if (Page.visit_status[ident]==null) return;
   var lastVisit = Page.visit_status[ident][id];
   var timeReset = Page.visit_status[ident]['timeReset'];
   if (timeReset && (!lastVisit || timeReset > lastVisit)) lastVisit = timeReset;
   if (!lastVisit || lastVisit < timestamp) {
      if (threadUrl) {
         document.write('<a class="visit-status-new" href="' + threadUrl + ('#thread-first-new') + '">NEW!</a>'); 
         if (Page.isFirstNew) Page.isFirstNew = false;
      } else {
         document.write('<span class="visit-status-new"'+(Page.isFirstNew ? ' id="thread-first-new"' : '')+'>NEW!</span>');
      };
   };
};

Page.updateThreadUrl = function (ident, id, timestamp, threadUrl) {
   if (Page.visit_status[ident]==null) return;
   var lastVisit = Page.visit_status[ident][id];
   if (!lastVisit ||lastVisit < timestamp) {
      if (threadUrl) {
         var elLink = document.links[document.links.length-1];
         elLink.href = threadUrl + (lastVisit ? '#thread-first-new' : '');
      };
   };
};

Page.highlightPostsByUser = function (username) {
   if (!username) {
      //jen pokud je v hash hightlight-user
      var hash = document.location.hash;
      if (hash.indexOf('highlight-user')!=1) return;
      username = hash.substring(16);
   }
   if (!username) return;
   
   var postEls = document.getElementsByTagName('*');
   for (var i=0; i<postEls.length; i++) {
      if (postEls[i].className.indexOf('post-user')<0 || postEls[i].title!=username) continue;
      postEls[i].className += ' highlight';
   };
};

Page.thumb_count = 0;
Page.processImage = function (src) {
    var elid = "discussion-thumb-"+Page.thumb_count;
    Page.thumb_count++;
    
    document.write('<div id="'+elid+'" class="discussion-thumb"></div>');
    
    var Imago = new Image();
    Imago.src = src;
    Imago.onload = function () {
      var longer = this.width>this.height ? this.width : this.height;
      var ratio = 150/longer;
      if (ratio > 1) ratio = 1;
      var newW = Math.round(this.width*ratio);
      var newH = Math.round(this.height*ratio);
      document.getElementById(elid).innerHTML = '<a  style="margin-top: '+((154-newH)/2)+'px" href="'+this.src+'" onclick="Page.showPicture(this, \''+this.src+'\', '+this.width+', '+this.height+'); return false;"><img src="'+this.src+'" width="'+newW+'" height="'+newH+'" /></a>';
    };
};
Page.pClear = function () {
   var divs = document.getElementsByTagName('div');
   for (var i=0; i<divs.length; i++) {
      if (divs[i].className.indexOf('content')==-1) continue;
      var ps = divs[i].getElementsByTagName('p');
      for(var j=0; j<ps.length; j++) {
	 if ((ps[j].firstChild && ps[j].firstChild.nodeValue!='') || (ps[j].lastChild && ps[j].lastChild.nodeValue!='')) continue;
	 ps[j].style.clear = 'left';
      };
   };

};

Page.setAFilterElements = function (dateModeId, dateId, dateToId, seasonFromId, seasonToId, valueModeId, minValDstId, minValPtsId) {
	Page.p = {
		dateModeEl : document.getElementById(dateModeId),
		dateEl : document.getElementById(dateId),
		dateToEl : document.getElementById(dateToId),
		seasonFromEl : document.getElementById(seasonFromId),
		seasonToEl : document.getElementById(seasonToId),
		valueModeEl : document.getElementById(valueModeId),
		minValDstEl : document.getElementById(minValDstId),
		minValPtsEl : document.getElementById(minValPtsId)
	};
	Page.adjustDateMode(Page.p.dateModeEl);
	Page.adjustValueMode(Page.p.valueModeEl);
	
	var els = Page.p.dateModeEl.form.elements;
	
	function checkDefault (sel) {
		sel.style.backgroundColor = sel.value=='' ? 'white' : '';
	};
	for (var i=0; i<els.length; i++) {
		if (els[i].nodeName.toLowerCase()!='select') continue;
		checkDefault(els[i]);
		if (typeof els[i].onchange=='function') continue;
		els[i].onchange = function (sel) {
			this.style.backgroundColor = this.value=='' ? 'white' : '';
		};
	};
};

Page.adjustDateMode = function (el) {
	Page.switchControl(Page.p.dateEl, el.value=='dmy' || el.value=='period');
	Page.switchControl(Page.p.dateToEl, el.value=='period');
	Page.switchControl(Page.p.seasonFromEl, el.value=='season');
	Page.switchControl(Page.p.seasonToEl, el.value=='season');
};
Page.adjustValueMode = function (el) {
	Page.switchControl(Page.p.minValDstEl, el.value=='dst');
	Page.switchControl(Page.p.minValPtsEl, el.value=='pts');
};
Page.switchControl = function (el, show) {
	el.disabled = show ? false : true;
	el.style.display = show ? '' : 'none';
};
Page.adjustDate = function (el, FromEl, ToEl) {
	is_period_enabled = Page.p.dateModeEl.value!='dmy';
	is_this_date_to = (el==ToEl);
	
	if (is_period_enabled) {
		if (FromEl.selectedIndex < ToEl.selectedIndex) {
			if (is_this_date_to) {
				FromEl.options[ToEl.selectedIndex].selected = true;
			} else {
				ToEl.options[FromEl.selectedIndex].selected = true;
			};
		};
	} else {
		ToEl.options[FromEl.selectedIndex].selected = true;
	};
	
	el.style.backgroundColor = el.value=='' ? 'white' : '';
};

Page.readCookie = function (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;
};

/** metodu volat PO container node **/
var TRANSLATE = {};
Page.prepareTranslate = function (containerNode) {
  var srcEls = containerNode.getElementsByTagName('*');
  for (var i=0; i<srcEls.length; i++) {
     if (srcEls[i].title) {
        TRANSLATE[srcEls[i].title] = srcEls[i].innerHTML;
     };
  };
};

/*eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+c.toString(a)+'\\b','g'),k[c])}}return p}('e.d=c(0){b 0=a.9(\'8\');0.7=\'6\';0.5=\'4\';0.3=\'1\';0.2(0)};',15,15,'f||appendChild|value|defense|name|hidden|type|input|createElement|document|var|function|sDefense|Page'.split('|')))*/
