//init cufon
Cufon.replace('h2')('h3')('h4')('h5')('h6')('small')('#ulMainNav li a', {hover: true})('#ulMainNavJQ li a', {hover: true});
VideoJS.setupAllWhenReady();
$(function() {
  //MAIN NAVIGATION
  $('#ulMainNav li a').animateHover({
    bgcolor: '#b0bc22',
    duration: 400
  });
  
  $('#ulSocialNav li a').animateHover({
    bgcolor: 'transparent',
    spanclass: 'hover',
    preFn: positionImage,
    duration: 400
  });  
  
  $('#pnlVerticalScroll li a').animateHover({
    bgcolor: '#b0bc22',
  preFn: animateNewsSetup,
    duration: 400
  });    
  $('#pnlHorizontalArchive li a').animateHover({
    bgcolor: '#b0bc22',
  preFn: animateNewsSetup,
     duration: 400
  });    
  //pnlHorizontalArchive
  //SIDEBAR NAVI
  $('#ulSidebar li a').animateHover({
    bgcolor: '#b0bc22',
    ignoreHeight: true,
    duration: 400
  });  
  
  $('#ulTweet li a').animateHover({
    bgcolor: '#b0bc22',
    ignoreHeight: true,
    duration: 400
  });    
    
  $('#ulArchive li a').animateHover({
    bgcolor: '#b0bc22',
    ignoreHeight: true,
    duration: 400
  });  
  
  if($('#Home').length > 0) {
    $('#ulNewsFeed li a').animateHover({
      bgcolor: '#b0bc22',
      spanclass: 'hover',
      preFn: animateNewsSetup,
      duration: 400
    });
    
    $('#ulBlogFeed li a').animateHover({
      bgcolor: '#b0bc22',
      spanclass: 'hover',
      ignoreClass: 'tweet',
      duration: 400
    });    
    
    $("#ulWork").slider1903({
      toggleEvery: 1,
      toggleNavClass: 'ulFtrPos',
      type: 'fade',
      speed: 900
    });  
    
    $("#ulNewsFeed").slider1903();
  }
  if($('#CaseStudies').length > 0) {
 
  }
if($('#CaseStudies').length > 0) {  
  $("#ulWork").slider1903({
    toggleEvery: 1,
    toggleNavClass: 'ulFtrPos',
    type: 'fade',
    speed: 900,
    auto: true,
    autoSpeed: 6000
  });    
}
  function animateNewsSetup(e, object) {
    $(object).children('.hover').append('<div class="plus" />').find('img').css('left','0');
  }
  
  function positionImage(e, object) {$(object).children('.hover').find('img').css('left','-39px');}
  
  /* vertical feature on work page */
    if($("#pnlVerticalScroll").length > 0) {
    $("#pnlCurrentWork").after(genNavigationText("navVert", "scrollUp", "up", "scrollDown", "down"));
    var posi = $("#pnlCurrentWork").position();
    $("#navVert").css("top", posi.top + $("#pnlCurrentWork").height() + (6)+"px");
    var liHeight = $("#pnlVerticalScroll li a").height() + 20;
    var totalRows = Math.ceil($("#pnlVerticalScroll li").length / 4);
    //alert(totalRows);
    var curRow = 0;
    
    if(curRow == 0) {$(".scrollUp").addClass("innactive");};
    
    $("#pnlVerticalScroll").height(liHeight * totalRows);
    $("#navVert li a").click(function(e){
      e.preventDefault();
      //var position = $("#pnlVerticalScroll").position();
      //alert(position.top);
      
      if($(this).attr('class') == "scrollUp" && curRow != 0) {
        $(".scrollDown").removeClass("innactive");
        var curpos = liHeight * curRow;
        var move = curpos - liHeight;
        $("#pnlVerticalScroll").stop(true, true).animate({top : -move+'px'}, 700, 'easeOutQuint')
        curRow--;
        if(curRow == 0) {$(this).addClass("innactive");};
      } else if($(this).attr('class') == "scrollUp" && curRow == 0){
        //$(this).addClass("innactive");
      }
      if($(this).attr('class') == "scrollDown" && curRow != totalRows - 2) {
        $(".scrollUp").removeClass("innactive");
        var curpos = liHeight * curRow;
        var move = curpos + liHeight;                
        $("#pnlVerticalScroll").stop(true, true).animate({top : -move+'px'}, 700, 'easeOutQuint')
        curRow++;
        if(curRow == totalRows - 2) {$(this).addClass("innactive");};
      } else if($(this).attr('class') == "scrollDown" && curRow == totalRows - 2) {
        //$(this).addClass("innactive");
      }
      //alert(curRow);
    });
    
    //horizontal
    $("#pnlArchivedWork").after(genNavigationText('navHori', 'scrollLeft', 'back', 'scrollRight', 'next'));
    var posiH = $("#pnlArchivedWork").position();
    $("#navHori").css("top", posiH.top + $("#pnlArchivedWork").height() + (23)+"px");
    
    var liWidth = $("#pnlHorizontalArchive li a").width() + 20;
    var totalLi = $("#pnlHorizontalArchive li").length;
    //var totalMoves = Math.ceil($("#pnlHorizontalArchive li").length);
    var curCol = 0;
    
    if(curCol == 0) {$(".scrollLeft").addClass("innactive");}
    
    $("ul#pnlHorizontalArchive").width((liWidth * totalLi));
    
    $("#navHori li a").click(function(e) {
      e.preventDefault();
      
      if($(this).attr('class') == "scrollLeft" && curCol != 0) {
        $(".scrollRight").removeClass("innactive");
        var curpos = liHeight * curCol;
        var move = curpos - liWidth;
        $("#pnlHorizontalArchive").stop(true, true).animate({left : -move+'px'}, 700, 'easeOutQuint')
        curCol--;
        if(curCol == 0) {$(this).addClass("innactive");};        
      }
      if($(this).attr('class') == "scrollRight" && curCol != totalLi - 3) {
        $(".scrollLeft").removeClass("innactive");
        var curpos = liHeight * curCol;
        var move = curpos + liWidth;
        $("#pnlHorizontalArchive").stop(true, true).animate({left : -move+'px'}, 700, 'easeOutQuint')
        curCol++;
        if(curCol == totalLi - 3) {$(this).addClass("innactive");};        
      }      
    });
  }
  
    function genNavigationText(id, bback, tback, bnext, tnext) {
    return "<ul id='"+id+"'><li><a href='#' class='"+bback+"'>"+tback+"</a></li><li><a href='#' class='"+bnext+"'>"+tnext+"</a></li></ul>";
  }
  
});
