﻿//code by amr
$(function () {
 
    $('#lnk_login').fancybox({
        'type': 'ajax'
    });

    $('.signIn').click(function () {
        ulElement = null;
        $("#lnk_login").trigger('click');
        return false;
    });

    $('a.login-link').click(function () {
        ulElement = $(this);

        if ($('.Zeppelin .signIn').is(':visible')) {
            $("#lnk_login").trigger('click');
        } else {
        getActualLink($(this));
        }
        return false;
    });



    // 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 showAlert(msg) {
    $('#info-dialog .info').html(msg);
    $('#info-dialog').jqmShow();
}

function showTooltip(tipAnchor, msg) {
    var template = $('#cfTip-templates .tooltip-tabs-container').clone();
    $(template).find('.tooltip_content').html(msg);
    
    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');
    });
}


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;
}

function login(p1, p2, loginType, loadingEl) {
    
    loadingEl.css('display', 'inline');
    loggingInWindow = openLoggingInWindow();

    $.post(connectLoginUrl + '?Action=PortalLogin&rnd=' + new Date().getTime(), { p1: p1, p2: p2, loginType: loginType },
            function (data) {
                if (data.success == true) {
                    $('#login-frame').unbind().load(function () {
                        if (ulElement != null) {
                            setTimeout(function () { getActualLink(ulElement, true, data, loadingEl) }, 1000);
                        } else {
                            if (Zeppelin.isEAC(data.companyName)) {
                                loggingInWindow.close();
                            } else {
                                loggingInWindow.restore(portalUrl);
                            }
                            $.fancybox.close();

                            Zeppelin.setLoggedInStatus(true, data.companyName);
                        }
                    }).attr('src', data.url);
                } else {
                    loadingEl.css('display', 'none');
                    loggingInWindow.close();

                    showTooltip(loadingEl.next(), data.msg);
                }
            }, 'json'
        ); 
}



function getActualLink(link, fromLoginDialog, portalLoginData, loadingEl) {
    if (link.attr('func') == 'refresh') {
        if (loggingInWindow)
            loggingInWindow.close();

        if (fromLoginDialog) {
            $.fancybox.close();
            //$('#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) {
                                    if (loadingEl) loadingEl.css('display', 'none');
                                    if (data.success == true) {
                                        if (fromLoginDialog) {
                                            //$('#login-dialog').jqmHide();
                                            $.fancybox.close();
                                            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');

                                            showTooltip(loadingEl.next(), data.msg);
                                        } 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) {
                                        if (loadingEl) loadingEl.css('display', 'none');
                                        if (data.success == true) {
                                            if (fromLoginDialog) {
                                                //$('#login-dialog').jqmHide();
                                                $.fancybox.close();

                                                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');

                                                showTooltip(loadingEl.next(), data.msg);
                                            } else {
                                                showAlert(data.msg);
                                            }
                                        }
                                    }, 'json'
                            );
    }
}
