/**
 * Nestle Professional Product Lightbox
 * Open lightbox, populate with product
 * Developed by David Shanley <david.shanley@europe.mccann.com>
 *  
 *  depends on mootools.
 * @author David Shanley
 */
var Lightbox = { 
	init: function() {
		this.overlay = new Element('div', { "id" : "product_overlay"});
		this.container = new Element('div', {'id': 'product_container','display' : 'none'});
		this.content_area = new Element('div', { 'id' : 'product_content_area'})
		var close = this.closeLightbox.bind(this); // close
		this.overlay.addEvent('click',close);
		this.container.addEvent('click',close);
		this.container.set('html','<h3 style="padding: 20px;">loading, please wait a moment....</h3>');
		
		
		this.fx = { 
			overlay: new Fx.Tween(this.overlay),
			container: new Fx.Tween(this.container),
			content_area: new Fx.Tween(this.content_area)
		}
	},
	configure: function() {
	
		this.init();
		this.lightbox_open = true;
		this.overlay.injectInside(document.body);
		
	},
	closeLightbox: function() {
		var fx = this.fx;
		var container = this.container;
		var overlay = this.overlay;
		var content_area = this.content_area;
		
		var fadeOutOverlay  = function() {
			fx.overlay.start('opacity',0).chain(function() { overlay.destroy(); container.destroy(); content_area.destroy();  })
			
		}
		this.fx.content_area.start('opacity','0').chain(fadeOutOverlay);
		this.lightbox_open = false;
		
		/* re-populate quick links */
		generateQuicklinks();
		 
	},
	loadOverlay: function() {
		
		this.overlay.setStyles({'display' : 'block','top': window.getScrollTop(), 'height': window.getHeight(),'z-index': '100'});
		
		/* set up containers */
		this.container.injectInside(document.body);
		this.content_area.setStyle('opacity','0');
		this.container.setStyles({'display' : 'block','top': window.getScrollTop(), 'height': window.getHeight(), 'z-index': '200','text-align': 'center'});
		this.content_area.setStyle('z-index','300');
		
		/* start */
		var loadContainer = this.loadContainer.bind(this);
		this.fx.overlay.start('opacity','0','0.85').chain(loadContainer);
		
	
	},
	
	loadContainer: function() {
		
		/* go XSLT */
		var product = this.transformXSL();
		
		/* load content */
		if (Browser.Engine.trident) { // IE
			this.container.empty();
			this.content_area.set('html',product);
		} else {
			this.container.empty();
			this.content_area.appendChild(product);
		}
		
		/* inject! */
		this.content_area.injectInside(this.container);
		
		/* fade in product */
		this.fx.content_area.start('opacity','1')
		
		
	},
	open: function(id) {
		
		/* clear quick links */
		clearQuicklinks();
		
		this.configure();
		this.product_id = id;
		this.loadOverlay();
		
	},
	scroll: function() {
		if($defined(this.lightbox_open) && this.lightbox_open) {
			this.overlay.setStyle('top',window.getScrollTop());
			//this.container = window.getScrollTop()
		}
	},
	resize: function() {
		if($defined(this.lightbox_open) && this.lightbox_open) {
			this.overlay.setStyle('height',window.getHeight());
			//this.container = window.getScrollTop()
		}
	},
	transformXSL: function() {
		
		/* create xml */
		grabXML();
		var xmlobj = XML_DOM;
		
			/* grab the root (products) */
			var root = xmlobj.getElementsByTagName('products')[0]; //root 
			
			/* selected product var */
			var selectedProductNode;
			for (j = 0; j < root.childNodes.length; j++) {
				
				/* each product */
				if (root.childNodes[j].nodeType != 1) {
					continue;
				}
				if( root.childNodes[j].attributes[0].nodeValue == this.product_id) {
					selectedProductNode = root.childNodes[j]; // selected child node;
				}
			}
			
			/* XSL - same for all products */
			
			if (USER_TYPE == null || USER_TYPE == "enduser") {
				grabXSL('xslt/product-notech.xsl')
			} else {
				grabXSL('xslt/product.xsl')
			}
			var productXSL = XSL_DOM;
		    var parsedXSL;
	
			if (window.ActiveXObject) { // ie
				parsedXSL = selectedProductNode.transformNode(productXSL);
				
			} 
			
			/* everything else */
			if(document.implementation && document.implementation.createDocument) { // everything else
				xsltProcessor=new XSLTProcessor();
				xsltProcessor.importStylesheet(productXSL);
				parsedXSL = xsltProcessor.transformToFragment(selectedProductNode,document);
					
			}
				
		return parsedXSL
	}
}