/*
 * 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 childrenCorner() {
  // this global attributes
  this.wuid = null;
  this.widget = null;
  
  // widget attributes
  // this.field1 = null;
}

childrenCorner.prototype = new mgWidgetObject();

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

// this method is call when the DOM / window is loaded
childrenCorner.prototype.load = function()
{
  // add here your load event
  // always use jQuery's namespace event
  
  jQuery(this.widget).addClass('w-children-corner');
  
  jQuery('li.widget-tabs-nav-item a', this.widget).bind('click', [this, 'onClickTab']);
  
  
  /**
   * Lorsque l'utilisateur ajoute la recette � ses recettes
   *
   * @author Vincent Guillon
   * @since 29 juin 09
   */  
  jQuery('a.link-user-recipe', this.widget).bind(
    'click',
    [this, 'updateUserRecipe']
  );


  this.initSendToFriendsPopup();
  
}

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

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

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


/**
 * Met � jour un objet UserRecipe
 *
 * @author Vincent Guillon
 * @since 29 juin 09
 */
childrenCorner.prototype.updateUserRecipe = function ()
{
  // Si l'utilisateur n'est pas connect�
  if (!User.isLogged())
  {
    jQuery('#login-popup-filler').jqm({
      ajax: Portal.linkToAction('childrenCorner', 'loginPopup', this.wuid, []),
      overlay: 50
    });
    
    Site.handleUserAuthentification();

    return;
  }

  var input = jQuery('#user_recipe_is_published', this.widget);
  
  if (input.attr('value') == 0)
  {
    input.attr('value', 1);
  }
  else
  {
    input.attr('value', 0);
  }

  var form = jQuery('form#update_user_recipe_form', this.widget);
  var url = Portal.linkToAction('childrenCorner', 'updateUserRecipe', this.wuid, form.serializeArray());

  jQuery.ajax({
    type: 'POST',
    cache: false,
    url: url,
    success: function (data)
    {
      jQuery('a.link-user-recipe', this.widget).toggle();
    }
  });
}

childrenCorner.prototype.onClickTab = function(event)
{
  event.preventDefault();
  
  var tab = jQuery(event.target, this.widget).parents('li');
  
  jQuery('#' + this.wuid + '_widget_settings_category_id_' + 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('childrenCorner', 'show', 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['childrenCorner'] = true;
