// ShopMyLikes.com Javascript

const DEBUG = false

// HTML IDs and Class names that must be in sync with template output
// generation: tpl/mycategories.header.html
//
const MY_CATEGORY_ITEM_ID_PREFIX = "myCategoryItem_";
const CATEGORY_ITEM_ICON_ID_PREFIX = "categoryItemIcon_";
const MY_CATEGORY_ITEM_ICON_ID_PREFIX = "myCategoryItemIcon_";
const MY_CATEGORY_ITEM_CLASS = "myCategoryItem";
const EMPTY_MY_CATEGORY_ITEM_ID = "emptyMyCategoryItem";
const EMPTY_MY_CATEGORY_ICON_CLASS = "emptyMyCategoryIcon";
const EMPTY_MY_CATEGORY_TEXT_CLASS = "emptyMyCategoryText";
const MY_CATEGORIES_LIST_ID = "myCategoriesList";
const ADD_CATEGORY_ICON_CLASS = "addCategoryIcon";
const REMOVE_CATEGORY_ICON_CLASS = "removeCategoryIcon";
const ADDED_CATEGORY_ICON_CLASS = "addedCategoryIcon";
const WAITING_CATEGORY_ICON_CLASS = "waitingCategoryIcon";

var Z_INDEX = 900;
var PD_TOP = 50;
var PD_LEFT = 50;

//--------------------
function SMT_showMoreFilterItems( featureSymbol, url )
{
    replaceId = 'availableFiltersBody_'+featureSymbol;
    var waiting = '<div id="'+replaceId+'" class="availableFiltersBodyAll">';
    waiting += '<input class="waitingCategoryIcon" type="button"></input>';
    waiting += '</div>'
    $("#" + replaceId).replaceWith(waiting);
    
    try
    {
	   $.get( url, 
			{  }, 
			function(data) { 
                   $("#" + replaceId).replaceWith(data);
			}
             );
    }
    catch (e)
    {
	   if ( DEBUG )
		  alert( "SMT_showMoreFilterItems Exception" + e );
	   $("#" + replaceId).replaceWith('<div class="asyncError">Error</div>');

    }
}

//--------------------
function SMT_changeIcon( tagPrefix, categoryKey, cssClass )
{
    $("#" + tagPrefix + categoryKey).attr( 'class', cssClass );
}

//--------------------
function SMT_addCategoryAsync( categoryKey, categoryName, debug )
{
    try
    {
	   SMT_changeIcon( CATEGORY_ITEM_ICON_ID_PREFIX,
				    categoryKey, 
				    WAITING_CATEGORY_ICON_CLASS );

	   $.get( "/async", 
			{ type: "addCategory",
			  categoryKey: categoryKey }, 
			function(data) { 

			    SMT_changeIcon( CATEGORY_ITEM_ICON_ID_PREFIX,
							categoryKey, 
							ADDED_CATEGORY_ICON_CLASS );

			    if ( /true/.test( data ))
				   SMT_addCategory( categoryKey, categoryName )
			    else
			    {
				   if (DEBUG)
					  alert( categoryName + " is already in your list" );
			    }
			}
             );
    }
    catch (e)
    {
	   SMT_changeIcon( CATEGORY_ITEM_ICON_ID_PREFIX,
				    categoryKey, 
				    ADD_CATEGORY_ICON_CLASS );
	   if ( DEBUG )
		  alert( "SMT_addCategoryAsync Exception" + e );
    }
}

//--------------------
function SMT_removeCategoryAsync( categoryKey, categoryName, debug )
{
    SMT_changeIcon( MY_CATEGORY_ITEM_ICON_ID_PREFIX,
				categoryKey, 
				WAITING_CATEGORY_ICON_CLASS );

    try
    {
	   $.get( "/async", 
			{ type: "removeCategory",
			  categoryKey: categoryKey }, 
			function(data) { 

			    SMT_removeCategory( categoryKey, categoryName )
			    
			    if ( DEBUG && ! /true/.test( data ))
				   alert( categoryName + " is not in your list" );
			}
             );
    }
    catch (e)
    {
	   SMT_changeIcon( MY_CATEGORY_ITEM_ICON_ID_PREFIX,
				    categoryKey, 
				    REMOVE_CATEGORY_ICON_CLASS );
	   if ( DEBUG )
		  alert( "SMT_removeCategoryAsync Exception" + e );
    }
}

//--------------------
function SMT_addCategory( categoryKey, categoryName )
{
    try
    {
	   // Don't add dupes
	   if ( $("#" + MY_CATEGORY_ITEM_ID_PREFIX + categoryKey).size() > 0 )
	   {
		  if ( DEBUG )
			 alert( categoryName + " is already added" );
		  return;
	   }

	   $("#" + EMPTY_MY_CATEGORY_ITEM_ID).remove();
	   itemHtml = SMT_getSelectedCategoryItem( categoryKey, categoryName );
	   $("#" + MY_CATEGORIES_LIST_ID).append( itemHtml );

	   SMT_changeIcon( CATEGORY_ITEM_ICON_ID_PREFIX,
				    categoryKey, 
				    ADDED_CATEGORY_ICON_CLASS );
	   
    }
    catch (e)
    {
	   SMT_checkIfSelectedCategorysEmpty();
	   if ( DEBUG )
		  alert( "Exception" + e );
    }
}

//--------------------
function SMT_getSelectedCategoryItem( categoryKey, categoryName )
{
    var str = '';
    
    str += '<div id="' + MY_CATEGORY_ITEM_ID_PREFIX + categoryKey
	   + '" class="' + MY_CATEGORY_ITEM_CLASS + '">';

    str += '<input id="' + MY_CATEGORY_ITEM_ICON_ID_PREFIX + categoryKey
	   + '" class="' + REMOVE_CATEGORY_ICON_CLASS
	   + '" type="button" onclick="javascript:SMT_removeCategoryAsync(\''
	   + categoryKey + '\', \'' + categoryName + '\');" />'
	   + '<span class="myCategoryItemName">' + categoryName + '</span></div>';
    
    return str;
}

//--------------------
function SMT_removeCategory( categoryKey, categoryName )
{
    try
    {
	   $("#" + MY_CATEGORY_ITEM_ID_PREFIX + categoryKey).remove();
	   SMT_changeIcon( CATEGORY_ITEM_ICON_ID_PREFIX,
				    categoryKey, 
				    ADD_CATEGORY_ICON_CLASS );

	   SMT_checkIfSelectedCategorysEmpty();
    }
    catch (e)
    {
	   if ( DEBUG )
		  alert( "Exception" + e );
    }
}

//--------------------
function SMT_checkIfSelectedCategorysEmpty()
{
    if ( $("." + MY_CATEGORY_ITEM_CLASS).size() > 0 )
	   return;
    
    emptyHtml = '<div id="'+EMPTY_MY_CATEGORY_ITEM_ID+'" class="myCategoryItem">'
	   +          '<div class="'+EMPTY_MY_CATEGORY_ICON_CLASS+'"></div>'
	   +          '<div class="'+EMPTY_MY_CATEGORY_TEXT_CLASS+'">Nothing Defined</div>'
	   +          '<div style="clear: both;" ></div>'
	   +          '</div>'
    
    $("#" + MY_CATEGORIES_LIST_ID).prepend( emptyHtml );
}

//--------------------
function showdiv( id ) 
{
    document.getElementById(id).style.visibility = 'visible';
    document.getElementById(id).style.display = '';
}

//--------------------
function hidediv( id ) 
{
    document.getElementById(id).style.visibility = 'hidden';
    document.getElementById(id).style.display = 'none';
}

//--------------------
function showhidediv( id ) 
{
    var visibility = document.getElementById(id).style.visibility;
    if ( visibility == 'hidden' )
        showdiv( id );
    else
        hidediv( id );
}

//--------------------
function toggleDescription( productId )
{
    showhidediv( "desc_"+productId+"_short" );
    showhidediv( "desc_"+productId+"_long" );
}

//--------------------
function showDiv( windowId )
{
    var div = document.getElementById(windowId);
    div.setAttribute( 'style', 'z-index: 900' );
    div.style.visibility = 'visible';
    div.style.display = '';

}

//--------------------
function showProductDetails( windowId )
{
    var div = document.getElementById(windowId);

    var style = 'z-index: '+Z_INDEX;
    Z_INDEX += 1;
    div.setAttribute( 'style', style );
    div.style.visibility = 'visible';
    div.style.display = '';

}

//--------------------
function hideDiv( windowId )
{
    var div = document.getElementById(windowId);
    div.style.visibility = 'hidden';
    div.style.display = 'none';
}

//--------------------
function handlePageLoadEvent()
{
    // If the "add" page, we set the icon to "checked" for all categories
    // already showing in the "my" area.
    
}

