(function($) { 
	$.fn.iframeoverlay = function(options) {
		$.fn.iframeoverlay.defaults = {
			overlay_id: 'overlay_box',
			overlay_content_id: 'overlay_box_content',
			bg_id: 'lightbox_bg',
			animationSpeed: 'normal', /* fast/slow/normal */
			opacity: 0.80, /* Value between 0 and 1 */
			close: 'close window',
			empty_file: '/html/images/upload/SWF/podcast/empty.php',
			auto_file: ''
		};
			
		// build main options before element iteration
		var opts = $.extend({}, $.fn.iframeoverlay.defaults, options);
		
		var bgEl;
		var boxEl;
		
		$(window).resize(function(){ _resizeOverlay(); });
		
		$(this).each(function() {
			//click event open buttons
			$(this).bind('click',function(){
				url = $(this).attr('href');
				openOverlay(url);
				return false;
			});
        });
		
		var openOverlay = function(url) {
			hideElemMainPage();
			
			if($('#'+opts.overlay_id).size() == 0) {
				_buildOverlay(url); // If the overlay is not there, inject it!
			}
			_resizeOverlay();
			
			bgEl = $('#'+opts.bg_id);
			boxEl = $('#'+opts.overlay_id);
			
			bgEl.css('opacity',0).show().fadeTo(opts.animationSpeed,opts.opacity, function(){
				boxEl.fadeIn(opts.animationSpeed,function(){
					i_width = ( parseFloat(grab_param('width',url)) ) ? grab_param('width',url) : "425";
					i_height = ( parseFloat(grab_param('height',url)) ) ? grab_param('height',url) : "344";
					markup = '<iframe src ="'+url+'" width="'+i_width+'" height="'+i_height+'" frameborder="no"></iframe>';
					
					boxEl.animate({
						'width': i_width,
						'height': i_height	
					},opts.animationSpeed,function(){
						_resizeOverlay();
						boxEl.find('#'+opts.overlay_content_id).html(markup);
						
						boxEl.find('iframe').load(function(e){ 
							if (opts.empty_file == $(this).attr('src')) closeOverlayAnim();
						});
						
					});		
				});	
			});

			
		};
		
		var closeOverlay = function() {
			if (opts.empty_file != '') boxEl.find('iframe').attr('src',opts.empty_file);
			else closeOverlayAnim();
		};	
		
		var closeOverlayAnim = function() {
			bgEl.hide();
			boxEl.hide();
			boxEl.attr('style', 'display:none');
			showElemMainPage();
		};	
		
		var _buildOverlay = function(url) {
			var markup = '<div id="'+opts.overlay_id+'">';
			var cssClass = grab_param('cssClass',url);
			if (cssClass != 'dark' && cssClass != 'light') {
				cssClass = '';
			}
			markup += '<a class="close '+cssClass+'" id="close_iframe_overlay">'+opts.close+'</a>';
			markup += '<div id="'+opts.overlay_content_id+'"></div>';
			markup += '</div>';
			
			$('body').append(markup);
			if($('#'+opts.bg_id).size() == 0) $('body').append('<div id="'+opts.bg_id+'"></div>');
			
			$('#'+opts.bg_id).bind('click',function(){
				closeOverlay();
				return false;
			});
			
			$(window).unload( function () {
				closeOverlay();
			});
			
			$('#close_iframe_overlay').bind('click',function(){
				closeOverlay();
				return false;
			});
		};
		
		var _resizeOverlay = function() {
			$('#'+opts.bg_id).css({
				'height':($(document).height()+20),
				'width':$(window).width()
			});
		};
		
		
		function grab_param(name,url){
		  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+name+"=([^&#]*)";
		  var regex = new RegExp( regexS );
		  var results = regex.exec( url );
		  if( results == null )
			return "";
		  else
			return results[1];
		}
		
		function hideElemMainPage() {
			// To fix the bug with IE select boxes
			if($.browser.msie && $.browser.version == 6){
				$('select').css('visibility','hidden');
			};
					
			// Hide the flash
			$('object,embed').css('visibility','hidden');
		}
		
		function showElemMainPage() {
			// To fix the bug with IE select boxes
			if($.browser.msie && $.browser.version == 6){
				$('select').css('visibility','visible');
			};
					
			// Hide the flash
			$('object,embed').css('visibility','visible');
		}
		
		
		if (opts.auto_file != "") openOverlay(opts.auto_file);	
	};
	
	
})(jQuery);