//poppy v0.1 - jQuery Popup for iFrames with inner links.
//progged by Giacomo Barbalinardo for landmaxx.de
(function ($) {
    $.fn.poppy = function () {
        // set default option values
		if($('body').find('div.poppyWrapper')){
			$('body').find('div.poppyWrapper').remove();
			//$('body').find('div.poppyClose').remove();
		}
        var args = arguments[0] || {}; // It's your object of arguments
		var dataSource = args.dataSource;
		var pagingStart = args.pagingStart;
		var element = $(this[0]);
        // iterate over the matched elements passed to the plugin
		if(args != ''){
			var link = args;
		}else{
			var link = element.attr("href");
		}
		var iframe = '<iframe height="100%" frameborder="0" scrolling="no" width="668" marginheight="0" src="' + link +'" marginwidth="0"></iframe>';
		var div = document.createElement('div');
        div.className = "poppyWrapper";
		//div.style.cssText = "background-color:red;";
		var leftTopCorner = '<div class="poppyLHc"></div>';
		var header = '<div class="poppyHead"></div>';
		var rightTopCorner = '<div class="poppyRHc"></div>';
		var clear = '<div class="clearPoppy"></div>';
		var leftContent = '<div class="poppyLC"></div>';
		var content = '<div class="poppyContent">' + iframe + '</div>';
		var rightContent = '<div class="poppyRC"><div class="poppyClose" id="closer"></div></div>';
		var leftBottomCorner = '<div class="poppyLBc"></div>';
		var footer = '<div class="poppyBottom"></div>';
		var rightBottomCorner = '<div class="poppyRBc"></div>';
		$('body').append(div);
		//$('body').append('<div class="poppyClose" id="closer"></div>');
		$('.poppyWrapper').append(leftTopCorner);
		$('.poppyWrapper').append(header);
		$('.poppyWrapper').append(rightTopCorner);
		$('.poppyWrapper').append(clear);
		$('.poppyWrapper').append(leftContent);
		$('.poppyWrapper').append(content);
		$('.poppyWrapper').append(rightContent);
		$('.poppyWrapper').append(clear);
		$('.poppyWrapper').append(leftBottomCorner);
		$('.poppyWrapper').append(footer);
		$('.poppyWrapper').append(rightBottomCorner);
		$('.poppyWrapper').append(clear);
		$('.poppyWrapper').fadeIn("slow");
		var iFrames = document.getElementsByTagName('iframe');

			// Resize heights.
			function iResize()
			{
				// Iterate through all iframes in the page.
				for (var i = 0, j = iFrames.length; i < j; i++)
				{
					// Set inline style to equal the body height of the iframed content.
					var height = iFrames[i].contentWindow.document.body.offsetHeight;
					iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
					$('.poppyLC').css("height", height);
					$('.poppyRC').css("height", height);
					$('.poppyContent').css("height", height);
					$('.poppyClose').fadeIn("slow");
				}
			}

			// Check if browser is Safari or Opera.
			if ($.browser.safari || $.browser.opera)
			{
				// Start timer when loaded.
				$('iframe').load(function()
					{
						setTimeout(iResize, 0);
					}
				);

				// Safari and Opera need a kick-start.
				for (var i = 0, j = iFrames.length; i < j; i++)
				{
					var iSource = iFrames[i].src;
					iFrames[i].src = '';
					iFrames[i].src = iSource;
				}
			}
			else
			{
				// For other good browsers.
				$('iframe').load(function()
					{
						// Set inline style to equal the body height of the iframed content.
						var height = this.contentWindow.document.body.offsetHeight;
                                                this.style.height =  height + 'px';
						$('.poppyLC').css("height", height);
						$('.poppyRC').css("height", height);
						$('.poppyContent').css("height", height);
						$('.poppyClose').fadeIn("slow");
                                                checkIResize();
                                                
					}
				);
			}
    }
})(jQuery);
