//Test to see whether the DOM is ready,replacement of JQUERY's $(document).ready(function(){...})
(function(){

    var DomReady = window.DomReady = {};

	// Everything that has to do with properly supporting our document ready event. Brought over from the most awesome jQuery. 

    var userAgent = navigator.userAgent.toLowerCase();

    // Figure out what browser is being used
    var browser = {
    	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
    	safari: /webkit/.test(userAgent),
    	opera: /opera/.test(userAgent),
    	msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),
    	mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
    };    

	var readyBound = false;	
	var isReady = false;
	var readyList = [];

	// Handle when the DOM is ready
	function domReady() {
		// Make sure that the DOM is not already loaded
		if(!isReady) {
			// Remember that the DOM is ready
			isReady = true;
        
	        if(readyList) {
	            for(var fn = 0; fn < readyList.length; fn++) {
	                readyList[fn].call(window, []);
	            }
            
	            readyList = [];
	        }
		}
	};

	// From Simon Willison. A safe way to fire onload w/o screwing up everyone else.
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
	    window.onload = func;
	  } else {
	    window.onload = function() {
	      if (oldonload) {
	        oldonload();
	      }
	      func();
	    }
	  }
	};

	// does the heavy work of working through the browsers idiosyncracies (let's call them that) to hook onload.
	function bindReady() {
		if(readyBound) {
		    return;
	    }
	
		readyBound = true;

		// Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
		if (document.addEventListener && !browser.opera) {
			// Use the handy event callback
			document.addEventListener("DOMContentLoaded", domReady, false);
		}

		// If IE is used and is not in a frame
		// Continually check to see if the document is ready
		if (browser.msie && window == top) (function(){
			if (isReady) return;
			try {
				// If IE is used, use the trick by Diego Perini
				// http://javascript.nwbox.com/IEContentLoaded/
				document.documentElement.doScroll("left");
			} catch(error) {
				setTimeout(arguments.callee, 0);
				return;
			}
			// and execute any waiting functions
		    domReady();
		})();

		if(browser.opera) {
			document.addEventListener( "DOMContentLoaded", function () {
				if (isReady) return;
				for (var i = 0; i < document.styleSheets.length; i++)
					if (document.styleSheets[i].disabled) {
						setTimeout( arguments.callee, 0 );
						return;
					}
				// and execute any waiting functions
	            domReady();
			}, false);
		}

		if(browser.safari) {
		    var numStyles;
			(function(){
				if (isReady) return;
				if (document.readyState != "loaded" && document.readyState != "complete") {
					setTimeout( arguments.callee, 0 );
					return;
				}
				if (numStyles === undefined) {
	                var links = document.getElementsByTagName("link");
	                for (var i=0; i < links.length; i++) {
	                	if(links[i].getAttribute('rel') == 'stylesheet') {
	                	    numStyles++;
	                	}
	                }
	                var styles = document.getElementsByTagName("style");
	                numStyles += styles.length;
				}
				if (document.styleSheets.length != numStyles) {
					setTimeout( arguments.callee, 0 );
					return;
				}
			
				// and execute any waiting functions
				domReady();
			})();
		}

		// A fallback to window.onload, that will always work
	    addLoadEvent(domReady);
	};

	// This is the public function that people can use to hook up ready.
	DomReady.ready = function(fn, args) {
		// Attach the listeners
		bindReady();
    
		// If the DOM is already ready
		if (isReady) {
			// Execute the function immediately
			fn.call(window, []);
	    } else {
			// Add the function to the wait list
	        readyList.push( function() { return fn.call(window, []); } );
	    }
	};
    
	bindReady();
	
})();

DomReady.ready(function(){loadLightBoxes()})




  //Loads lightboxes	
  function loadLightBoxes(){
	  light_boxes = document.getElementsByClassName('LB-white-content',document.body)
	  
	  var divTag = document.createElement("div");
	  divTag.id = "light_box_fade";
	  divTag.className = 'LB-black-overlay';
          divTag.onclick = HideLightBox;
	  document.body.appendChild(divTag);

	  for(var i=0;i<light_boxes.length;i++)
	  {
		lightbox = light_boxes[i];
		width = parseInt(lightbox.style.width)/2;
		width = Math.ceil(width);
		var centerX = Math.ceil((get_window_width()/3)) - width;
		lightbox.style.left = centerX + 'px';
		//Applying fixed or not fixed according to div's title
		if(lightbox.title && lightbox.title > '' && lightbox.title == 'fixed')
		{
			lightbox.style.position = 'absolute';
			lightbox.style.top = '20%';
		}

	  }

	  var fade = document.getElementById('light_box_fade');

	  fade.style.height = get_document_height() > get_window_height() ? get_document_height() + 'px' : get_window_height() + 'px';
	  if(typeof document.body.style.maxHeight === "undefined") // For IE6
	  {
		fade.style.width =  document.body.scrollWidth + 20 + 'px';
	  }
	  else // For other browsers
	  {
		fade.style.width =  '100%';
	  }
}

var current_light_box; 
var is_modal = false; 
 
function ShowLightBox(lightbox, isModal) 
{ 	
	document.getElementById(lightbox).style.display = 'block';
	document.getElementById('light_box_fade').style.display = 'block';			
	
	current_light_box = lightbox; 
	if (isModal) 
		is_modal = true; 
	else is_modal = false;
	
	if(typeof document.body.style.maxHeight === "undefined") // For IE6(Hide all select box tag)
	{		
		select_boxes = document.getElementsByTagName('select');
		for(var i=0;i<select_boxes.length;i++)
		select_boxes[i].style.visibility = 'hidden';
	}		
} 
 
function HideLightBox() 
{ 
	if (document.getElementById(current_light_box)) 
	{ 		 
		document.getElementById(current_light_box).style.display = 'none';
		document.getElementById('light_box_fade').style.display = 'none';	
		 current_light_box = ''; 
	} 
	
	if(typeof document.body.style.maxHeight === "undefined")// For IE6(Shows all select box tag)
	{ 
		select_boxes = document.getElementsByTagName('select');
		for(var i=0;i<select_boxes.length;i++)
		select_boxes[i].style.visibility = 'hidden';
	}	
	
}


function get_window_width()
{	
	return screen.width;
}

function get_window_height()
{	
	return screen.height;
}

function get_document_height()
{
		
	switch(parseInt(navigator.appVersion)){
	case 5 :		
		return document.body.offsetHeight + 20;
	break;
	case 4 :
		return document.body.offsetHeight + 20;
	break;
	case 9 :
		return document.body.offsetHeight + 20;
	break;
	}		
}

document.getElementsByClassName = function(className, parentElement) {
  if (typeof parentElement == 'string'){
    parentElement = document.getElementById(parentElement);
  } else if (typeof parentElement != 'object' ||
             typeof parentElement.tagName != 'string'){
    parentElement = document.body;
  }
  var children = parentElement.getElementsByTagName('*');
  var re = new RegExp('\\b' + className + '\\b');
  var el, elements = [];
  var i = 0;
  while ( (el = children[i++]) ){
    if ( el.className && re.test(el.className)){
      elements.push(el);
    }
  }
  return elements;
}

function ShowLightBoxFrame()
{
  var ll=document.getElementById('loglink');
  var lt=document.getElementById('logtable');
  if ( ll && lt )
    {
      lt.innerHTML='<img src="/images/picturess/logo1.gif" width=96 height=29 border=0 align="absmiddle"> <b style="font-size:16px;">&</b> <img src="/images/picturess/probook_logo.png" width=96 height=23 border=0 align="absmiddle">';
      lt.innerHTML+='<div style="margin-top:10px; margin-bottom:10px;">Регистрираните в <b>Probook</b> потребители<br> имат <a href="http://www.imot.bg/pcgi/imot.cgi?act=16&mode=6" class="proprofile" target="_blank">специални предимства</a> при използване на <b>imot.bg</b></div>';
      lt.innerHTML+='<center><iframe id="logframe" src="'+ll.href+'" width=455 height=270 marginwidth=0 marginheight=0 frameborder=0 scrolling="no">&nbsp;</iframe><br><a href="javascript:void(0)" class="proprofile" onclick="HideLightBox()">ЗАТВОРИ</a></center>';
      ShowLightBox('logtable');
    }
}

