/**********************************/
/*          VENTANA MODAL         */
/**********************************/
jQuery.extend(jQuery, {
	vModal: function(options) {
		var opt = {
			ancho: 500,
			id: null
		};
    	$.extend(opt, options);
		
		if(opt.id!=null) 	var contenido = $('#'+opt.id).html();
		else 				var contenido = "ERROR #001";
		
		if($.browser.msie && $.browser.version <= 6.0) {
			$('head').append('<link rel="stylesheet" href="../css/ie6.css" media="all" />');
		}
		
		$('body').append('<div class="lb_overlay"></div>');
		$('.lb_lightbox').remove();
		$('body').append('<div class="lb_lightbox"><table class="lightbox"><tr><td class="lb_tl"></td><td class="lb_tt"></td><td class="lb_tr"></td></tr><tr><td class="lb_ll"></td><td class="lb_main lb">'+contenido+'</td><td class="lb_rr"></td></tr><tr><td class="lb_bl"></td><td class="lb_bb"></td><td class="lb_br"></td></tr></table><div class="lb_close"></div></div>');
		$('.lb_overlay').css({
			opacity: 0,
			zIndex: 10000,
			display: '',
			background: '#000',
			width: '100%',
			height: $(document).height()+'px',
			position: 'absolute',
			top:0,
			left:0
		}).fadeTo(200, 0.4)
		
		$('.lb_lightbox').css({ zIndex: 10001 });
		
		$('.lb_lightbox table.lightbox').css({
			borderCollapse: 'collapse',
			width: opt.ancho+'px'
		});
		
		$('.lb_lightbox .lb_close').click(function() {
			$('.lb_lightbox').fadeOut(250, function() {
				$('.lb_overlay').fadeOut(100, function() {
					$('.lb_lightbox').remove();
					$('.lb_overlay').remove();
				});
			});
		});
		
		$('.lb_lightbox').centerInClient().focus();	

		lb_callback();
		return false;
	}
});

$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
		
		y = (y < 0) ? 0 : y;
		
        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}