var prevSlideTitle;
var tooltipTimer;
var currTagList;
var currScroll = 0;
var maxScrollWidth;
$(document).ready(function(){
    $('.insiderSlide').hide();
    maxScrollWidth = $('.tagListTd:visible').innerWidth();

    /*
    $('#tooltipLeft').css('background-image','url(/images/shell_v2/insiders_cycle/tooltip_left_'+$('.insiderSlidePhoto:eq('+($('.insiderSlidePhoto').length - 1)+')').attr('id')+'.png)');
    $('#tooltipRight').css('background-image','url(/images/shell_v2/insiders_cycle/tooltip_right_'+$('.insiderSlidePhoto:eq(1)').attr('id')+'.png)');
    */
    $('#insiderSlider *,#expanders,#insiderSelector,#tagSelector').bind('mouseover',function(){ $('#insiderPhotos').cycle('pause'); })
    $('#insiderSlider *,#expanders,#insiderSelector,#tagSelector').bind('mouseout',function(){ $('#insiderPhotos').cycle('resume'); })
     
    $('#tagType1,#tagType2').bind('mousedown',switchTagType);
    $('#tagType1,#tagType2').bind('mouseover',function(){
      if(!$(this).hasClass('activeTagType')) {
        $(this).children('img').each(function(){
          this.src = this.src.replace('tagtype_inactive_','tagtype_hover_');
        });
        $(this).toggleClass('hoverTagType');
      }
    });
    $('#tagType1,#tagType2').bind('mouseout',function(){
      if($(this).hasClass('hoverTagType')) {
        $(this).children('img').each(function(){
          this.src = this.src.replace('tagtype_hover_','tagtype_inactive_');
        });
        $(this).toggleClass('hoverTagType');
      }
    });

    $('.insiderSlideTagLeft').mousedown(function(){scrollLeft(this);});
    $('.insiderSlideTagLeft').mouseup(function(){stopScrolling();});
    $('.insiderSlideTagLeft').mouseout(function(){stopScrolling();});
    
    $('.insiderSlideTagRight').mousedown(function(){scrollRight(this);});
    $('.insiderSlideTagRight').mouseup(function(){stopScrolling();});
    $('.insiderSlideTagRight').mouseout(function(){stopScrolling();});
    /*
    $('#insiderSlideNavLeft').bind('mouseover',function(){
      clearTimeout(tooltipTimer);
      $('#tooltipLeft').fadeIn();
    });
    
    $('#insiderSlideNavLeft').bind('mouseout',function(){
      tooltipTimer = setTimeout("$('#tooltipLeft').fadeOut()",500);
    });
    
    $('#insiderSlideNavRight').bind('mouseover',function(){
      clearTimeout(tooltipTimer);
      $('#tooltipRight').fadeIn();
    });
    
    $('#insiderSlideNavRight').bind('mouseout',function(){
      tooltipTimer = setTimeout("$('#tooltipRight').fadeOut()",500);
    });
     */
  });

  function switchTagType()
  {
    $('#insiderSlide .activeTagType img').each(function(){
      this.src = this.src.replace(/tagtype_(left|right)/i,"tagtype_inactive_$1");
    });
    $('#insiderSlide .tagType').removeClass('hoverTagType');
    $('#insiderSlide .tagType').toggleClass('activeTagType');
    $('#insiderSlide .activeTagType img').each(function(){
    this.src = this.src.replace(/(_hover_|_inactive_)/i,'_');
    });
    $('#tagList1,#tagList2').toggleClass('activeTagList');
    currScroll=0;
    currTagList = $('.tagListTd:visible span a');
  }

  var started = 0;
  function onBeforeSlide(curr, next, opts, fwd)
  {
    delay = 1200;
    prevIndex = opts.nextSlide - 1;
    nextIndex = opts.nextSlide + 1;
    $('#photoOverlay').hide();
    $('#photoOverlay').html("");

    //$('#tooltipLeft').css('background-image','url(/images/shell_v2/insiders_cycle/tooltip_left_'+$('.insiderSlidePhoto:eq('+prevIndex+')').attr('id')+'.png)');
    //$('#tooltipRight').css('background-image','url(/images/shell_v2/insiders_cycle/tooltip_right_'+$('.insiderSlidePhoto:eq('+nextIndex+')').attr('id')+'.png)');
    $('#tagList2 .tagListBottom').scrollTo('a:eq(0)',300,'easeout');
    $('#tagList1 .tagListBottom').scrollTo('a:eq(0)',300,'easeout');
    $('.activeTagList .insiderSlideTagLeft').css('background-position','0px -694px');
    $('.activeTagList .insiderSlideTagRight').css('background-position','0px -658px');
    if(started > 0)
    {
      $('#tagTypeSelect div,.tagListBottom').fadeOut();
    }
    else
    {
      started = 1;
    }
    
    var newSlide = $('.insiderSlide[title='+next.id+']');
    if(newSlide.children('.photoOverlay').length == 0)
    {
      $('#insiderSlide').hide();
      $('#insiderCycle').css('overflow','visible');
    }
    else
    {
      $('#insiderSlide').show();
      $('#insiderCycle').css('overflow','hidden');
    }
  }

  function onAfterSlide(curr, next, opts, fwd)
  {
    var newSlide = $('.insiderSlide[title='+next.id+']');
    clearTimeout(keepScrolling);
    if(newSlide.children('.photoOverlay').length > 0)
    {
      if(newSlide.children('.photoOverlay').html().length > 0)
        $('#photoOverlay').append(newSlide.children('.photoOverlay').clone());
      else
        $('#photoOverlay').html('');
      $('#allTagsLink').attr('href',newSlide.children('.allTagsLink').attr('href'));
      $('#tagType1 .tagTypeBottom').html(newSlide.children('.tagType1').html());
      $('#tagType2 .tagTypeBottom').html(newSlide.children('.tagType2').html());
      $('#tagList1 .tagListBottom').html(newSlide.children('.tagList1').html());
      $('#tagList2 .tagListBottom').html(newSlide.children('.tagList2').html());
      currScroll=0;
      if($('#photoOverlay').html().length > 0)
        $('#photoOverlay,#tagTypeSelect div,.tagListBottom').fadeIn(1250);
      else
        $('#tagTypeSelect div,.tagListBottom').fadeIn(1250);
      currTagList = $('.tagListTd:visible span a');
    }
    else
    {
      $('#photoOverlay').html('');
      $('#tagType1 .tagTypeBottom').html('');
      $('#tagType2 .tagTypeBottom').html('');
      $('#tagList1 .tagListBottom').html('');
      $('#tagList2 .tagListBottom').html('');
    }
    //alert(currTagList[0].width);
  }
  
  var keepScrolling,scrollImg;
  var delay = 500;

  function stopScrolling() {
    clearTimeout(keepScrolling);
    scrollImg = '';
    delay = 500;
  }

  function scrollLeft(img)
  {
    clearTimeout(keepScrolling);
    scrollImg = img;
    keepScrolling = setTimeout("scrollLeft(scrollImg)",500);
    var reachedVisibleWidth = 0;
    for(var i = currScroll;i >= 0;i--)
    {
      if(reachedVisibleWidth - $(currTagList[i]).outerWidth() < -1 * (maxScrollWidth + 14))
      {
        break;
      }
      else
      {
        currScroll = i;
        reachedVisibleWidth -= $(currTagList[i]).outerWidth();
      }
    }
    if(currScroll <= 0) {
      currScroll = 0;
      $('.activeTagList .insiderSlideTagLeft').css('background-position','0px -694px');
      $('.activeTagList .insiderSlideTagRight').css('background-position','0px -658px');
    }
    else {
      $('.activeTagList .insiderSlideTagLeft').css('background-position','0px -622px');
      $('.activeTagList .insiderSlideTagRight').css('background-position','0px -658px');
    }
    $('.tagListTd:visible span').scrollTo('a:eq('+(currScroll)+')',300,'easeout');
  }

  function scrollRight(img)
  {
    clearTimeout(keepScrolling);
    scrollImg = img;
    keepScrolling = setTimeout("scrollRight(scrollImg)",500);
    var reachedVisibleWidth = 0;
    for(var i = currScroll;i < currTagList.length;i++)
    {
      if($(currTagList[i]).outerWidth() + reachedVisibleWidth > (maxScrollWidth + 14))
      {
        break;
      }
      else
      {
        reachedVisibleWidth += $(currTagList[i]).outerWidth();
        currScroll = i;
      }
    }
    
    if(currScroll >= $('.activeTagList .tagListTd span a').length-5) {
      currScroll = $('.activeTagList .tagListTd span a').length - 1;
      $('.activeTagList .insiderSlideTagRight').css('background-position','0px -730px');
      $('.activeTagList .insiderSlideTagLeft').css('background-position','0px -622px');
    }
    else {
      $('.activeTagList .insiderSlideTagRight').css('background-position','0px -658px');
      $('.activeTagList .insiderSlideTagLeft').css('background-position','0px -622px');
    }
    $('.tagListTd:visible span').scrollTo('a:eq('+(currScroll)+')',300,'easeout');
  }

