﻿$(function() {
    $('.link').each(function() {
        var item = new Sunhat.Web.ShortlistItem();
        var pID = $(this).attr('name');
        item.propertyID = parseInt(pID);

        var shortlist = new Sunhat.Web.Shortlist();
        if (shortlist.contains(item))
            $(this).click(function(event) { removeFromShortlist(event,pID); }).html('Remove from Shortlist');
        else
            $(this).click(function(event) { addToShortlist(event, pID); }).html('Add to Shortlist');
    });
    $('#tblFeatures').show();
    $('#searchMore').html('Hide Features');
});
                                

function addToShortlist(event, id) {
    
    var item = new Sunhat.Web.ShortlistItem();
    item.propertyID = id;

    var shortlist = new Sunhat.Web.Shortlist();
    shortlist.add_item(item);

    var el = event.target;

    $(el).css({ 'backgroundColor': '#fff' }).fadeOut(function() { $(this).html('Remove from Shortlist').fadeIn(); })
         .unbind()
         .click(function(event) { removeFromShortlist(event, id); });
    
}

function removeFromShortlist(event, id) {

    var item = new Sunhat.Web.ShortlistItem();
    item.propertyID = id;

    var shortlist = new Sunhat.Web.Shortlist();
    shortlist.remove_item(item);

    var el = event.target;
    
    $(el).css({ 'backgroundColor': '#fff' }).fadeOut(function() { $(this).html('Add to Shortlist').fadeIn(); })
         .unbind()
         .click(function(event) { addToShortlist(event, id); });
}
