var WdScrollGallery = function(img_obj,is_hotel){
    this.icons_subdir = "square60";
    this.images_subdir = "org";
    this.WdSG_container;
    this.WdSG_scrollable;
    this.elnts_show = 7;
    this.container_top;
    
    this.initialize = function (){    
        this.showOverlay();   
        this.createBlocks();
        jQ(".WdSG_scrollable").scrollable();   
        this.bindImgClick();      
        jQ(window).bind("scroll resize", function () {    
            galPosition();
        });    
        galPosition();    
    };  
    
    this.showOverlay = function(){
        var WdSG_overlay = jQ('<div id="WdSG_overlay"></div>');
        jQ("body").append(WdSG_overlay);        
        jQ("#WdSG_overlay").css({             
             height: jQ(document).height(),
             opacity: 0
        })
        .show()        
        .animate({opacity:0.8})
        .click(function(){removeBlocks();});       
    };
    
    this.createBlocks = function (){
        this.WdSG_container = jQ('<div id="WdSG_container"></div>');        
        this.WdSG_container.append('<a class="prev browse left"></a>');          
        this.WdSG_scrollable = jQ('<div class="items"></div>');              
        
        var c=0;
        var col = jQ('<div></div>');
       for(var i=0; i<img_obj.length;i++){            
            if(c == this.elnts_show){
              this.WdSG_scrollable.append(col);  
              col = jQ('<div></div>');
              c = 0;  
            }
            
            var image_src = img_obj[i]['url'];
            if(is_hotel == 1){
              //image_src = image_src.replace(/\.(jpe?g|png)$/ig,".gif");  
            }
            
           col.append('<img src="'+image_src.replace("\/"+this.images_subdir+"\/", "/"+this.icons_subdir+"/")+'" id="wd_img'+i+'" title="'+img_obj[i]['caption']+'">');
           c++;           
        } 
        this.WdSG_scrollable.append(col);
        this.WdSG_container.append(jQ('<div class="WdSG_scrollable"></div>').append(this.WdSG_scrollable));
        
        this.WdSG_container.append('<a class="next browse right"></a>');
        this.WdSG_container.append('<div id="WdSG_image_wrap"><div id="WdSG_image_caption"></div><div id="WdSG_closebut"></div><img src="/js/wdslidegallery/blank.gif" width="500" height="375"></div>');
        jQ("body").append(this.WdSG_container);  
        this.galPosition();     
        this.container_top = parseInt(jQ("#WdSG_container").css("top"));
    };
    
    this.removeBlocks = function (){
        jQ("#WdSG_container,#WdSG_overlay").animate({opacity:0},function(){jQ(this).remove();});
        jQ(window).unbind("scroll resize"); 
    };
    
    this.galPosition = function (){   
        var container_height = 559;//jQ("#WdSG_container").height();
        var container_width = 762;//jQ("#WdSG_container").width();
        var gal_top = Math.round(jQ(window).scrollTop());    
        this.container_top = Math.round(jQ(window).height()/2 - container_height/2);
        var container_width = Math.round(jQ(window).width()/2 - container_width/2);  
        jQ("#WdSG_container").css({top:gal_top+this.container_top+'px',left:container_width},'fast');
    };
    
    this.bindImgClick = function (){
        jQ(".items img").click(function() {            
            
            if (jQ(this).hasClass("active")) { return; }  
            
            var url_id = parseInt(jQ(this).attr("id").replace("wd_img", "")); 
            var url = img_obj[url_id]['url'];
            if(is_hotel == 1){
              
            }
            var caption =  jQ(this).attr("title");   
            var wrap = jQ("#WdSG_image_wrap").fadeTo("medium", 0.5);

            var img = new Image();
            img.onload = function() {
                wrap.fadeTo("fast", 1);
                wrap.find("img").attr("src", url)
                .width(this.width).height(this.height);
                wrap.find("#WdSG_image_caption").html(caption);
                galPosition();
            };
            
            img.src = url;
            jQ(".items img").removeClass("active");
            jQ(this).addClass("active");
        }).filter(":first").click();
        
        jQ("#WdSG_closebut").click(function() {
           removeBlocks();
        })
    };     
    
    this.initialize(); 
    
    return false;   
};
