function AlbumStream()
     {
          this.backward_control = ".slide_backward";
          this.forward_control = ".slide_forward";
          this.image_stream = ".album_stream_buffer";
          this.album_token = "#photo_album_";
          
          this.streamUrl = "http://underconstruction.us.com/hurricanes.com/index.php/ajax/photogallery_image_loader/#album_id#/#num_photos#";
          
              //determines how many albums to grab at a time
          this.num_photos = 3;                                                
              //used to determine the width of the stream container when new images come in
          this.single_image_width = 80;
              //keeps track of each photoalbums total loaded photocount
          this.current_photo_count = new Array();
          
          this.initial_width = $(this.image_stream).width();
          this.current_id = null;
          
              //animation intervals
          this.forward_animation_time = 2000;
          this.backward_animation_time = 2000;
          this.forward_animation_timeout = 400;
          this.backward_animation_timeout = 400;
          
          
          var albumStream = this;
          var toolbox = new JSToolbox();                                      
          
          this.setup = function()
          {   
              this.setupControls();
          }
          
          this.setupControls = function()
          {
              toolbox.setMouseCursor($(albumStream.backward_control));
              toolbox.setMouseCursor($(albumStream.forward_control));
              
              $(albumStream.forward_control).click(function(){
                  
                  id = toolbox.extractId($(this).attr("id"));
                  albumStream.current_id = id;
                  url = albumStream.constructUrl(id, albumStream.num_photos);
                  stream = albumStream.constructImageStream(id);
                  albumStream.slideForward(stream);
              });
              
              $(albumStream.backward_control).click(function(){
                  id = toolbox.extractId($(this).attr("id"));
                  stream = albumStream.constructImageStream(id);
                  albumStream.slideBackward(stream);
              });
              
              $(".in_this_album_image").click(function(e){
                    albumStream.displayPhoto("#image_popup", $(this).attr("id"), e);
               }).live("click", function(e){
                    albumStream.displayPhoto("#image_popup", $(this).attr("id"), e);
               });
              
              
              
              $("#image_popup").click(function(){
                    albumStream.hidePhoto("#"+$(this).attr("id"));
              });
              
              	$(document).keyup(function(e) {
                    if (e.keyCode == 27) { albumStream.hidePhoto("#image_popup"); }   // esc
               });
          }
          
          this.displayPhoto = function(element, image_src, mouse)
          {
               var loader = $("#image_popup_photo_loader");
               var image_popup = $("#image_popup_photo");
               image_popup.attr('src', image_src).css("max-width", $(window).width()).css("max-height", $(window).height()).css("display", "none");
                         
               $(element).fadeIn("fast");
               $(element).css("top", 0).css("left", 0).css("height", $(document).height()).css("width", $(window).width());
               
               $(loader).css("position", "relative")
                         .css("left", 0)
                         .css("left", $(document).width()/2 - loader.width()/2)
                         .css("top", 0)
                         .css("top", $(document).scrollTop()+ $(window).height()/2 - $(loader).height()/2);
                         
               $(loader).fadeIn("fast");
               
               image_popup.load(function(){
                    setTimeout(function(){
                         loader.fadeOut("fast");
                         image_popup.fadeIn("fast");
                         
                         image_popup.css("left", 0)
                                   .css("left", $(document).width()/2 - image_popup.width()/2)
                                   .css("top", 0)
                                   .css("top", $(document).scrollTop()+ $(window).height()/2 - $(image_popup).height()/2);

                    
                    }, 1000);
                    
               });
          }
          
          this.hidePhoto = function(element)
          {
               $("#image_popup_photo").attr("src", "");
               $("#image_popup_photo_loader").fadeOut("fast");
               $(element).fadeOut("fast");
          }
          
          
          this.addPhotoCount = function(id, count)
          {
              if(this.current_photo_count[id] == null)
              {
                  this.current_photo_count[id] = count;
              }
              else
              {
                  this.current_photo_count[id] += count;
              }
          }
          
          this.getPhotoCount = function(id)
          {
              if(this.current_photo_count[id] != null)
              {
                  return this.current_photo_count[id];
              }
              
              return 0;
          }
          
          this.constructUrl = function(album_id, num_photos)
          {
              var string = this.streamUrl.replace("#album_id#", album_id).replace("#num_photos#", num_photos);
              return string;
          }
          
          this.constructImageStream = function(id)
          {
              return this.album_token+id+" "+this.image_stream;
          }
          
          this.test = function(num_photos, streamUrl, image_container)
          {
              setInterval(function(){
                  albumStream.getPhotos(num_photos, streamUrl, image_container)}
              , 1200);
          }
          
          this.getPhotos = function(num_photos, streamUrl, image_container)
          {         
              $.getJSON(streamUrl, function(data){
                 
                 albumStream.addPhotoCount(albumStream.current_id, data.photos.length);
                 
                 images = "";
                 $.each(data.photos, function(index){
                      images+= "<img src = \""+data.photos[index]['thumbimage']+"\" class = \"in_this_album_image\" id = \""+data.photos[index]['largeimage']+"\" /> ";
                  });
                 // alert(images);
                 $(image_container).append(images).css("width", albumStream.initial_width+albumStream.single_image_width*albumStream.getPhotoCount(albumStream.current_id) );
              });
          }
          
          this.slideForward = function(image_container)
          {
                  if($(image_container).is(":animated"))
                  {
                      return;
                  }
                  marginleft = ( parseInt($(image_container).css("margin-left").replace("px", "")))-(albumStream.single_image_width*albumStream.num_photos);
                  
                  if(Math.abs(marginleft) == $(image_container).width())
                  {
                      //alert("hey oh");
                      url = albumStream.constructUrl(albumStream.current_id, albumStream.num_photos);
                      albumStream.getPhotos(albumStream.num_photos, url, image_container);
                      
                      setTimeout(function(){
                          $(image_container).animate({ marginLeft: (-1*albumStream.single_image_width*albumStream.getPhotoCount(albumStream.current_id)) }, albumStream.forward_animation_time);
                      }, albumStream.forward_animation_timeout);
                  }
                  else
                  {
                      setTimeout(function(){
                          $(image_container).animate({ marginLeft: (marginleft)}, albumStream.backward_animation_time);
                      }, albumStream.forward_animation_timeout);
                  }
          }
          
          this.slideBackward = function(image_container)
          {
              if($(image_container).is(":animated"))
              {
                  return;
              }
              
              setTimeout(function(){
                  marginleft = ( parseInt($(image_container).css("margin-left").replace("px", "")))+(albumStream.single_image_width*albumStream.num_photos);
                  
                  if(marginleft <= 0)
                      $(image_container).animate({ marginLeft: marginleft}, albumStream.backward_animation_time);
              }, albumStream.backward_animation_timeout);
          }
          
          this.showPhoto = function()
          {
             
          }
     }
     
     function JSToolbox()
     {
          this.setMouseCursor = function(item)
          {
              $(item).mouseover(function(){
                  $(this).css("cursor", "pointer"); 
              }).mouseout(function(){
                  $(this).css("cursor", "default");
              });
          }
          
          //given a string, assumes the id is the last token of that string
          // (default) delimted by '_'.
          this.extractId = function(rawId, delimiter)
          {
              if(delimiter == null)
                  delimiter = "_";
              var arr = rawId.split(delimiter);
              return arr[arr.length-1];
          }
     }
