function preloadImage(url) {
  var img = new Image();
  img.src = url;
}

function setBodyColor(id,nr) {
  $(id).style.background = "#990000 url(/dziekanat/images/dbgd0" + nr + ".png) repeat-y";  
  return false;
}
function setColor(id,nr) {
  $(id).style.background = "#990000 url(/dziekanat/images/dbg0" + nr + ".png) repeat-y";  
  return false;
}

function getCookies() {
  var cookies = {}
  document.cookie.split(";").each(
     function(elem) {
       elems = elem.split("=");
       cookies[elems[0].strip()] = elems[1];
     })
  return cookies;
}

function toggleSheet(id) {
    var ul = $(id);         
    var cat = $(id + '_cat');

    if (ul.style.display == "none") {
       //cat.className = 'rolldown';
       cat.src = '/dziekanat/images/bullet_menu_on.gif';
       //ul.style.display = "block";
       Effect.BlindDown(ul);
       //document.cookie = id+'=show; path=/';
    } else {
       //cat.className = 'rollup';
       cat.src = '/dziekanat/images/bullet_menu_off.gif';
       Effect.BlindUp(ul);
       //ul.style.display = "none";
       //document.cookie = id+'=hide; path=/';
    }
}

function redirect_to(url) {
  return function() {
    window.location.href = url;
    return false;
  }
}

/*
 * Thickbox - One box to rule them all.
 * Oryginalny kod jest autorstwa: Cody Lindley (http://www.codylindley.com)
 * Under an Attribution, Share Alike License
 */

showImage = function(a, width) {
  var t = a.title;
  thickboxShowImage(t, a.rel, width);
  a.blur();
  return false;
};

showHTML = function(a, width, height) {
  var t = a.title || a.innerHTML || a.href;
  thickboxShowHtml(t, a.href, width, height);
  a.blur();
  return false;
};

var showMessageTimeoutId; //musimy by?? w stanie wy????czy?? ustawione w przysz??o??ci wy????czenie okienka, je??li 
                          //u??ytkownik postanowi?? wy????czy?? je r??cznie

showMessage = function(msg, width) {
  thickboxRemove(); //na wszelki wypadek
  if (!$('info-box')) {
    new Insertion.Bottom("body", "<div id='info-box' style='display: none;'></div>");
  } else {
    Element.hide('info-box');
  }
  Event.observe("info-box", "click", destroyMessage);
  var arrayPageSize = getPageSize();
  var thickboxTop = getPageScroll() + (arrayPageSize[3] / 6);
  var thickboxLeft = (arrayPageSize[0] - width) / 2;
  Element.setStyle("info-box", { top: thickboxTop + "px",
                                 left: thickboxLeft + "px",
                                 width: width + "px" });
  $('info-box').innerHTML = "<img src='/images/close_dlg.gif' alt='zamknij'/><div id='info-box-msg'>" + msg + "</div>";
  Element.show("info-box");
  new Effect.Highlight("info-box");
  showMessageTimeoutId = window.setTimeout("destroyMessage()", 10000);
}

function destroyMessage() {
  window.clearTimeout(showMessageTimeoutId);
  if ($('info-box')) {
    new Effect.Fade('info-box', { afterFinish: function() {if ($('info-box')) { Element.remove('info-box');}}});
  }
}

function thickboxPrepare() {
  setSelectBoxesVisibility("hidden");
  new Insertion.Bottom("body", "<div id='thickbox_overlay' style='display: none'></div>" + 
                               "<div id='thickbox_window' style='display: none;'></div>");
  setOverlayHeight();
  Event.observe("thickbox_overlay", "click", thickboxRemove);
  new Insertion.Bottom("body", "<div id='thickbox_load' style='display: none'>" + 
                               "<img src='/images/circle_animation.gif' /></div>");
  var arrayPageSize = getPageSize();
  var top = getPageScroll() + ((arrayPageSize[3] - 100) / 2);
  var left = (arrayPageSize[0] - 100) / 2;
  Element.setStyle("thickbox_load", { top: top + "px",
                                      left: left + "px"});
                                                                                                                                                
  Event.observe("thickbox_load", "click", thickboxRemove);
  Element.setStyle("thickbox_window", { width: arrayPageSize[1] + "px" });
  new Effect.Appear('thickbox_overlay', { duration: 0.2, from: 0.0, to: 0.6 });
  new Effect.Appear('thickbox_load', { duration: 0.2, from: 0.0, to: 1.0 });

}

function thickboxShowImage(caption, url, width) {
    thickboxPrepare();
    var imgPreloader = new Image();
    imgPreloader.onload = function() {
      TB_WIDTH = imgPreloader.width + 30;
      TB_HEIGHT = imgPreloader.height + 60;
      new Insertion.Bottom("thickbox_window", "<div id='thickbox_closeWindowImg'>" +
       //"<img src='/images/arrow_prev.gif' alt='poprzedni obrazek' style='margin: 0;border: 0;'/>" +
         "<img src='/images/close.gif' alt='zamknij' style='margin: 0;border: 0;'/>" +
       //"<img src='/images/arrow_next.gif' alt='nast??pny obrazek' style='margin: 0;border: 0;'/>" +
         "</div>" +
         "&nbsp;<img id='thickbox_Image' src='" + url + "' width='" + 
          imgPreloader.width + "' height='" + imgPreloader.height + 
         "' alt='" + caption + "'/>" + "<div id='thickbox_caption'>" + 
          caption + "</div><div id='thickbox_closeWindow'>" + 
         "<a href='#' id='thickbox_closeWindowButton'>zamknij</a></div>"); 

      Event.observe("thickbox_closeWindowButton", "click", thickboxRemove);
      Event.observe("thickbox_closeWindowImg", "click", thickboxRemove);
      Event.observe("thickbox_Image", "click", thickboxRemove);

      thickboxPosition(TB_WIDTH, TB_HEIGHT);
    }
    imgPreloader.src = url;
}

function thickboxShowHtml(url, width, height, text) {
    //thickboxPrepare(); - to wywo??uj?? jeszcze przed new Ajax.Request.
    TB_WIDTH = width + 30;
    TB_HEIGHT = height + 40;
    ajaxContentW = TB_WIDTH - 30;
    ajaxContentH = TB_HEIGHT - 45;
    if (!$('thickbox_ajaxContent')) {
      new Insertion.Bottom("thickbox_window", "<div id='thickbox_closeAjaxWindow'>" +
                        "<a href='#' onclick='thickboxRemove(); return false;' " +
                        "    id='thickbox_closeWindowButton'>zamknij okno</a></div>" + 
                        "<div id='thickbox_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");
    }
    $('thickbox_ajaxContent').innerHTML = text;
    thickboxPosition(TB_WIDTH, TB_HEIGHT);
}

//helper functions below
function thickboxRemove() {
    //Je??li znajdujemy si?? jeszcze w fazie ??adowania, to wy????cz wszystko od razu.
    if ($('thickbox_load')) {
       Element.remove('thickbox_load');
       Element.remove("thickbox_window");
       Element.remove("thickbox_overlay");
       setSelectBoxesVisibility("visible");
    } else if ($('thickbox_window')) {
       Effect.Fade("thickbox_window", { duration: 0.8, afterFinish: function() {
        Element.remove("thickbox_window");
        Element.remove("thickbox_overlay");
        setSelectBoxesVisibility("visible");
      }});
    }
    return false;
}

function showArticle(url) {
  if (!$('thickbox_overlay')) {
    thickboxPrepare(); 
  }
  if ($('thickbox_ajaxContent')) {
    $('thickbox_ajaxContent').innerHTML = '<p align="center"><img src="/images/loader.gif" alt="??aduj??..."/></p>';
  }

  new Ajax.Request(url, {asynchronous:true, evalScripts:true}); 
  return false;
}

function thickboxPosition(width, height) {
    var arrayPageSize = getPageSize();
    var thickboxTop = getPageScroll() + (arrayPageSize[3] / 15);
    var thickboxLeft = (arrayPageSize[0] - width) / 2;
    Element.setStyle("thickbox_window", { top: thickboxTop + "px", 
                                          left: thickboxLeft + "px",
                                          width: width + "px",
                                          height: height + "px"});
    Element.remove("thickbox_load");
    //Element.show('thickbox_window');
    Effect.Appear("thickbox_window");
}

function setOverlayHeight() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
                                                                                                                             
  if (window.innerHeight && window.scrollMaxY) {
     yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
     yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
     yScroll = document.body.offsetHeight;
  }
                                                                                                                             
  $('thickbox_overlay').style.height = yScroll +"px";
}


//
// Code from lightbox 2.02
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
  var yScroll;
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }
  return yScroll;
}

// -----------------------------------------------------------------------------------


// Code by Lokesh Dhakar - http://www.huddletogether.com
// 3/31/06
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	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;
}

// -----------------------------------------------------------------------------------


function setSelectBoxesVisibility(visibility){
  $A(document.getElementsByTagName("select")).each(function(select) {
    select.style.visibility = visibility;
  });
}
                                                                                                                             

