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

photoGrid.prototype = new mgWidgetObject();

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

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

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

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

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

photoGrid.prototype.onClickTab = function(event)
{
  event.preventDefault();
  
  var tab    = jQuery(event.target, this.widget).parents('li');
  var tab_id = tab.attr('id');
  var values = tab_id.split('-');

  jQuery('#' + this.wuid + '_widget_settings_selected_tab', this.widget).val(values[1]);
  
  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('photoGrid', '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['photoGrid'] = true;
