/*
 * Created By: Chris Campbell Website: http://particletree.com Date: 2/1/2006
 * 
 * Inspired by the lightbox implementation found at
 * http://www.huddletogether.com/projects/lightbox/
 */

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS, browser, version, total, thestring;
var lbValid = '';
/*-----------------------------------------------------------------------------------------------*/

// Browser detect script origionally created by Peter Paul Koch at
// http://www.quirksmode.org/
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', lbInitialize, false);
//Event.observe(window, 'load', getBrowserInfo, false);

document.observe("dom:loaded", function() {
  lbInitialize();
  getBrowserInfo();
});
// Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	width : 800,
	height : 600,
	ofy : 'auto',
	ofx : 'hidden',

	initialize : function(ctrl) {
		if (ctrl.name == 'lbOn_alert') {
			this.width = 300;
			this.height = 200;
			this.ofy = 'hidden';
			this.ofx = 'hidden';
		} else if (ctrl.name == 'lbOn_inform') {
			this.width = 350;
			this.height = 250;
			this.ofy = 'auto';
			// this.ofy = 'hidden';
			this.ofx = 'hidden';
		} else if (ctrl.name == 'lbOn_inform2') {
			this.width = 350;
			this.height = 250;
			this.ofy = 'hidden';
			this.ofx = 'hidden';
		}
		this.content = ctrl.href;
		$('overlay').setStyle({opacity: .5});
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this),
				false);
		ctrl.onclick = function() {
			return false;
		};
	},

	// Turn everything on - mainly the IE fixes
	activate : function() {

		// make applet invisible
		ap = document.getElementById("myCB");
		if (ap != null) {
			ap.style.visibility = "hidden"; // visible
			// ap.style.display = "none"; // block
		}

		lb = document.getElementById('lightbox');
		if (lb) {
			lb.style.width = this.width + "px";
			lb.style.height = this.height + "px";
			lb.style.overflowY = this.ofy;
			lb.style.overflowX = this.ofx;
			lb.style.top = Math.round((xClientHeight() - this.height) / 2)
					+ "px";
			lb.style.left = Math.round((xClientWidth() - this.width) / 2)
					+ "px";
		}

		if (browser == 'Internet Explorer') {
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0, 0);
			this.hideSelects('hidden');
		}

		this.displayLightbox("block");
	},

	// Ie requires height to 100% and overflow hidden or else you can scroll
	// down past the lightbox
	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;
	},

	// 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;
		}
	},

	// 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);
	},

	displayLightbox : function(display) {
		if ($('overlay'))
			$('overlay').style.display = display;
		if ($('lightbox'))
			$('lightbox').style.display = display;
		if (display != 'none')
			this.loadInfo();
	},

	// Begin Ajax request based off of the href of the clicked linked
	loadInfo : function() {
		var myAjax = new Ajax.Request(this.content, {
			method : 'get',
			parameters : "",
			onComplete : this.processInfo.bindAsEventListener(this)
		});

	},

	// Display Ajax response
	processInfo : function(response) {
		$('lbLoadMessage').style.display = 'none';
		if (response.responseText == 'PROCESS_LINK') {
			document.location.href = this.content.replace('&checknow=1', '')
					.replace('getajax=selinst&', '');
			return 0;
		}
		if (this.content.match(/(goto=.*)/)) {
			response.responseText = response.responseText.replace('cw\.pl\?',
					'cw.pl?' + RegExp.$1 + '&');
		}
		info = "<div id='lbContent'>" + response.responseText + "</div>";
		// info = "<div id='lbContent'>" + response.responseText + "</div>" +
		// '<div id="lbLoadMessage">' + '<p>Loading</p>' + '</div>';
		;

		new Insertion.Before($('lbLoadMessage'), info)
		// lbc = document.getElementById('lbContent');
		// if(lbc == null)
		// {
		// lb = document.getElementById('lightbox');
		// lb.innerHTML = info + '<div id="lbLoadMessage">' + '<p>Loading</p>' +
		// '</div>';
		// }
		// else
		// {
		// alert('found ' + lbc.innerHTML);
		// }

		$('lightbox').className = "done";
		this.actions();
	},

	// Search through new links within the lightbox, and attach click event
	actions : function() {
		lbActions = document.getElementsByClassName('lbAction');

		for (i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel]
					.bindAsEventListener(this), false);
			lbActions[i].onclick = function() {
				return false;
			};
		}

	},

	// Example of creating your own functionality once lightbox is initiated
	insert : function(e) {
		// link = Event.element(e).parentNode;
		link = Event.element(e);
		Element.remove($('lbContent'));
		$('lbLoadMessage').style.display = 'block';
		var myAjax = new Ajax.Request(link.href, {
			method : 'get',
			parameters : "",
			onComplete : this.processInfo.bindAsEventListener(this)
		});

	},

	// Example of creating your own functionality once lightbox is initiated
	deactivate : function() {
		ap = document.getElementById("myCB");
		if (ap != null) {
			ap.style.visibility = "visible"; // visible
			// ap.style.display = "block"; // block
		}

		Element.remove($('lbContent'));

		if (browser == "Internet Explorer") {
			this.setScroll(0, this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}

		this.displayLightbox("none");
	}
}

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

// Onload, make all links that need to trigger a lightbox active
function lbInitialize() {
	addLightboxMarkup();
	// lbox = document.getElementsByClassName('lbOn');
	lbox = document.getElementsByName('lbOn');
	for (i = 0; i < lbox.length; i++) {
		lbValid = new lightbox(lbox[i]);
	}
	lbox = document.getElementsByName('lbOn_alert');
	for (i = 0; i < lbox.length; i++) {
		lbValid = new lightbox(lbox[i]);
	}
	lbox = document.getElementsByName('lbOn_inform');
	for (i = 0; i < lbox.length; i++) {
		lbValid = new lightbox(lbox[i]);
	}
	lbox = document.getElementsByName('lbOn_inform2');
	for (i = 0; i < lbox.length; i++) {
		lbValid = new lightbox(lbox[i]);
	}
}

// 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.
function addLightboxMarkup() {
	overlay = $('overlay');
	if (!overlay) {
		overlay = document.createElement('div');
		overlay.id = 'overlay';
	}

	lb = $('lightbox');
	if (!lb) {
		lb = document.createElement('div');
		lb.id = 'lightbox';

		// Nele 2007-07-12
		lb.style.width = "800px";
		lb.style.height = "600px";
		lb.style.top = Math.round((xClientHeight() - 600) / 2) + "px";
		lb.style.left = Math.round((xClientWidth() - 800) / 2) + "px";

		lb.className = 'loading';
		lb.innerHTML = '<div id="lbLoadMessage">' + '<table width="60%" align="center"><tr><td style="font-size: 14px; font-family: \'Trebuchet MS\',Tahoma,Verdana,Helvetica,sans-serif">Loading...</td><td><img src="/images/loading_white.gif" /></td></tr></table>' + '</div>';

		bod = document.getElementsByTagName('body')[0];

		bod.appendChild(overlay);
		bod.appendChild(lb);
	}
}

// -----------------------------------------------------------------------------
// The following code snippets have been copied from http://cross-browser.com.
// -----------------------------------------------------------------------------

// xClientWidth, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of
// the GNU LGPL

function xClientWidth() {
	var v = 0, d = document, w = window;
	if (d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement
			&& d.documentElement.clientWidth) {
		v = d.documentElement.clientWidth;
	} else if (d.body && d.body.clientWidth) {
		v = d.body.clientWidth;
	} else if (xDef(w.innerWidth, w.innerHeight, d.height)) {
		v = w.innerWidth;
		if (d.height > w.innerHeight)
			v -= 16;
	}
	return v;
}

// xClientHeight, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of
// the GNU LGPL

function xClientHeight() {
	var v = 0, d = document, w = window;
	if (d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement
			&& d.documentElement.clientHeight) {
		v = d.documentElement.clientHeight;
	} else if (d.body && d.body.clientHeight) {
		v = d.body.clientHeight;
	} else if (xDef(w.innerWidth, w.innerHeight, d.width)) {
		v = w.innerHeight;
		if (d.width > w.innerWidth)
			v -= 16;
	}
	return v;
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload
// functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}

}

function lbAlert(href) {
	link = document.createElement('a');
	link.href = href;
	link.name = 'lbOn_alert';
	lb = new lightbox(link);
	lb.activate();
}

function lbInform(href) {
	link = document.createElement('a');
	link.href = href;
	link.name = 'lbOn_inform';
	lb = new lightbox(link);
	lb.activate();
}

function lbInform2(href) {
	link = document.createElement('a');
	link.href = href;
	link.name = 'lbOn_inform2';
	lb = new lightbox(link);
	lb.activate();
}
