$(function() {

  // Options for SuperBGImage
  $.fn.superbgimage.options = {
    id: 'superbgimage', // id for the containter
    z_index: 0, // z-index for the container
    inlineMode: 0, // 0-resize to browser size, 1-do not resize to browser-size
    showimage: 1, // number of first image to display
    vertical_center: 0, // 0-align top, 1-center vertical
    transition: 2, // 0-none, 1-fade, 2-slide down, 3-slide left, 4-slide top, 5-slide right, 6-blind horizontal, 7-blind vertical, 90-slide right/left, 91-slide top/down
    transitionout: 1, // 0-no transition for previous image, 1-transition for previous image
    randomtransition: 0, // 0-none, 1-use random transition (0-7)
    showtitle: 0, // 0-none, 1-show title
    slideshow: 1, // 0-none, 1-autostart slideshow
    slide_interval: 6000, // interval for the slideshow
    randomimage: 0, // 0-none, 1-random image
    speed: 'slow', // animation speed
    preload: 1, // 0-none, 1-preload images
    onShow: superbgimage_show, // function-callback show image
    onClick: superbgimage_click, // function-callback click image
    onHide: superbgimage_hide, // function-callback hide image
    onMouseenter: superbgimage_mouseenter, // function-callback mouseenter
    onMouseleave: superbgimage_mouseleave, // function-callback mouseleave
    onMousemove: superbgimage_mousemove // function-callback mousemove
  };

  // initialize SuperBGImage
  $('#thumbs').superbgimage();

});

// function callback on hiding image
function superbgimage_hide(img) {
  $('#showtitle').hide();
}

// function callback on showing image
// get title and display it
function superbgimage_show(img) {
  $('#superbgimage').css('background', 'none');
  $('#superbgimage').append($('#showtitle'));
  $('#showtitle p.imagecount').html('image ' + img + ' of ' + $.superbg_imgIndex);
  if ($('#thumbs1').css('display') == 'block') {
    $('#showtitle p.title').html($('#thumbs1 a' + "[rel='" + img + "']").attr('title'));
  } else {
    $('#showtitle p.title').html($('#thumbs2 a' + "[rel='" + img + "']").attr('title'));
  }
  $('#showtitle').fadeIn('fast');
}

// function callback on clicking image, show next slide
function superbgimage_click(img) {
  $('#thumbs').nextSlide();
}

my_slideshowActive = false;

// function callback onmouseenter, stop slideshow, show pause-indicator
function superbgimage_mouseenter(img) {
  if ($.superbg_slideshowActive) {
    my_slideshowActive = true;
    if ($('#pause').length == 0) { 
      $('body').prepend('');
    }
    $('#pause').css('position', 'absolute').css('z-index', 3).show();
    return $('#thumbs').stopSlideShow();
  }
}

// function callback onmouseleave, start slideshow, hide pause-indicator
function superbgimage_mouseleave(img) {
  if (my_slideshowActive && ($('#pause').length > 0) && ($('#pause').css('display') == 'block'))  { 
    $('#pause').hide();
    return $('#thumbs').startSlideShow();
  }  
}

// function callback onmousemove, show and move pause-indicator
function superbgimage_mousemove(img, e) {
  if (my_slideshowActive && ($('#pause').length > 0)) { 
    $("#pause").css("top",(e.pageY + 20) + "px").css("left",(e.pageX + 20) + "px").show();
  }
}

/*************** *******************/

$(function() {

  // update superbgimage options
  function update_superbgOptions() {

    // speed
    var newspeed = 0;
    if (($("input[name='optspeed']").val() == 'slow') || ($("input[name='optspeed']").val() == 'normal') || ($("input[name='optspeed']").val() == 'fast')) {
      newspeed = $("input[name='optspeed']").val(); 
    } else {
      newspeed = parseInt($("input[name='optspeed']").val(), 10); 
      if (isNaN(newspeed)) {
        newspeed = 'slow';
        $("input[name='optspeed']").val('slow');
      }
    }
    
    // slidshow interval
    var newinterval = parseInt($("input[name='optinterval']").val(), 10); 
    if (isNaN(newinterval)) {
      newinterval = 5000;
      $("input[name='optinterval']").val(newinterval);
    }
    if ($.superbg_slideshowActive) { // restart slideshow
      clearInterval($.superbg_interval);
      return $('#thumbs').startSlideShow();
    }

    // transition out
    var newtransitionout = 0;
    if ($("input[name='opttransout']:checked").val() == 'on') {
      newtransitionout = 1;
    } else {
      newtransitionout = 0;
    }

    // random transition
    var newrandomtransition = 0;
    if ($("input[name='optrandomtrans']:checked").val() == 'on') {
      newrandomtransition = 1;
    } else {
      newrandomtransition = 0;
    }
    
    // random image
    var newrandomimage = 0;
    if ($("input[name='optrandom']:checked").val() == 'on') {
      newrandomimage = 1;
    } else {
      newrandomimage = 0;
    }

    // onclick-callback
    if ($("input[name='optclick']:checked").val() == 'on') {
      onclickfunc = superbgimage_click;
      $('#superbgimage img').each(function() { // add click-callback to all images
        $(this).unbind('click').click(function(){ superbgimage_click($(this).attr('rel')); });
      });  
    } else {
      onclickfunc = null;
      $('#superbgimage img').each(function() { // remove click-callback from all images
        $(this).unbind('click');
      });  
    }

    // onshow-callback
    if ($("input[name='optshow']:checked").val() == 'on') {
      onshowfunc = superbgimage_show;
      $('#showtitle').fadeIn('fast');
    } else {
      onshowfunc = null;
      $('#showtitle').hide();
    }
    
    // update options
    $.fn.superbgimage.options = { 
      transition: parseInt($("#transition").val(), 10),
      speed: newspeed,
      slide_interval: newinterval,
      transitionout: parseInt(newtransitionout, 10),
      randomtransition: parseInt(newrandomtransition, 10),
      randomimage: parseInt(newrandomimage, 10),
      onClick: onclickfunc,
      onShow: onshowfunc
    };
  
  }

  // hide options
  $("#options").css('height','15px').css('padding', '0px').addClass('hidden').children().hide();
  $("#options .legend").show();
  
  // hide set 2
  $("#thumbs2").hide().addClass('hidden');
  
  // fade overlay with controls, fade container to display titles
  $('#overlay').fadeTo('slow', 0.75);
  $('#showtitle').fadeTo('slow', 0.40);
  $('#showtitle').hover(
    function () {
      $(this).fadeTo('fast', 1.00);
    },
    function () {
      $(this).fadeTo('fast', 0.40);
    }
  );

  // prev slide
  $('a.prev').click(function() {
    return $('#thumbs').prevSlide();
  });

  // next slide
  $('a.next').click(function() {
    return $('#thumbs').nextSlide();
  });

  // start slideshow
  $('a.start').click(function() {
    update_superbgOptions();
    return $('#thumbs').startSlideShow();
  });

  // stop slideshow
  $('a.stop').click(function() {
    my_slideshowActive = false;
    return $('#thumbs').stopSlideShow();
  });
  
  // change transition with selectbox
  $("#transition").change(function() {
    update_superbgOptions();
  });  

  // change option speed
  $("input[name='optspeed']").change(function() {
    update_superbgOptions();
  });    
  
  // change option slide_interval
  $("input[name='optinterval']").change(function() {
    update_superbgOptions();
  });    

  // change option transitionout
  $("input[name='opttransout']").click(function() {
    update_superbgOptions();
  });  
  
  // change option randomtransition
  $("input[name='optrandomtrans']").click(function() {
    update_superbgOptions();
  });
  
  // change option randomimage
  $("input[name='optrandom']").click(function() {
    update_superbgOptions();
  });  

  // change option onClick-callback
  $("input[name='optclick']").click(function() {
    update_superbgOptions();
  });  
  
  // change option onShow-callback
  $("input[name='optshow']").click(function() {
    update_superbgOptions();
  });    

  // toggle overlay
  $("h1 a").click(function() {
    $(this).blur();
    if ($("#overlay").hasClass('hidden')) {
      $("#overlay").css('height','auto').removeClass('hidden').children().show();
      if ($('#thumbs').hasClass('hidden')) {
        $('#thumbs').hide();
      }
    } else {
      $("#overlay").css('height','100px').addClass('hidden').children().hide();
      $("h1").show();
    }
    return false;
  });  
  
});

