;(function($) {
    $(document).ready(function() {
          // conference article rotater
          $(".rotating-container").each(function() {
             var rotating_timeout = null;
             var poll_timeout = null;
             var the_container = this;
             var current_contents = $(the_container).html();
             var new_contents = current_contents;
             var url_root = 'http://' + location.host;

             function display_next_article(lines, next_to_display, is_dynamic) {
                 lines.hide();
                 if (is_dynamic && (next_to_display == 0)) {
                     if (current_contents != new_contents) {
                         the_container = $(the_container).empty().prepend(new_contents);
                         lines = $("span.rotating", the_container);
                         current_contents = new_contents;
                         display_next_article(lines, next_to_display, true);
                     }
                 }
                 $(lines.get(next_to_display)).fadeIn("slow");
                 if (next_to_display >= (lines.length-1)) {
                     next_to_display = 0;
                 } else {
                     next_to_display += 1;
                 }
                 if (rotating_timeout) clearTimeout(rotating_timeout);
                 rotating_timeout = setTimeout(function() {
                     display_next_article(lines, next_to_display, is_dynamic);
                 }, 5000);
             }
             display_next_article($("span.rotating", the_container), 0, true);

             function poll_for_updated_content() {
                 $.ajax({
                     type: "GET",
                     url: url_root,
                     data: "ere_live_template=presentation-info",
                     dataType: "json",
                     cache: false,
                     success: function(data) {
                         new_contents = data.html;
                     },
                     complete: function() {
                         if (poll_timeout) clearTimeout(poll_timeout);
                         poll_timeout = setTimeout(function() {
                             poll_for_updated_content();
                         }, 30000);
                     }
                 });
             }
             poll_for_updated_content();
          });


          // conference expander stuff
          $(".expand-controller.expand").click(function() {
              $("#conference-live-elements").show();
              $(this).hide();
              $(this).siblings(".retract").show();
              return false;
          });

          $(".expand-controller.retract").click(function() {
              $("#conference-live-elements").hide();
              $(this).hide();
              $(this).siblings(".expand").show();
              return false;
          });
        
        // chars left
        $("form.new-attendee textarea#tweet").each(function() {
            var the_textarea = this;
            function update_chars_left() {
                var max_len = 140;
                var tweet_len = the_textarea.value.length;
                if (tweet_len >= max_len) {
                    the_textarea.value = the_textarea.value.substring(0, max_len); // truncate
                    $('#chars-left').html("0");
                } else {
                    $('#chars-left').html(max_len - tweet_len);
                }
            }
            $(the_textarea).keyup(function() {
                update_chars_left();
            });
            update_chars_left();
        });
        
        // notification closer
        $("div.notifier a.close-box").click(function() { 
            $(this).blur();
            $(this).parent("div.notifier").fadeOut(500);
            return false; 
        });
        
        // carousel
        $("#sessions-carousel").each(function() {
            var $carousel = $(this);
            var $sessions = $carousel.find(".sessions-list .session");
            var num_sessions = $sessions.length;
            var selected_id;
            
            $sessions.each(function(i) {
                if ($(this).hasClass("selected")) {
                    selected_id = i;
                }
            });
            
            var $selected_session = $carousel.find(".sessions-list .session.selected");
            var $next_control = $carousel.find("#session-next");
            var $previous_control = $carousel.find("#session-previous");
            
            $carousel
                .bind("init.carousel", {}, listen_for_clicks)
                .bind("next.carousel", {}, show_next)
                .bind("previous.carousel", {}, show_previous)
            ;
            $carousel.trigger("init.carousel");
            
            function listen_for_clicks(e) {
                $next_control.click(function() {
                    $(this).blur();
                    $carousel.trigger("next.carousel");
                    return false;
                });
                $previous_control.click(function() {
                    $(this).blur();
                    $carousel.trigger("previous.carousel");
                    return false;
                });
            }
            
            function show_next() {
                var $current = $sessions.eq(selected_id);
                if (selected_id >= (num_sessions - 1)) {
                    selected_id = 0;
                    var $next = $sessions.eq(selected_id);
                } else {
                    selected_id++;
                    var $next = $sessions.eq(selected_id);
                }
                $current.hide("slide", { direction: "left" }, 500);
                $next.show("slide", { direction: "right" }, 500);
            }
            
            function show_previous() {
                var $current = $sessions.eq(selected_id);
                if (selected_id == 0) {
                    selected_id = num_sessions-1;
                    var $previous = $sessions.eq(selected_id);
                } else {
                    selected_id--;
                    var $previous = $sessions.eq(selected_id);
                }
                $current.hide("slide", { direction: "right" }, 500);
                $previous.show("slide", { direction: "left" }, 500);
            }
        });
        
        // carousel revealer
        $(".revealer").click(function() {
            $(this).blur();
            var $revealee = $($(this).attr("href"));
            if ($revealee.is(":hidden")) {
                $(this).find(".ui-icon").removeClass("ui-icon-triangle-1-e").addClass("ui-icon-triangle-1-s");
                $revealee.show("blind", { direction: "vertical" }, 250);
            } else {
                $(this).find(".ui-icon").removeClass("ui-icon-triangle-1-s").addClass("ui-icon-triangle-1-e");
                $revealee.hide("blind", { direction: "vertical" }, 250);
            }
            return false;
        });
    });
})(jQuery);