﻿$(function() {
    // Init jqm
    $('#login-dialog').jqm({ modal: true, onShow: onJqmShow, onHide: onJqmHide });
    $('#info-dialog').jqm({ modal: true, onShow: onJqmShow, onHide: onJqmHide });

    $('#signinbtn').click(function() {
        var f = $('div.Zeppelin div.content iframe')[0];
        var doc = f.contentWindow ? f.contentWindow.document :
				f.contentDocument ? f.contentDocument : f.document;
        $('input#frmPassword', doc).focus();
    });

    function onJqmShow(hash) {
        $('body').addClass('modal-visible');
        hash.w.show();
    }
    function onJqmHide(hash) {
        $('body').removeClass('modal-visible');
        hash.w.hide();
        hash.o.remove();
    }
    function openLoggingInWindow() {
        var loggingInWindow = window.open('', 'loggingIn' + parseInt((Math.random() * 1000000)), 'width=50,height=10,resizable=1,scrollbars=yes');
        var doc = loggingInWindow.document;
        doc.open('text/html', 'replace');
        doc.write('<html><head><title>Logging in...</title></head><body style="font-family: Helvetica, Arial, Tahoma;font-size: 20px;color: white;font-weight: bold;background-color: #505050;text-align: center;">Logging in...</body></html>');
        doc.close();
        loggingInWindow.restore = function(url) {
            loggingInWindow.resizeTo(1024, 768);
            loggingInWindow.location.href = url;            
        };
        return loggingInWindow;
    }

    $('a.login-link').click(function() {
        var link = $(this);
        var loggingInWindow;

        $('#login-signin').unbind().click(function() {
            var ecode = $('#login-ecode').val();
            if (ecode != '') {
                $('#login-wait').css('display', 'inline');
                loggingInWindow = openLoggingInWindow();

                $.post(connectLoginUrl + '?Action=PortalLogin&rnd=' + new Date().getTime(), { Ecode: ecode },
                            function(data) {
                                if (data.success == true) {
                                    $('#login-frame').unbind().load(function() {
                                        setTimeout(function() { getActualLink(link, true, data) }, 1000);
                                    }).attr('src', data.url);
                                } else {
                                    loggingInWindow.close();
                                    $('#login-wait').css('display', 'none');

                                    var template = $('#cfTip-templates .tooltip-tabs-container').clone();
                                    $(template).find('.tooltip_content').html(data.msg);

                                    var tipAnchor = $('#login-signin');
                                    tipAnchor.cfTip('remove');
                                    tipAnchor.unbind('blur').cfTip({
                                        type: 'error',
                                        scope: 'body',
                                        tabs: true,
                                        content: template
                                    }).blur(function() {
                                        $this = $(this);
                                        thisTooltip = $this.data('cfTip');
                                        if (thisTooltip && !thisTooltip.hasClass('tooltipHovered')) {
                                            $this.cfTip('remove');
                                        }
                                    }).focus();

                                    tipAnchor.data('cfTip').unbind('mouseleave');
                                    tipAnchor.data('cfTip').bind('mouseleave', function() {
                                        tipAnchor.cfTip('remove');
                                    });
                                }
                            }, 'json'
                        );

            }
            return false;
        });


        if ($('.Zeppelin .signIn').is(':visible')) {
            $('#login-ecode').val('');
            $('#login-dialog').jqmShow();
        } else {
            getActualLink(link);
        }

        function getActualLink(link, fromLoginDialog, portalLoginData) {
            if (link.attr('func') == 'refresh') {
                if (loggingInWindow)
                    loggingInWindow.close();

                if (fromLoginDialog) {
                    $('#login-dialog').jqmHide();
                }
                window.location.reload(true);
                return;
            }

            if (!loggingInWindow) {
                loggingInWindow = openLoggingInWindow();
            }

            if (!link.attr('linkCode') || link.attr('linkCode') == '') {
                $.post(connectLoginUrl + '?Action=GetConnectLink&rnd=' + new Date().getTime(), { ConnectPageName: link.attr('link') },
                            function(data) {
                                $('#login-wait').css('display', 'none');
                                if (data.success == true) {
                                    if (fromLoginDialog) {
                                        $('#login-dialog').jqmHide();
                                        Zeppelin.setLoggedInStatus(true, portalLoginData.companyName);
                                    }
                                    if (link.attr('func') == 'redirect') {
                                        loggingInWindow.restore(data.url);
                                    } else {
                                        loggingInWindow.close();
                                        window.location.href = data.url;
                                    }
                                } else {
                                    loggingInWindow.close();

                                    if (fromLoginDialog) {
                                        Zeppelin.signOut();
                                        $.post(connectLoginUrl + '?Action=PortalLogout&rnd=' + new Date().getTime(), null, function(data) { }, 'json');

                                        var template = $('#cfTip-templates .tooltip-tabs-container').clone();
                                        $(template).find('.tooltip_content').html(data.msg);

                                        var tipAnchor = $('#login-signin');
                                        tipAnchor.cfTip('remove');
                                        tipAnchor.unbind('blur').cfTip({
                                            type: 'error',
                                            scope: 'body',
                                            tabs: true,
                                            content: template
                                        }).blur(function() {
                                            $this = $(this);
                                            thisTooltip = $this.data('cfTip');
                                            if (thisTooltip && !thisTooltip.hasClass('tooltipHovered')) {
                                                $this.cfTip('remove');
                                            }
                                        }).focus();

                                        tipAnchor.data('cfTip').unbind('mouseleave');
                                        tipAnchor.data('cfTip').bind('mouseleave', function() {
                                            tipAnchor.cfTip('remove');
                                        });
                                    } else {
                                        showAlert(data.msg);
                                    }
                                }
                            }, 'json'
                        );
            } else {
                $.post(connectLoginUrl + '?Action=GetPortalSystemLink&rnd=' + new Date().getTime(), { Link: link.attr('link') ? link.attr('link') : '', LinkCode: link.attr('linkCode') },
                                function(data) {
                                    $('#login-wait').css('display', 'none');
                                    if (data.success == true) {
                                        if (fromLoginDialog) {
                                            $('#login-dialog').jqmHide();

                                            if (portalLoginData.companyName) {
                                                Zeppelin.setLoggedInStatus(true, portalLoginData.companyName);
                                            }
                                        }
                                        if (link.attr('func') == 'redirect') {
                                            loggingInWindow.restore(data.url);
                                        } else {
                                            loggingInWindow.close();
                                            window.location.href = data.url;
                                        }
                                    } else {
                                        loggingInWindow.close();

                                        if (fromLoginDialog) {
                                            Zeppelin.signOut();
                                            $.post(connectLoginUrl + '?Action=PortalLogout&rnd=' + new Date().getTime(), null, function(data) { }, 'json');

                                            var template = $('#cfTip-templates .tooltip-tabs-container').clone();
                                            $(template).find('.tooltip_content').html(data.msg);

                                            var tipAnchor = $('#login-signin');
                                            tipAnchor.cfTip('remove');
                                            tipAnchor.unbind('blur').cfTip({
                                                type: 'error',
                                                scope: 'body',
                                                tabs: true,
                                                content: template
                                            }).blur(function() {
                                                $this = $(this);
                                                thisTooltip = $this.data('cfTip');
                                                if (thisTooltip && !thisTooltip.hasClass('tooltipHovered')) {
                                                    $this.cfTip('remove');
                                                }
                                            }).focus();

                                            tipAnchor.data('cfTip').unbind('mouseleave');
                                            tipAnchor.data('cfTip').bind('mouseleave', function() {
                                                tipAnchor.cfTip('remove');
                                            });
                                        } else {
                                            showAlert(data.msg);
                                        }
                                    }
                                }, 'json'
                        );
            }
        }

        return false;
    });

    function showAlert(msg) {
        $('#info-dialog .info').html(msg);
        $('#info-dialog').jqmShow();
    }

});	