/*
 * EasyModal - jQuery Plugin
 * Modal Windows
 *
 * Alexandra Cianci
 * Consorzio Mario Negri Sud
 *
 * Version: 0.2 (15/04/2010)
 * Requires: jQuery v1.4+
 *
 */

(function($) {

	$.fn.modal = function(options) {
		var defaults = {
			speed: 1000,
			containerwidth: '250px',
//			closebutton: '<a href="#" class="modalCloseImg" title="Close"></a>'
			closebutton: '<a href="#" class="closeButton close" title="Close"><span>close</span></a>'
		};

		// Extend our default options with those provided.
		if (options) {
			var opts = $.extend(defaults, options);
		}

		var ie6 = $.browser.msie && parseInt($.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object";
		// if (ie6) { alert("ie6"); }

		
		// build modal windows
		$('.modal-window').each(function() {
			$(this).prepend(opts.closebutton);
			if(ie6) {
				$(this).append('<div class="clear"></div>');
			}
			//$(this).show();
			//alert("ciao");
			//$(this).prepend('<div id="closeWindow"><a title="Close" id="closeWindowButton" class="close" href="#"><span>close</span></a></div>');
			//$(this).prepend('<a href="#" class="modalCloseImg" title="Close"></a>');
		});


		return this.each(function(index) {   
            $.modal(this, opts, ie6);
        });
    };
 
	// close modal windows
	$.fn.close = function() {
		$('#modal-overlay').remove();
		$('.modal-window').hide();
	}

	$.modal = function(id, opts, ie6) {
		$(id).click(function(e) {
			//Cancel the link behavior
			e.preventDefault();
			//Get the A tag
			var id = $(this).attr('href');
			//$(id).modal();
			$.fn.close();
			//Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
						
		/*	var overlay = '<div id="modal-overlay"></div>';
			$('body').append(overlay);
			$('#modal-overlay').css({'width':maskWidth,'height':maskHeight});
			//$('#modal-overlay').fadeIn(1000);	
			$('#modal-overlay').fadeTo(opts.speed,0.7);	
		*/	
			var overlay_wrapper = $('<div id="modal-overlay"></div>').appendTo( $('BODY') );
			$(overlay_wrapper).css({'width':maskWidth,'height':maskHeight});
			$(overlay_wrapper).css({'position':'fixed','left':'0','top':'0'});
			if (ie6) {
				$(overlay_wrapper).css({'position':'absolute'});
			}
	
			$(overlay_wrapper).fadeTo(opts.speed,0.7);	
			
			//Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();
			
			//var debug = "winH: "+winH+"; cssTop: "+cssTop+"; id.height: "+$(id).height();
			//alert(debug);

	// add navigation
/*	$(id).append(
		nav_left	= $('<a name="modal" href="#scheda1" id="modal-link-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a>'),
		nav_right	= $('<a name="modal" href="#scheda3" id="modal-link-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a>')
	);
		if ((currentArray.length > 1) || currentIndex != 0) {
			nav_left.show();
		}

		if ((currentArray.length > 1) || currentIndex != (currentArray.length -1)) {
			nav_right.show();
		}

/*		nav_left.click(function(e) {
			e.preventDefault();
			$.fancybox.prev();
		});

		nav_right.click(function(e) {
			e.preventDefault();
			$.fancybox.next();
		});
*/
			//Set the popup window to center
			var cssTop = parseInt( winH/2) - $(id).height()/2 + $(document).scrollTop();
			$(id).css('top', cssTop );		
			$(id).css('left', winW/2-$(id).width()/2);
		
			//transition effect
			$(id).fadeIn(opts.speed); 
			
			if($(id).height() >= winH) {
				var newTop = parseInt( ($(id).height() - winH)/2 );
				$(id).animate({ top:"+="+newTop+"px" }, "slow");

			}
			//if mask is clicked
			$('#modal-overlay').click(function() {
				$.fn.close();
			});			
			//if close link is clicked
			$('.close').click(function(e) {
				e.preventDefault();
				$.fn.close();
			});			
	/*
			$.fancybox.next = function() {
				return $.fancybox.pos( currentIndex + 1);
			};
			
			$.fancybox.prev = function() {
				return $.fancybox.pos( currentIndex - 1);
			};
			$.fancybox.pos = function(pos) {
				if (busy) return;
		
				pos = parseInt(pos);
		
				if (pos > -1 && currentArray.length > pos) {
					selectedIndex = pos;
					fancybox_start();
				}
		
				if (currentOpts.cyclic && currentArray.length > 1 && pos < 0) {
					selectedIndex = currentArray.length - 1;
					fancybox_start();
				}
		
				if (currentOpts.cyclic && currentArray.length > 1 && pos >= currentArray.length) {
					selectedIndex = 0;
					fancybox_start();
				}
		
				return;
			};
		*/
		
				});
		
	

	};

})(jQuery);

