$( function() {
   //$('.menu .kierunek > div').not('.sel').hide();

   initLightbox();

	$('span.epoczta').each( function() { $(this).after("<a href='mailto:"+$(this).text()+"'>"+$(this).text()+"</a>").remove(); } );
});
/*
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
*/
var loadingImage = '/rekrutacja/gfx/loading.gif';    
var closeButton = '/rekrutacja/gfx/close.gif';    
d=document;

function getPageScroll(){

   var yScroll;

   if (self.pageYOffset) {
      yScroll = self.pageYOffset;
   } else if (d.documentElement && d.documentElement.scrollTop){    // Explorer 6 Strict
      yScroll = d.documentElement.scrollTop;
   } else if (d.body) {// all other Explorers
      yScroll = d.body.scrollTop;
   };

   arrayPageScroll = new Array('',yScroll);
   return arrayPageScroll;
};

//
// 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 = d.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
   } else if (d.body.scrollHeight > d.body.offsetHeight){ // all but Explorer Mac
      xScroll = d.body.scrollWidth;
      yScroll = d.body.scrollHeight;
   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = d.body.offsetWidth;
      yScroll = d.body.offsetHeight;
   };
   
   var windowWidth, windowHeight;
   if (self.innerHeight) { // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
   } else if (d.documentElement && d.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = d.documentElement.clientWidth;
      windowHeight = d.documentElement.clientHeight;
   } else if (d.body) { // other Explorers
      windowWidth = d.body.clientWidth;
      windowHeight = d.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;
};


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
   var now = new Date();
   var exitTime = now.getTime() + numberMillis;
   while (true) {
      now = new Date();
      if (now.getTime() > exitTime)
         return;
   };
};

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
   // prep objects
   var objOverlay = d.getElementById('overlay');
   var objLightbox = d.getElementById('lightbox');
   var objCaption = d.getElementById('lightboxCaption');
   var objImage = d.getElementById('lightboxImage');
   var objLoadingImage = d.getElementById('loadingImage');
   var objLightboxDetails = d.getElementById('lightboxDetails');
   
   var arrayPageSize = getPageSize();
   var arrayPageScroll = getPageScroll();

   // center loadingImage if it exists
   if (objLoadingImage) {
      objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
      objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
      objLoadingImage.style.display = 'block';
   };

   // set height of Overlay to take up whole page and show
   objOverlay.style.height = (arrayPageSize[1] + 'px');
   objOverlay.style.display = 'block';

   // preload image
   imgPreload = new Image();

   imgPreload.onload=function(){
      objImage.src = objLink.href;

      // center lightbox and make sure that the top and left values are not negative
      // and the image placed outside the viewport
      var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
      var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
      
      objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
      objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


      objLightboxDetails.style.width = imgPreload.width + 'px';
      
      if(objLink.getAttribute('title')){
         objCaption.style.display = 'block';
         objCaption.style.width = imgPreload.width + 'px';
         objCaption.innerHTML = objLink.getAttribute('title');
      } else {
         objCaption.style.display = 'none';
      };
      
      // A small pause between the image loading and displaying is required with IE,
      // this prevents the previous image displaying for a short burst causing flicker.
      if (navigator.appVersion.indexOf("MSIE")!=-1){
         pause(250);
      }; 

      if (objLoadingImage) {  objLoadingImage.style.display = 'none'; };

      // Hide select boxes as they will 'peek' through the image in IE
      $('select').css('visibility','hidden');
      //selects = d.getElementsByTagName("select");
      //  for (i = 0; i != selects.length; i++) {
      //          selects[i].style.visibility = "hidden";
      //  };

   
      objLightbox.style.display = 'block';

      // After image is loaded, update the overlay height as the new image might have
      // increased the overall page height.
      arrayPageSize = getPageSize();
      objOverlay.style.height = (arrayPageSize[1] + 'px');
      
      return false;
   };

   imgPreload.src = objLink.href;
   
};

//
// hideLightbox()
//
function hideLightbox()
{
   // get objects
   objOverlay = d.getElementById('overlay');
   objLightbox = d.getElementById('lightbox');

   // hide lightbox and overlay
   objOverlay.style.display = 'none';
   objLightbox.style.display = 'none';

   // make select boxes visible
   $('select').css('visibility','visible');
   //selects = d.getElementsByTagName("select");
   // for (i = 0; i != selects.length; i++) {
   //   selects[i].style.visibility = "visible";
   //};
};

//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{
   
 //  if (!d.getElementsByTagName) return;

   $('a[rel=lightbox]').bind('click', function() {showLightbox(this); return false;} );

   //var anchors = d.getElementsByTagName("a");

   // loop through all anchor tags
   //for (var i=0; i<anchors.length; i++){
   //   var anchor = anchors[i];
//
//      if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
//         anchor.onclick = function () {showLightbox(this); return false;};
//      };
//   };

   var objBody = d.getElementsByTagName("body").item(0);
   
   // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
   var objOverlay = d.createElement("div");
   objOverlay.setAttribute('id','overlay');
   objOverlay.onclick = function () {hideLightbox(); return false;};
   objOverlay.style.display = 'none';
   objOverlay.style.position = 'absolute';
   objOverlay.style.top = '0';
   objOverlay.style.left = '0';
   objOverlay.style.zIndex = '90';
   objOverlay.style.width = '100%';
   objBody.insertBefore(objOverlay, objBody.firstChild);
   
   var arrayPageSize = getPageSize();
   var arrayPageScroll = getPageScroll();

   // preload and create loader image
   var imgPreloader = new Image();
   
   // if loader image found, create link to hide lightbox and create loadingimage
   imgPreloader.onload=function(){

      var objLoadingImageLink = d.createElement("a");
      objLoadingImageLink.setAttribute('href','#');
      objLoadingImageLink.onclick = function () {hideLightbox(); return false;};
      objOverlay.appendChild(objLoadingImageLink);
      
      var objLoadingImage = d.createElement("img");
      objLoadingImage.src = loadingImage;
      objLoadingImage.setAttribute('id','loadingImage');
      objLoadingImage.style.position = 'absolute';
      objLoadingImage.style.zIndex = '150';
      objLoadingImageLink.appendChild(objLoadingImage);

      imgPreloader.onload=function(){};   // clear onLoad, as IE will flip out w/animated gifs

      return false;
   };

   imgPreloader.src = loadingImage;

   // create lightbox div, same note about styles as above
   var objLightbox = d.createElement("div");
   objLightbox.setAttribute('id','lightbox');
   objLightbox.style.display = 'none';
   objLightbox.style.position = 'absolute';
   objLightbox.style.zIndex = '100';   
   objBody.insertBefore(objLightbox, objOverlay.nextSibling);
   
   // create link
   var objLink = d.createElement("a");
   objLink.setAttribute('href','#');
   //objLink.setAttribute('title','Zamknij/Close');
   objLink.onclick = function () {hideLightbox(); return false;};
   objLightbox.appendChild(objLink);

   // preload and create close button image
   var imgPreloadCloseButton = new Image();

   // if close button image found, 
   imgPreloadCloseButton.onload=function(){

      var objCloseButton = d.createElement("img");
      objCloseButton.src = closeButton;
      objCloseButton.setAttribute('id','closeButton');
      objCloseButton.style.position = 'absolute';
      objCloseButton.style.zIndex = '200';
      objLink.appendChild(objCloseButton);

      return false;
   };

   imgPreloadCloseButton.src = closeButton;

   // create image
   var objImage = d.createElement("img");
   objImage.setAttribute('id','lightboxImage');
   objLink.appendChild(objImage);
   
   // create details div, a container for the caption and keyboard message
   var objLightboxDetails = d.createElement("div");
   objLightboxDetails.setAttribute('id','lightboxDetails');
   objLightbox.appendChild(objLightboxDetails);

   // create caption
   var objCaption = d.createElement("div");
   objCaption.setAttribute('id','lightboxCaption');
   objCaption.style.display = 'none';
   objLightboxDetails.appendChild(objCaption);

   // create keyboard message
   //var objKeyboardMsg = d.createElement("div");
   //objKeyboardMsg.setAttribute('id','keyboardMsg');
   //objKeyboardMsg.innerHTML = 'press <a href="#" onclick="hideLightbox(); return false;"><kbd>x</kbd></a> to close';
   //objLightboxDetails.appendChild(objKeyboardMsg);


};

/*
Written by Steve Tucker, 2006, http://www.stevetucker.co.uk
Full documentation can be found at http://www.stevetucker.co.uk/page-innerxhtml.php
Released under the Creative Commons Attribution-Share Alike 3.0  License, http://creativecommons.org/licenses/by-sa/3.0/

Change Log
----------
15/10/2006	v0.3	innerXHTML official release.
21/03/2007	v0.4	1. Third argument $appendage added (Steve Tucker & Stef Dawson, www.stefdawson.com)
			2. $source argument accepts string ID (Stef Dawson)
			3. IE6 'on' functions work (Stef Dawson & Steve Tucker)
*/
innerXHTML = function($source,$string,$appendage) {
	// (v0.4) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk
	if (typeof($source) == 'string') $source = document.getElementById($source);
	if (!($source.nodeType == 1)) return false;
	var $children = $source.childNodes;
	var $xhtml = '';
	if (!$string) {
		for (var $i=0; $i<$children.length; $i++) {
			if ($children[$i].nodeType == 3) {
				var $text_content = $children[$i].nodeValue;
				$text_content = $text_content.replace(/</g,'&lt;');
				$text_content = $text_content.replace(/>/g,'&gt;');
				$xhtml += $text_content;
			}
			else if ($children[$i].nodeType == 8) {
				$xhtml += '<!--'+$children[$i].nodeValue+'-->';
			}
			else {
				$xhtml += '<'+$children[$i].nodeName.toLowerCase();
				var $attributes = $children[$i].attributes;
 				for (var $j=0; $j<$attributes.length; $j++) {
					var $attName = $attributes[$j].nodeName.toLowerCase();
					var $attValue = $attributes[$j].nodeValue;
					if ($attName == 'style' && $children[$i].style.cssText) {
						$xhtml += ' style="'+$children[$i].style.cssText.toLowerCase()+'"';
					}
					else if ($attValue && $attName != 'contenteditable') {
						$xhtml += ' '+$attName+'="'+$attValue+'"';
					}
				}
				$xhtml += '>'+innerXHTML($children[$i]);
				$xhtml += '</'+$children[$i].nodeName.toLowerCase()+'>';
			}
		}
	}
	else {
		if (!$appendage) {
			while ($children.length>0) {
				$source.removeChild($children[0]);
			}
			$appendage = false;
		}
		$xhtml = $string;
		while ($string) {
			var $returned = translateXHTML($string);
			var $elements = $returned[0];
			$string = $returned[1];
			if ($elements) {
				if (typeof($appendage) == 'string') $appendage = document.getElementById($appendage);
				if (!($appendage.nodeType == 1)) $source.appendChild($elements);
				else $source.insertBefore($elements,$appendage);
			}
		}
	}
	return $xhtml;
}
function translateXHTML($string) {
	var $match = /^<\/[a-z0-9]{1,}>/i.test($string);
	if ($match) {
		var $return = Array;
		$return[0] = false;
		$return[1] = $string.replace(/^<\/[a-z0-9]{1,}>/i,'');
		return $return;
	}
	$match = /^<[a-z]{1,}/i.test($string);
	if ($match) {
		$string = $string.replace(/^</,'');
		var $element = $string.match(/[a-z0-9]{1,}/i);
		if ($element) {
			var $new_element = document.createElement($element[0]);
			$string = $string.replace(/[a-z0-9]{1,}/i,'');
			var $attribute = true;
			while ($attribute) {
				$string = $string.replace(/^\s{1,}/,'');
				$attribute = $string.match(/^[a-z1-9_-]{1,}="[^"]{0,}"/i);
				if ($attribute) {
					$attribute = $attribute[0];
					$string = $string.replace(/^[a-z1-9_-]{1,}="[^"]{0,}"/i,'');
					var $attName = $attribute.match(/^[a-z1-9_-]{1,}/i);
					$attribute = $attribute.replace(/^[a-z1-9_-]{1,}="/i,'');
					$attribute = $attribute.replace(/;{0,1}"$/,'');
					if ($attribute) {
						var $attValue = $attribute;
						if ($attName == 'value') $new_element.value = $attValue;
						else if ($attName == 'class') $new_element.className = $attValue;
						else if ($attName == 'style') {
							var $style = $attValue.split(';');
							for (var $i=0; $i<$style.length; $i++) {
								var $this_style = $style[$i].split(':');
								$this_style[0] = $this_style[0].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
								$this_style[1] = $this_style[1].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
								if (/-{1,}/g.test($this_style[0])) {
									var $this_style_words = $this_style[0].split(/-/g);
									$this_style[0] = '';
									for (var $j=0; $j<$this_style_words.length; $j++) {
										if ($j==0) {
											$this_style[0] = $this_style_words[0];
											continue;
										}
										var $first_letter = $this_style_words[$j].toUpperCase().match(/^[a-z]{1,1}/i);
										$this_style[0] += $first_letter+$this_style_words[$j].replace(/^[a-z]{1,1}/,'');
									}
								}
								$new_element.style[$this_style[0]] = $this_style[1];
							}
						}
						else if (/^on/.test($attName)) $new_element[$attName] = function() { eval($attValue) };
						else $new_element.setAttribute($attName,$attValue);
					}
					else $attribute = true;
				}
			}
			$match = /^>/.test($string);
			if ($match) {
				$string = $string.replace(/^>/,'');
				var $child = true;
				while ($child) {
					var $returned = translateXHTML($string,false);
					$child = $returned[0];
					if ($child) $new_element.appendChild($child);
					$string = $returned[1];
				}
			}
			$string = $string.replace(/^\/>/,'');
		}
	}
	$match = /^[^<>]{1,}/i.test($string);
	if ($match && !$new_element) {
		var $text_content = $string.match(/^[^<>]{1,}/i)[0];
		$text_content = $text_content.replace(/&lt;/g,'<');
		$text_content = $text_content.replace(/&gt;/g,'>');
		var $new_element = document.createTextNode($text_content);
		$string = $string.replace(/^[^<>]{1,}/i,'');
	}
	$match = /^<!--[^<>]{1,}-->/i.test($string);
	if ($match && !$new_element) {
		if (document.createComment) {
			$string = $string.replace(/^<!--/i,'');
			var $text_content = $string.match(/^[^<>]{0,}-->{1,}/i);
			$text_content = $text_content[0].replace(/-->{1,1}$/,'');			
			var $new_element = document.createComment($text_content);
			$string = $string.replace(/^[^<>]{1,}-->/i,'');
		}
		else $string = $string.replace(/^<!--[^<>]{1,}-->/i,'');
	}
	var $return = Array;
	$return[0] = $new_element;
	$return[1] = $string;
	return $return;
}
