function AlbumStream()
       {
            this.backward_control = ".slide_backward";
            this.forward_control = ".slide_forward";
            this.image_stream = ".album_stream_buffer";
            this.album_token = "#photo_album_";
            
            this.base_path = "http://www.hurricanesbargrill.com/";
            this.ajax_path = "index.php/ajax/photogallery_image_loader/";
            this.streamUrl = this.base_path+this.ajax_path+"#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);
                });
            }
            
            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\" /> ";
                    });
                   // 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;
                    }
                    
                    //alert($(image_container).css("margin-left").replace("px", ""));
                    //alert(parseInt($(image_container).css("margin-left").replace("px", "")));
                    var containerMarginLeft = ( parseInt($(image_container).css("margin-left").replace("px", "")))-(albumStream.single_image_width*albumStream.num_photos);
                    
                    //ie workaround... returns auto for margin-left if no margin is set rather than 0. Thus a NaN value is generated.
                    if(isNaN(containerMarginLeft))
                    {
                         containerMarginLeft = 0-(albumStream.single_image_width*albumStream.num_photos);
                    }
                    
                    if(Math.abs(containerMarginLeft) == $(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: (containerMarginLeft)}, 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);
            }
       }
       
       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];
            }
       }
