modalWindow = {
    bgId        : '#fader_bg',
	messageID	: '#popup-message',
	loadingID	: '#loading-block',
    _init       : false,
    popup       : '.popup-list',
	hideTimeOut	: 3000,
	lock		: false,
	
	oldTitle	: null,
	
    init        : function() {
        $(document.body).append('<div id="fader_bg"></div>');
        
        var docHeight = $(document).height();
        $('#fader_bg').css({
            opacity: 0.75,
            height: docHeight + "px",
            display : "none"
        });
        
        $("#fader_bg, .cancel").click(function(){
			if (modalWindow.lock)
				return false;
			
            modalWindow.hide();
			
			return true;
        });
        
        $(document).keypress(function(e){
            if (e.keyCode == 27){
				if (modalWindow.lock)
					return false;
			
                modalWindow.hide();
            }
			
			return true;
        });
        
        $(window).resize(function(){
            var new_width = $(window).width();
            $('#fader_bg').css({
                width: new_width + "px"
            });            
        });
        
        
        
		modalWindow._init = true;
    },
	
    show        : function(id, lock) {
        if (!modalWindow._init) {
            modalWindow.init();
        }      
        
		if (lock === true) {
			modalWindow.lock = true;
		}
		
        $(modalWindow.bgId).width($(window).width() + "px");
        $(modalWindow.bgId).height($(document).height() + "px");
        $(modalWindow.bgId).css('display', 'block');  
        
        if (typeof(id) != 'undefined') {
            modalWindow.showUI(id);
        }
    },
    
    
    
    showVideo : function(id) {
        if (!modalWindow._init) {
            modalWindow.init();
        }      
        
        $(modalWindow.bgId).width($(window).width() + "px");
        $(modalWindow.bgId).height($(document).height() + "px");
        $(modalWindow.bgId).css('display', 'block');  
        
        if (typeof(id) != 'undefined') {
            modalWindow.centerVideo(id);
            $(id).css('display', 'block');
        }
    },    
    
	
    hide        : function() {
        $(modalWindow.bgId).css('display', 'none');
        modalWindow.hideUI();
		modalWindow.lock = false;
		
		if ($("#videoplayer").length>0){
    		$("#videoplayer").remove();
    	}		
		
		
    },
	
    showUI      : function (id) {
		modalWindow.hideUI();
    	modalWindow.center(id);
    	$(id).css('display', 'block');
    },
	
    hideUI      : function () {
    	if(modalWindow.oldTitle){
    		$(modalWindow.loadingID + ' h3').text(modalWindow.oldTitle);
    		modalWindow.oldTitle = null;
    	}
    	
        $(modalWindow.popup).css('display', 'none');
        if ($("#videoplayer").length>0){
	        $("#videoplayer").css('display','none');
        }
    },
	
	showLoading : function (title) {
		modalWindow.show(modalWindow.loadingID, true);
		
		if(typeof title != 'undefined'){
			modalWindow.oldTitle = $(modalWindow.loadingID + ' h3').text();
			$(modalWindow.loadingID + ' h3').text(title);
		}
		
	},
	
	showMessage	: function (text) {
		modalWindow.hideUI();
		$(modalWindow.messageID).find('h3').html(text);
		modalWindow.show(modalWindow.messageID);
		setTimeout(modalWindow.hide, modalWindow.hideTimeOut);
	},
	
	needRefresh	: function () {
		modalWindow.hideUI();
		$(modalWindow.messageID).find('h3').html("Ouch! Something went wrong with that last update. Please refresh this page <a href='javascript:location.reload()'>Refresh</a>... you never know!");
		modalWindow.show(modalWindow.messageID, true);
	},
	
	showSaveList: function () {
		modalWindow.show("#save-list");
	},
	
	showOverwriteList: function () {
		modalWindow.show("#overwrite-list");
	},
	
	netLoad		: function () {
		
	},
	
	netError	: function () {
		
	},
	
    center 		: function (id) {
    	var left = Math.round(($(window).width() - $(id).width())/2);
    	var top = Math.round(($(window).height() - $(id).height())/2);
    	$(id).css ({
    		'left' 		: left + 'px',
    		'top' 		: top + 'px',
			'position' 	: 'fixed',
			'z-index'	: 10
    	});
    },
    
    
    centerVideo : function (id) {
    	//var left = Math.round(($(window).width() - $(id).width())/2);
    	//var top = Math.round(($(window).height() - $(id).height())/2) + $(window).scrollTop();
    	var left = Math.round(($(window).width() - $(id).width())/2);
    	var top = Math.round(($(window).height() - $(id).height())/2);
    	$(id).css ({
    		'left' 		: left + 'px',
    		'top' 		: top + 'px',
			'position' 	: 'fixed',
			'z-index'	: 10
    	});
    }
};
