$(document).ready(function(){
  bind_panel_events();

  var first_page = $('.top-navigation li:first a').attr('title');
  var last_page = $('.top-navigation li').eq(-2).find('a').attr('title');
  var current_page = $('.top-navigation li a.active').attr('title');
  var sliding_divs = new Object();
  sliding_divs[current_page] = $('#slider .sliding-content');
  
  enable_buttons();
  
  function get_prev_page(){
    if(current_page == first_page){
      return last_page;
    }else{
      var prev_page = '';
      $('.top-navigation li a').each(function(){
        if($(this).attr('title') == current_page){
          prev_page = $(this).parent('li').prev('li').find('a').attr('title');
          return;
        }
      });
      return prev_page;
    }
  }
  
  function get_next_page(){
    if(current_page == last_page){
      return first_page;
    }else{
      var next_page = '';
      $('.top-navigation li a').each(function(){
        if($(this).attr('title') == current_page){
          next_page = $(this).parent('li').next('li').find('a').attr('title');
          return;
        }
      });
      return next_page;
    }
  }
  
  function load_page(url, direction){
    if(sliding_divs[url] == undefined){
      $.get(url, function(data){
        sliding_divs[url] = $(data).find('.sliding-content');
        loop(url, direction, true);
        
        bind_panel_events();        
      },'html');
    }else{
      loop(url, direction, false);
    }
  }
  
  function loop(url, direction, isnew){
    set_navigation(url);
    $current_content = sliding_divs[current_page];
    var $next_content = sliding_divs[url];
    if(direction == 'right'){
      if($next_content.is('#slider .sliding-content:first-child') && !isnew){
        $('#slider').css('left', '+=956');
      }
      if($current_content.is('#slider .sliding-content:last-child')){
        $next_content.appendTo('#slider');
      }
      
      $('#slider').animate({left: '-=956'}, 1000, function(){
        enable_buttons();
      });
    }
    if(direction == 'left'){
      if($current_content.is('#slider .sliding-content:first-child')){
        $next_content.prependTo('#slider');
        $('#slider').css('left', '-=956');        
      }
      $('#slider').animate({left: '+=956'}, 1000, function(){
        enable_buttons();
      });
    }
    current_page = url;
  }
  
  function set_navigation(url){
    $('.top-navigation li a.active').removeClass('active');
    $('.top-navigation li a').each(function(){
      if($(this).attr('title') == url){
        $(this).addClass('active');
        return;
      }
    });
  }
  
  function disable_buttons(){
    $('div.pan-left').unbind('click');
    $('div.pan-right').unbind('click');
  }
  
  function enable_buttons(){
    $('div.pan-left').click(function(){
      disable_buttons();
      load_page(get_prev_page(), 'left');
    });
    
    $('div.pan-right').click(function(){
      disable_buttons();
      load_page(get_next_page(), 'right');
    });
  }
});

function bind_panel_events(data){
  $(".panel-color").unbind('click').click(function () {
    var $panel = $(this).parent(".promo-panel");
    var $flashurlspan = $(this).find('.flashurl');
    var flashurl = $flashurlspan.text();
    if(flashurl != ''){
      $(this).find('.myFlash').flash({
        swf: flashurl,
        width: 728,
        height: 90,
        play: false
      });
      $flashurlspan.text('');
    }
    $panel.animate({height:"304px"});
    $panel.find(".close-promo-panel").fadeIn(500);   
    $panel.find(".promo-cta").fadeOut(500);   
    $(".promo-panel").removeClass("nudge");     
  });
  
  $(".close-promo-panel").unbind('click').click(function () {
    var $panel = $(this).parent(".promo-panel");
    $panel.animate({height:"77px"});
    $panel.find(".close-promo-panel").fadeOut(500);   
    $panel.find(".promo-cta").fadeIn(500);   
    $(".promo-panel").addClass("nudge");  
  });
}
