﻿///<reference path="~/Scripts/jQuery/jquery-1.3.2.min.js" />
///<reference path="~/Scripts/jQuery/jquery-ui-1.7.3.min.js" />

/*
 * PSC.Dialog v 1.0.0
 *
 * Copyright (c) 2010 Peter St Clair
 * All rights reserved.
 *
 * Date: 2010-07-14
 */
;


(function($) {
    $.namespace("PSC.Dialog");
    PSC.Dialog = {

        version: '1.0.0',
        activeInstance: null,
        defaultOptions: {
            title: 'Rennie Beach Tipping Society',
            width: 600,
            height: 400,
            modal: true,
            iframe: true,
            useIframeTitle: false,
            formFieldSelector: null,
            elementId: null,
            defaultReturnValue: null
        },
        options: {},
        onClose: function() { },
        returnValue: null,
        initialize: function(e) {
            //Apply any dialog behaviours
            $('.psc-dialog').click(function(e) {
                e.preventDefault();
                return PSC.Dialog.open(this);
            });
        },

        setTitle: function(title) {
            ///<summary>
            /// Set's the dialog windows Title    
            ///</summary>
            $('#ui-dialog-title-psc-dialog').html(title);
        },
        //Opens any dialogs for the system
        open: function(src, opts) {

            //iframe:true,width:850,height:550,modal:true,onClose:loadLegalData,formFields:['#ctl00_ctl00_BodyContentPlaceholder_BodyContentPlaceholder_ctrl1', '#ctl00_ctl00_BodyContentPlaceholder_BodyContentPlaceholder_ctrl2']
            var url = '';


            if (typeof (src) != 'string') {
                url = src.href;
                var qs = null;

                var srcOptions = window['eval']('({' + src.getAttribute('rel') + '})');
                if (typeof (srcOptions.onClose) != 'undefined' && srcOptions != null) {
                    PSC.Dialog.onClose = srcOptions.onClose;
                }

                this.options = $.extend({}, this.defaultOptions, srcOptions);

                if (src.title && src.title != '') {
                    this.options.title = src.title;
                }
            } else {
                url = src;
                this.options = $.extend({}, this.defaultOptions, opts);
                if (typeof (opts.onClose) != 'undefined' && opts != null) {
                    PSC.Dialog.onClose = opts.onClose;
                }
            }

            PSC.Dialog.returnValue = this.options.defaultReturnValue;

            if (this.options.formFieldSelector != null && this.options.formFieldSelector.length > 0) {

                elements = jQuery(this.options.formFieldSelector.join(','))
                qs = elements.serialize();

                //Strip off ASP.NET auto-generated field prefixes to simplify QS
                nameValues = jQuery.query.load('?' + qs);
                qsParams = {};
                jQuery.each(nameValues.get(), function(key, val) {
                    var nameSegments = key.split('$');
                    isElementArray = !isNaN(parseInt(nameSegments[nameSegments.length - 1]));
                    if (isElementArray) {
                        //handle multiple values for lists.
                        eval('var simpleKey = nameSegments[nameSegments.length - 2];if (qsParams.simpleKey) {qsParams.simpleKey.push(val);} else {qsParams.simpleKey = [val];}');
                    } else {
                        eval('qsParams.' + nameSegments[nameSegments.length - 1] + ' = \'' + val + '\'');
                    }
                });
                qs = jQuery.param(qsParams);

                if (qs.length > 0) {
                    url += ((url.indexOf('?') > -1) ? '&' : '?') + qs;
                }
            }

            if (this.options.iframe == true) {

                this.activeInstance = '#psc-dialog';
                var iframeHtml = '<div id="psc-dialog" style="overflow:hidden;">' +
                                 '  <iframe id="psc-dialog-iframe" class="psc-dialog-content" frameborder="0" name="psc-dialog-iframe" src="javascript:false;document.write(\'\');" width="100%" height="100%" style="display:none;"></iframe>' +
                                 '<div>';
                $(document.body).append(iframeHtml);

                var iframe = $('#psc-dialog-iframe');
                iframe[0].src = url;

                if (this.options.useIframeTitle == true) {
                    $(document).bind('PopupReady', function(e) {
                        var title = $('#psc-dialog-iframe').contents().find('title').html().trim();
                        PSC.Dialog.setTitle(title);
                    });
                }
            }
            else {
                this.activeInstance = this.options.elementId;
            }

            $(this.activeInstance).dialog({
                bgiframe: true,
                autoOpen: true,
                width: this.options.width + 25,
                height: this.options.height + 25,
                modal: this.options.modal,
                title: this.options.title,
                show: 'blind',
                open: function(e, ui) {
                    if (PSC.Dialog.options.iframe == true) {
                        //IE needs a delay, as the blind effect seems to cause problems,
                        //by having the contents of the IFrame disappear    
                        if ($.browser.msie) {
                            setTimeout(function() { $('#psc-dialog-iframe').show(); }, 500);
                        } else {
                            $('#psc-dialog-iframe').show();
                        }
                    }
                },
                close: function(e, ui) {
                    var onCloseHandler = PSC.Dialog.onClose;
                    PSC.Dialog.onClose = function() { };
                    
                    if (PSC.Dialog.options.iframe == true) {
                        $('#psc-dialog-iframe')
                            .html('')
                            .attr('src', 'javascript:false;document.write(\'\');')
                            .hide()
                            .remove();
                    }
                    $(PSC.Dialog.activeInstance).dialog('destroy').remove();

                    if (onCloseHandler != null) {
                        if (typeof onCloseHandler === "string") {
                            eval(onCloseHandler)
                        }
                        else {
                            onCloseHandler(PSC.Dialog.returnValue);
                        }
                    }
                    if (typeof ($.unblockUI) != 'undefined')
                        $.unblockUI();
                }
            });
            return false;
        },

        setReturnValue: function(returnValue, isIframeDialog, autoClose) {

            var win = (isIframeDialog && isIframeDialog == true) ? window.parent : window;
            if (win.PSC.Dialog) {
                win.PSC.Dialog.returnValue = returnValue;
                if (autoClose == true) {
                    win.PSC.Dialog.close();
                }
            }
        },

        close: function(retVal) {

            if (typeof (retVal) != 'undefined') {
                PSC.Dialog.returnValue = retVal;
            }
            $(PSC.Dialog.activeInstance).dialog('close');
        }
    };
})(jQuery);
