﻿$(document).ready(function () {
    $('#nav li').hover(
            function () {
                //show its submenu
                $('ul', this).slideDown(250);
            },
            function () {
                //hide its submenu
                $('ul', this).slideUp(250);
            }
          );

    previousHeight = $('#content').height();
    fixrightborder();

    function fixrightborder() {
        if (previousHeight < $(window).height()) {
            $('#content').height($('#leftcol').height() - 130);
        }
    }

    $(window).resize(fixrightborder)
                      .scroll(fixrightborder);
});

(function ($) {
          $.fn.ContentSlider = function (options) {
              var defaults = {
                  width: '860px',
                  height: '250px',
                  speed: 400,
                  easing: 'easeOutQuad',
                  textResize: false,
                  IE_h2: '26px',
                  IE_p: '11px'
              }
              var defaultWidth = defaults.width;
              var o = $.extend(defaults, options);
              var w = parseInt(o.width);
              var n = this.children('.cs_wrapper').children('.cs_slider').children('.cs_article').length;
              var x = -1 * w * n + w; // Minimum left value
              var p = parseInt(o.width) / parseInt(defaultWidth);
              var thisInstance = this.attr('id');
              var inuse = false; // Prevents colliding animations

              function moveSlider(d, b) {
                  var l = parseInt(b.siblings('.cs_wrapper').children('.cs_slider').css('left'));
                  if (isNaN(l)) {
                      var l = 0;
                  }
                  var m = (d == 'left') ? l - w : l + w;
                  if (m <= 0 && m >= x) {
                      b.siblings('.cs_wrapper')
                        .children('.cs_slider')
                          .animate({ 'left': m + 'px' }, o.speed, o.easing, function () {
                              inuse = false;
                          });
                  }
              }

              return this.each(function () {

                  // Resize the font to match the bounding box
                  if (o.textResize === true) {
                      var h2FontSize = $(this).find('h2').css('font-size');
                      var pFontSize = $(this).find('p').css('font-size');
                      $.each(jQuery.browser, function (i) {
                          if ($.browser.msie) {
                              h2FontSize = o.IE_h2;
                              pFontSize = o.IE_p;
                          }
                      });
                      $(this).find('h2').css({ 'font-size': parseFloat(h2FontSize) * p + 'px', 'margin-left': '66%' });
                      $(this).find('p').css({ 'font-size': parseFloat(pFontSize) * p + 'px', 'margin-left': '66%' });
                      $(this).find('.readmore').css({ 'font-size': parseFloat(pFontSize) * p + 'px', 'margin-left': '66%' });
                  }

                  $(".paging a:first").addClass("active");

                  //Paging and Slider Function
                  rotate = function () {
                      var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                      var sliderPosition = triggerID * w; //Determines the distance the image reel needs to slide

                      $(".paging a").removeClass('active'); //Remove all active class
                      $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

                      $('.cs_slider').animate({
                          left: -sliderPosition
                      }, 500);
                  };

                  //Rotation  and Timing Event
                  rotateSwitch = function () {
                      play = setInterval(function () { //Set timer - this will repeat itself every 7 seconds
                          $active = $('.paging a.active').next(); //Move to the next paging
                          if ($active.length === 0) { //If paging reaches the end...
                              $active = $('.paging a:first'); //go back to first
                          }
                          rotate(); //Trigger the paging and slider function
                      }, 7000); //Timer speed in milliseconds (5 seconds)
                  };

                  //On Hover
                  $(".contentslider").hover(function () {
                      clearInterval(play); //Stop the rotation
                  }, function () {
                      rotateSwitch(); //Resume rotation timer
                  });

                  rotateSwitch(); //Run function on launch

                  $(".paging a").click(function () {
                      $active = $(this); //Activate the clicked paging                        

                      //Reset Timer
                      clearInterval(play); //Stop the rotation
                      rotate(); //Trigger rotation immediately
                      //rotateSwitch(); // Resume rotation timer
                      return false; //Prevent browser jump to link anchor
                  });
              });              
          }
      })(jQuery)

$(function () {
    $('#contentslider').ContentSlider({
        width: '840px',
        height: '250px',
        speed: 800,
        easing: 'easeInOutSine'
    });
}); 
