/*
Site: IFRA
HTTP: www.ifra.com
Date: 2009-05-22
*/


/**
 * Site - Initialize
 */
$(document).ready(
  function() {
    function_view_node_mark();
    function_tooltip();
    function_view_custom_frontpage_teaser();
  }
);


/**
 * View - Node - Mark
 */
function function_view_node_mark() {

  // MARK NODES WITH ADDITIONAL CSS CLASSES
  if ($("div.view-content").length > 0) {
    $("div.view-content").find("div.node").each(
      function(number_i){
        if (number_i % 2 == 0) {
          $(this).addClass("node-even");
        } else {
          $(this).addClass("node-odd");
        }
      }
    )
  }

};


/**
 * Tooltip
 */
function function_tooltip() {
  // TOOLTIP
  $('.tooltip').removeAttr('title');

  $('.tooltip-trigger').html('');

  $('.tooltip-trigger').hover(
    function() {
      $(this).parent().children('.tooltip-display').fadeIn(300);
    },
    function() {
      $(this).parent().children('.tooltip-display').fadeOut(100);
    }
  );

};


/**
 * View - Custom - Frontpage - Teaser
 */
var number_custom_frontpage_count = 0;
var number_rows_length = 0;
var string_mode = 'auto';
var boolean_slide_block = false;

function function_view_custom_frontpage_teaser() {


  $('div.mainbar div.view-custom-frontpage-teaser').hover(
    function () {
      string_mode = 'stop';
    },
    function () {
      string_mode = 'auto';
      // clear interval
      window.clearInterval(interval_view_custom_frontpage_teaser);
      // slide manually triggered
      function_view_custom_frontpage_teaser_slide();
      // set interval again
      interval_view_custom_frontpage_teaser = window.setInterval("function_view_custom_frontpage_teaser_slide()", 6000);
    }
  );
  // Get values of HTML elements
  number_rows_length = $('div.mainbar div.view-custom-frontpage-teaser').find('.views-row-odd, .views-row-even').length;
  number_rows_width = $('div.mainbar div.view-custom-frontpage-teaser').innerWidth();

  // Set random start position
  number_custom_frontpage_count = Math.round(Math.random()*number_rows_length + 0.5);
  $('div.mainbar div.view-custom-frontpage-teaser .view-content').css( 'marginLeft', '-' + ( Number(number_custom_frontpage_count) * Number(number_rows_width) ) + 'px' );

  // Simulate carousel - Clone first element and append it
  $('div.mainbar div.view-custom-frontpage-teaser .views-row-first').clone().appendTo( $('div.mainbar div.view-custom-frontpage-teaser .view-content') );
  $('div.mainbar div.view-custom-frontpage-teaser .view-content').css('width', (number_rows_length+1)*number_rows_width + 'px')
  interval_view_custom_frontpage_teaser = window.setInterval("function_view_custom_frontpage_teaser_slide()", 6000);

}

var number_custom_frontpage_count_old;

function function_view_custom_frontpage_teaser_slide() {

  // Create animation
  if ( string_mode == 'auto' || string_mode == 'slide' ) {
    if ( string_mode == 'auto' && boolean_slide_block == true ) {
      boolean_slide_block = false;
    }
    if ( number_custom_frontpage_count >= number_rows_length ) {
      $('div.mainbar div.view-custom-frontpage-teaser .view-content').css('margin-left', '0px');
      number_custom_frontpage_count = 0;
    }
    if ( string_mode == 'auto' && boolean_slide_block == false ) {
      number_custom_frontpage_count++;
    }
    if ( number_custom_frontpage_count_old != number_custom_frontpage_count && boolean_slide_block == false ) {

      boolean_slide_block = true;

      $('div.mainbar div.view-custom-frontpage-teaser .view-content').animate( { marginLeft: '-' + ( Number(number_custom_frontpage_count) * Number(number_rows_width) ) + 'px' }, 1500,
        function () {
          boolean_slide_block = false;
          if (string_mode == 'slide') {
            function_view_custom_frontpage_teaser_slide();
          }
        }
      );
      
      number_custom_frontpage_count_old = number_custom_frontpage_count;
    }
  }
}

