/**
 * Plugin per trasformare un div (nascosto) in una popup
 * 
 * @author Daniele Tieghi
 * 
 */


	jQuery.fn.createPopup = function(relativeFolder, forceScreenWidth) {

		if (!relativeFolder)
			relativeFolder = '';
		
		if (!forceScreenWidth)
			forceScreenWidth = 0;
		
		popupDiv = jQuery(this).clone();
		
		popupWidth = jQuery(this).outerWidth(true);
		popupHeight = jQuery(this).outerHeight(true);
		
		popupCoverLayerColor	= '#FFF';
		popupBackgroundColor	= '#FFF';
		popupImagesFolder		= '/javascript/images.popup/';
		popupBorderWidth		= 25;
		
		var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		
		jQuery('body').append('<div id="PopupCoverLayer" style="display:none; background-color: ' + popupCoverLayerColor + '; height:' + jQuery(document).height() + 'px; left: 0px; position:absolute; top: 0px; width:100%; opacity: 0.8; filter: alpha(opacity=80); z-index:9999;"></div>');
		
		if (forceScreenWidth)
			jQuery('body').append('<div id="PopupContainerLayer" style="display:none;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:' + ((forceScreenWidth-popupWidth-popupBorderWidth)/2)  + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth) + 'px; width:' + (popupWidth + popupBorderWidth*2) + 'px; height:' + (popupHeight + popupBorderWidth*2) + 'px; z-index:99999;"></div>');
		else
			jQuery('body').append('<div id="PopupContainerLayer" style="display:none;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:50%;margin-left: -' + (popupWidth/2 + popupBorderWidth) + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth) + 'px; width:' + (popupWidth + popupBorderWidth*2) + 'px; height:' + (popupHeight + popupBorderWidth*2) + 'px; z-index:99999;"></div>');

		jQuery('#PopupContainerLayer').append(popupDiv);
		popupDiv.css({'background-color': popupBackgroundColor, 'margin-top': popupBorderWidth + 'px', 'margin-left': popupBorderWidth + 'px'});
		popupDiv.show();
		
		if (jQuery.browser.msie && (ie55 || ie6)) {
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.top_left.png\', sizingMethod=\'scale\');position:absolute;top:0px;left:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.top_right.png\', sizingMethod=\'scale\');position:absolute;top:0px;right:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.bottom_left.png\', sizingMethod=\'scale\');position:absolute;bottom:0px;left:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.bottom_right.png\', sizingMethod=\'scale\');position:absolute;bottom:0px;right:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.top.png\', sizingMethod=\'scale\');position:absolute;top:0px;left:'+popupBorderWidth+'px;width:'+popupWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.bottom.png\', sizingMethod=\'scale\');position:absolute;bottom:0px;left:'+popupBorderWidth+'px;width:'+popupWidth+'px;height:'+popupBorderWidth+'px;" ></div>');
			
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.left.png\', sizingMethod=\'scale\');position:absolute;left:0px;top:'+popupBorderWidth+'px;height:'+popupHeight+'px;width:'+popupBorderWidth+'px;" ></div>');
			jQuery('#PopupContainerLayer').append('<div style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.right.png\', sizingMethod=\'scale\');position:absolute;right:0px;top:'+popupBorderWidth+'px;height:'+popupHeight+'px;width:'+popupBorderWidth+'px;" ></div>');
		} else {
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;top:0px;left:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.top_left.png" />');
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;top:0px;right:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.top_right.png" />');
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;bottom:0px;left:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.bottom_left.png" />');
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;bottom:0px;right:0px;width:'+popupBorderWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.bottom_right.png" />');
			
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;top:0px;left:'+popupBorderWidth+'px;width:'+popupWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.top.png" />');
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;bottom:0px;left:'+popupBorderWidth+'px;width:'+popupWidth+'px;height:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.bottom.png" />');
			
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;left:0px;top:'+popupBorderWidth+'px;height:'+popupHeight+'px;width:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.left.png" />');
			jQuery('#PopupContainerLayer').append('<img style="position:absolute;right:0px;top:'+popupBorderWidth+'px;height:'+popupHeight+'px;width:'+popupBorderWidth+'px;" src="' + relativeFolder +  popupImagesFolder + 'popup.right.png" />');
		}
		
		jQuery('#PopupCoverLayer').css('filter', 'alpha(opacity=80)').fadeIn('fast', function() {
				jQuery('#PopupContainerLayer').fadeIn('fast', function() {
					if (jQuery.browser.msie && (ie55 || ie6)) {
						if (forceScreenWidth)
							jQuery('body').append('<div id="PopupCloseLayer" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.close.png\', sizingMethod=\'scale\');cursor:pointer;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:' + ((forceScreenWidth-popupWidth-popupBorderWidth)/2)  + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth - 3) + 'px; width:33px; height:33px; z-index:99999;" ></div>');
						else
							jQuery('body').append('<div id="PopupCloseLayer" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + relativeFolder +  popupImagesFolder + 'popup.close.png\', sizingMethod=\'scale\');cursor:pointer;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:50%;margin-left: ' + (popupWidth/2 - 8) + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth - 3) + 'px; width:33px; height:33px; z-index:99999;" ></div>');
					} else {
						if (forceScreenWidth)
							jQuery('body').append('<img id="PopupCloseLayer" style="cursor:pointer;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:' + ((forceScreenWidth-popupWidth-popupBorderWidth)/2)  + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth - 3) + 'px; width:33px; height:33px; z-index:99999;" src="' + relativeFolder +  popupImagesFolder + 'popup.close.png" />');
						else 
							jQuery('body').append('<img id="PopupCloseLayer" style="cursor:pointer;position:absolute;top:' + ($(window).scrollTop() + (jQuery(window).height() / 2)) + 'px;left:50%;margin-left: ' + (popupWidth/2 - 8) + 'px;margin-top: -' + (popupHeight/2 + popupBorderWidth - 3) + 'px; width:33px; height:33px; z-index:99999;" src="' + relativeFolder +  popupImagesFolder + 'popup.close.png" />');
							
					}
				});
			});
		
		jQuery('#PopupCoverLayer').live('click', function() {
			jQuery('#PopupCloseLayer').remove();
			jQuery('#PopupContainerLayer').fadeOut('fast', function() {
					jQuery('#PopupContainerLayer').remove();
					jQuery('#PopupCoverLayer').fadeOut('fast', function() {jQuery('#PopupCoverLayer').remove()});
				});
			
		});
		jQuery('#PopupCloseLayer').live('click', function() {
			jQuery('#PopupCloseLayer').remove();
			jQuery('#PopupContainerLayer').fadeOut('fast', function() {
				jQuery('#PopupContainerLayer').remove();
				jQuery('#PopupCoverLayer').fadeOut('fast', function() {jQuery('#PopupCoverLayer').remove()});
			});
			
		});
		
	}
	

