/**
 * jquery.yakabox.initializer.js
 *
 * jQuery initialization routines for Yakabox
 *
 * $Revision: 1.21 $
 * $Date: 2012/01/11 15:37:37 $
 * Last edited by: $Author: danderson $
 */
var navToActivityViewer = function()
{
 if( typeof window.opener === 'object' && window.opener !== null && ! window.opener.closed && window.opener.name === 'activityviewer' )
 {
   self.close();
 }
 else
 {
   window.location.href='/activityViewer/';
 }
 return true;
};

var add = {
  stuff: function( oSettings )
  {
    var settings = {};

    if( typeof oSettings === 'object' && oSettings !== null && typeof oSettings.getFullSettings === 'function' )
    {
      settings = oSettings.getFullSettings();
    }

    return new ContentAddModuleController( settings );
  },
  document: function()
  {
    return add.stuff( new ContentAddSettings_Document() );
  },
  link: function ()
  {
    return add.stuff( new ContentAddSettings_Link() );
  },
  event: function ()
  {
    return add.stuff( new ContentAddSettings_Event() );
  },
  application: function ()
  {
    return add.stuff( new ContentAddSettings_Resource() );
  },
  blog: function()
  {
    return add.stuff( new ContentAddSettings_VLogEntry() );
  },
  message: function()
  {
    return add.stuff( new ContentAddSettings_Message() );
  },
  page: function()
  {
    return add.stuff( new ContentAddSettings_SiteComponent() );
  }
};

var initHotKeyNavigation = function()
{
  if( ! initHotKeyNavigation.bound )
  {
    $( document ).bind( 'keydown', function( e )
    {
      if( e.altKey && e.ctrlKey )
      {
        switch( e.which )
        {
          case 61: // =
            add.stuff();
            break;

          case 68: // d
            add.document();
            break;

          case 76: // l
            add.link();
            break;

          case 69: // e
            add.event();
            break;

          case 65: // a
            add.application();
            break;

          case 66: // b
            add.blog();
            break;

          case 77: // m
            add.message();
            break;

          case 80: // p
            add.page();
            break;

          case 89: // y
            if( ! window.location.pathname.match( /\/activityViewer\// ) )
            {
              navToActivityViewer();
            }
            break;
        }
      }
    } );
  }

  initHotKeyNavigation.bound = true;
};
initHotKeyNavigation.bound = false;

var bDocumentWidgetUIInitialized = false;
var initWidgetUI = function( context )
{
  if( typeof context == 'string' )
  {
    context = $('#' + context);
    if( context.length != 1 )
    {
      context = null;
    }
  }
  if( typeof context !== 'object' || context === null )
  {
    if( bDocumentWidgetUIInitialized == false )
    {
      context = document;
		  bDocumentWidgetUIInitialized = true;
    }
    else
    {
      return false;
    }
  }

  //Stripes
  $( context ).next( '.ui-yakawidget-list-alternating li:even').addClass( 'ui-state-default ui-corner-all');

   //hover states on the static widgets
  $( '#dialog_link, ul#icons li, .ui-yakawidget-header, .ui-yakawidget-table td div.ui-state-default, ul.ui-yakawidget-list li, ul.ui-yakawidget-list li .fltrt, ul.ui-yakawidget-list-horizontal-menu li, button', context ).hover( function()
    {
      $( this ).addClass( 'ui-state-hover' );
    },
    function()
    {
      $( this ).removeClass( 'ui-state-hover' );
    }
  );

  /**
   * TODO: WHEN ALL YAKA-WIDGET-HEADERS HAVE BEEN REPLACED WITH NEW MARKUP/CLASSES, REMOVE THIS CODE
   *        REPLACEMENT CODE IS BELOW
   */
  $( '.ui-yakawidget-header .ui-yakawidget-open, .ui-yakawidget-header .ui-widget-title', context ).click( function( e )
  {
    $( this ).parent().next().toggle( 'blind' );

    var arrow = $(this).parent().find( '.ui-yakawidget-open span' )

    if( arrow.hasClass( 'ui-icon-triangle-1-s' ) )
    {
      arrow.removeClass( 'ui-icon-triangle-1-s' ).addClass( 'ui-icon-triangle-1-e' );
    }
    else
    {
      arrow.removeClass( 'ui-icon-triangle-1-e' ).addClass( 'ui-icon-triangle-1-s' );
    }

    e.preventDefault();
  });

  /**
   * TODO: THIS IS THE REPLACEMENT CODE FOR YAKA-WIDGET-HEADERS (TWISTY OPEN/CLOSE)
   *        WHEN ALL MARKUP FOR SUCH HEADERS IS REPLACED, THIS CODE REMAINS AND THE ABOVE IS REMOVED
   */
  $( 'div.ybx-widget-header', context ).widgetheader();
};

$( '#ui-layout-flex' )
  .click( function()
  {
		$(this).find('.ui-icon').toggleClass( 'ui-icon-arrowthickstop-1-w ui-icon-arrowthickstop-1-e');
		
    $( '.ui-yakabox-layout-twoColFixLtHdr' ).toggleClass( 'ui-yakabox-layout-flex' );
  } );


$( '.head' )
  .click( function()
  {
		var headArrow = $(this).find( '.ui-icon' );
		if( headArrow.hasClass( 'ui-icon-triangle-1-s' ) )
    {
      headArrow.removeClass( 'ui-icon-triangle-1-s' ).addClass( 'ui-icon-triangle-1-e' );
    }
    else
    {
      headArrow.removeClass( 'ui-icon-triangle-1-e' ).addClass( 'ui-icon-triangle-1-s' );
    }
		$( this ).toggleClass( 'selected ui-state-active  ui-background-flat ui-border-bottom' ).next().toggle( 'blind' );
  } )
  .next( '.collapse' )
    .hide();


initWidgetUI();

// Initialize jQuery tooltip plugin.
$( '.ybx-tooltip' ).tooltip();

//Wire up microprofile panel sliding/etc
( function()
{
  var $ybxpanel = $( '#ybx-panel' ),
      $ybxpaneltrigger = $( '.ybx-panel-trigger' ),
      $SearchDataSearchWidgetAlphaToolbar = $( '#SearchData_SearchWidget_AlphaToolbar' ),
      $body = $( 'body' );

  $ybxpaneltrigger.click( function( e )
  {
    var bPanelVisible = $ybxpanel.is( ':visible' );

    $ybxpaneltrigger
      .find( 'span' )
        .toggleClass( 'ui-icon-circle-triangle-e', ! bPanelVisible )
        .toggleClass( 'ui-icon-circle-triangle-w', bPanelVisible );

    if( bPanelVisible )
    {
      $ybxpanel.hide( 'slide', { direction: 'right', distance: 100 }, 400);

      if( ! $SearchDataSearchWidgetAlphaToolbar.is( ':visible' ) )
			{
				$body.removeClass( 'ui-yakabox-layout-panel ui-yakabox-layout-flex' );
			}
			else
		  {
        $body.removeClass( 'ui-yakabox-layout-panel' );
      }
    }
    else
    {
      $ybxpanel.show( 'bounce', { direction: 'left', times: 2, distance:10 }, 400 );

      $body.addClass( 'ui-yakabox-layout-flex ui-yakabox-layout-panel' );

      $ybxpaneltrigger.parent().show( 'bounce', { direction: 'left', times: 2, distance: 10 }, 400 );
    }

    e.preventDefault();
  } );
}() );
