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

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

recipesTop10.prototype = new mgWidgetObject();

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

// this method is call when the DOM / window is loaded
recipesTop10.prototype.load = function()
{
  // add here your load event
  // always use jQuery's namespace event
  //jQuery(window).bind('click.recipesTop10', [this, 'onClickButton']);
  
  jQuery('li.widget-tabs-nav-item a', this.widget).bind('click', [this, 'onClickTab']);
}

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

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

recipesTop10.prototype.onClickButton = function()
{
  // catch your on click button
  alert('you click on the button from the widget recipesTop10 : ' + this.wuid);
}

recipesTop10.prototype.onClickTab = function(event)
{
  event.preventDefault();
  
  var tab = jQuery(event.target, this.widget).parents('li');
  
  jQuery('#' + this.wuid + '_widget_settings_sort_by_' + tab.attr('id'), this.widget).attr('checked', true);
  
  jQuery('li.widget-tabs-nav-item', this.widget).removeClass('widget-tabs-selected');  
  tab.addClass('widget-tabs-selected');
  
  var formSettings = jQuery('form[wuid=' + this.wuid + ']', this.widget).serializeArray();
  var url = Portal.linkToAction('recipesTop10', 'refreshList', this.wuid, formSettings);

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

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