/**
 * jquery.overlay 0.14. Overlay HTML with eyecandy.
 * 
 * http://flowplayer.org/tools/overlay.html
 *
 * Copyright (c) 2008 Tero Piirainen (support@flowplayer.org)
 *
 * Released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * >> Basically you can do anything you want but leave this header as is <<
 *
 * Since  : 0.01 - 03/11/2008
 * Version: 0.14 - Thu Nov 06 2008 12:20:24 GMT-0000 (GMT+00:00)
 */ 
(function($) { 
		
	function Overlay(el, params, evt) {
		
		var self = this;
		
		// setup options
		var opts = { 
			speed:500,
			top:100,
			close: null,
			onClose: null,
			onBeforeLoad: null, 
			onLoad: null, 
			closeOnClick: true
		};
		
		if (typeof params == 'function') {
			params = {onLoad: params};	
		}
		
		$.extend(opts, params); 
		
		//alert(opts.toSource());
		
		// setup content
		var content = $(el.attr("rel"));
		
		
		if (!content.length) {			
			alert("Cannot find element: " + el.attr("rel") + ", missing \"#\" in rel- attribute?");
			return;
		}		
		
		// one instance visible at once
		if (content.is(":visible")) {
			return;	
		}
		
		
		// close button
		if (!opts.close) {
			if (!$('.modal-overlay').length)
				$('body').append('<div class="modal-overlay"></div>')
				
			//if (!$('.close').length)
				content.prepend('<div class="close"></div>');
				
			opts.close = "div.close";
		} 
		
		var closeButton = content.find(opts.close);
		
		closeButton.one("click.overlay", function() { 
			close();  
		});
				
		
		// setup growing image		
		var width = content.outerWidth();
		var height = content.outerHeight();
		var img = $("#_overlayImage");
		
		//alert(width);
		//alert(height);
		
		if (!img.length) {
			img = $("<img id='_overlayImage'>");
			img.css({zIndex:9999,border:0,position:'absolute'}).width(width).hide() 
			$('body').append(img);  				
		}
		
		
		//alert(width);
		//alert(img.width());
		
		var bg = content.attr("bg");
		
		if (!bg) {
			bg = content.css("backgroundImage");
			bg = bg.substring(bg.indexOf("(") + 1, bg.indexOf(")"));
			content.attr("bg", bg); 
			content.css("backgroundImage", "none");
		}		
		
		// replace hyphens so that Opera works
		img.attr("src", bg.replace(/\"/g, "")); 
		img.attr('onload', 'fixPNG(this)');
				
				
		function fireEvent(evt) {
			var fn = opts[evt];
			//alert(evt);
			
			if (fn) {
				
				try {  
					return fn.call(img, content, el, closeButton);
					
				} catch (error) {
					alert("Error calling overlay::" + evt + ", " + error);
					return false;
				} 					
			}
			return true;			
		}
		
		// top & left
		var w = $(window); 
		//var _top = w.scrollTop() + opts.top; 	
		//var _left = w.scrollLeft() + Math.max((w.width() - img.width()) / 2, 0);		

		var _top = '50%';
		var _left = '50%';

		//var _top = w.height() / 2 + 'px';
		//var _left = w.width() / 2 + 'px';
		
		//margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px
		//alert('top: ' + top);
		//alert('left: ' + left);
		
		if (fireEvent("onBeforeLoad") === false) {
			return;	
		}
		
		// animate image and expose content over it
		//img.css({top:evt.pageY, left:evt.pageX, width:0}).show();
		
		if ( (jQuery.browser.msie && parseInt(jQuery.browser.version.substr(0,1)) >= 7) || jQuery.browser.mozilla) {
			img.css('position', 'fixed');
		}
		
		img.css({
			//position:'fixed', 
			width: 0,
			top: _top, 
			left: _left,
			marginTop: '-' + (height / 2) + 'px',
			marginLeft: '-' + (width / 2) + 'px'
			//visibility: 'visible'
		})			
		
		
		
		img.animate({top: _top, left: _left, width:width}, opts.speed, function() { 
			$('.modal-overlay')
				.show();
			
			if ( (jQuery.browser.msie && parseInt(jQuery.browser.version.substr(0,1)) >= 7) || jQuery.browser.mozilla) {
				content.css('position', 'fixed');
			}
			
			content.css({
				//position:'fixed', 
				top: _top, 
				left: _left,
				margin: 0,
				padding: 0,
				marginTop: '-' + (height / 2) + 'px',
				marginLeft: '-' + (width / 2) + 'px'
				//visibility: 'visible'
			}).fadeIn("fast", function() { 
				fireEvent("onLoad");
				
				var z = img.css("zIndex");
				if (z == 'auto') z = 0;
				closeButton.add(content).css("zIndex", ++z);	
				
				// when window is clicked outside overlay, we close
				if (opts.closeOnClick) {					
					w.bind("click.overlay", function(evt) {
						var target = $(evt.target);
						if (target.attr("id") == '_overlayImage') { return; }
						if (target.attr("bg")) { return; }
						if (target.parents("[bg]").length) { return; }					
						close(); 
					});						
				}
				
				
			});
			
		});				
		
		// close action
		function close() {
			if (fireEvent("onClose") === false) {
				return;	
			}
			
			if (img.is(":visible") || content.is(":visible")) {
				img.hide(); //.css('visibility', 'hidden');
				content.hide(); //.css('visibility', 'hidden');		
				
				
				$('.modal-overlay').hide();
				
				if (opts.closeOnClick) {
					w.unbind("click.overlay");
				}
				w.unbind("keypress.overlay");
			} 
		}		
		
		// keyboard::escape
		w.bind("keypress.overlay", function(evt) {
			if (evt.keyCode == 27) {
				close();	
			}
		});		

	
		$.overlayClose = function() {
			close();	
		};
	}
	
	// jQuery plugin initialization
	$.fn.overlay = function(params) {   
		this.bind("click.overlay", function(e) {
			var temp =  new Overlay($(this), params, e);
			return e.preventDefault();
		}); 
		
		return this; 
	}; 
	
	
	$.extend({
		overlayClose : function() 
		{
			Overlay.close();
		}
	});
	
	
})(jQuery);

