//$(document).ready(function () {alert('foo');});

function playPause() {
  var button = $('#playpause');
  if (button.attr('src').match(/stop/)) {
    button.attr('src', button.attr('src').replace(/stop/, 'play'));
  } else {
    $('#mycarousel img[@src*=rs]').each(function() {
      $(this).attr('src', $(this).attr("src").replace(/rs/g,"vs"));
    });           
    button.attr('src', button.attr('src').replace(/play/, 'stop'));
  }
}

function toggleCallback(running, interval) {
  var image = $(this);
  var width = image.width();
  var left = image.offset({relativeTo:image.parents('div#main')[0]}).left - $('#main').offset().left;
  if (running) {
    if (left > 0) {
      if (image.attr("src").match(/vs/g)) {
        image.attr("src", image.attr("src").replace(/vs/g,"rs"));
      } else {
        image.attr("src", image.attr("src").replace(/rs/g,"vs"));
      }
      return true;
    }
  } else {
    if (left < 2*width) {
      if (image.attr("src").match(/vs/g)) {
        $('img[@src*=rs]', image.parents('ul')).each(function() {
          $(this).attr('src', $(this).attr("src").replace(/rs/g,"vs"));
        });
        image.attr("src", image.attr("src").replace(/vs/g,"rs"));
        return ((interval) ? true : false);
      } else {
        image.attr("src", image.attr("src").replace(/rs/g,"vs"));
        return true;
      }
    } else {
      $('img[@src*=rs]', image.parents('ul')).each(function() {
        $(this).attr('src', $(this).attr("src").replace(/rs/g,"vs"));
      });           
      return true;
    }
  }
  return false;
}

jQuery(document).ready(function() {
  var res = jQuery('#mycarousel').jCarouselLite({
    auto: 1000,
    speed: 2000,
    visible: 3,
    scroll: 1,
    circular: true,
    divfitting: false,
    btnToggle:'#mycarousel img',
    toggle:toggleCallback,
    btnPlayPause:'#playpause',
    playpause:playPause
  });
  jQuery("#nav img").mouseover(function() {
    var img = $(this);
    if (img.attr("src").match(/00/g)) {
      img.attr("src", img.attr("src").replace(/00/g,"01"));
      img.css("cursor","pointer");
    } else {
      img.attr("src", img.attr("src").replace(/01/g,"00")); 
    }
  });
  jQuery("#nav img").mouseout(function() {
    var img = $(this);
    if (img.attr("src").match(/01/g)) {
      img.css("cursor","pointer");
      img.attr("src", img.attr("src").replace(/01/g,"00"));
    } else {
      img.attr("src", img.attr("src").replace(/00/g,"01")); 
    }
  });
  jQuery(".funcNav img").mouseover(function() {
    var img = $(this);
    if (img.attr("src").match(/00/g)) {
      img.attr("src", img.attr("src").replace(/00/g,"01"));
      img.css("cursor","pointer");
    } else {
      img.attr("src", img.attr("src").replace(/01/g,"00")); 
    }
  });
  jQuery(".funcNav img").mouseout(function() {
    var img = $(this);
    if (img.attr("src").match(/01/g)) {
      img.css("cursor","pointer");
      img.attr("src", img.attr("src").replace(/01/g,"00"));
    } else {
      img.attr("src", img.attr("src").replace(/00/g,"01")); 
    }
  });
});

