var selSlide = 0; 
$(function() { 
  $(document).bgStretcher({
    images: [bgImg],
    imageWidth: 1600, imageHeight: 840
  });
  $("#GALLERY li a").colorbox();
  $('#SLIDE_0').fadeIn();
  $("#SLIDE_SELECTOR ul li a.item0").css("background-position","0px -58px");
  $("#SLIDE_SELECTOR ul li a").each(function (i){
    $(this).click(function(event){
      $("#SLIDE_SELECTOR ul li a").each(function (x){
        $(this).css("background-position","");
      });
      val = $(this).attr("class").toString().replace('item','');
      if (val<numSlides) { 
        event.preventDefault();
        selSlide = val;
        tmp = val;
        tmp++;
        if (tmp == numSlides) { 
          if (!$.support.opacity) {
            $('#SLIDE_BUTTONS').hide();
            $('#FINAL_SLIDE_BUTTON').show();
          } else {
            $('#SLIDE_BUTTONS').fadeOut();
            $('#FINAL_SLIDE_BUTTON').fadeIn();
          }
        } else {
          if (!$.support.opacity) {
            $('#FINAL_SLIDE_BUTTON').hide();
            $('#SLIDE_BUTTONS').show();
          } else {
            $('#FINAL_SLIDE_BUTTON').fadeOut();
            $('#SLIDE_BUTTONS').fadeIn();
          }
        }
        $("#SLIDES .slides").each(function (z){
          $(this).hide();
        });
        if (!$.support.opacity) 
          $('#SLIDE_'+val).show();
        else
          $('#SLIDE_'+val).fadeIn();
        $(this).css("background-position","0px -58px");
      }
    });
  });
  $('.next_slide').click(function() {
    $('.item'+selSlide).css("background-position","");
    $('#SLIDE_'+selSlide).hide();
    selSlide++;
    if (selSlide==numSlides) { 
      selSlide = 0;
    }
    if (selSlide+1==numSlides) {
      if (!$.support.opacity) {
        $('#SLIDE_BUTTONS').hide();
        $('#FINAL_SLIDE_BUTTON').show();
      } else {
        $('#SLIDE_BUTTONS').fadeOut();
        $('#FINAL_SLIDE_BUTTON').fadeIn();
      }
    } else {
      if (!$.support.opacity) {
        $('#SLIDE_BUTTONS').show();
        $('#FINAL_SLIDE_BUTTON').hide();
      } else {
        $('#SLIDE_BUTTONS').fadeIn();
        $('#FINAL_SLIDE_BUTTON').fadeOut();
      }
    }
    if (!$.support.opacity)
      $('#SLIDE_'+selSlide).show();
    else
      $('#SLIDE_'+selSlide).fadeIn();
    $('.item'+selSlide).css("background-position","0px -58px");
  });
  $('.notify').click(function() {
    $('#NOTIFY_ME').toggle();
  });
  $("#NOTIFY_ME").bind("mouseenter",function(){ }).bind("mouseleave",function(){
    if (!$.support.opacity) 
      $('#NOTIFY_ME').hide();
    else
      $('#NOTIFY_ME').fadeOut(); 
  });
}); 
