/*
* Desription: library.js
* Author: Igor Jamnicky
*/

/* adaptive layout */

var origWidth;

function upDateLayout() {
 var agt = navigator.userAgent;
 if (document.styleSheets && agt.indexOf("KHTML") == -1)  document.styleSheets[1].disabled = clientWidth()>980;
 origWidth = clientWidth(); 
} 
window.onresize = function () {if (origWidth != clientWidth()) window.location.href = window.location.href;}

/* catch js errors */

function chyba(a,b,c) {return true;}
window.onerror = chyba;

/* common functions*/

function gE(o) {return document.getElementById(o)}
function cE(o) {return document.createElement(o)}
function clientWidth() {var de = document.documentElement; return self.innerWidth || de && de.clientWidth || document.body.clientWidth}

/* object demo */

var demo = {
 timer : null,
 curr : null,
 elm : null,
 demoin : null,
 url : '',
 title : '',
 oldTitle : '',
 init : function () {
 demo.oldTitle = document.title;
 if (clientWidth()>980) 
 document.getElementsByTagName('h1')[0].getElementsByTagName('a')[0].innerHTML += '<span> BETA</span>';
 var links = gE('side').getElementsByTagName('a');
 if (links[0].id == "sample") 
 evnt.addEvent(links[0],"mousedown",function(e){demo.create(e);},false) ;
 else {
 for(var i=0; i<links.length; i++) {   
 evnt.addEvent(links[i],"mousedown",function(e){demo.create(e);},false);
 };
 if (document.cookie) {
 var cookies = document.cookie;
 var url = '';
 if (cookies.indexOf('URL=')!= -1) {
 var lastIndex = cookies.indexOf(';');
 url = cookies.substring(4,lastIndex);};
 if (url.length>5) {
 demo.load();
 demo.replace(url);
 if (url.indexOf("http")==-1) { 
 url = url.substring(3);
 url = url.split('/');
 url = url[0] + "/" + url[1];
 };
 for(var i=0; i<links.length; i++) 
 if (links[i].href.indexOf(url) != -1) demo.curr =links[i];
 if (gE('drag')) gE('drag').style.visibility = "visible";
 if (clientWidth()>980)  {
 menu.collapse();
 var index = 0;
 var dls = gE('side').getElementsByTagName('dl');
 for(var j=0; j<dls.length; j++) {
 var link = gE('side').getElementsByTagName('dl')[j].getElementsByTagName('a');    
 for(var i=0; i<link.length; i++)
 if (link[i] == demo.curr) index = j;};
 var dds = gE('side').getElementsByTagName('dl')[index].getElementsByTagName('dd');
 for (var i=0; i<dds.length; i++) dds[i].style.display = "block";
 demo.curr.className += " select";};
 demo.url = demo.curr.href;
 demo.title = demo.curr.title;
 document.title = demo.oldTitle + " -- " + demo.title;
 note.show();
 info.show();
 email.show();
 gE('url').innerHTML = '<a href="'+demo.url+'">'+demo.url+'</a>';
 gE('footer').className = "right";};
 };
  //gE('footer').innerHTML = '<a href="../rss" title="RSS kanál">RSS</a> <a href="../home/map.php" title="Mapa stránok">Mapa</a> <a href="javascript: if(window.print) window.print();" title="Tlač stránky">Tlačiť</a> <a href="http://fyzika.utc.sk/~jamnic/fyzikalnicek/contact.php" title="Kontakt na autora">Kontakt</a> <a href="javascript: window.scroll(0,0);">Nahor</a>';
 gE('footer').innerHTML += ' <a href="javascript: if(window.print) window.print();" title="Tlač stránky">Tlačiť</a> <a href="javascript: window.scroll(0,0);">Nahor</a>';
 if (clientWidth()>980) { 
 var mark = cE('div');
 mark.id = 'remark';
 mark.innerHTML = "Kliknutím na hlavičku rozbalíte zoznam ukážok.";
 gE('side').appendChild(mark); };
 };
 if (clientWidth()>980) { 
 var col = (gE('line').style.backgroundColor);
 if (col.substr(0,1)=="#") col = col.substr(1);
 else { var rgb = col.substr(4, col.length-5);
 rgb = rgb.split(','); 
 var hexStr = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
 col = hexStr[Math.floor(rgb[0]/16)]+hexStr[Math.floor(rgb[1]/16)]+hexStr[Math.floor(rgb[2]/16)];};
 var fern = new Image();
 fern.onload = function () {gE('side').appendChild(fern);}
 fern.src = "../img/fern.php?col="+col;};
 },
 create : function(e) {
 evnt.stopEvent(e);
 this.elm = evnt.getEventTarget(e);
 if (this.elm.nodeType == 3) this.elm = this.elm.parentNode;
 if (this.curr != this.elm) {
 if (this.timer) clearTimeout(this.timer) ;
 this.hide();
 this.url = this.elm.href;
 this.title = this.elm.title;
 document.title = this.oldTitle + " -- " + this.title;
 this.curr = this.elm;
 this.load();
 this.timer = setTimeout("demo.show();",250);
 };
 },
 load : function () {
 if (demo.demoin) { demo.demoin.parentNode.removeChild(demo.demoin); demo.demoin = null;}
 var html = '<iframe width="100%" height="100%" frameborder="0" scrolling="auto" name="frm" id="frm">';
 html += '<p>&nbsp;&nbsp;Povoľte prosím vnútorné rámce!<a id="endDemo" href="" title="Späť na pôvodnú stránku" onclick="return demo.remove();">X</a></p>';
 html += '</iframe><a id="endDemo" href="" title="Späť na pôvodnú stránku" onclick="return demo.remove();">X</a>';
 var demoin = cE('div');
 demoin.id = 'demoin';
 demoin.innerHTML = html;
 gE('demo').appendChild(demoin);
 demo.demoin = demoin;
 }, 
 show : function () {
 this.replace(this.url);
 if (gE('drag')) gE('drag').style.visibility = 'visible';
 if (clientWidth()>980) this.curr.className += " select";
 info.show();
 gE('demo').style.visibility = 'visible';
 gE('url').innerHTML = '<a href="'+demo.url+'">'+demo.url+'</a>';
 gE('footer').className = "right";
  },
 hide : function () {
 if (demo.curr != null) {demo.curr.className = demo.curr.className.replace("select",""); demo.curr = null;};
 note.hide();
 email.hide();
 if (gE('drag')) gE('drag').style.visibility = 'hidden';
 gE('demo').style.visibility = 'hidden';
 gE('info').style.visibility = 'hidden';
 gE('footer').style.visibility = 'hidden';
 },
 remove : function () {
 if (demo.demoin) { demo.demoin.parentNode.removeChild(demo.demoin); demo.demoin = null;}
 demo.hide();
 if (gE('drag')) gE('drag').style.visibility = 'hidden';
 info.remove();
 if (document.cookie) document.cookie = "URL='-'";
 document.title = demo.oldTitle;
 gE('footer').className = "";
 gE('url').innerHTML = '&nbsp;';
 return false;
 },
 replace : function (url) {
 if (gE('drag')) { 
 gE('drag').style.left = 717 + "px";
 gE('demo').style.width = 720 + "px";};
 var frm = gE('frm');
 if (frm.contentWindow) frm.contentWindow.document.location.replace(url); /* IE 5.5, IE 6, IE 7, Firefox, Opera */
 else if (frames) frames['frm'].location.replace(url); /* IE 5 */
 if (document.cookie) document.cookie = "URL="+url;
 return false;
 },
 addFavorite : function () { 
 if (window.external)  
 window.external.AddFavorite(demo.url, demo.title); 
 else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function') && window.confirm("Chcete uložiť záložku do bočného panelu?")) 
 window.sidebar.addPanel(demo.title,demo.url,'');
 else if (!window.opera) window.alert('Vaša vybraná ukážka môže byť pridaná k záložkám cez menu\n kliknutím pravého tlačidla myši nad plochou rámca ukážky.'); 
 return false; 
 },
 print : function (drg) {
 var drag = (drg==1) ? '<div id="drag" style="left: 717px; top: 0;">&nbsp;</div>' : '';
 document.write('<div id="demo"></div>'+drag+'<div id="note"><div id="notein"></div></div>');
 } 
}

/* object note */

var note = {
 init : function () {
 if (gE('note')) {
 evnt.addEvent(gE('note'),'mouseover',function(){gE('notein').className="hover";},false);
 evnt.addEvent(gE('note'),'mouseout',function(){gE('notein').className="";},false);
 }
 },
 show : function () {
 var html = 'Želáte si vrátiť sa <a href="" onclick="return demo.replace(demo.url)">k pôvodnej ukážke</a>?';
 if (gE('drag')) html += ' Rozmery okna môžete zmeniť metódou ťahaj a pusť pravej strany rámčeka okna pomocou myši.';
 html += ' Ukážku môžete zavrieť kliknutím na ikonku v pravom rohu okna.';
 gE('notein').innerHTML = html;
 gE('note').style.display = 'block';
 },
 hide : function () {
 gE('notein').innerHTML="";
 gE('note').style.display = 'none';
 }
}

/* object info */

var info = {
 loader : null,
 init : function () {
 if (gE('infoin')) {
 evnt.addEvent(gE('info'),'mouseover',function(){gE('infoin').className="hover";},false);
 evnt.addEvent(gE('info'),'mouseout',function(){gE('infoin').className="";},false);
 }
 },
 load : function (url) {
 var trg = gE('infoin');
 while (trg.hasChildNodes()) trg.removeChild(trg.childNodes[0]);
 this.loader = cE('script');
 this.loader.type = "text/javascript";
 this.loader.src = url;
 try {trg.appendChild(this.loader);}
 catch(e) {window.alert("Váš prehliadač neumožňuje zobraziť informáciu o ukážke");}
 },
 show : function () {
 var url = demo.url+'/info.php?js=true';
 info.load(url);
 gE('info').style.visibility = 'visible';
 note.show(); 
 setTimeout("email.show();",200);
 setTimeout("gE('footer').style.visibility = 'visible';",300);
 },
 remove : function () {
 var url1 = 'info.php?js=true';
 info.load(url1);
 setTimeout("gE('info').style.visibility = 'visible';",200);
 setTimeout("gE('footer').style.visibility = 'visible';",300);
 }
}

/* object email */

var email = {
 name : '',
 address : '',
 show : function () {
 if (gE('recommend')) {
 var html = '<form action="" onsubmit="return email.send();"><fieldset><legend>Odporúčiť ukážku</legend><label for="meno">Vaše meno:</label> <input type="text" size="20" id="meno" name="meno" value="" />';
 html += ' <label for="komentar">Adresa známeho:</label> <input type="text" size="20" id="adresa" name="adresa" value="@" /> <input type="submit" class="submit" value="Poslať" /></fieldset></form>';
 gE('recommend').innerHTML = html;
 gE('recommend').style.display = "block";
 };
 },
 hide : function () {
 if (gE('recommend')) {
 gE('recommend').innerHTML = "";
 gE('recommend').style.display = "none";
 };
 },
 send : function () {
 this.name = gE('meno').value;
 this.address = gE('adresa').value;
 if (this.name == "") {
 alert("Prosím vyplniť Vaše meno");
 gE('meno').focus();}
 else if (this.address == "@") {
 alert("Prosím vyplniť adresu Vašho známeho");
 gE('adresa').focus();}
 else if (this.address.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/g,"")==null) {
 alert("Nesprávne vyplnená adresa Vašho známeho");
 gE('adresa').focus();}
 else { 
 demo.replace('../comments/mail.php?meno='+escape(this.name)+'&adresa='+this.address+'&url='+demo.url);
 if (document.cookie) document.cookie = 'URL='+demo.url;
 gE('meno').value = "";
 gE('adresa').value = "@";
 window.scroll(0,0);
 };
 return false;
 }
}

/* object drag&drop */

var drag = {
 start : function () {
// if(gE('drag')) evnt.addEvent(gE('drag'),"mousedown",function(e) {drag.down(e);},false);
 if(gE('drag')) gE('drag').onmousedown = function(e) {drag.down(e);};
 },
 down : function (e) {
 this.dragobj = gE('drag');
 this.dragobj.zIndex = "100";
 this.dragobj.clickX = evnt.getEvent(e).clientX;
 this.dragobj.left = parseInt(this.dragobj.style.left);
 evnt.addEvent(document, 'mousemove', drag.move, true); 
 evnt.addEvent(document, 'mouseup', drag.up, true);
 return evnt.stopEvent(e);
 },
 move : function (e) {
 var tmp = drag.dragobj.left + evnt.getEvent(e).clientX - drag.dragobj.clickX;
 if (tmp > 717) tmp = 717; 
 if (tmp < 250) tmp = 250; 
 drag.dragobj.style.left = tmp +"px";
 gE('demo').style.width = tmp + 3 + "px";
 return evnt.stopEvent(e);
 },
 up : function (e) {
 evnt.removeEvent(document,'mousemove', drag.move, true); 
 evnt.removeEvent(document,'mouseup', drag.up, true); 
 return evnt.stopEvent(e);
 }
}

/* object menu */

var menu = {
 init : function () {
 var links = gE('side').getElementsByTagName('a');
 for(var i=0; i<links.length; i++) {
 links[i].className += "mode";
 links[i].onfocus = function(){this.blur();};
 links[i].onclick = function(){return false;};
 };
 if (clientWidth()<980) return;
 menu.collapse();
 var dds = gE('side').getElementsByTagName('dl')[0].getElementsByTagName('dd');
 for (var i=0; i<dds.length; i++) dds[i].style.display = "block";
 var dts = gE('side').getElementsByTagName('dt');
 for(var i=0; i<dts.length; i++) {
 dts[i].onclick = function(){menu.show(this)};
 dts[i].onfocus = function(){this.blur();};
 };
 },
 show : function (elm) {
 demo.remove();
 menu.collapse();
 var dds = elm.parentNode.getElementsByTagName('dd');
 for (var i=0; i<dds.length; i++) dds[i].style.display = "block";
 },
 collapse : function () {
 var dds = gE('side').getElementsByTagName('dd');
 for (var i=0; i<dds.length; i++) dds[i].style.display = "none";
 } 
}

/* object event */

var evnt = {
 getEvent : function(e) {
 e = arguments[0];
 e = e ? e : window.event;
 return e;
 },
 getEventTarget : function(e) {
 if (!this.getEvent(e).target) {
 this.getEvent(e).target = this.getEvent(e).srcElement;}
 return this.getEvent(e).target;
 },
 addEvent : function (elm,evType,func,set) {  
 if (elm.addEventListener) {elm.addEventListener(evType,func,set)}
 else if (elm.attachEvent) elm.attachEvent("on"+evType,func);
 },
 removeEvent : function (elm,evType,func,set) {
 if (elm.addEventListener) {elm.removeEventListener(evType,func,set)}
 else if (elm.attachEvent) return elm.detachEvent("on"+evType,func);
 },
 stopEvent : function (e) {
 if (this.getEvent(e).preventDefault) {
 this.getEvent(e).preventDefault();
 this.getEvent(e).stopPropagation();
 } else {
 this.getEvent(e).returnValue = false;
 this.getEvent(e).cancelBubble = true; 
 }
 return false;
 }
}

/* object modalbox */

var modalbox = {
 show : function (url) {
  var modal = document.getElementById("modalbox");
  var overlay = document.getElementById("overlay");
  overlay.style.display = "block";
  modal.style.display = "block";
  if (overlay.style.opacity != null) overlay.style.opacity = "0.6";
  else if (overlay.style.filter != null) overlay.style.filter = "alpha(opacity=60)";
  var html = '<a href="javascript:modalbox.hide()"><img src="../cssjs/escape.png" title="Späť" alt="X" /></a>';
  if (window.opera) {
  html += '<object type="text/html" data="'+url+'" width="600" height="380">';
  html += '<p>Povoľte prosím vnútorné rámce</p>';
  html += '<\/object';
  } else {
  html += '<iframe src="'+url+'" width="600" height="380" frameborder="0" scrolling="auto"><\/iframe>';
  } 
  var dlg = document.createElement("div");
  dlg.id = "dlg";
  dlg.innerHTML = html;
  modal.appendChild(dlg);
  if (document.all && !window.opera) window.scroll(0,0);
  return false;
 },
 hide : function () {
  var modal = document.getElementById("modalbox"); 
  while (modal.hasChildNodes()) modal.removeChild(modal.childNodes[0]);
  modal.style.display = "none";
  document.getElementById("overlay").style.display = "none";
 }
}

/* add events */

evnt.addEvent(window,'load',drag.start,false);
evnt.addEvent(window,'load',info.init,false);
evnt.addEvent(window,'load',note.init,false);


