﻿//===================================================================================
//  Javascript for Plan / Community Details
//===================================================================================

$(document).ready(function() {
    DetailsEvents();
});
var addedNotes = "";

var GalleryImages = [];

function DetailsEvents()
{
    $(".AddToFavorites_Details").click(function(event) {
        event.preventDefault();
        var offset = $(this).offset();
        if (logged_in == true)
        {
            showAddToFavoritesControl($("span.ProductName").html(), (offset.top - 100), 10);
        }
        else
        {
            showLoginControlSpecial(offset.top - 100, 10);
        }
        
    });
    
    $(".ManageFavorite_Details").click(function(event) {
        event.preventDefault();
        var offset = $(this).offset();
        var manageControl = $("#ManageFavoriteControl");
        if (manageControl.is(":hidden"))
            manageControl.css({top:offset.top-140}).fadeIn("fast");
        else
            manageControl.fadeOut("fast");
    });
    
    //AddToFavorites button
    $("#btnAddFavorite").click(function(event) {
        event.preventDefault();
        addedNotes = $(".AddToFavoritesNotes").val();
        clickedAddFavoriteButton($(".FavoriteType").html(), $(".ProductId").html(), onFavoriteAdded, onFavoriteAddedError);//In FavoritesGUI.js
    });
    
    $("#mngRemoveFav").click(function(event) {
        event.preventDefault();
        $("#ManageFavoriteControl").fadeOut("fast");
        RemoveFromFavorites($(".FavoriteId").html(), onFavoriteRemoved, onFavoriteRemovedError);
    });
    
    //Make Notes Editable
    $(".EditNotes_Details").editable(
        function(value, settings) {
            var favId = $(".FavoriteId").html();
            //$(".MyNotesLabel").show();
            return EditNotes(favId, value, onNotesEdited, onNotesEditedError);
        },
        {
            indicator   : "Saving...",
            tooltip     : "Click to edit...",
            type        : "textarea",
            onblur      : "cancel",
            submit      : "Save",
            event       : "edit"
        }
    );
    $(".EditNotes_Details").click(function() {
        //$(".MyNotesLabel").hide();
        $(".EditNotes_Details").trigger("edit");
        $(".EditNotes_Details").next().focus();
    });
    
    $("#mngEditNotes").bind("click", function(event) { 
        event.preventDefault();
        //$(".MyNotesLabel").hide();
        $(".EditNotes_Details").trigger("edit"); 
        $(".EditNotes_Details form textarea").focus();
    });
    //If the photogallery link is in the page and if the Construction Photo button is disabled
    if (($(".spanPhotoGallery").length > 0))
    {
        var productId = $("span.ProductId").text();
        var productType = $("span.ProductType").text();
        GetImageGalleryByProductId(productId, productType, onImageGalleryReceived, onImageGalleryReceivedError);
    }
    $(".spanPhotoGallery a").click(function(event) {
            event.preventDefault();
            if (GalleryImages != null)
                $.slimbox(GalleryImages, 0, {loop:true});
     });
     
    //Options Gallery button
     $(".lnkOptionsGallery").click(function(event) {
         if ($(".OptionsGalleryElevations").size() > 0) {
             event.preventDefault();
             $(".OptionsGalleryElevations").show();
         }
     });

     $(".lnkOptionsGallery2").click(function(event) {
         if ($(this).siblings(".OptionsGalleryElevations2").size() > 0) {
             event.preventDefault();
             $(this).siblings(".OptionsGalleryElevations2").show();
         }
     });
}



function onImageGalleryReceived(result)
{
    if (result != null && result.length != null && result.length > 1)
    {
        for (var i=0; i<result.length; i++)
        {
            GalleryImages[i] = [];
            GalleryImages[i][0] = 'http://www.mihomes.com' + result[i].Link;
            GalleryImages[i][1] = result[i].Description;
        }
        $(".spanConstructionPhotos").hide();
        $(".spanPhotoGallery").show();
    }
    else
    {
        $(".spanPhotoGallery").hide();
    }
}

function onImageGalleryReceivedError(result)
{
    //alert("ERROR receiving images!");
}

function onFavoriteAdded(result)
{
    $(".AddToFavorites_Details").hide();
    $(".ManageFavorite_Details").show();
    $(".FavoriteId").html(result);
    if (addedNotes.length == 0)
        addedNotes = "Click to edit...";
    $(".EditNotes_Details").html(addedNotes);
}

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)
{
    $(".AddToFavorites_Details").show();
    $(".ManageFavorite_Details").hide();
    $(".FavoriteId").html("");
}

function onFavoriteRemovedError(result)
{
    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.");
}
