﻿//===================================================================================
//  Javascript for Plan / Community Listings
//===================================================================================
var readyFunctionsComplete = false;
$(document).ready(function() {
    if (readyFunctionsComplete == false)
    {
        listingEvents();
        readyFunctionsComplete = true;
    }
});
var cur_favoriteTypeId;
var cur_productId;
var cur_notes;
function listingEvents()
{
    var favoriteListings = $(".RemoveFromFavorites[id*='remove_']").parents(".PCListing");
    highlightListing(favoriteListings);
    
    //Set position of 'Add to Favorites' popup to that of the link
    $(".AddToFavorites").click(function(event) {
        event.preventDefault();
        addedListing = $(this).parents(".PCListing");
        var offset = addedListing.offset();
        var top = offset.top - 60;
        var name = GetNewFavoriteDetails(this.id);
        if (logged_in == true)
        {
            showAddToFavoritesControlListing(name, top);
        }
        else
        {
            showLoginControlListing(top);
        }
    });
    
    $(".RemoveFromFavorites, .mngRemoveFav").click(function(event) {
        event.preventDefault();
        var removeFavoriteId = $(this).parents(".PCListing").find(".RemoveFromFavorites").attr("id").split('_')
        removeAction = removeFavoriteId[1];
        var favId = removeFavoriteId[2];
        removedListing = $(this).parents(".PCListing");
        RemoveFromFavorites(favId, onFavoriteRemoved, onFavoriteRemovedError);
    });
    //AddToFavorites button
    $("#btnAddFavorite").click(function(event) {
        event.preventDefault();
        cur_notes = $(".AddToFavoritesNotes").val();
        clickedAddFavoriteButton(cur_favoriteTypeId, cur_productId, onFavoriteAdded, onFavoriteAddedError);//In FavoritesGUI.js
    });
    
    //Make Notes Editable
    $(".EditNotes").editable(
        function(value, settings) {
            var favId = $(this).parents(".PCListing").children(".FavoriteId").text();
            //$(this).parents(".PCListing").find(".MyNotesLabel").show();
            //$(this).next().hide();
            return EditNotes(favId, value, onNotesEdited, onNotesEditedError);
        },
        {
            indicator   : "Saving...",
            tooltip     : "Click to edit...",
            type        : "textarea",
            event       : "edit",
            onblur      : "cancel",
            submit      : "Save"
        }
    );
    
    $(".EditNotes").bind("click", function() {
        //$(this).parents(".PCListing").find(".MyNotesLabel").hide();
        //$(this).next().show();
        $(this).trigger("edit"); 
    });
    
    $(".mngEditNotes").bind("click", function(event) { 
        event.preventDefault();
        //$(".MyNotesLabel").hide();
        $(this).parents(".PCListing").find(".EditNotes").trigger("edit").next().show(); 
    });
    
    $("img.ExpandLinks").click(function() {
        $(this).parent().children("div.ManageLinks").show("slow");
        $(this).hide().next().show();
    });
    
    $("img.CollapseLinks").click(function() {
        $(this).parent().children("div.ManageLinks").hide("slow");
        $(this).hide().prev().show();
    });
    
    $("a.PhotoGalleryLink").click(function(event) {
        event.preventDefault();
        var productId = $(this).parents(".PCListing").children(".ProductId").text();
        var productType = $(this).parents(".PCListing").children(".ProductType").text();
        GetImageGalleryByProductId(productId, productType, onImageGalleryReceived, onImageGalleryReceivedError);
    });
}

function onImageGalleryReceived(result)
{
    var images = [];
    for (var i=0; i<result.length; i++)
    {
        images[i] = [];
        images[i][0] = 'http://www.mihomes.com' + result[i].Link;
        images[i][1] = result[i].Description;
    }
    if (images != null)
        $.slimbox(images, 0, {loop:true});
}

function onImageGalleryReceivedError(result)
{
    alert("ERROR receiving images!");
}

var addedListing;
var removedListing;
var removeAction = "unhighlight";
//After clicking on a 'Add To Favorites Link', this will run.  It will set the
//favoriteTypeId, productId, and plan/community name based on the value of the
//anchor tag's ID
function GetNewFavoriteDetails(target_text)
{
    var fav_array = target_text.split('_');
    cur_favoriteTypeId = fav_array[1];
    cur_productId = fav_array[2];
    var name = fav_array[3];
    
    var productNameSpan;
    if (cur_favoriteTypeId == 1)
    {
        productNameSpan = name + " Plan";
    }
    else if (cur_favoriteTypeId == 2)
    {
        productNameSpan = name + " Inventory Home";
    }
    else if (cur_favoriteTypeId == 3)
    {
        productNameSpan = name + " Community";
    }
    return productNameSpan;
}

function getCheckedListings()
{
    return $(".TabContent:visible").find("input.CompareCheck:checked").parents(".PCListing");
}


function onFavoriteAdded(result)
{
    var removeFavId =  "remove_" + removeAction + "_" + result;
    addedListing.find(".RemoveFromFavorites").attr("id", removeFavId);
    if (cur_notes.length == 0)
    {
        cur_notes = "Click to edit";
    }
    addedListing.find(".DivFavoriteNotes").show().children(".FavoriteNotes").css("display", "block").children(".EditNotes").text(cur_notes);
    addedListing.children(".FavoriteId").text(result);
    highlightListing(addedListing);
}

function onFavoriteAddedError(result)
{
    alert("There was a problem adding this favorite.  You may need to refresh the page and possibly log back into My M/I Homes.");
}

function onFavoriteRemoved(result)
{
    if (removeAction == "collapse")
    {
        collapseListing(removedListing);
    }
    else
    {
        unHighlightListing(removedListing);
        removedListing.find(".FavoriteNotes").hide();
    }
    var numFavsSpan = $(".PCList_Viewing").children("div:visible").children("span");
    var numFavs = parseInt(numFavsSpan.text(), 10) - 1;
    numFavsSpan.text(numFavs);
}

function onFavoriteRemovedError(result, userContext)
{
    alert("There was a problem removing this favorite.  You may need to refresh the page and possibly log back into My M/I Homes.");
}

function onNotesEdited(result)
{

}

function onNotesEditedError(result)
{
    alert("There was a problem editing the notes.  You may need to refresh the page and possibly log back into My M/I Homes.");
}

function highlightListing(listing, favoriteId)
{
    listing.find(".RemoveFromFavorites").show();
    listing.find(".AddToFavorites").hide();
    var manageFav = listing.find(".NonFavManage").removeClass("NonFavManage").addClass("FavListingManage")
    if (manageFav.find("input.CompareCheck").length > 0)
        manageFav.addClass("NoStar");
    else
        manageFav.addClass("Star");
    listing.find(".ExpandLinks").show();
}

function unHighlightListing(listing)
{
    listing.find(".RemoveFromFavorites").removeAttr("id").hide()
    listing.find(".AddToFavorites").show()
    listing.css({ border:"none", marginLeft:"4px" });
    listing.find(".PCListing_TitlePrice h3 img").remove();
    listing.find(".FavListingManage").removeClass("FavListingManage").removeClass("NoStar").removeClass("Star").addClass("NonFavManage");
    listing.find(".ManageLinks").hide();
    listing.find(".ExpandLinks, .CollapseLinks").hide();
}

function collapseListing(listing)
{
    listing.fadeOut("slow", function()
    {
        listing.remove();
    });
}

function getListingByFavoriteId(favoriteId)
{
    return $("span.FavoriteId:contains('" + favoriteId +"')").parents(".PCListing");
}

