﻿( function($) {

$(document).ready(function () {
    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[set^=#]').click(function () {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('set'); //Get Popup href to define size
        var popQS_URL = $(this).attr('qs_url'); //get url if using a query select picker
        //Pull Query & Variables from href URL
        var query = popURL.split('?');
        var dim = query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
        var id = dim[1].split('=')[1];

        document.getElementById("varItemId").value = id;


        var fnLoc = -1
        var fieldName

        //set up the iframe if needed
        if (popQS_URL != undefined) {
            //check and replace [fieldname] with the value of that field if necessary
            fnLoc = popQS_URL.indexOf('[')
            if (fnLoc != -1) {
                //need to extract field name and replace with a value
                fieldName = popQS_URL.substring(fnLoc)
                fieldName = fieldName.replace('[', '')
                fieldName = fieldName.replace(']', '')
                popQS_URL = popQS_URL.replace('[' + fieldName + ']', document.getElementById(fieldName).value)
            }
            document.getElementById('QS_Frame').src = 'queryform_lu.aspx?' + popQS_URL;
            document.getElementById('QS_Writeback_ID_Field').value = $(this).attr('qs_idField');
            document.getElementById('QS_Writeback_Desc_Field').value = $(this).attr('qs_descField');
        }

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a class="close btnClose_2"><div class="btnClosePopup btnClose_2" title="Cancel this action" alt="Cancel" /></a>');

        //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        //Apply Margin to Popup
        $('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

        return false;
    });

    //Close Popups and Fade Layer
    $('a.close, #fade, .cancel').live('click', function () { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function () {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });

    $('button.btnResetFader').click(function () {
        $('#fade , .popup_block').fadeOut(function () {
            $('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });
});

function qsSelect(id, desc) {
    //write the values in id & desc back to the fields described on the lookup popups hidden fields
    //QS_Writeback_ID_Field
    //QS_Writeback_Desc_Field
    var idField = document.getElementById('QS_Writeback_ID_Field').value
    var descField = document.getElementById('QS_Writeback_Desc_Field').value

    document.getElementById(idField).value = id
    //Fire onchange event in case there's a javascript onchange event against hidden control.
    try
    {
        document.getElementById(idField).onchange()
    }
    catch (err)
    {}

    document.getElementById(descField).value = desc
    document.getElementById('cmdClose').click()
}
})(jQuery);

