if(!window['cmba']) {
  window['cmba'] = {};
  var cmba = window['cmba'];
}

(function(){
  $(document).ready(function() {
    cmba.heroshots();
    cmba.sponsors();
    cmba.drealtors();
  });
  
  $(window).load(function() {
    if($('body').height() < $('html').height()) {
      $('#ft').height($('#ft').height() + ($('html').height() - $('body').height()));
    }
  });
  
  cmba.heroshots = function() {
    var heroshots = $('.heroshot');
    
    heroshots.each(function() {
      var heroshot = $(this);
      var images = heroshot.find('img');
      var fade = 2; // Length of the transistion in seconds
      var duration = 5; // How long to show each heroshot in seconds
      
      if(images.size() < 2) { return; }
      
      setInterval(function() {
        var current = images.filter(':visible');
        var next = current.next('img,a').size() ? current.next('img,a') : images.eq(0);
        current.fadeOut(fade * 1000);
        (next.find('img').size() ? next.find('img') : next).fadeIn(fade * 1000);
      }, (fade + duration) * 1000);
      
      images.hide().eq(0).show();
    });
  }
  
  cmba.sponsors = function() {
    var container = $('#sponsors');
    if(!container.size()) { return; }

    container.addClass('scripted');
    var list = container.find('ul');

    var gradient = $(jQuery('<div class="gradient"></div>'));
    container.prepend(gradient);

    var sponsors = container.find('li');
    if(sponsors.length < 3) { return; }

    sponsors.each(function() {
      list.width(list.width() + $(this).outerWidth({'margin':true}));
    });
    var listWidth = list.width();

    sponsors.eq(0).attr({'current':'current'});
    
    setInterval(function() {
      var current = list.find('[current]');
      current.removeAttr('current');
      var next = current.next('li');
      if(!next.size() || !next.next('li').size()) {
        list.width(list.width() + listWidth);
        sponsors.clone(true).appendTo(list);
        if(!next.size()) {
          next = current.next('li');
        }
      }
      next.attr({'current':'current'});
      list.animate({
        'left': (0 - next.position().left - Math.round(next.width() / 2) + Math.round(container.width() / 2)) + 'px'
      }, 1 * 1000);
      gradient.unbind('click').css({'cursor':'default'});
      if(next.find('a').size()) {
        gradient.bind('click',function(){
          window.location = next.find('a').attr('href');
        }).css({'cursor':'pointer'});
      }
    }, 5 * 1000); // The length that each sponsor should show in milliseconds
    
    list.css({
      'left': Math.round(container.width() / 2) - Math.round(sponsors.eq(0).width() / 2)
    })
    if(sponsors.eq(0).find('a').size()) {
      gradient.bind('click',function(){
        window.location = sponsors.eq(0).find('a').attr('href');
      }).css({'cursor':'pointer'});
    }
}

cmba.drealtors = function() {
    var container = $('#drealtors');
    if (!container.size()) { return; }

    container.addClass('scripted');
    var list = container.find('ul');

    var gradient = $(jQuery('<div class="gradient"></div>'));
    container.prepend(gradient);

    var drealtors = container.find('li');
    if (drealtors.length < 3) { return; }

    drealtors.each(function() {
        list.width(list.width() + $(this).outerWidth({ 'margin': true }));
    });
    var listWidth = list.width();

    drealtors.eq(0).attr({ 'current': 'current' });

    setInterval(function() {
        var current = list.find('[current]');
        current.removeAttr('current');
        var next = current.next('li');
        if (!next.size() || !next.next('li').size()) {
            list.width(list.width() + listWidth);
            drealtors.clone(true).appendTo(list);
            if (!next.size()) {
                next = current.next('li');
            }
        }
        next.attr({ 'current': 'current' });
        list.animate({
            'left': (0 - next.position().left - Math.round(next.width() / 2) + Math.round(container.width() / 2)) + 'px'
        }, 1 * 1000);
        gradient.unbind('click').css({ 'cursor': 'default' });
        if (next.find('a').size()) {
            gradient.bind('click', function() {
                window.location = next.find('a').attr('href');
            }).css({ 'cursor': 'pointer' });
        }
    }, 5 * 1000); // The length that each sponsor should show in milliseconds

    list.css({
        'left': Math.round(container.width() / 2) - Math.round(drealtors.eq(0).width() / 2)
    })
    if (drealtors.eq(0).find('a').size()) {
        gradient.bind('click', function() {
            window.location = drealtors.eq(0).find('a').attr('href');
        }).css({ 'cursor': 'pointer' });
    }
}

})();