$(document).ready(function(){
  // Movie carousel
  (function(){
    var number_of_items = 0,
        visible_items = 4;
    
    function horizontal_carousel(item_width) {
      var current_index = 0;

      // Tween carousel
      function scroll_carousel(index) {
        var scroll_amount = item_width * index;
        $("#movies-carousel").animate({scrollLeft: scroll_amount}, 200);
      }
      
      // Next Button
      $("#movies-carousel + .prev + .next").click(function(){
        current_index++;
        
        if(current_index > number_of_items-visible_items) {
          current_index = 0;
        }
        
        scroll_carousel(current_index);
      });
      
      // Previous Button
      $("#movies-carousel + .prev").click(function(){
        current_index--;
        if(current_index < 0) {
          current_index = number_of_items - visible_items
        } 
        scroll_carousel(current_index);
      });
    }
    
    // intializer
    (function(){
      // turn the first day on the day navigation back active
      $("#carousel-navigation li:first").addClass("active");

      // count the number of items for the first day
      number_of_items = $(".movies-set:first li.movie").size();
      
      // initialize carousel for the first day
      horizontal_carousel(211);
    })();
    
    // Vertical scroller
    (function(){
      var item_height = $("#movies-carousel li.carousel-block").height();
      
      $("#carousel-navigation li.not-empty").click(function(){
        // calculates what day was clicked on the navigation bar
        var current_index = $("#carousel-navigation li").index(this),
            scroll_amount = current_index * item_height;
            
        // resets the carousel and scrolls it down to the proper day
        $("#movies-carousel")
          .stop()
          .animate({scrollLeft: 0}, 200)
          .animate({scrollTop: scroll_amount}, 500);
          
        number_of_items = $("li.movie", $(".movies-set")[current_index]).size();
        
        // highlights the proper day
        $("#carousel-navigation li").removeClass("active");
        $(this).addClass("active");
      });
    })();

  })();
  
  (function(){
    function prepare_modal_window(current_movie_link, date_offset){

    $.get(current_movie_link, {date_offset: date_offset}, function(data){   
        // append data and close x
        $("#movie-info-box").html("<div class=\"close\"></div>" + data);

        // turn off loading spinner
        $("#movie-info-box").css("background-image", "none");

        // save states not currently in view
        var quick_facts_items = $("#movie-info-box .quick-facts li").remove(),
            plot_schedule = $("#movie-info-box .plot").remove();

        // append initial state
        $("#movie-info-box .quick-facts").append(plot_schedule);

        // save initial states
        var info_view = $("#movie-info-box").html();


        // bind to quick fact navigation
        function quick_facts_links() {
          var index = $("p.quick-facts-navigation span").index(this);
          $("#movie-info-box .quick-facts").html(quick_facts_items[index]);
          
          if($(this).html() == "Movie Feedback") {
            $("#new_feedback").jaxy();
            $(".email-fields, .form-buttons").hide();
            $("#feedback_body").css("height", "20px");
          }

          if($(this).html() == "Tweets" && $("body.now-playing").size != 0) {
            var query = escape("watched movie " + $("h2", $(this).parent().parent()).html()),
                twitter_url = "http://search.twitter.com/search.json?&q=" + query + "&rpp=20&callback=?";

            if($("ul.twitter-data").html() === null) {
              $.getJSON(twitter_url, function(data){
                $("ul.quick-facts li.tweets").css("background-image", "none");
                $("ul.quick-facts li.tweets").append("<ul class=\"twitter-data\"></ul>");
                $.each(data.results, function(){
                  var data_string = "<li>";

                  data_string += "<img src=\"" + this.profile_image_url + "\" class=\"profile-pic\" />";
                  data_string += "<div class=\"text\">" + this.text + "</div>";
                  data_string += "</li>";

                  $(".twitter-data").append(data_string);
                });
              }); 
            }
          }
        }

        $("p.quick-facts-navigation span").bind("click", quick_facts_links);


        // trailer animations
        function trailer_showtime_navigation() {
          $("#movie-info-box .quick-facts").html(plot_schedule);
          return false;
        }

        $(".showtime-navigation a.plot-showtime").bind("click", trailer_showtime_navigation);

      });
    }

    // Modal window 
    $("ul.movies-set li.movie a").click(function(){
      $("#container").after("<div id=\"movie-info-box\"></div>");

      $("#movie-info-box").css("left", $(this).position().left)
                          .css("top", 300)
                          .css("width", 151)
                          .css("height", 276);


      var current_movie_link = $(this).attr("href");
          center_offset = ($(document).width()-$("#movie-info-box").width())/2 - 300,
          this_box_width = $(this).width();

      var offset = $("ul.day li.carousel-block").index($(this).parent().parent().parent());    

      $("#movie-info-box").animate({
        left: center_offset, 
        top: 250, 
        width: 717,
        height: 503 }, 300, function(){
          prepare_modal_window(current_movie_link, offset);
      });

      return false;
    });


    // closes movie info box when x is clicked
    $("#movie-info-box .close").live("click", function(){
      $("#movie-info-box").remove();
    });
  })();

  (function(){
    $(".complete-list-button").click(function(){
      $.get("/movies", {filter: "list_on_specific_date", date: $("#carousel-navigation li.active .day").html()}, function(data){
        $("#container").append("<div id=\"complete-list-modal\"></div>");
        $("#complete-list-modal").append(data);
        $("#complete-list").accordion({header: ".information-summary", collapsible: true, active: false});
        $("#complete-list-modal").css("top", $(window).scrollTop()+50)
                                 .css("height", $(window).height()-100);
                                 
        $("#complete-list-modal").prepend("<img src=\"/images/movie_info_close.png\" class=\"close-complete-list\"/>");
      }, "script");
      
      return false;
    });
    
    $(".close-complete-list").live("click", function(){
      $("#complete-list-modal").fadeTo(500,0);
      $("#complete-list-modal").remove();
    });
  })();

  var poster_width = $("#movies-carousel .poster").width(),
      poster_height = $("#movies-carousel .poster").height();
      
  $("#movies-carousel .poster").hover(
    function(){
      $(this).stop().animate({width: poster_width*1.1, height: poster_height*1.1, marginLeft: "-10px", marginTop: "-18px"}, 200);
    },
    function(){
      $(this).stop().animate({width: poster_width, height: poster_height, marginLeft: 0, marginTop: 0}, 200);
    }
  );
  
});