﻿$(document).ready(function () {
    $('a.popMenuLink[href^=#]').click(function () {
        var popURL = $(this).attr('href').substring(1).split('?');

        //Pull Query & Variables from href URL
        var popID = popURL[0]; //Get Popup Name
        var queries = popURL[1].split('&');
        var popWidth = '500';
        var popLeft = '0';
        var popTop = '0';
        for (var i in queries) {
            var fields = queries[i].split('=');
            switch (fields[0]) {
                case 'w':
                    popWidth = fields[1];
                    break;
                case 'x':
                    popLeft = fields[1];
                    break;
                case 'y':
                    popTop = fields[1];
                    break;
            }
        }
        return showPopupMenu(popID, popWidth, $(this).offset(), $(this).width(), $(this).height(), popLeft, popTop);
    });

    //Close Popups and Fade Layer
    $('a.close, #fadeMenu').live('click', function () { //When clicking on the close or fade layer...
        $('#fadeMenu , .popupMenu').fadeOut();
        return false;
    });
    $(window).resize(function () {
        var bkgndList = $('#fade, #fadeMenu');
        if (bkgndList.length != 0) {
            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(document).width();
            bkgndList.css({ width: maskWidth, height: maskHeight });
        }
    });

    //Close Popups and Fade Layer
    $('#fade').live('click', function () { //When clicking on the close or fade layer...
        if($('.popup_block').hasClass('modal')){
            return false;
        }
        $('#fade , .popup_block').fadeOut();
        return false;
    });
    //Close Popups and Fade Layer
    $('a.close').live('click', function () { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut();
        return false;
    });
    $("a.submitBtn").click(function () {
        $('#fade , .popup_block').fadeOut();
        if ($(this).is('.refreshAddInfoByPhone')) {
            refreshAddInfoByPhone($(this));
        }
        return false;
    });
});

function showPopupMenu(popID, popWidth, menuOffset, menuWidth, menuHeight, popLeft, popTop) {

    //Fade in the Popup and add close button
    var w = Number(popWidth);
    $('#' + popID).fadeIn().css({ width: w });
    w -= 40;
    $('#' + popID).children(':first').css({ width: w });


    var x = Number(popLeft) + (menuOffset.left + (menuWidth - $('#' + popID).width()) / 2);
    var y = Number(popTop) + menuOffset.top + menuHeight;

    //Apply Margin to Popup
    $('#' + popID).css({ left: x, top: y});

    //Fade in Background
    if ($('#fadeMenu').length == 0) {
        $('body').append('<div id="fadeMenu"></div>'); //Add the fade layer to bottom of the body tag.
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();
        $('#fadeMenu').css({ width: maskWidth, height: maskHeight, opacity:0.3 });
    }
    $('#fadeMenu').fadeIn();  //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
}

function refreshAddInfoByPhone(submitBtn) {
    var searchScenarios = submitBtn.parents('#jobHistoryRecords').children('#refreshOptions').find('#searchScenarios');
    var popURL = submitBtn.attr('href').substring(1).split('?');
    var queries = popURL[1].split('&');
    var popData = '';
    for (var i in queries) {
        var fields = queries[i].split('=');
        switch (fields[0]) {
            case 'data':
                popData = fields[1];
                break;
        }
    }
    $.ajax({
        url: '/PersonalInfo/RefreshAdditionalBusinessInfoByPhone?record=' + popData + '%3A' + searchScenarios.val(),
        success: function (data) {
            $('#additionalInfoByPhone').html(data);
        }
    });
}

function bindPopupLink(popLinkClass) {
    $('a.' + popLinkClass + '[href^=#]').live('click', function () {
        var popURL = $(this).attr('href').substring(1).split('?');

        //Pull Query & Variables from href URL
        var popID = popURL[0]; //Get Popup Name
        var queries = popURL[1].split('&');
        var popWidth = '500';
        var popLeft = '0';
        var popTop = '0';
        var popData = '';
        for (var i in queries) {
            var fields = queries[i].split('=');
            switch (fields[0]) {
                case 'w':
                    popWidth = fields[1];
                    break;
                case 'x':
                    popLeft = fields[1];
                    break;
                case 'y':
                    popTop = fields[1];
                    break;
                case 'data':
                    popData = fields[1];
                    break;
            }
        }
        return showPopup(popID, popWidth, popData);
    });
}

function showPopup(popID, popWidth, popTopData) {

    //Fade in the Popup and add close button
    var width = Number(popWidth);

    var popup = $('#' + popID);
    if (popTopData != undefined) {
        var submitLink = popup.find('a.submitBtn');
        submitLink.attr('href', '#?data=' + popTopData);
    }

    popup.fadeIn().css({ 'width': width });

    width -= 40;
    popup.children(':first').css({ 'width': width });

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding width defined in the css
    var popTop = $(window).scrollTop() + ($(window).height() - popup.height()) / 2;
    var popLeft = $(window).scrollLeft() + ($(window).width() - popup.width()) / 2;

    //Apply Margin to Popup
    popup.offset({ top: popTop, left: popLeft });

    //Fade in Background
    if ($('#fade').length == 0) {
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();
        $('#fade').css({ 'width': maskWidth, 'height': maskHeight, 'filter': 'alpha(opacity=80)' });
    }
    $('#fade').fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
}

