var xCoord = 0;
var yCoord = 0
var xCoordBtn = 0;
var yCoordBtn = 0;
var product = null;
var web_context = null;
// browser tests
var isIE = /MSIE/.test(navigator.userAgent);
var isFirefox = /Firefox/.test(navigator.userAgent);

jQuery(document).ready(function() {
    //Open PRODUCT AVAILABLE AT POP UP
    jQuery('a.availAt').each(function(i, n) {
        jQuery(n).click(function() {
            getBtnPositionOnScreen(this);
            hideAvailableAtPopupContainer(n);

            product = jQuery(n).find('.product-id-hidden').attr('value')
            web_context = jQuery(n).find('.web-context-hidden').attr('value')

            jQuery(this).find('img').attr('id', 'availAtBtnClicked-' + product);
            jQuery(this).find('img').removeAttr('src');

            if (document.getElementById('sort-options-select-box-id'))
            {
                var option_select_box = document.getElementById('sort-options-select-box-id');
                option_select_box.disabled = true;
            }

            jQuery('div#btnAvailAtToPopupContainer-').css('display', 'block');
            jQuery(this).find('img').attr('src', web_context + '/images/btn-availableAt-popup.gif');
            jQuery('div#btnAvailAtToPopupContainer-' + product).css('display', 'block');
            jQuery('div#btnAvailAtToPopupContainer-' + product).css('left', xCoordBtn + 'px');
            jQuery('div#btnAvailAtToPopupContainer-' + product).css('top', yCoordBtn + 'px');
        });
    });
	
	//Close PRODUCT AVAILABLE AT POP UP
    jQuery('a.closeAvailAtPopup').each(function(i, n) {
        jQuery(n).click(function() {

            product = jQuery(n).find('.product-id-hidden').attr('value')
            web_context = jQuery(n).find('.web-context-hidden').attr('value')
            jQuery("img#availAtBtnClicked-" + product).attr('src', web_context + '/images/btn-availableAt.gif');
            jQuery('div#btnAvailAtToPopupContainer-' + product).css('display', 'none');
            jQuery("img#availAtBtnClicked-" + product).removeAttr('id');

            if (document.getElementById('sort-options-select-box-id'))
            {
                var option_select_box = document.getElementById('sort-options-select-box-id');
                option_select_box.disabled = false;
            }

        });
    });
  
  //Open ADD TO LIST AT POP UP
    jQuery('a.addToList').each(function(i, n) {
        jQuery(n).click(function() {
            getBtnPositionOnScreen(this);
            hideAvailableAtPopupContainer(n);

            var documentHeight = jQuery(document).height();
            jQuery('.black_overlay').css('height', documentHeight + 'px');
            jQuery('.black_overlay').css('z-index', 8000);
            jQuery('.black_overlay').fadeIn(900);

            if (document.getElementById('sort-options-select-box-id'))
            {
                var option_select_box = document.getElementById('sort-options-select-box-id');
                option_select_box.disabled = true;
            }

            jQuery('img.manualReplaced').css('display', 'block');
            jQuery('img.manualReplaced').css('left', xCoord + 'px');
            jQuery('img.manualReplaced').css('top', yCoord + 'px');
            jQuery('img.manualReplaced').css('z-index', 9000);

            jQuery('div.btnAddToListPopupContainer').css('left', xCoordBtn + 'px');
            jQuery('div.btnAddToListPopupContainer').css('top', yCoordBtn + 'px');
            jQuery('div.btnAddToListPopupContainer').css('z-index', 9000);

            var product = jQuery(n).find('.product-id-hidden').attr('value')
            var web_context = jQuery(n).find('.web-context-hidden').attr('value')
            jQuery.ajax({
                async: false,
                url:  web_context + '/gift_lists/show_add_to_list_options/' + product,
                type: 'GET',
                dataType: 'html',
                timeout: 10000,
                error: function() {
                    jQuery('div#processing-hover').hide();
                },
                beforeSend: function(request) {
                    jQuery('div#processing-hover').show();
                },
                success: function(html) {
                    // do something with xml
                    jQuery('div#processing-hover').hide();
                    jQuery('div#add_to_list_pop_up').html(html)
                }
            });
            jQuery('div.btnAddToListPopupContainer').css('display', 'block');
        });
    });
	
	//Close PRODUCT AVAILABLE AT POP UP
    jQuery('input.btn-cancel').click(function() {

        if (document.getElementById('sort-options-select-box-id'))
        {
            var option_select_box = document.getElementById('sort-options-select-box-id');
            option_select_box.disabled = false;
        }

        jQuery('.black_overlay').fadeOut(900);
        jQuery('img.manualReplaced').css('display', 'none');
        jQuery('div.btnAddToListPopupContainer').css('display', 'none');
    });

});

function hideAvailableAtPopupContainer(n) {
    if (product != null && web_context != null) {

        if (document.getElementById('sort-options-select-box-id'))
        {
            var option_select_box = document.getElementById('sort-options-select-box-id');
            option_select_box.disabled = false;
        }

        jQuery('div#btnAvailAtToPopupContainer-' + product).css('display', 'none');
        web_context = jQuery(n).find('.web-context-hidden').attr('value')
        jQuery("img#availAtBtnClicked-" + product).attr('src', web_context + '/images/btn-availableAt.gif');
    }
}

function hideJQueryPopUpsForAddToList() {

    if (document.getElementById('sort-options-select-box-id'))
    {
        var option_select_box = document.getElementById('sort-options-select-box-id');
        option_select_box.disabled = false;
    }

    jQuery('.black_overlay').fadeOut(900);
    jQuery('img.manualReplaced').css('display', 'none');
    jQuery('div.btnAddToListPopupContainer').css('display', 'none');
}

//get position of the clicked available button
function getBtnPositionOnScreen(btnObj) {
    var popUpObj = jQuery(btnObj);
    var coordinates = popUpObj.offset();
    var btnHeightGenerated = 0;

    if (isFirefox) {
        btnHeightGeneratedModif = 13;
        btnReplacementHeightModif = 3;
    } else {
        btnHeightGeneratedModif = 16;
        btnReplacementHeightModif = 0;
    }

    xCoord = coordinates.left;
    yCoord = coordinates.top - btnReplacementHeightModif;

    xCoordBtn = coordinates.left;
    yCoordBtn = coordinates.top + btnHeightGeneratedModif; //13 is the button height, popup needs to go below the button

}
