jQuery(document).ready(function()
{

  jQuery.easing.def = "easeOutQuart";

  jQuery('#latestNews #up').html('&#9650;').attr('title',"Up");
  jQuery('#latestNews #down').html('&#9660;').attr('title',"Down");

  var newsTimer = 0;
  var newsDir = 'up';
  var newsDelay = 2000;
  var newsSpeed = 800;
  var newsTimer2 = 0;
  
  var panelTimer = 0;
  var panelDelay = 5000;
  
  function newsAni()
  {
    if(newsDir=='up')
    {
      return window.setInterval(function()
      {
        jQuery('#latestNews ul li:first-child').animate({'marginTop':'-30px'},newsSpeed,'linear',function()
        {
          jQuery(this).css('marginTop','0').appendTo(jQuery('#latestNews ul'));
        });
        window.clearInterval(newsTimer2);
      },newsDelay);
    }
    else if(newsDir=='down')
    {
      return window.setInterval(function()
      {
        jQuery('#latestNews ul li:last-child').css('marginTop','-30px').hide().prependTo(jQuery('#latestNews ul')).queue(function(next)
        {
          jQuery('#latestNews ul li:first-child').show().animate({'marginTop':'0px'},newsSpeed,'swing');
          next();
        });       
        window.clearInterval(newsTimer2);
      },newsDelay);
    }
    
  }
  
  newsTimer = newsAni();

  jQuery('#latestNews a.ctl').click(function(e)
  {
    e.preventDefault();
    $newDir = jQuery(this).attr('id');
    if($newDir != newsDir)
    {
      newsDir = $newDir;
      window.clearInterval(newsTimer);
      newsDelay=10;
      newsTimer2 = newsAni();
      newsDelay=2000;
      newsTimer = newsAni();
    }
  });
  
  var $fpImgs = jQuery('div#front-page-images ul#panels li');
  
  if($fpImgs.length)
  {
    $fpImgs.closest('ul').css('width',666*$fpImgs.length+'px');
  
    jQuery('div#front-page-images').append('<ul id="fpi-nav"></ul>');
    
    var $fpImgsNav = jQuery('ul#fpi-nav');
    
    $fpImgs.each(function(i)
    {
      var newNavItm = '<li id="nav' + i + '"><a href="#">Panel ' + (i+1) + '</a></li>';
      $fpImgsNav.append(newNavItm);
    });
    
    var fpImgsVisId = parseInt($fpImgs.filter(':visible').attr('id').replace('img',''));
    jQuery('ul#fpi-nav li#nav'+fpImgsVisId).addClass('active');
    
  }
  
  jQuery('ul#fpi-nav li a').live('click',function(e)
  {
    e.preventDefault();
    
    $fpNavLi = jQuery(this).closest('li');
    
    if(!$fpNavLi.hasClass('active'))
    {
      var fpImgId = parseInt($fpNavLi.attr('id').replace('nav',''));

      //jQuery('ul#panels li:first-child').css('marginLeft', (0 - fpImgId * 666) + 'px');
      jQuery('ul#panels').animate({'marginLeft': (0 - fpImgId * 666) + 'px'},'slow','swing');
      
      jQuery('ul#fpi-nav li').removeClass('active');
      $fpNavLi.addClass('active');
    }
  });
  
  function panelTimerGo(panelTimer)
  {
    panelTimer = window.setInterval(function()
    {
        var $linkActive = jQuery('ul#fpi-nav li.active');

        var $linkNext = $linkActive.next('li').find('a');

        if(!$linkNext.length)
        {
            $linkNext = jQuery('ul#fpi-nav li:first-child a');
        }
        $linkNext.click();
        
    },panelDelay);
  }
  
  panelTimerGo(panelTimer);
  
  jQuery('body.single-wpsc-product ul.images.thumbs li:first-child').addClass('active');
  
  jQuery('body.single-wpsc-product ul.thumbs li a').click(function(e)
  {
    e.preventDefault();
    
    $thumbLi = jQuery(this).closest('li');
    
    if(!$thumbLi.hasClass('active'))
    {
      var tImgId = parseInt($thumbLi.attr('id').replace('thumb',''));
    
      jQuery('body.single-wpsc-product ul.images.full li:first-child').animate({'marginLeft': (0 - tImgId * 432) + 'px'},'slow','swing');
      jQuery('body.single-wpsc-product ul.thumbs li').removeClass('active');
      $thumbLi.addClass('active');
    }
    window.clearInterval(panelTimer);
    panelTimerGo(panelTimer);
  });

  $twPanel = jQuery('div#twitter_panel');
  var panelHeight = $twPanel.height() - $twPanel.find('div.top').height() - $twPanel.find('div.bottom').height();
  $twPanel.find('div.panel').css('height',panelHeight+"px");

});
