﻿/* Save to shortlist */
$(document).ready(function() {
    // get all our links
    var shortlistLinks = $(".shortlistSave");

    if (AreCookiesOn()) {
        // add click events to the watchlist links
        shortlistLinks.click(saveShortlistClicked);
    }
    else {
        // remove all the shortlist links
        shortlistLinks.click(function(event) {
            event.preventDefault();
            alert("Cookies are required for this feature.");
        });
    }
});

function saveShortlistClicked(event) {
    // stop the redirect
    event.preventDefault();

    // get the calling link
    var link = $(event.target);
    var parentDiv = link.parent();

    // make the ajax call
    jQuery.get(link.attr("href"), function() {
        // update parent paragraph
        parentDiv.html("<a href='/Shortlist/' class='shortlistSaved'>Saved to shortlist</a>");
		
        var countSpan = $(".shortlistnum span");
		
        // update the shortlist count
        if (countSpan.html() == "10+" || countSpan.html() == "10") {
        	// more than 10
        	// append the count
        	countSpan.html("10+");

        	// update the class
        	$(".shortlistnum span").attr("class", "more");
        }
        else if (countSpan.html() == "" || countSpan.html() == null) {
        	// append the count
        	$(".shortlist").append("<span>1</span>");
        	// update the class
        	$(".shortlist").attr("class", "shortlistnum");
        	$(".shortlistnum span").attr("class", "");
        } else {
        	//Increment the number
        	var currentCount = parseInt(countSpan.html());
        	countSpan.html(currentCount + 1);
        	if (currentCount == 9)
        		$(".shortlistnum span").attr("class", "ten");
        	else
        		$(".shortlistnum span").attr("class", "");
        }
    });

    // update the button to show saving
    link.removeClass("shortlistSave");
    link.addClass("shortlistSaving");
}

function getAvailabilityRows(houseID, modifier) {
    var monthYear = GetMonthYearArray($('#calDate').val(), modifier);
    var m = monthYear[0];
    var y = monthYear[1];

    //make sure it's within the date limit (i.e it's a month in our dropdown)
    if ($("#calDate option[value='" + m + "-" + y + "']").text()) {
        var url = "/AJAX/GetListingAvailability.aspx?houseID=" + houseID + "&d=1&m=" + m + "&y=" + y;
        jQuery.get(url, function(data) {
            // write the table
            $("table.Calendar").html(data);

            // set dropdown if necessary (we've used the prev/next buttons)
            if (modifier) { $('#calDate').val(m + "-" + y); }
        });
    }
}


function GetMonthYearArray(monthYear, modifier) {
    var hyphen = monthYear.indexOf("-");
    var m = new Number(monthYear.substring(0, hyphen));
    var y = new Number(monthYear.substring(hyphen + 1));

    //modify our dates... do y first since we are changing m!!
    if (modifier == "prev") {
        y = (m != 1) ? y : y - 1;
        m = (m != 1) ? m - 1 : 12;
    }
    if (modifier == "next") {
        y = (m != 12) ? y : y + 1;
        m = (m != 12) ? m + 1 : 1;
    }

    return  new Array(m,y);    
}

