var detect = navigator.userAgent.toLowerCase();
var total,thestring,yPos;

function getBrowserInfo() {
	var browser, version, OS;
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
	return browser;
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
var urlKotelezo="";
var urlKotelezoX="";
var urlKotelezoY="";

function addLightboxMarkup() {
	var bod 		= document.getElementsByTagName('body')[0];
	var overlay 	= document.createElement('div');
	overlay.id		= 'overlay';
	var lb			= document.createElement('div');
	lb.id			= 'lightbox';
	lb.className 	= 'loading';
	lb.innerHTML	= '<div id="lbLoadMessage">loading</div>';
	bod.appendChild(overlay);
	bod.appendChild(lb);

    var opacity = 60;
	overlay.style.opacity = (opacity / 100);
	overlay.style.MozOpacity = (opacity / 100);
	overlay.style.KhtmlOpacity = (opacity / 100);
	overlay.style.filter = "alpha(opacity=" + opacity + ")";
	
	if (urlKotelezo!="") {
      ajax_activate(urlKotelezo,urlKotelezoX,urlKotelezoY);
	}
}

function innerDimension() {
	var x,y;

	// all except Explorer
	if (self.innerHeight)
	{
		x = self.innerWidth;
		y = self.innerHeight;

	// Explorer 6 Strict Mode
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;

	// other Explorers
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}

	return {width: x, height: y};
}

function offsetDimension() {
	var x,y;

	// all except Explorer
	if (self.pageYOffset)
	{
		x = self.pageXOffset;
		y = self.pageYOffset;

	// Explorer 6 Strict
	} else if (document.documentElement && document.documentElement.scrollTop) {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;

	// all other Explorers
	} else if (document.body) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}

	return {x: x, y: y}
}

// Turn everything on - mainly the IE fixes
function ajax_activate(url, width, height, opacity) {
    var ol=$('overlay');
    if (!ol) {
        addLightboxMarkup();
        ol=$('overlay');
    }

    if (!opacity) {
        opacity = 60;
    }
	ol.style.opacity = (opacity / 100);
	ol.style.MozOpacity = (opacity / 100);
	ol.style.KhtmlOpacity = (opacity / 100);
	ol.style.filter = "alpha(opacity=" + opacity + ")";

    hideSelects('hidden');
    displayLightbox("block", url, width, height);
}

// In IE, select elements hover on top of the lightbox
function hideSelects(visibility) {
    var browser=getBrowserInfo();
    if (browser == 'Internet Explorer'){
	    selects = document.getElementsByTagName('select');
	    for(i = 0; i < selects.length; i++) {
		    selects[i].style.visibility = visibility;
	    }
    }
	iframes = document.getElementsByTagName('iframe');
	for(i = 0; i < iframes.length; i++) {
		iframes[i].style.visibility = visibility;
	}
	objects = document.getElementsByTagName('object');
	for(i = 0; i < objects.length; i++) {
		objects[i].style.visibility = visibility;
	}
}

// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
function prepareIE(height, overflow) {
	var bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;

	var htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overflow;
}

function displayLightbox(display, url, width, height){
	$('overlay').style.display = display;
	$('lightbox').style.display = display;
	if(display != 'none') loadInfo(url, width, height);
}

// Write an iFrame instead of using an AJAX call to pull the content
function loadInfo(content, width, height) {
    if (width==undefined) {
        width='940';
    }
    if (height==undefined) {
        height='660';
    }
    offset=100;
	var w = innerDimension().width;
	var h = innerDimension().height;
	var ox = offsetDimension().x;
	var oy = offsetDimension().y;
	if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) {
		var l = ((w) / 2) - ((width) / 2) + ox;
		var t = ((h) / 2) - ((height) / 2) + oy-offset;
	} else {
		var l = ((w) / 2) - ((width) / 2) + ox;
		var t = ((h) / 2) - ((height) / 2) + oy-offset;
	}
    if (t < 5) {
      t=0;
    }

	l = Math.round(l);
	t = Math.round(t);
	
	$('lightbox').style.top = t+'px';
	$('lightbox').style.left = l+'px';
	$('lightbox').style.width = width+'px';
	$('lightbox').style.height = height+'px';

	info = "<div id='lbContent'><iframe src='"+content+"'></iframe></div>";
	          document.getElementById('lightbox').innerHTML="<iframe src='"+content+"' width='"+width+"' height='"+height+"'  border='0' marginwidth='0' marginheight='0' hspace='0' vspace='0' frameborder='0' scrolling='no' allowtransparency='true'></iframe>";
			  //<style type='text/css'>body {opacity:1; filter:alpha(opacity=50);}</style>


//	new Insertion.Before($('lbLoadMessage'), info)
//	$('lightbox').className = "done";
//	var myAjax = new Ajax.Request(content, { method: 'get',
//			                                 onSuccess: function(transport) {
//                                                 $('lbContent').update(transport.responseText);
//     			                             }
//    });
}

// Example of creating your own functionality once lightbox is initiated
function ajax_deactivate(){
	parent.$('overlay').style.display = 'none';
	parent.$('lightbox').style.display = 'none';
	selects = parent.document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
//	Element.remove(parent.$('lbContent'));
    hideSelects('visible');
}







// png képek átalakítása Internet Explorer számára
// forrás: http://vbence.web.elte.hu/ie_png_alpha.html
// szerző: Varga Bence
 
function alphaFixIE() {
	var s, i, j;
 
	// IMG
	var els = document.getElementsByTagName("IMG");
	for (i=0; i<els.length; i++) {
		s = els[i].src;
		if (s.toLowerCase().indexOf(".png") != -1) {
			els[i].src = "spacer.gif";
			els[i].style.filter += "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + s + "', sizingMethod=image);";
		}
	}
 
	// CSS: background
	for (i=0; i<document.styleSheets.length; i++) {
		var pos = document.styleSheets[i].href.lastIndexOf("/");
		var cssDir = (pos != -1) ? document.styleSheets[i].href.substring(0, pos + 1) : "";
		for (j=0; j<document.styleSheets[i].rules.length; j++) {
			var style = document.styleSheets[i].rules[j].style;
			if (style.backgroundImage.toLowerCase().indexOf(".png") != -1) {
				var filename = style.backgroundImage.substring(4, style.backgroundImage.length - 1);
				if (filename.indexOf("http://") != 0 && filename.indexOf("/") != 0)
					filename = cssDir + filename;
				style.backgroundImage = "none";
				style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + filename + "', sizingMethod='crop');";
			}
		}
	}
}
 
if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Windows") != -1)
	window.attachEvent("onload", alphaFixIE);

