//Globals
var GallerixWidgets = GallerixWidgets || {};
GallerixWidgets.fn = GallerixWidgets.fn || {};
  
$(document).ready(function() {


  GallerixWidgets.init();


});





GallerixWidgets.init = function () {

  GallerixWidgets.inlineWidgets =  Drupal.settings.gallerixWidgets['inlineWidgets'];
  GallerixWidgets.viewspaceWidgets =  Drupal.settings.gallerixWidgets['viewspaceWidgets'];
  GallerixWidgets.linkWidgets =  Drupal.settings.gallerixWidgets['linkWidgets'];
      
  $('.gallerix-widget.collapsible').each(function() {
      var open = true; 
      var hover_text = $(this).find('#' + this.id + '-icon').attr('title');

      var hide = function () {
        open = open ? false : true;
        $(this).attr('title', (open ? 'Hide' : hover_text));
        $(this).siblings().toggle();      
      }
      
      
      if ($(this).is('.collapsed')) {
        var icon = $(this).find('img.gallerix-widget-icon');
        hide.apply(icon);
      }
      else {
        $(this).find('#' + this.id + '-icon').attr('title', 'Hide');
      }   
      $(this).find('img.gallerix-widget-icon').click(hide);
  });
  
  
  
  
  $('.gallerix-widget-viewspace-link').each(function() {
    var open = true;
    var hover_text = $(this).find('#' + this.id + '-icon').attr('title');  
    
    var hover_open_text = hover_text.split('_')[1];    
    hover_text = hover_text.split('_')[0];

    

    if($('#' + this.id + '-' + 'edit').is('.collapsed')) {
      open = false;
      $(this).find('#' + this.id + '-icon').attr('title', hover_text);
      $('#' + this.id + '-' + 'edit').hide(); 
      
    }
    else {
      $(this).find('#' + this.id + '-icon').attr('title', hover_open_text);
    }
    
    
    $(this).click(function () {
      open = open ? false : true;    
      $(this).find('#' + this.id + '-icon').attr('title', open ? hover_open_text : hover_text);
    
      $('#' + this.id + '-' + 'edit').toggle();
      return false;     
    });      
    
  });
  
  $('.gallerix-widget-link-external').each(function() {
    $(this).attr('target', '_blank');
  });


}








/**
 * Callback performed after each fetch operation.
 *
 */
Gallerix.fn.updateWidgets = function(image) { 
  
  for (widget in GallerixWidgets.inlineWidgets)  {
    $(widget).html(image[GallerixWidgets.inlineWidgets[widget]] + '');
  }
  for (widget in GallerixWidgets.linkWidgets)  {
    $(widget).attr('href', image[GallerixWidgets.linkWidgets[widget]] + '');
  }   
  for (widget in GallerixWidgets.viewspaceWidgets)  {
    $(widget).html(image[GallerixWidgets.viewspaceWidgets[widget]] + '');
  }   
}





/**************************** Utilities *******************************/




/**
 * Attach a behavior to whatever elements match the criteria.
 *
 * @param criteria the selector.
 *
 * @param type the type of event to attach.
 *
 * @param callback the function to execute when the event is triggered.
 */
GallerixWidgets.attachEvent = function (criteria, type, callback) {
  $(criteria).each(function() {
    if (!$(this).is('.activated')) {
      $(this)[type](function() {


        GallerixWidgets.fn[callback](this);     
        return false;
      });
      $(this).addClass('activated');
      
    }
    
  });  
}









