(function($)
{
  $(function(){
    
    // fix ie scroll bug
    if($.browser.msie)
    {
      $('#bg').bind('mousewheel', function(event)
      {
        if(event.wheelDelta > 0)
        {
          // down
          $('#wrapper').scrollTop($('#wrapper').scrollTop() - 40);
        }
        else
        {
          // down
          $('#wrapper').scrollTop($('#wrapper').scrollTop() + 40);
        }
      });
    }
    
    
    /*
     *
     *  Toggble news_items
     */
    $('.news_item h2').click(function(e)
    {
      e.preventDefault();
      if($(this).parent().find('.news_body').is(':hidden'))
      {
        $(this).parent().find('.news_body').slideDown(500);
      }
      else
      {
        $(this).parent().find('.news_body').slideUp(500);
      }
    });
    
    /**
     *
     * News items become less opacity on mouse over 
     */
    $('.news_item').hover(function()
    {
//      $('.news_item_bg', this).stop().animate({opacity: '1'}, {duration: 1000});
      $('.news_item_bg', this).stop().fadeTo(1000, '1.0')
    }, function()
    {
      $('.news_item_bg', this).stop().fadeTo(1000, '0.5');
//      $('.news_item_bg', this).stop().animate({opacity: '.5'}, {duration: 1000});
    });


    // init posiiton
    $('.vlist', '#col1_content').css('top', $('#wrapper').scrollTop());
    var scrollTimer;
    var scrollStart = 220;
    var scrollDelay = 900;
    $('#wrapper').scroll(function(e)
    {
      var scrollTop = $('#wrapper').scrollTop();
      var marginTop = null;
      if(scrollTop > scrollStart)
      {
        clearTimeout(scrollTimer);
        marginTop = scrollTop;

      } else if(scrollTop < 220)
      {
        clearTimeout(scrollTimer);
        marginTop = 0;
      }

      if(marginTop != null)
      {
        scrollTimer = setTimeout(function()
        {
          $('.vlist', '#col1_content').animate({
            'top': marginTop
          }, {
            duration: scrollDelay,
            easing: 'easeInOutCirc'
          })
        }, 500);
      }
    });

    $('.csc-textpic-imagewrap', '#col3 .image_slider').each(function(index, item)
    {
      $(this).attr('style', '');
      if(!$(this).hasClass('.csc-textpic-single-image'))
      {
        $(this).parent().slider();
      }
    });
  });
  
  
  
  /**
   *
   *  Slider Plugin
   *
   */
  $.fn.slider = function()
  {
    var options = {durationFactor: 7};
    var self = this;
    
    /**
     * Slide function
     */
    function slide(direction, imageSetWidth, sliderElement)
    {
      var imageSetElement = $('ul', sliderElement);
      var currentLeftOffset = parseInt($(imageSetElement).css('left').replace(/px/, ''));
      if(direction == 'right')
      {
        var leftOffset = imageSetWidth - $(sliderElement).width();
        var duration = (leftOffset + currentLeftOffset) * options.durationFactor;
        if(leftOffset > 0)
        {
          $(imageSetElement).stop().animate({left: 0 - leftOffset}, {easing: 'linear', duration: duration});
        }
      }
      else
      {
        var duration = -1 * currentLeftOffset * options.durationFactor;
        if(currentLeftOffset < 0)
        {
          $(imageSetElement).stop().animate({left: 0}, {duration: duration, easing: 'linear'});
        }
      }
      
    };
    
    return this.each(function(index, item)
    {
      $(this).append('<a class="left_arrow"/>').append('<a class="right_arrow"/>');
      
      // calc the width for each element
      var imageSetWidth = 0;
      var padding = 10;
      $(this).find('ul li').each(function(index, liItem)
      {
        imageSetWidth += $(liItem).width() + padding;
      });
      imageSetWidth -= padding;
      
      var sliderElement = this;
      $(this).find('.left_arrow').mousedown(function()
      {
        slide('left', imageSetWidth, sliderElement);
      }).mouseup(function()
      {
        $('ul', sliderElement).stop();
      });
      
      $(this).find('.right_arrow').mousedown(function()
      {
        slide('right', imageSetWidth, sliderElement);
      }).mouseup(function()
      {
        $('ul', sliderElement).stop();
      });
    });
  };
  
})(jQuery);
