/**
* KiteBox - A lite lightbox that flies...
* Depends on YUI (Yahoo, Dom, Event), with shortcuts as $D, $E, and $ (get)
* @author Mike Healy www.mikehealy.com.au
* @version 0.9.1b 	forked to define YUI shortcuts specifically for Holus Boalus. Better solution is required!
* @license Do anything except sell it "as is", or distribute without this attribution notice. Commercial projects okay! 
*/


var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $ = $D.get;

var KiteBox = function(container, cfg) {
	
	this.imageContainer;
	this.blocker;
	
	
	/**
	 * Add/Remove event listeners on image
	 * happens regularly because img comes and goes (IE support)
	 * @param {String} action add or remove
	 */	
	this.imageListeners = function(action) {
		
		var img = $('KiteBox_image');
		
		//ADD Listeners
		if(action.toLowerCase() == 'add') {
			$E.on(img, 'click', this.hide, null, this);
			$E.on(document, 'keyup', function(ev) {
				if(ev.keyCode == 27) this.hide();
			}, null, this);
		}
		
		//Remove Listeners
		else {
			$E.removeListener(img, 'click');
			$E.removeListener(document, 'keyup');
		}
	};
	
	
	/**
	* Hide the lightbox image
	* @param {Object} ev
	*/
	this.hide = function(ev) {
		
		//Remove old Listeners
		this.imageListeners('remove');
		
		this.imageContainer.style.display = 'none';
		this.blocker.style.display = 'none';
		var img = $('KiteBox_image');
		if(img) img.parentNode.removeChild(img);
	};
	
	
	/**
	* Load image from <a>
	* @param {Event} ev click event
	*/
	this.loadImage = function(ev) {
		var tgt = $E.getTarget(ev);
		
		//Ascend DOM to find link
		var count = 0;
		while(tgt.tagName && tgt.tagName.toLowerCase() != 'a' && count < 5) {
			tgt = tgt.parentNode;
			count++;
		}
		if(!tgt.tagName || tgt.tagName.toLowerCase() != 'a') return; //No <a> link found
		
		//Follow hyperlink if not image link
		if(tgt.href.toLowerCase().search(/(jpe?g|gif|png|bmp)$/) == -1) {
			return;
		}
		else $E.stopEvent(ev);

		//Create image (must be created & destroyed to resize properly)
		var img = document.createElement('img'); 
		img.id = 'KiteBox_image';
		img.src = tgt.href;
		img.alt = tgt.title;
		$('KiteBox').appendChild(img);
		
		//Listen for close
		this.imageListeners('add');
		
		
		//Enable page blocking element
		var blocker = $('KiteBox_block');
		$D.setStyle(blocker, 'opacity', 0.85);
		var w = ($D.getViewportWidth() > $D.getDocumentWidth()) ? $D.getViewportWidth() : $D.getDocumentWidth();
		var h = ($D.getViewportHeight() > $D.getDocumentHeight()) ? $D.getViewportHeight() : $D.getDocumentHeight();
		
		blocker.style.width = w.toString().concat('px');
		blocker.style.height = h.toString().concat('px');
		blocker.style.display = 'block';
		
		//Position vertically to ensure image is in viewport
		var scrollY = $D.getDocumentScrollTop();
		//console.log(scrollY);
		scrollY = scrollY.toString().concat('px');
		img.parentNode.style.marginTop = scrollY;
		img.parentNode.style.display = 'block';
	};
	
	
	
	/**
	 * Constructor
	 * @param {Mixed} container String ID or element reference to link container el
	 * @param {Object} cfg Config Object literal (optional)
	 * 		loadingImage	path to loading image
	 */
	this.constructor = function(container, cfg) {
		
		//Setup YUI shortcuts here for other installations
		//Use cfg for portability
		
		//Create Background Blocker
		var body = document.getElementsByTagName('body')[0];
		var blocker = document.createElement('div');
		blocker.id = 'KiteBox_block';
		body.appendChild(blocker); //hidden by default
		
		//Create Image Container & Element
		var con = document.createElement('div');
		con.id = 'KiteBox';
		body.appendChild(con);
		
		//Make available to other methods
		this.imageContainer = con;
		this.blocker = blocker;
		
		
		//Get Container
		if(typeof container == 'string') container = $(container);
		this.container = container;
		
		//Listen
		$E.on(container, 'click', this.loadImage, null, this);
	};
	this.constructor(container, cfg);
};