jQuery(function ($) {
    $('.gallery-controls > .icon_download, .gallery-controls > .icon_share').live('click', function() {
        var popup = $(this).siblings('.fly-out');
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();
//        $('.fly-out-move').hide();

        if (hidden) {
            popup.show();
        } else {
            popup.hide();
        }
    });

    $('.gallery-controls > .icon_edit, .gallery-controls > .icon_none').live('click', function() {
        var id = $(this).attr('id');
        var index = id.indexOf('-');
        var flyoutId = id.substr(index+1);
        var popup = $('#fly-out-' + flyoutId);
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();
//        $('.fly-out-move').hide();

        if (hidden) {
            popup.show().position({
                "my": "center top",
                "at": "center bottom",
                "of": this,
                "offset": "0 10",
                "collision": "none none",
                "bgiframe": true
            });
        } else {
            popup.hide();
        }
    });

    $('.gallery-controls > .icon_playlist').live('click', function() {
        $("#currentPlaylistItem").attr("id", "");
        var container = $(this).siblings('.fly-out').children('.gallery-controls-add-playlist-wrapper').children('.playlist_command_wrapper');
        var successful = container.children('.playlist_success.active');
        if (successful.length > 0) {
            successful.removeClass('active');
            successful.siblings('.playlist_already').addClass('active');
        } else {
            container.children('.playlist_command').attr("id", "currentPlaylistItem");
        }
        var popup = $(this).siblings('.fly-out');
        var hidden = popup.is(':hidden');

        //close all others
        $('.fly-out').hide();

        if (hidden) {
            popup.show();
        } else {
            popup.hide();
        }
    });

    $('.fly-out .close').live('click', function() {
        $(this).parents('.fly-out').hide();
    });

    //close .fly-out when clicked outside of .fly-out
    $(document).bind('mousedown.fly-out', function(e) {
        if (!$(e.target).is('.fly-out') && $(e.target).parents('.fly-out').length == 0) {
            $('.fly-out').each(function() {
                var $this = $(this);
                if (!$this.is(':hidden')) {
                    $this.hide();
                }
            });
        }
    });

    $.fn.addedToPlaylist = function(result, message) {
        jQuery('#currentPlaylistItem').removeClass('active');
        if (result > 0) {
            jQuery("#edit-change-playlist-id").change();
            jQuery('#currentPlaylistItem').siblings('.playlist_success').addClass('active');
        } else if (result == 0) {
            jQuery('#currentPlaylistItem').siblings('.playlist_already').addClass('active');
        } else {
            alert(message);
        }
    };

    $.fn.removedFromPlaylist = function(pliid) {
      $('.fly-out').hide();
      $("#" + pliid).parents('tr').remove();
    };

    $.fn.resetPlaylistIcon = function() {
        jQuery('.playlist_success.active').removeClass('active');
        jQuery('.playlist_already.active').removeClass('active');
        jQuery('.playlist_command').addClass('active');
    };
});


;
(function ($) {
Drupal.settings.views = Drupal.settings.views || {'ajax_path': '/views/ajax'};

Drupal.quicktabs = Drupal.quicktabs || {};

Drupal.quicktabs.getQTName = function (el) {
  return el.id.substring(el.id.indexOf('-') +1);
}

Drupal.behaviors.quicktabs = {
  attach: function (context, settings) {
    $.extend(true, Drupal.settings, settings);
    $('.quicktabs-wrapper', context).once(function(){
      Drupal.quicktabs.prepare(this);
    });
  }
}

// Setting up the inital behaviours
Drupal.quicktabs.prepare = function(el) {
  // el.id format: "quicktabs-$name"
  var qt_name = Drupal.quicktabs.getQTName(el);
  var $ul = $(el).find('ul.quicktabs-tabs:first');
  $ul.find('li a').each(function(i, element){
    element.myTabIndex = i;
    element.qt_name = qt_name;
    var tab = new Drupal.quicktabs.tab(element);
    var parent_li = $(element).parents('li').get(0);
    if ($(parent_li).hasClass('active')) {
      $(element).addClass('quicktabs-loaded');
    }
    $(element).once(function() {$(this).bind('click', {tab: tab}, Drupal.quicktabs.clickHandler);});
  });
}

Drupal.quicktabs.clickHandler = function(event) {
  var tab = event.data.tab;
  var element = this;
  // Set clicked tab to active.
  $(this).parents('li').siblings().removeClass('active');
  $(this).parents('li').addClass('active');

  // Hide all tabpages.
  tab.container.children().addClass('quicktabs-hide');
  
  if (!tab.tabpage.hasClass("quicktabs-tabpage")) {
    tab = new Drupal.quicktabs.tab(element);
  }

  tab.tabpage.removeClass('quicktabs-hide');
  return false;
}

// Constructor for an individual tab
Drupal.quicktabs.tab = function (el) {
  this.element = el;
  this.tabIndex = el.myTabIndex;
  var qtKey = 'qt_' + el.qt_name;
  var i = 0;
  for (var key in Drupal.settings.quicktabs[qtKey].tabs) {
    if (i == this.tabIndex) {
      this.tabObj = Drupal.settings.quicktabs[qtKey].tabs[key];
      this.tabKey = key;
    }
    i++;
  }
  this.tabpage_id = 'quicktabs-tabpage-' + el.qt_name + '-' + this.tabKey;
  this.container = $('#quicktabs-container-' + el.qt_name);
  this.tabpage = this.container.find('#' + this.tabpage_id);
}

if (Drupal.ajax) {
  /**
   * Handle an event that triggers an AJAX response.
   *
   * We unfortunately need to override this function, which originally comes from
   * misc/ajax.js, in order to be able to cache loaded tabs, i.e. once a tab
   * content has loaded it should not need to be loaded again.
   *
   * I have removed all comments that were in the original core function, so that
   * the only comments inside this function relate to the Quicktabs modification
   * of it.
   */
  Drupal.ajax.prototype.eventResponse = function (element, event) {
    var ajax = this;

    if (ajax.ajaxing) {
      return false;
    }
  
    try {
      if (ajax.form) {
        if (ajax.setClick) {
          element.form.clk = element;
        }
  
        ajax.form.ajaxSubmit(ajax.options);
      }
      else {
        // Do not perform an ajax request for already loaded Quicktabs content.
        if (!$(element).hasClass('quicktabs-loaded')) {
          ajax.beforeSerialize(ajax.element, ajax.options);
          $.ajax(ajax.options);
          if ($(element).parents('ul').hasClass('quicktabs-tabs')) {
            $(element).addClass('quicktabs-loaded');
          }
        }
      }
    }
    catch (e) {
      ajax.ajaxing = false;
      alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message);
    }
    return false;
  };
}


})(jQuery);
;

