$(function(){
  //Open external links in new window (xhtml compliant)
  $("a[rel='external']").attr('target', '_blank');
  
  //Dealers search - old version - DROPPED FOR NOW!!!
  $("#searchDealerFormXXX").submit(function(event){
      event.preventDefault();        
      var data = $(".product").val();

      $(".product").addClass("ajax-loader");
      $.post(
          'index.php?id=100', {product: data},
          function(data){
              $('#searchDealersResultsYYY').html(data);
              $(".product").removeClass("ajax-loader");
              $(".product").addClass("ajax-loader-none");
          }
      )
  });
  
  // MARQUEE - on DOM ready
  $(".marquee").marquee({
    loop: -1
    // this callback runs when the marquee is initialized
    , init: function ($marquee, options){
      debug("init", arguments);
      if( $marquee.is("#marquee2") ) options.yScroll = "bottom";
    }
    // this callback runs before a marquee is shown
    , beforeshow: function ($marquee, $li){
      debug("beforeshow", arguments);
    }
    // this callback runs when a after message has being shown
    , aftershow: function ($marquee, $li){
      debug("aftershow", arguments);
      // find the author
      var $author = $li.find(".author");
      // hide the author
      if( $author.length ) $("#marquee-author").find("> span").fadeOut(250);
    }
  });

  // MAP FADE
  $('.fade-area-1').children().hover(function() {
    $(this).siblings().stop().fadeTo(500,0.5);
  }, function() {
    $(this).siblings().stop().fadeTo(500,1);
  });

  // FRONT GALLERY
  var galleries = $('.front_gallery_wrapper .ad-gallery').adGallery();
  $('#switch-effect').change(
    function() {
      galleries[0].settings.effect = $(this).val();
      return false;
    }
  );
  $('#toggle-slideshow').click(
    function() {
      galleries[0].slideshow.toggle();
      return false;
    }
  );
  $('#toggle-description').click(
    function() {
      if(!galleries[0].settings.description_wrapper) {
        galleries[0].settings.description_wrapper = $('#descriptions');
      }
      else {
        galleries[0].settings.description_wrapper = false;
      }
      return false;
    }
  );

  //Load the slideshow
  theRotator();
  $('div.rotator').fadeIn(1000);
  $('div.rotator ul li').fadeIn(1000); // tweek for IE

});

// CUFON FONT

//Cufon.replace('h3', { fontFamily: 'fritids_font' });
//Cufon.replace('.marquee', { fontFamily: 'fritids_font' });

// MARQUEE
var use_debug = false;

function debug(){
  if( use_debug && window.console && window.console.log ) console.log(arguments);
}

var iNewMessageCount = 0;

function addMessage(selector){
  // increase counter
  iNewMessageCount++;
  // append a new message to the marquee scrolling list
  var $ul = $(selector).append("<li>New message #" + iNewMessageCount + "</li>");
  // update the marquee
  $ul.marquee("update");
}

function pause(selector){
  $(selector).marquee('pause');
}

function resume(selector){
  $(selector).marquee('resume');
}

// IMAGE ROTATOR
function rotate() {
  //Get the first image
  var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));
  
  if ( current.length == 0 ) current = $('div.rotator ul li:first');
  
  //Get next image, when it reaches the end, rotate it back to the first image
  var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
  
  //Set the fade in effect for the next image, the show class has higher z-index
  next.css({opacity: 0.0})
  .addClass('show')
  .animate({opacity: 1.0}, 1000);
  
  //Hide the current image
  current.animate({opacity: 0.0}, 1000)
  .removeClass('show');
}

function theRotator() {
  //Set the opacity of all images to 0
  $('div.rotator ul li').css({opacity: 0.0});
  
  //Get the first image and display it (gets set to full opacity)
  $('div.rotator ul li:first').css({opacity: 1.0});
  
  //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
  self.setInterval("rotate()",8000);
}

//Fullscreen
var FullscreenrOptions = { width: 1920, height: 1200, bgID: '#bgimg' };
jQuery.fn.fullscreenr(FullscreenrOptions);

