﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="../jquery-1.2.6-vsdoc.js"/>

// Modals

SCOTT.CSC.Tools.GroceryList.Modals = function(){};

SCOTT.CSC.Tools.GroceryList.Modals.prototype =
{
    DisplayStatusModal : function(params)
    {
        /// params : { title : '', message : '', hideDelay : 5000 };
        /// optionally these values can be passed as 3 arguments in order
        /// additional options are possible as the params object
        
        var __app = this;
        var context = $('#StatusModal');
        var title, message, hideDelay;
        
        clearTimeout(this.StatusModalTimeout);
        
        // handle the parameters
        if (arguments.length === 1 && params && typeof params === 'object')
        {
            title = params.title;
            message = params.message;
            hideDelay = params.hideDelay;
        }
        else
        {
            title = arguments[0];
            message = arguments[1];
            hideDelay = arguments[2];
        }
        
        if (!title)
        {
            title = 'Status';
        }
        $('.ModalTitle', context).text(title);
        $('.Message span', context).text(message);
        window.status = title + ': ' + message;
        
        $(context).hide().slideDown(250, function()
        {
            if (!hideDelay)
            {
                hideDelay = 5000;
            }
            
            $('#StatusModal').unbind('click');
            $('#StatusModal').css('cursor', 'pointer').click(function()
            {
                __app.HideStatusModal();
                return false;
            });
        
            clearTimeout(this.StatusModalTimeout);
            this.StatusModalTimeout = setTimeout(function()
            {
                __app.HideStatusModal();
            }, hideDelay);
        });
    },
    HideStatusModal : function()
    {
        var __app = this;
        var context = $('#StatusModal');
        
        $(context).slideUp(250, function()
        {
            clearTimeout(this.StatusModalTimeout);
            $('.Message span', context).empty();
        });
    },
    DisplayPromptModal : function(params)
    {
        /// params : { title : '', message : '', text : '', okCallback : function(){}, cancelCallback : function(){} };
        /// optionally these values can be passed as 5 arguments in order
        /// additional options are possible as the params object
    
        var __app = this;
        var context = $('#PromptModal');
        var title, message, text, okCallback, cancelCallback, okHandler, cancelHandler;
        
        // handle the parameters
        if (arguments.length === 1 && params && typeof params === 'object')
        {
            title = params.title;
            message = params.message;
            text = params.text;
            okCallback = params.okCallback;
            cancelCallback = params.cancelCallback;
        }
        else
        {
            title = arguments[0];
            message = arguments[1];
            text = arguments[2];
            okCallback = arguments[3];
            cancelCallback = arguments[4];
        }
        
        if (!title)
        {
            title = 'Prompt';
        }
        $('.ModalTitle', context).text(title);
        
        $('.Message span', context).text(message);
        
        if (!text)
        {
            text = '';
        }
        $('.Text input.tb', context).val(text);
        
        $(context).hide().slideDown(250, function()
        {
            $('.Text input.tb', context).focus();
        });
        
        $('.OKBtn', context).unbind('click');
        $('.CancelBtn', context).unbind('click');
        $('.Text input.tb', context).unbind('keydown');
        
        okHandler = function()
        {
            var text = $('.Text input.tb', context).val();
            if (okCallback && typeof okCallback === 'function')
            {
                okCallback(text);
            }
            __app.HidePromptModal();
            return false;
        };
        
        cancelHandler = function()
        {
            if (cancelCallback && typeof cancelCallback === 'function')
            {
                cancelCallback();
            }
            __app.HidePromptModal();
            return false;
        };
        
        $('.OKBtn', context).css('cursor', 'pointer').click(okHandler);
        $('.CancelBtn', context).css('cursor', 'pointer').click(cancelHandler);
        
        $('.Text input.tb', context).keydown(function(ev)
        {
            if (ev.keyCode === 13)
            {
                okHandler();
                return false;
            }
            else if (ev.keyCode === 27)
            {
                cancelHandler();
                return false;
            }
            else
            {
                return true;
            }
        });  
        
    },
    HidePromptModal : function()
    {
        var __app = this;
        var context = $('#PromptModal');
        
        $(context).slideUp(250, function()
        {
            $('.Message span', context).empty();
        });
    },
    DisplayConfirmModal : function(params)
    {
        /// params : { title : '', message : '', responseCallback : function(){} };
        /// optionally these values can be passed as 3 arguments in order
        /// additional options are possible as the params object
    
        var __app = this;
        var context = $('#ConfirmModal');
        var title, message, responseCallback, okHandler, cancelHandler;
        
        // handle the parameters
        if (arguments.length === 1 && params && typeof params === 'object')
        {
            title = params.title;
            message = params.message;
            responseCallback = params.responseCallback;
        }
        else
        {
            title = arguments[0];
            message = arguments[1];
            responseCallback = arguments[2];
        }
        
        if (!title)
        {
            title = 'Prompt';
        }
        $('.ModalTitle', context).text(title);
        
        if (!message)
        {
            message = 'Are you sure?';
        }
        $('.Message span', context).text(message);
        
        $(context).hide().slideDown(250);
        
        $('.OKBtn', context).unbind('click');
        $('.CancelBtn', context).unbind('click');
        
        okHandler = function()
        {
            if (responseCallback && typeof responseCallback === 'function')
            {
                responseCallback(true);
            }
            __app.HideConfirmModal();
            return false;
        };
        
        cancelHandler = function()
        {
            if (responseCallback && typeof responseCallback === 'function')
            {
                responseCallback(false);
            }
            __app.HideConfirmModal();
            return false;
        };
        
        $('.OKBtn', context).css('cursor', 'pointer').click(okHandler);
        $('.CancelBtn', context).css('cursor', 'pointer').click(cancelHandler);
    },
    HideConfirmModal : function()
    {
        var __app = this;
        var context = $('#ConfirmModal');
        
        $(context).slideUp(250);
    }
};

// Add Utility functions to Application
SCOTT.CSC.Extend(SCOTT.CSC.Tools.GroceryList.Application, 
    SCOTT.CSC.Tools.GroceryList.Modals);
