if( typeof( JSEN ) != "undefined" || !JSEN )
{
    var JSEN =
    {
        // Base URL
        baseUrl : "http://www.missworld.com/",

        default_action_13 : ".def-link",

        // GeoLocation obj
        geoLocation : null,

        // Get geo location information
        GetGeoLocation : function()
        {
            $.ajax({ type : "GET", url : "http://j.maxmind.com/app/geoip.js", dataType : "script", async : true, success : function( s ) {
                JSEN.geoLocation          = {  };
                JSEN.geoLocation.lat      = geoip_latitude();
                JSEN.geoLocation.lng      = geoip_longitude();
                JSEN.geoLocation.country  = geoip_country_name();
                JSEN.geoLocation.city     = geoip_city();
            } });
        },

        dialogErrorsDivsIds : Array(),
        dialogErrorsDivs : 0,
        dialogErrorsClose : function ()
        {
            for( var i = 0; i < this.dialogErrorsDivsIds.length; i++ )
            {
                container_obj = $(this.dialogErrorsDivsIds[i]);
                if( container_obj )
                    container_obj.hide();
            }
        },

        refreshPage : function()
        {
            rand_no = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);
            if( document.location.href.search( '/\\?/' ) == -1 )
                document.location = document.location.href + "?&_JSENr=" + rand_no;
            else
                document.location = document.location.href + "&_JSENr=" + rand_no;
        },

        dialogErrors : function( error_arr )
        {
            if( !error_arr || typeof( error_arr ) != "object" || !error_arr.length )
                return;

            this.dialogErrorsClose();

            for( var i = 0; i < error_arr.length; i++ )
            {
                container_obj = false;
                appendto_obj = false;
                if( !error_arr[i].container || !error_arr[i].container.length )
                {
                    if( !error_arr[i].appendto || !error_arr[i].appendto.length )
                        continue;

                    appendto_obj = $(error_arr[i].appendto);
                    if( !appendto_obj )
                        continue;
                } else
                {
                    container_obj = $(error_arr[i].container);
                    if( !container_obj )
                        continue;

                    this.dialogErrorsDivsIds.push( error_arr[i].container );
                }

                if( appendto_obj )
                {
                    container_id = "JSENDiaErr" + this.dialogErrorsDivs;
                    appendto_obj.append( '<div id="' + container_id + '"></div>' );
                    this.dialogErrorsDivs++;
                    container_obj = $("#" + container_id);

                    if( !container_obj )
                        continue;

                    this.dialogErrorsDivsIds.push( "#" + container_id );
                }

                if( !container_obj.hasClass( "ui-state-error" ) )
                    container_obj.addClass( "ui-state-error" );
                if( !container_obj.hasClass( "dialog-field-error" ) )
                    container_obj.addClass( "dialog-field-error" );

                if( error_arr[i].text_message && error_arr[i].text_message.length )
                    container_obj.text( error_arr[i].text_message );
                if( error_arr[i].html_message && error_arr[i].html_message.length )
                    container_obj.html( error_arr[i].html_message );


                container_obj.show();
            }
        },

        closeAjaxDialog : function( suffix )
        {
            if( $("#JSENd" + suffix) ) { $("#JSENd" + suffix).remove(); };
        },

        // Create AJAX request
        createAjaxDialog : function( o )
        {
            var defaults = {
                width             : 600,
                height            : 400,
                suffix            : "",
                cache_response    : false,
                stack             : true,
                url               : null,
                draggable         : false,
                title             : "",
                parent_tag        : "body",
                cssclass          : []
            };

            var options = $.extend( defaults, o );

            options.cssclass.push( "jsenOverlay" );

            if( ( typeof( options.url ) != "undefined" ) &&
                ( options.url ) )
            {
                // Remove Dialog ( if previously created )
                if( $("#JSENd" + options.suffix) ) { $("#JSENd" + options.suffix).remove(); };

                // Create Dialog
                $(options.parent_tag).append( '<div id="JSENd' + options.suffix + '"></div>' );

                if( $("#JSENd" + options.suffix) )
                {
                    $("#JSENd" + options.suffix).dialog( {
                            width: options.width,
                            height: options.height,
                            draggable: options.draggable,
                            dialogClass: options.cssclass,
                            stack: options.stack,
                            title: options.title,

                            modal: true,
                            minHeight: 300,
                            overlay: { opacity: 0.4, background: "#000" },
                            show: "",
                            autoOpen: false,
                            center: true,
                            resizable: false
                           } );

                    $.ajax({
                        type: "GET",
                        url: options.url,
                        cache: options.cache_response,
                        async: true,

                        success: function( html ) {
                            $("#JSENd" + options.suffix).html( html );
                            $("#JSENd" + options.suffix).dialog( "open" );
                            //$(".jsenOverlay").dialogAlignCenter();
                        },

                        error: function( err ) {
                            $("#JSENd" + options.suffix).html( "ERROR!!!" );
                            $("#JSENd" + options.suffix).dialog( "open" );
                            //$(".jsenOverlay").dialogAlignCenter();
                        }
                    });

                    $("#JSENd" + options.suffix).dialog( "option", "position", "center" );
                };

            }
        },

        // Close Loading div...
        closeLoadingDialog : function( suffix )
        {
            // Remove Dialog ( if previously created )
            if( $("#jsen_loading" + suffix) ) { $("#jsen_loading" + suffix).remove(); };
        },

        // Create Loading div...
        createLoadingDialog : function( o )
        {
            var options = $.extend( {
                width       : 320,
                height      : 100,
                suffix      : "",
                message     : "",
                stack       : true,
                draggable   : false,
                title       : "Please wait...",
                parent_tag  : "body",
                cssclass    : []
            }, o );

            //options.cssclass.push( "jsenOverlay" );

            // Remove Dialog ( if previously created )
            if( $("#jsen_loading" + options.suffix) ) { $("#jsen_loading" + options.suffix).remove(); };

            // Create Dialog
            $(options.parent_tag).append( '<div id="jsen_loading' + options.suffix + '"></div>' );

            if( $("#jsen_loading" + options.suffix) )
            {
                $("#jsen_loading" + options.suffix).dialog( {
                        width: options.width,
                        height: options.height,
                        draggable: options.draggable,
                        dialogClass: options.cssclass,
                        stack: options.stack,
                        title: options.title,

                        modal: true,
                        minHeight: 300,
                        overlay: { opacity: 0.4, background: "#000" },
                        show: "",
                        autoOpen: false,
                        center: false,
                        resizable: false
                       } );

                $("#jsen_loading" + options.suffix).html( options.message + '<div id="loading-animation-pb"></div>' );

                $( "#loading-animation-pb" ).progressbar({ value: 100 });

                $("#jsen_loading" + options.suffix).dialog( "option", "position", "center" );
                $("#jsen_loading" + options.suffix).dialog( "open" );
                //$(".jsenOverlay").dialogAlignCenter();

            };
        },



        keyPressHandler : function()
        {
            var r;
            if( (r = $(JSEN.default_action_13)).length > 0 )
            {
                r.each( function()
                {
                    var a = $(this);
                    while( a.attr( "tagName" ) != "FORM" )
                    {
                        a = a.parent()
                    };

                    a.find("input").keypress( function(e){ if( ( e.which && e.which == 13 ) || (e.keyCode && e.keyCode == 13) ) { a.find(JSEN.default_action_13).click(); return false } else { return true } } );

                }
                )
            }
        }

    };
};


$.fn.dialogAlignCenter = function() {
    return this.each(function(){
        var obj = $(this);
        obj.css({ top: ( $(window).scrollTop() + ($(window).height() - $(this).height())/2  ) + "px" });
    });
};



