/*
 * This file is part of the mgWidgets package.
 * (c) 2008-2009 Qarmaq / MenuGourmet
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

function menusSelections(){
  // this global attributes  
  this.wuid = null;
  this.widget = null;
  
  // widget attributes
  // this.field1 = null;
  //
}

menusSelections.prototype = new mgWidgetObject();

// this method is call when the object is created
menusSelections.prototype.init = function(wuid) {
  // common code
  this.wuid = wuid;
  this.widget = jQuery('#' + wuid);
  jQuery(window).bind('load.menusSelections', [this, 'load']);
  
  // extra params
  // place here your extra init information
  
}

// this method is call when the DOM / window is loaded
menusSelections.prototype.load = function()
{
  // add here your load event
  // always use jQuery's namespace event
  //jQuery(window).bind('click.menusSelections', [this, 'onClickButton']);
  
  jQuery('a.link_view_list', this.widget).bind(
    'click.menusSelections', 
    [this, 'onClickViewList']
  );
  
  jQuery('a.link_view_photo', this.widget).bind(
    'click.menusSelections',
    [this, 'onClickViewPhoto']    
  );
  
  jQuery('li.widget-tabs-nav-item a', this.widget).bind(
    'click.menusSelections',
    [this, 'onClickTab']
  );
  
  jQuery('div.menu-link-' + this.wuid, this.widget).bind(
    'click.menusSelections',
    [this, 'onClickMenu']
  );
}

menusSelections.prototype.unload = function()
{
  // place here all code to unbind event
  // always use jQuery's namespace event
  jQuery(window).unbind('load.menusSelections', [this, 'load']);
  this.load = function() {}; // make sure the metthod is not exececuted
  
  jQuery(window).unbind('click.menusSelections', [this, 'onClickButton']);
  this.onClickButton = function() {};
}

menusSelections.prototype.onClickViewList = function(event)
{
  event.preventDefault();

  jQuery('#' + this.wuid + '_widget_settings_view_mode_image', this.widget).removeAttr('checked');
  jQuery('#' + this.wuid + '_widget_settings_view_mode_list', this.widget).attr('checked', 'checked');
  
  this.updateContent(event);
  
  return false;
}

menusSelections.prototype.onClickViewPhoto = function(event)
{
  event.preventDefault();
  
  jQuery('#' + this.wuid + '_widget_settings_view_mode_list', this.widget).removeAttr('checked');
  jQuery('#' + this.wuid + '_widget_settings_view_mode_image', this.widget).attr('checked', 'checked');
  
  this.updateContent(event);
  
  return false;
}

menusSelections.prototype.onClickTab = function(event)
{
  event.preventDefault();
  
  var tab = jQuery(event.target, this.widget).parents('li');
  
  jQuery('#' + this.wuid + '_widget_settings_selected_' + tab.attr('id'), this.widget).attr('checked', 'checked');
  
  this.updateContent(event);
  
  return false;
}

menusSelections.prototype.onClickMenu = function(event)
{
  document.location.href = jQuery(event.target).attr('goto');
  
  return false;
}

menusSelections.prototype.resize = function(size)
{
  log('You have resized the widget to : ' + size + 'px');
}

menusSelections.prototype.onClickButton = function()
{
  // catch your on click button
}

menusSelections.prototype.updateContent = function (event)
{
  var wuid = this.wuid;
  event.preventDefault();

  if (this.is_guest_mode != '1')
  {
    jQuery('form.widget-form-settings', this.widget).trigger('submit');
  }
  else
  {
    var form = jQuery('form.widget-form-settings', this.widget);
    var url = Portal.linkToAction('menusSelections', 'show', this.wuid, form.serializeArray());

    jQuery.ajax({
      type: 'POST',
      cache: false,
      url: url,
      beforeSend: function(request)
      {
        jQuery('#' + wuid + ' span.widget-loading-indicator').addClass('widget-loading-indicator-display');
      },
      success: function (data)
      {
        jQuery('div#' + wuid + ' div.widget-content', this.widget).html(data);
      },
      complete: function (request)
      {
        jQuery('#' + wuid + ' span.widget-loading-indicator').removeClass('widget-loading-indicator-display');
      }
    });
  }
  
  return false;
}

// set javascript for this widget as loaded 
WIDGET_LOADED['menusSelections'] = true;
