// Note: No conflict function

// This function must be called after including the jQuery javascript file, but before including any other conflicting library, 
// and also before actually that other conflicting library gets used, in case jQuery is included last. noConflict can be called at 
// the end of the jQuery.js file to globally disable the $() jQuery alias. jQuery.noConflict returns a reference to jQuery, so it 
// can be used to override the $() alias of the jQuery object.
// Use jQuery via jQuery(...)
//jQuery.noConflict();

jQuery(document).ready(function(){
  jQuery("#banner").bannerCycle();
});

(function($){
  $.fn.bannerCycle = function(options) {
    var defaults = {
    };
    var options = $.extend(defaults, options);
    
    return this.each(function() {
      var obj = $(this);
      var container_height = 110;
      
      bannerCycleInit();
      
      function bannerCycleInit()
      {
        obj.find('li').remove();
        obj.addClass('loading');
        bannerLoadContent();
      }
      
      function bannerLoadContent()
      {
        $.getJSON('/' + locale + '/api/banner.json', function(data){
          // Fill the banner
          $.each(data, function(key, value){
            var li    = $('<li class="item"></li>');
            var a     = $('<a></a>');
            var span  = $('<span class="name"></span>');
            var img   = new Image();
            
            $(img).load(function(){
              $(this).hide();
              span.html(value.name);
              a.attr('href', value.link);
              a.append(this); // Add image
              a.append(span); // Add span
              li.append(a);
              obj.find('ul').append(li);
              $(this).show();
              
              // On the last image, get things started
              if (key == data.length - 1) { 
                obj.removeClass('loading');
                li.addClass('active');
                bannerCycleCurrent();
              }
            }).error(function(){
              //console.log('something went wrong here');
            }).attr('src', value.photo).attr('alt', value.name);
          });
        });
      }
      
      function bannerCycleCurrent()
      {
        var current     = obj.find(".active");
        var img         = current.find("img").eq(0);
        var img_height  = img.height();
        img.animate({ "top" : -(img_height - container_height) }, 7000, function(){
          bannerCycleToNext(obj);
        });
      }

      function bannerCycleToNext()
      {
        var current = obj.find(".active");
        var next    = bannerCycleGetNext(current);
        next.css({ "z-index" : 870, "display" : "block" });
        next.find("img").css({ "top" : 0 });
        current.css({ "z-index" : 880 }).fadeOut("slow", function(){
          current.css({ "z-index" : 860 }).removeClass("active");
          next.css({ "z-index" : 880 }).addClass("active");
          bannerCycleCurrent(obj);
        });
      }

      function bannerCycleGetNext(current)
      {
        var curr_index  = obj.find(".item").index(current);
        var length      = obj.find(".item").length;
        if (curr_index >= (length - 1)) {
          return obj.find(".item").eq(0);
        } else {
          return obj.find(".item").eq(curr_index + 1);
        }
      }
      
    });  
  };
})(jQuery);