/**
 * jquery.yakabox.initializer.js
 *
 * jQuery initialization routines for Yakabox
 *
 * $Revision: 1.4 $
 * $Date: 2009/10/20 18:10:27 $
 * Last edited by: $Author: jauldrid $
 */
var navToActivityViewer = function()
{
 if( typeof window.opener !== 'undefined' && window.opener !== null && 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()
{
  var hotkeybindings = {
    keydown: [
      { combi: 'CTRL+=', func: add.stuff },
      { combi: 'CTRL+d', func: add.document },
      { combi: 'CTRL+l', func: add.link },
      { combi: 'CTRL+e', func: add.event },
      { combi: 'CTRL+a', func: add.application },
      { combi: 'CTRL+b', func: add.blog },
      { combi: 'CTRL+m', func: add.message },
      { combi: 'CTRL+p', func: add.page },
      { combi: 'CTRL+y', func: function()
        {
          if( ! window.location.pathname.match( /\/activityViewer\// ) )
          {
            navToActivityViewer();
          }
        }
      }
    ]
  };

  var boundEvent, binding;
  for( boundEvent in hotkeybindings )
  {
    for( var i = 0; i < hotkeybindings[boundEvent].length; i++ )
    {
      binding = hotkeybindings[boundEvent][i];

      $( document ).bind( boundEvent, binding.combi, binding.func );
    }
  }

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

var initWidgetUI = function( context )
{
  if( typeof context !== 'object' || context === null )
  {
    context = document;
  }

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

	$( '.ui-yakawidget-header .ui-yakawidget-open, .ui-yakawidget-header .ui-widget-title', context ).click( function()
  {
    $( this ).parent().next().toggle( 'fast' );

    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' );
    }
  } );
}

$( function()
{
  $( '.head' ).click( function()
  {
    $( this ).toggleClass( 'selected' ).next().toggle( 'fast' );
  } ).next( '.collapse' ).hide();

  initWidgetUI();
} );