// when the DOM is ready...
$(document).ready(function () {

if( $('#slider').length > 0  ) {

    var $is_mini = $('#slider').hasClass('mini');

    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
      $panels.css({
        'display' : 'block',
        'float' : 'left',
        'position' : 'relative' // IE fix to ensure overflow is hidden
      });
      
      // calculate a new width for the container (so it holds all panels)
      if ($panels.length > 0) // needs to be here, of the following line can cause JS errors
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear

    $scroll_css = $is_mini ? { 'overflow': 'hidden', 'left': '0' } : { 'overflow': 'hidden', 'left': '-30px' }
    var $scroll = $('#slider .scroll').css($scroll_css);

    // apply our left + right buttons
    if ( $scroll.width() < $panels[0].offsetWidth*$panels.length ) {
        if ($is_mini) {
            $scroll
              .after('<img class="scrollButtons left" src="'+template_directory+'/images/layout/toggler-links-small.png" />')
              .after('<img class="scrollButtons right" src="'+template_directory+'/images/layout/toggler-rechts-small.png" />');
        } else {
            $scroll
              .after('<img class="scrollButtons left" src="'+template_directory+'/images/layout/toggler-links.png" />')
              .after('<img class="scrollButtons right" src="'+template_directory+'/images/layout/toggler-rechts.png" />');

            if (false && $('#slider .navigation').length > 0) {
                $scroll
                  .after('<img class="scrollButtons pause" src="'+template_directory+'/images/layout/toggler-pause.png" />')
                  .after('<img class="scrollButtons play" src="'+template_directory+'/images/layout/toggler-play.png" />');

                $('#slider .scrollButtons.pause').click( function() {
                    $scroll.trigger('stop');
                    $('#slider .scrollButtons.pause').hide();
                    $('#slider .scrollButtons.play').show();
                });
                $('#slider .scrollButtons.play').click( function() {
                    $scroll.trigger('start');
                    $('#slider .scrollButtons.play').hide();
                    $('#slider .scrollButtons.pause').show();
                });
            }
        }
        
        $scroll.mouseover( function() {
    //        $('#slider .scrollButtons.left').show();
    //        $('#slider .scrollButtons.right').show();
        });

        $scroll.mouseout( function() {
    //        $('#slider .scrollButtons.left').hide();
    //        $('#slider .scrollButtons.right').hide();
        });

    }


    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
      $container.css('paddingTop') : 
      $container.css('paddingLeft')) 
      || 0) * -1;
      
    var scrollOptions = {
      target: $scroll, // the element that has the overflow
      
      // can be a selector which will be relative to the target
      items: $panels,
      
      exclude: ( $scroll.width() > $panels[0].offsetWidth ) ? ( $scroll.width() / $panels[0].offsetWidth - 1 ) : 0,
      
      navigation: '.navigation a',
      
      // selectors are NOT relative to document, i.e. make sure they're unique
      prev: 'img.left', 
      next: 'img.right',
      
      // allow the scroll effect to run both directions
      axis: 'xy',
      
      onAfter: trigger, // our final callback
      
      offset: offset,
      
      // duration of the sliding effect
      duration: $is_mini ? 300 : 500,
      
      // easing - can be used with the easing plugin: 
      // http://gsgd.co.uk/sandbox/jquery/easing/
      easing: 'swing',
      
      // this is the autoscroll bit
      interval: 5000,
      
      force: true
      
    };

    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);

    if (window.location.hash) {
      trigger({ id : window.location.hash.substr(1) });
      $('#'+window.location.hash.substr(1)+' .popuplink').click();
    } else {
      trigger({ id : "1" });
      $('ul.navigation a:first').click();
      updateLabel("1");
    }

}

if( $('#post_slider').length > 0  ) {

    var $panels = $('#post_slider .scrollContainer > div');
    var $container = $('#post_slider .scrollContainer');

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
      $panels.css({
        'display' : 'block',
        'float' : 'left',
        'position' : 'relative' // IE fix to ensure overflow is hidden
      });
      
      // calculate a new width for the container (so it holds all panels)
      if ($panels.length > 0) // needs to be here, of the following line can cause JS errors
        $container.css('width', $panels[0].offsetWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear

    $scroll_css = { 'overflow': 'hidden', 'left': '0' };
    var $scroll = $('#post_slider .scroll').css($scroll_css);

    // no left + right buttons

    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
    var offset = parseInt((horizontal ? 
      $container.css('paddingTop') : 
      $container.css('paddingLeft')) 
      || 0) * -1;
      
    var scrollOptions = {
      target: $scroll, // the element that has the overflow
      
      // can be a selector which will be relative to the target
      items: $panels,
      
      exclude: ( $scroll.width() > $panels[0].offsetWidth ) ? ( $scroll.width() / $panels[0].offsetWidth - 1 ) : 0,
      
      navigation: '.navigation a',
      
      // allow the scroll effect to run both directions
      axis: 'xy',
      
      onAfter: trigger, // our final callback
      
      offset: offset,
      
      // duration of the sliding effect
      duration: 300,
      
      // easing - can be used with the easing plugin: 
      // http://gsgd.co.uk/sandbox/jquery/easing/
      easing: 'swing',
      
      // this is the autoscroll bit
      interval: 5000,
      
      force: true
      
    };

    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
    $('#post_slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);

    if (window.location.hash) {
      trigger({ id : window.location.hash.substr(1) });
      $('#'+window.location.hash.substr(1)+' .popuplink').click();
    } else {
      trigger({ id : "1" });
      $('ul.navigation a:first').click();
    }

}

    // handle nav selection
    function selectNav() {
      $(this)
        .parents('ul:first')
          .find('a')
            .removeClass('selected')
          .end()
        .end()
        .addClass('selected');
    }

    // tiffany added. updates the label
    function updateLabel(id) {
      var label = $('#slider').find('div[id$="' + id + '"]').attr("title");
      $('#previewLabel p').html(label);
     }
     
    $('#slider .navigation').find('a').click(selectNav);

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
      var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
      if ( !el )
        var el = $('#post_slider .navigation').find('a[href$="' + data.id + '"]').get(0);
        
      selectNav.call(el);
      updateLabel(data.id);
    }

});
