﻿/// <reference path="jquery.js"/>
/*
* jmodal
* version: 2.0 (05/13/2009)
* @ jQuery v1.3.*
*
* Licensed under the GPL:
*   http://gplv3.fsf.org
*
* Copyright 2008, 2009 Jericho [ thisnamemeansnothing[at]gmail.com ] 
*  
*/
$.extend($.fn, {
    hideJmodal: function() {
        $('#jmodal-overlay').animate({ opacity: 0 }, function() { $(this).hide(); });
        $('#jquery-jmodal,.jmodal-loading').fadeOut();
        var interval = $('#jquery-jmodal').data("loadingInterval");
        if (interval) {
            $('#jquery-jmodal').data("loadingInterval", null);
            clearInterval(interval);
        }
    },
    jmodal: function(setting) {
        var ps = $.fn.extend({
            full: false,
            delay: true,
            data: {},
            buttonText: { ok: 'Ok', cancel: 'Cancel' },
            okEvent: function(e) { },
            title: 'JModal Dialog',
            content: 'This is a jquery plugin!',
            load: null,
            initWidth: 500,
            initHeight: null,
            hasCloser:null
        }, setting);
        ps.winWidth = $(window).width();
        ps.winHeight = $(window).height();
        ps.docWidth = $(document).width();
        ps.docHeight = $(document).height();
        ps.docSclLeft = $(document).scrollLeft();
        ps.docSclTop = $(document).scrollTop();
        ps.marginTop = 0;
        ps.marginLeft = 0;
        ps.loading = $(".jmodal-loading");
        ps.finish = function() {
            if (!ps.initHeight && ps.iframe) {
                $('#jquery-jmodal').invisible().show();
                ps.initHeight = window.frames[ps.iframe.attr("name")].document.body.scrollHeight + 2;
                $('#jquery-jmodal').visible().hide();
            }
            if (ps.initHeight < ps.winHeight) {
                ps.marginTop = (ps.winHeight - ps.initHeight) / 2 + ps.docSclTop - 10;
            }

            $("#jmodal-container,#jmodal-container iframe").css({
                height: ps.initHeight
            });
            $('#jquery-jmodal').css({
                height: ps.initHeight + 20,
                top: ps.marginTop
            });
            if (ps.delay) {
                ps.loading.fadeOut();
                var interval = $('#jquery-jmodal').data("loadingInterval");
                if (interval) {
                    $('#jquery-jmodal').data("loadingInterval", null);
                    clearInterval(interval);
                }
            }
            $('#jquery-jmodal').fadeIn();
            if (ps.load) {
                if (document.readyState != "complete" || window.frames[ps.iframe.attr("name")].document.readyState != "complete")
                    return setTimeout(ps.load, 1000);
                ps.load();
            }
        }
        if ($('#jquery-jmodal').length == 0) {
            $('<div id="jmodal-overlay" class="jmodal-overlay"/>' +
               "<div class='jmodal-loading'><div></div></div>" +
                '<div class="jmodal-main" id="jquery-jmodal">' +
                    '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;height:100%;">' +
                        '<tr>' +
                            '<td class="jmodal-top-left jmodal-png-fiexed">&nbsp;</td>' +
                            '<td class="jmodal-border-top jmodal-png-fiexed">&nbsp;</td>' +
                            '<td class="jmodal-top-right jmodal-png-fiexed">&nbsp;</td>' +
                        '</tr>' +
                    '<tr>' +
                        '<td class="jmodal-border-left jmodal-png-fiexed">&nbsp;</td>' +
                        '<td class="jmodal-container" id="jmodal-container">' +
                        '</td>' +
                        '<td class="jmodal-border-right jmodal-png-fiexed">&nbsp;</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td class="jmodal-bottom-left jmodal-png-fiexed">&nbsp;</td>' +
                        '<td class="jmodal-border-bottom jmodal-png-fiexed">&nbsp;</td>' +
                        '<td class="jmodal-bottom-right jmodal-png-fiexed">&nbsp;</td>' +
                    '</tr>' +
                    '</table>' +
                '</div>').appendTo($(document.body));
            ps.loading = ps.loading && ps.loading.length > 0 ? ps.loading : $(".jmodal-loading");
            $('#jmodal-overlay').css({
                height: ps.docHeight,
                opacity: 0
            }).click($.fn.hideJmodal);
        }
        $('#jmodal-overlay').css({ opacity: 0 });
        $('#jquery-jmodal').hide();


        if (ps.full) {
            $("td.jmodal-container").html('<div class="jmodal-content" id="jmodal-container-content" />')
        }
        else {
            $("td.jmodal-container").html('<div class="jmodal-title" />' +
                            '<div class="jmodal-content" id="jmodal-container-content" />' +
                            '<div class="jmodal-bottom">' +
                                '<input type="button" value="' + ps.buttonText.ok + '" />&nbsp;&nbsp;<input type="button" value="' + ps.buttonText.cancel + '" />' +
                            '</div>');
        }


        if (!ps.full) {
            $('#jquery-jmodal')
            .find('.jmodal-title')
                .html(ps.title)
                    .next().css({ padding: "20px 10px" })
                        .next()
                            .children('input:first-child')
                                .attr('value', ps.buttonText.ok)
                                    .unbind('click')
                                        .one('click', function(e) {
                                            var args = {
                                                complete: $.fn.hideJmodal
                                            };

                                            ps.okEvent(ps.data, args);
                                        })
                                            .next()
                                                .attr('value', ps.buttonText.cancel)
                                                  .unbind("click")
                                                    .one('click', $.fn.hideJmodal);
        }

        if (ps.content) {
            if (typeof ps.content == 'string') {
                $('#jmodal-container-content').html(ps.content);
            }
            if (typeof ps.content == 'function') {
                var e = $('#jmodal-container-content');
                e.holder = $('#jquery-jmodal');
                ps.content(e);
            }
            if (typeof ps.content.iframe !== "undefined" && ps.content.iframe != null) {
                {
                    //ps.iframe[0].setAttribute("name","jamod");
                    //alert(ps.iframe[0].outerHTML);
                    ps.iframe = $("<iframe frameborder=0   framespacing=0 scrolling='no'  hspace='0' width='100%' style='z-index:10;overflow:hidden' name='jmodalFrame" + new Date().valueOf() + "'  src='" + ps.content.iframe + "'></iframe>").load(ps.finish);
                    $('#jmodal-container-content').empty().append(ps.iframe);
                }
            }
            if (ps.content.jquery)
                $('#jmodal-container-content').empty().append(ps.content);
        }
        if(ps.hasCloser===true) //relative;
        {
        $('#jmodal-container-content').prepend($('<div style="width:100%; height:30px; background-color:#606060;"><div style="float:left; padding-left:10px;line-height:28px" class="White14"><strong>'+ps.title+'</strong></div><div style="float:right; padding:3px 3px 0px 0px;cursor:pointer;"><img src="images/Close001.png"></div> </div>').click(function(){$.fn.hideJmodal();}));
        }else(ps.hasCloser===false) //absolute;
         
        //layout
        $('#jmodal-overlay').show().animate({ opacity: 0.5 });
        if (ps.initWidth < ps.winWidth) {
            ps.marginLeft = (ps.winWidth - ps.initWidth) / 2 + ps.docSclLeft;
        }
        $("#jmodal-container,#jmodal-container iframe").css({
            width: ps.initWidth
        });
        $('#jquery-jmodal').css({
            width: ps.initWidth + 20,
            left: ps.marginLeft - 10
        });


        if (ps.delay) {
            ps.loading.fadeIn();
            var i = 0;
            var whirl = function() {
                ps.loading.find("div").css("top", (i * (-40)) + "px");
                i = (++i) % 12;
            }
            var loadingInterval = setInterval(whirl, 66);
            $('#jquery-jmodal').data("loadingInterval", loadingInterval)

        } else
            ps.finish();
        return ps;
    }
})