/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

Modified for Bonprix (jko)

/*-----------------------------------------------------------------------------------------------*/

document.observe("dom:loaded", function() {
	lightboxHelper.initializeLightbox();
});

var lightboxHelper = new Object();

lightboxHelper.detect = navigator.userAgent.toLowerCase();

lightboxHelper.initializeLightbox = function() {
	if(!lightboxHelper.initialized) {
		if(lightboxHelper.browser) {
			return;
		}
		lightboxHelper.getBrowserInfo();
		lightboxHelper.initialized = true;
		var body = document.getElementsByTagName('body')[0];
		Element.insert( body, { top: "<div id='overlay'></div>" } );
	}
	return true;
}

lightboxHelper.checkIt = function(string) {
	lightboxHelper.place = lightboxHelper.detect.indexOf(string) + 1;
	lightboxHelper.thestring = string;
	return lightboxHelper.place;
}

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

lightboxHelper.getBrowserInfo = function() {
	if (lightboxHelper.checkIt('konqueror')) {
		lightboxHelper.browser = "Konqueror";
		lightboxHelper.OS = "Linux";
	}
	else if (lightboxHelper.checkIt('safari')) lightboxHelper.browser 	= "Safari"
	else if (lightboxHelper.checkIt('omniweb')) lightboxHelper.browser 	= "OmniWeb"
	else if (lightboxHelper.checkIt('opera')) lightboxHelper.browser 		= "Opera"
	else if (lightboxHelper.checkIt('webtv')) lightboxHelper.browser 		= "WebTV";
	else if (lightboxHelper.checkIt('icab')) lightboxHelper.browser 		= "iCab"
	else if (lightboxHelper.checkIt('msie')) lightboxHelper.browser 		= "Internet Explorer"
	else if (!lightboxHelper.checkIt('compatible')) {
		lightboxHelper.browser = "Netscape Navigator"
			lightboxHelper.version = lightboxHelper.detect.charAt(8);
	}
	else lightboxHelper.browser = "An unknown browser";

	if (!lightboxHelper.version) lightboxHelper.version = lightboxHelper.detect.charAt(lightboxHelper.place + lightboxHelper.thestring.length);

	if (!lightboxHelper.OS) {
		if (lightboxHelper.checkIt('linux')) lightboxHelper.OS 		= "Linux";
		else if (lightboxHelper.checkIt('x11')) lightboxHelper.OS 	= "Unix";
		else if (lightboxHelper.checkIt('mac')) lightboxHelper.OS 	= "Mac"
		else if (lightboxHelper.checkIt('win')) lightboxHelper.OS 	= "Windows"
		else lightboxHelper.OS 								= "an unknown operating system";
	}
}

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(rel) {
		lightboxHelper.initializeLightbox();
		this.content = rel;
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (lightboxHelper.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){
		$('overlay').style.display = display;
		$(this.content).style.display = display;
		if(display != 'none') 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;};
		}
		Event.observe("overlay", 'click', this['deactivate'].bindAsEventListener(this), false);
	},
	
	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (lightboxHelper.browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	}
}