﻿//settings actionbar
var handlerRelativePath = '/PageActionBar/Handlers/PageActionBarHandler.ashx';
var actionBarExpandWidth = '242';
var actionBarExpandHeight = '130';
var actionBarAnimateTime = 100;
var currentPageId = 0;
var pageIsFavorite = false;
var NrOfFavorites = 0;
var transactionRunning = false;
var expanded = false;

$(document).ready(function() {
    if ($('#currentPageId').length > 0 && $('#currentPageId').val().length > 0)
        currentPageId = $('#currentPageId').val();

    populateFavorites(currentPageId);

    $('#pageActionBar')
      //on start hovering over the actionBar
      .hover(function() {

          //prevents the dialog from expanding when it should not
          if (transactionRunning || expanded)
              return;

          //calculate animation height of actionbar, 30px X number of favorites
          actionBarExpandHeight = 30 * NrOfFavorites;

          //animate top
          $('#pageActionBar_top').stop().animate({
              width: '+=' + actionBarExpandWidth,
              left: '-=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });
          //animate content
          $('#pageActionBarContent').stop().animate({
              width: '+=' + actionBarExpandWidth,
              height: '+=' + actionBarExpandHeight,
              left: '-=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });
          //animate footer
          $('#pageActionBar_foot').stop().animate({
              width: '+=' + actionBarExpandWidth,
              left: '-=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });

          $('#actionFavoritesContent').show(actionBarAnimateTime);

          expanded = true;
      },
      //on EXIT hover over the actionBar
      function() {
          if (transactionRunning)
              return;
        
          //animate top
          $('#pageActionBar_top').stop().animate({
              width: '-=' + actionBarExpandWidth,
              left: '+=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });
          //animate content
          $('#pageActionBarContent').stop().animate({
              width: '-=' + actionBarExpandWidth,
              height: '-=' + actionBarExpandHeight,
              left: '+=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });          
          //animate footer
          $('#pageActionBar_foot').stop().animate({
              width: '-=' + actionBarExpandWidth,
              left: '+=' + actionBarExpandWidth
          }, actionBarAnimateTime, function() {
              // Animation complete.
          });
          
          //hide favorite links
          $('#actionFavoritesContent').hide(actionBarAnimateTime);
          
          expanded = false;
      }
    );

    //hover over favorite icon, switch it and set flag if page is saved as favorite    
    $('#pageActionbar_star').hover(
      function() {
          if ($('#pageActionbar_star').attr('class') == 'notSelectedPageIcon')
              $('#pageActionbar_star').attr('class', 'selectedPageIcon');
          else
              pageIsFavorite = true;
      },
      function() {
          if (!pageIsFavorite)
              $('#pageActionbar_star').attr('class', 'notSelectedPageIcon');
      });

    //ActionBar Action - Save/remove PageAsFavorite
    $('#btnAddPageTofavorites').click(function() {
        transactionRunning = true;

        $('#actionFavorites').hide();
        $('#pageActionbar_star').hide();
        $('#pageActionBarLoading').show();

        var action = 'savecurrentpageasfavorite';
        
        if (pageIsFavorite)
            action = 'removecurrentpageasfavorite';

        //send action and arguments to our handler for processing
        $.ajax({
            type: 'POST',
            url: handlerRelativePath,
            data: 'action=' + action + '&pageid=' + currentPageId,
            success: function(msg) {
                if (msg.length > 0)
                    alert(msg);
                else {                    
                    toggleFavoritesIcon(action, currentPageId);
                    populateFavorites(currentPageId);

                    if (action == 'removecurrentpageasfavorite') {
                        pageIsFavorite = false;
                        NrOfFavorites--;
                    }
                    else {
                        pageIsFavorite = true;
                        NrOfFavorites++;
                    }

                    transactionRunning = false;
                }
            }
        });
    });

});

//removes page from the favoritestore via a asynchronous call to our handler
function removefavorite(pageId) {    
    
    $('#actionFavorites').hide();
    $('#pageActionbar_star').hide();
    $('#pageActionBarLoading').show();

    $.ajax({
        type: 'POST',
        url: handlerRelativePath,
        data: 'action=removepagefromfavorites' + '&pageid=' + pageId,
        success: function(msg) {

            if ($('#currentPageId').length > 0 && $('#currentPageId').val().length > 0)
                var currentPageId = $('#currentPageId').val();

            if (currentPageId.length > 0 && pageId == currentPageId) {
                setTimeout(function() { toggleFavoritesIcon('removecurrentpageasfavorite', pageId) }, 325);
                pageIsFavorite = false;
            }

            NrOfFavorites--;

            $('#actionFavorites').show();
            $('#pageActionbar_star').show();
            $('#pageActionBarLoading').hide();
            
            document.getElementById('favlink' + pageId).parentNode.style.display='none';            
        }
    });
}

//clear and repopulate the favorites via a asynchronous call to our handler
function populateFavorites(currentPageId) 
{
    $.ajax({
        type: 'POST',
        url: handlerRelativePath,
        data: 'action=getallfavorites' + '&pageid=' + currentPageId,
        success: function(msg) {
            $('#actionFavoritesContent').html(msg);            
            NrOfFavorites = $('#actionFavoritesContent > ul > li').length;
        }
    });
}

//toggle the favorites icon as selected or not depending on current page
function toggleFavoritesIcon(action, currentPageId) {           
    if (action == 'savecurrentpageasfavorite')
        $('#pageActionbar_star').attr('class', 'selectedPageIcon');
    else
        $('#pageActionbar_star').attr('class', 'notSelectedPageIcon');    
    
    $('#actionFavorites').show();
    $('#pageActionbar_star').show();
    $('#pageActionBarLoading').hide();    
 }

