﻿$(document).ready(function() {

    // Associations popup
    $('.associationLogos a').die('click');
    $('.associationLogos a').unbind('click');
    $('.associationLogos a').live('click', function() {

        $('#listingAssociationsPopup').css('height', $('body').height());
        var position = $(this).parents('.listing').position().top;
        position += $('.resultListing').position().top;
        $('#listingAssociationsPopup .popupContainer').css('top', position + 'px');

        var associationID = ($(this).attr('id').substring(12));

        $.ajax({
            type: "POST",
            url: '/methods/methods.aspx/getAssociation',
            data: '{"id" : "' + associationID + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
                data = JSON.parse(data.d);

                $('#popupTitle').html(data.title);
                $('#popupText').html(data.text);
                $('.popupRight').empty();
                $('.popupImg').attr('src', data.image);

                // If there is a link, show the image with the link
                if (data.link != "") {
                    if (data.link.indexOf("http://") >= 0) {
                        $('.popupRight').append('<a href="' + data.link + '">' +
                                        '<img src="' + data.image + '" alt="' + data.title + '" />' +
                                        '</a>');
                    }
                    else {
                        $('.popupRight').append('<a href="http://' + data.link + '">' +
                                        '<img src="' + data.image + '" alt="' + data.title + '" />' +
                                        '</a>');
                    }
                }
                else {
                    $('.popupRight').append('<img src="' + data.image + '" alt="' + data.title + '" />');
                }

                // Reset height for columns

                $('.associations .popupLeft, .associations .popupRight').css('height', 'auto');

                // Show the popup
                $('#listingAssociationsPopup').show();

                // Attempt to stretch whichever column is shorter
                if ($('.associations .popupRight').height() < $('.associations .popupLeft').height()) {
                    $('.associations .popupRight').css('height', $('.associations .popupLeft').height() + 'px');
                }

            }
        });


        return false;
    });

    $('#listingAssociationsPopup .popupContainer .closePopupWindow').live('click', function() {
        $('#listingAssociationsPopup').fadeOut('fast');
        return false;
    });

});