/**
 * Vehicles - JS
 * --------------------
 * Version 1.0
 * (c) by David Spickers (DSP-DEVEL) 2011
 */

var diashow = true;

jQuery.fn.delay = function(time,func){ //run a delayed function
  return this.each(function(){
    setTimeout(func,time);
  });
};

function showRememberCount(count)
{
  if (count>0)
  {
    $('#remember_link').html('Merkliste ('+count+')');
  } else
  {
    $('#remember_link').html('Merkliste');
  }
}

$(document).ready(function () {
  //Miniaturfahrzeuge unterhalb des Headers Hovereffekt
  $('div[class=help2]').each(function () {
    var elems = $(this).children('div');
    if (elems && elems.length>0)
    {
      elems.each(function () {
        $(this).mouseenter(function () {
          $(this).addClass('help2_hover');
          $(this).css('cursor','pointer');
        });
        $(this).mouseleave(function () {
          $(this).removeClass('help2_hover');
          $(this).css('cursor','auto');
        });
        $(this).click(function () {
          location.href = $(this).children('input[name^=route]').val();
          return false;
        });
      });
    }
  });
  //Fahrzeugtabellen Hovereffekt
  $('table[class=vehicle-content]>tbody>tr').each(function () {
    $(this).mouseenter(function () {
      $(this).parent().children('tr').each(function () {
        $(this).addClass('hover');
      });
    });
    $(this).mouseleave(function () {
      $(this).parent().children('tr').each(function () {
        $(this).removeClass('hover');
      });
    });
  });
  //Fahrzeug merken
  $('a[rel^=remember_vehicle]').live('click',function () {
    var v_id = parseInt($(this).attr('rel').replace('remember_vehicle_',''));
    if (v_id)
      Routing.rememberVehicle(v_id, function (json) {
        var lnk = $('a[rel^=remember_vehicle_'+v_id+']');
        if (json.ok)
        {
          notify(json.title,json.message);
          showRememberCount(json.remembered);
          lnk.attr('rel','forget_vehicle_'+v_id);
          lnk.html('Fahrzeug entfernen');
        } else
          notify(json.title,json.message, true);
      });
    return false;
  });
  //Fahrzeug entfernen
  $('a[rel^=forget_vehicle]').live('click',function () {
    var v_id = parseInt($(this).attr('rel').replace('forget_vehicle_',''));
    if (v_id)
      Routing.forgetVehicle(v_id, function (json) {
        var lnk = $('a[rel^=forget_vehicle_'+v_id+']');
        if (json.ok)
        {
          notify(json.title,json.message);
          showRememberCount(json.remembered);
          lnk.attr('rel','remember_vehicle_'+v_id);
          lnk.html('Fahrzeug merken');
        } else
          notify(json.title,json.message, true);
      });
    return false;
  });
  //Fahrzeug-Galerie-Effekt
  $('a[rel^=gallery_picture_]').each(function () {
    $(this).click (function () {
      showPicture($(this));
      return false;
    });
  });
  //Kontaktformular ausblenden
  if ($('.contact_success').val() != null)
    if (!$('.contact_success').hasClass('nofade'))
      $('.contact_success').fadeOut(4000, function () {
        $(this).remove();
      })
  //Page runterscrollen wenn ein Formularfehler aufgetaucht ist
  if ($('.error_list').offset() != null)
    $(document).scrollTop($('.error_list').offset().top);

  //Diashow
  $('a[rel^=diashow_stop]').click(function () {
    return stopDiashow($(this));
  });
  $('a[rel^=diashow_start]').click(function () {
    return startDiashow($(this));
  });

  //Diashow
  $('a[rel^=diashow_prev]').click(function () {
    var was_off = false;
    if (!diashow)
    {
      was_off = true;
      diashow = true;
    }
    var lnk = $('a[rel^=diashow_stop]');
    stopDiashow(lnk);
    diashow_tick(true,true);
    if (was_off)
    {
      diashow = false;
    }
    return false;
  });
  $('a[rel^=diashow_next]').click(function () {
    var was_off = false;
    if (!diashow)
    {
      was_off = true;
      diashow = true;
    }
    var lnk = $('a[rel^=diashow_stop]');
    stopDiashow(lnk);
    diashow_tick(false,true);
    if (was_off)
    {
      diashow = false;
    }
    return false;
  });

  var lnk = $('a[rel^=diashow_stop]');
  if (lnk)
  {
    var max_images = $('.vehicle-nonselected-gallery-pic').length;
    if (max_images>1)
    {
      $(document).delay(3000, function(){
        startDiashow(lnk);
      });
    }
  }
});

//Startet eine Diashow mit Fahrzeugbildern
function startDiashow(link)
{
  diashow = true;
  $(link).parent().children('img').attr('src','/images/icons/set01/Stop.png');
  $(link).attr('rel','diashow_stop');
  $(link).html('Diashow anhalten');
  $(link).unbind('click');
  $(link).click(function () {
    stopDiashow(link);
    return false;
  });
  diashow_tick();
  return false;
}

//Stoppt eine Diashow mit Fahrzeugbildern
function stopDiashow(link)
{
  diashow = false;
  $(link).parent().children('img').attr('src','/images/icons/set01/Play.png');
  $(link).attr('rel','diashow_start');
  $(link).html('Diashow starten');
  $(link).unbind('click');
  $(link).click(function () {
    startDiashow(link);
    return false;
  });
  return false;
}

function diashow_tick(left, ticking)
{
  var gallery_id = parseInt($('.vehicle-selected-gallery-pic').parent().parent().attr('id').replace('gallery_cont_picture_',''));
  if (gallery_id)
  {
    var max_images = $('.vehicle-nonselected-gallery-pic').length;
    if (max_images && max_images>0)
    {
      if (!left)
        gallery_id++;
      else
        gallery_id--;
      if (gallery_id > max_images)
      {
        gallery_id = 1;
      }
      if (gallery_id < 0)
      {
        gallery_id = 1;
      }
      showPicture($('#gallery_cont_picture_'+gallery_id).children(':first'));
      if (diashow && !ticking)
      {
        $(document).delay(3000, function(){
          if (diashow)
            diashow_tick();
        });
      }
    }
  }
}

function showPicture(link)
{
  var p_id = parseInt($(link).attr('rel').replace('gallery_picture_',''));
  if (p_id && p_id>0)
  {
    if ($('#ajax_loader').attr('class') == null)
      $('.vehicle-detailed-picture').append('<img id="ajax_loader" src="/images/shared/ajax-loader.gif" class="ajax-loader" />');
    Routing.getPicture(p_id, function (json) {
      if (json.ok)
      {
        var old_img = $('#big_picture');
        var parent = old_img.parent();
        old_img.css('position','absolute');
        old_img.attr('id','deleted_big_picture');
        old_img.fadeOut(500, function (){ $(this).remove(); });

        var n_img = $('<img>');
        n_img.attr('id','big_picture');
        n_img.attr('src',json.src);
        n_img.css('position','absolute');
        n_img.hide();
        $(parent).append(n_img);
        $(n_img).fadeIn(800);
        $('.vehicle-selected-gallery-pic').removeClass('vehicle-selected-gallery-pic');
        $('#gallery_picture_'+p_id).addClass('vehicle-selected-gallery-pic');
      } else
      {
        notify(json.title,json.message, true);
      }
    });
  }
  $('#ajax_loader').fadeOut(500, function () {$(this).remove();});
}
