/* 
function losuj() {
	style=new Array('zielony', 'dziwny', 'oldstyle', 'brazowy');
	var tryb=3;
	if(screen.width>1000)
	    tryb=4;
	var liczba=Math.floor(Math.random()*tryb);
	return style[liczba];
}
*/

/* STYLESWITCHER */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getActiveStylesheet();
setActiveStyleSheet(title);

/* Okienka z powiadomieniami */

function mesydzBox(text) {
        if(!document.createElement) return;
        m=document.getElementById('mesydzbox');
        if(!m){
                m=document.createElement('div');
                m.id='mesydzbox';
                m.style.zIndex=100;
                if(document.all&&!window.opera)
                    m.style.position='absolute';
                else
                    m.style.position='fixed';
		m.style.top=0;
		m.style.left=0;
		m.style.width='100%';
                m.style.padding='5px';
                m.style.borderBottom='1px solid #ff0000';
                m.style.backgroundColor='#ffaaaa';
                m.style.MozOpacity='0.9';
        }
        m.innerHTML=text;
        document.body.appendChild(m);
}

function magia() {
  function loadXMLDoc(url) {
     if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
     } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
  }
  function processReqChange() {
    if(req.readyState==4)
      if(req.status==200)
        czytajXML();
  }
  function czytajXML() {
    var text='<strong>Od momentu wejścia na tę stronę:</strong><ul>';
    var tree=req.responseXML.getElementsByTagName("matex")[0];
    var strefa=parseInt(tree.getElementsByTagName("strefa")[0].childNodes[0].nodeValue);
    var media=parseInt(tree.getElementsByTagName("media")[0].childNodes[0].nodeValue);
    var niusy=tree.getElementsByTagName("n");
    var niusyt=tree.getElementsByTagName("nt");
    var ln=niusy.length;
    var komentarze=tree.getElementsByTagName("kn");
    var komentarzet=tree.getElementsByTagName("knt");
    var lk=komentarze.length;
    if(media>0)
      text+='<li>W <a href="/strefa/media/">mediach</a> pojawiło się <strong>'+media+'</strong> nowych plików</li>';
    if(strefa>0)
      text+='<li>W <a href="/strefa/">strefie zamkniętej</a> pojawiło się <strong>'+strefa+'</strong> nowych wiadomości</li>';
    if(ln==1) {
      id=niusy[0].childNodes[0].nodeValue;
      tytul=niusyt[0].childNodes[0].nodeValue;
      text+='<li>Pojawił się nowy nius: <a href="/niusy/'+id+'/">'+tytul+'</a></li>';
    }
    if(ln>1) {
      text+='<li>Pojawiły się nowe niusy:';
      for(var i=0; i<ln; i++) {
        id=niusy[i].childNodes[0].nodeValue;
        tytul=niusyt[i].childNodes[0].nodeValue;
        text+=' <a href="/niusy/'+id+'/">'+tytul+'</a>';
        if(i<ln-1)
          text+=',';
      }
      text+='</li>';
    }
    if(lk==1) {
      id=komentarze[0].childNodes[0].nodeValue;
      tytul=komentarzet[0].childNodes[0].nodeValue;
      text+='<li>Pojawiły się komentarze do niusa: <a href="/niusy/'+id+'/">'+tytul+'</a></li>';
    }
    if(ln>1) {
      text+='<li>Pojawiły się komentarze do niusów:';
      for(var i=0; i<lk; i++) {
        id=komentarze[i].childNodes[0].nodeValue;
        tytul=komentarzet[i].childNodes[0].nodeValue;
        text+=' <a href="/niusy/'+id+'/">'+tytul+'</a>';
        if(i<lk-1)
          text+=',';
      }
      text+='</li>';
    }
    text=text+'</ul>';
    if(strefa+media+ln+lk>0)
      mesydzBox(text);
  }

  loadXMLDoc("/magia.php?czas=" + time);
}

function init() {
    if(typeof time != 'undefined')
    	setInterval('magia();', 120000);
}

/* Popupy */

function popup(v,t,e,p){
	if(!document.createElement) return;
	p=document.getElementById('popup');
	if(!p){
		p=document.createElement('div');
		p.id='popup';
		p.style.zIndex=100;
		if(document.all&&!window.opera)
		    p.style.position='absolute';
		else
		    p.style.position='fixed';
		p.style.padding='5px';
		p.style.border='1px dotted #000000';
		p.style.backgroundColor='#ffffff';
		p.style.MozOpacity='0.9';
	}
	p.move=function(e){
		e=e||event;
		p.style.left=e.clientX+5+"px";
		if(document.all&&!window.opera)
		    p.style.top=e.clientY+document.body.scrollTop+20+"px";
		else
		    p.style.top=e.clientY+20+"px";
	}
	p.hide=function(x){
		if(x=document.getElementById('popup')) document.body.removeChild(x)
	}
	p.move(e);
	p.innerHTML=t;
	document.body.appendChild(p);
	p.onmousedown=p.hide;
	v.onmouseout=p.hide;
}
