var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

function getBrowserInfo() {
	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";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

//Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {
	yPos : 0,
	xPos : 0,
	boxType: 'image',
	url: '',
	
	initialize: function() {
		
	},

	// Turn everything on - mainly the IE fixes
	activate: function(boxType, url){
		this.boxType = boxType;
		this.url = url;
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	displayLightbox: function(display) {
		if(this.boxType=='newsletter') {
			$('lightboxInner').style.display=display;
			$('lightbox').style.display=display;
			if(display != 'none') this.iframeLoader(608, 300);
		} else if(this.boxType=='image') {
			if(display != 'none') this.loadInfo();
		}
	},
	
	iframeLoader: function(width, height) {
		try{
			switch(targetSite) {
				case 'koncern':
					myUrl = 'http://nrgi.w11.dk/subscribtions/Koncern/';
					break;
				default: 
					myUrl = 'http://nrgi.w11.dk/subscribe.aspx';
					break;
			}
		}catch(e) {
			myUrl = 'http://nrgi.w11.dk/subscribe.aspx';
		}
		
		$('lightboxInner').style.width=width+"px";
		$('lightboxInner').style.height=height+"px";
		$('lightboxInner').style.marginTop="-"+Math.ceil(height/2)+"px";
		$('lightboxInner').style.marginLeft="-"+Math.ceil(width/2)+"px";
		$('lightboxInner').style.backgroundColor='#fff';
		$('lightboxInner').innerHTML='<div class="iframeDiv"><div class="iframeHeader"></div><div class="iframeClose" onclick="lbox.deactivate()"></div><iframe src="'+myUrl+'" frameborder="no" scrolling="no" width="608" height="350"></iframe></div>';
		/*
		$('lightboxInner').style.width=width+"px";
		$('lightboxInner').style.height=height+"px";
		$('lightboxInner').style.marginTop="-"+Math.ceil(height/2)+"px";
		$('lightboxInner').style.marginLeft="-"+Math.ceil(width/2)+"px";
		$('lightboxInner').style.backgroundColor='#fff';
		$('lightboxInner').innerHTML='<div class="iframeDiv"><div class="iframeHeader"></div><div class="iframeClose" onclick="lbox.deactivate()"></div><iframe src="http://nrgi.w11.dk/subscribe.aspx" frameborder="no" scrolling="no" width="608" height="350"></iframe></div>';
		*/
	},
	
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	deactivate: function(){
		$('lightboxInner').innerHTML='';
		
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	}
}

var lbox = new lightbox();

function replace_urls(input)
{
	return input.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '<a href="$&" target="_blank">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2" target="_blank">$2</a>');
}