/**
 * Created by IntelliJ IDEA.
 * User: johm
 * Date: 03-feb-2011
 * Time: 13:23:42
 * To change this template use File | Settings | File Templates.
 */

function lpsModalWindowClose() {
    $('#ModalPopupWindowContent').remove();
    $('#ModalPopupWindow').remove();
    $('#ModalPopupMask').remove();
};

(function($) {
    jQuery.fn.mWindow = function (options) {
            
            
        

        var config = {
            isAjaxContent: false,
            windowPostData: null,
            isIFrame: false,
            //objName: 'p0',
            zIndex: 500,
            maskColor: null,
            maskOpacity: 0.4,
            maskWidth: '100%',
            maskHeight: '100%',
            maskTop: '0px',
            maskLeft: '0px',
            windowWidth: 'auto',
            windowHeight: 'auto',
            windowTop: null,
            windowLeft: null,
            windowContent: null,
            contentPadding: '10px',
            showButtons: true,
            showSubmitButton: true,
            showCancelButton: true,
            btnSubmitCaption: 'Acept',
            btnCancelCaption: 'Cancel',
            btnSubmitCallback: function(){},
            btnCancelCallback : function(){},
            progressMsg: null

        };
        
        lpsModalWindowClose();
        
        var opts = jQuery.extend (config, options);

        return this.each (function () {
                    
                    
                    
                    
                    
            var obj = $(this);

            
            $('<div id="ModalPopupMask"></div>').appendTo('body');
            //$('#ModalPopupMask').css('display', 'none');
            //if IE6
            $('#ModalPopupMask').css('position', 'absolute');
            //
            $('#ModalPopupMask').css('position', 'fixed');
            $('#ModalPopupMask').css('z-index', config.zIndex);
            $('#ModalPopupMask').css('left', config.maskLeft);
            $('#ModalPopupMask').css('top', config.maskTop);
            $('#ModalPopupMask').css('width', config.maskWidth);
            $('#ModalPopupMask').css('height', config.maskHeight);
            $('#ModalPopupMask').css('min-height', config.maskHeight);
            
            if(config.maskColor!=null){
                $('#ModalPopupMask').css('background-color', config.maskColor);
            }
            
            $('#ModalPopupMask').css({
                opacity: config.maskOpacity
            });
            
            
            $('<div id="ModalPopupWindow"><div id="ModalPopupWindowContent"></div></div>').appendTo('body');
            $('#ModalPopupWindow').hide();
            
            
            
            
            
            if(config.isAjaxContent){
                //$('#ModalPopupWindow').load(config.windowContent);
                $.ajax({
                    type: 'POST',
                    url: config.windowContent,
                    data: config.windowPostData,
                    //data: 'id=' + id + '&selectedDate=' + selectedDate + '&selectedHour=' + selectedHour + '&' + str,
                    success: function(results) {
                        if(results){
                            $('#ModalPopupWindowContent').html(results+'<div class="clear"></div>');
                        }
                        if(config.showButtons == true) {
                            showButtons();
                        }
                        setCSSValues();
			            
                    }
                });
            } else {
                if(config.isIFrame){
                    var strIFrame = '<iframe id="lpsIFrame" name="lpsIFrame" style="" frameborder="0" scrolling="no" width="'+config.windowWidth+'" marginwidth="0" marginheight="0" height="'+config.windowHeight+'" src="'+config.windowContent+'"></iframe>';
                    $('#ModalPopupWindowContent').html(strIFrame+'<div class="clear"></div>');
                    $('#ModalPopupWindowContent').css('padding',config.contentPadding);
                    if(config.showButtons == true) {
                        showButtons();
                    }
                    setCSSValues();
                }else {
                    $('#ModalPopupWindowContent').html(config.windowContent+'<div class="clear"></div>');
                    if(config.showButtons == true) {
                        showButtons();
                    }
                    setCSSValues();
                }
            }

            
            
        });
		
		
                
                
        function setCSSValues(){
            var wTop = 0;
            var wLeft = 0;

            if(config.windowTop == null){
                wTop = '50%';
            } else {
                wTop = config.windowTop;
            }
            
            if(config.windowLeft == null){
                wLeft = '50%';
            }else{
                wLeft = config.windowLeft;
            }
            
			

            //$('#' + objContents).css('display', 'none');
            //if IE6
            $('#ModalPopupWindow').css('position', 'absolute');
            //
            $('#ModalPopupWindow').css('position', 'fixed');
            $('#ModalPopupWindow').css('z-index', config.zIndex + 10);
            $('#ModalPopupWindow').css('overflow', 'hidden');
            $('#ModalPopupWindow').css('float', 'left');
            $('#ModalPopupWindow').css('width', config.windowWidth);
            $('#ModalPopupWindow').css('height', config.windowHeight);
            $('#ModalPopupWindow').css('left', wLeft );
            $('#ModalPopupWindow').css('top', wTop );
            $('#ModalPopupWindow').css('padding', config.contentPadding );
            
            
            $('#ModalPopupWindowContent').css('width', config.windowWidth );
            //var contentHeight = config.windowHeight-$('#ModalPopupWindow_Buttons').outerHeight(true);
            var contentHeight = config.windowHeight-50;
            //alert("window h:"+config.windowHeight + "--- Buttons h:" + $('#ModalPopupWindow_Buttons').outerHeight(true) + "--- total:" + contentHeight);
            $('#ModalPopupWindowContent').css('height', contentHeight );
            $('#ModalPopupWindowContent').css('overflow', 'auto');
            
            
            //Define margin for center alignment (vertical   horizontal)
            var popMargTop = 0;
            if(config.windowTop == null){
                popMargTop = ($('#ModalPopupWindow').outerHeight(true)) / 2;
            }
            var popMargLeft = 0;
            if(config.windowLeft == null){
                popMargLeft = ($('#ModalPopupWindow').outerWidth(true)) / 2;
            }
		
            //Apply Margin to Popup
            $('#ModalPopupWindow').css({
                'margin-top' : -popMargTop,
                'margin-left' : -popMargLeft
            });
                       
            $('#ModalPopupWindow').addClass('lpsModalContent');
            
            $('#ModalPopupWindow').show();
            //$('#ModalPopupWindow').draggable();
        }
		
		
        function showButtons(){
            if($('#ModalPopupWindow_Buttons').length) {
                $('#ModalPopupWindow_Buttons').remove();
            }
            $('#ModalPopupWindow').append('<div id="ModalPopupWindow_Buttons" class="modalButtons"></div>');
            if(config.showCancelButton==true){
                $('#ModalPopupWindow_Buttons').append('<a href="#" id="ModalPopupWindow_btnCancel" class="modal-cancel-button" title="">' + config.btnCancelCaption + '</a>');
            }
            if(config.showSubmitButton==true){
                $('#ModalPopupWindow_Buttons').append('<a href="#" id="ModalPopupWindow_btnSubmit" class="modal-submit-button" title="">' + config.btnSubmitCaption + '</a>');
            }
            $('#ModalPopupWindow_Buttons').append('<div class="clear"></div>');
	
            if(config.showSubmitButton==true){
                $('#ModalPopupWindow_btnSubmit').bind('click', function(evt){
                    evt.preventDefault();
                    if(config.progressMsg!=null){
                        if($('#ModalPopupWindow_Buttons').length) {
                            $('#ModalPopupWindow_Buttons').remove();
                        }
                        $('#ModalPopupWindowContent').html(config.progressMsg);
                    }
                    config.btnSubmitCallback.call(this);
		            
                /*
		            $("#ModalPopupContent").fadeOut(100, function(){
		            	removeObjects();
					});
		            $("#ModalPopupWindow").fadeOut(100, function(){
		            	removeObjects();
					});
		            $("#ModalPopupMask").fadeOut(100, function(){
		            	removeObjects();
					});
		            */
                });
            }
	
            if(config.showCancelButton==true){
                $('#ModalPopupWindow_btnCancel').bind('click', function(evt){
                    evt.preventDefault();
                    config.btnCancelCallback.call(this);
		            
                    $("#ModalPopupContent").fadeOut(100, function(){
                        removeObjects();
                    });
                    $("#ModalPopupWindow").fadeOut(100, function(){
                        removeObjects();
                    });
                    $("#ModalPopupMask").fadeOut(100, function(){
                        removeObjects();
                    });
                });
            }
        }
		
        //aquí
        function removeObjects(){
			
            //config.btnSubmitCallback.call(this);
            $('#ModalPopupWindowContent').remove();
            $('#ModalPopupWindow').remove();
            $('#ModalPopupMask').remove();
        }
		
    };
	
	
})(jQuery)
