function makeScrollable(wrapper, scrollable, orientation){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);
	// Hide images until they are not loaded
	//scrollable.hide();
	
	var loading = $('<div class="loading">Loading...</div>').appendTo(wrapper);
	var compl_counter = 1;
	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('img');
		var completed = 0;
		
		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;	
			else {
			    //** consider the image as loaded if 10 attempts made
			    if (compl_counter>10)  completed++;
			}
		});
		
		if (completed == images.length){
			clearInterval(interval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				
				loading.hide();
			
				// Remove scrollbars	
				wrapper.css({overflow: 'hidden'});						
				
				if (orientation == 'horizontal'){
				    var o_tb = scrollable.find('div', 'th_bg');
				    
				    var a = $(o_tb[0]).css("margin-right");
		            a = isNaN(a)?0 : parseInt(a);
		            var b = $(o_tb[0]).css("margin-left");
		            b = isNaN(b)? 0: parseInt(b);
		            var c = $(o_tb[0]).css("width");
		            c = isNaN(c)? 0: parseInt(c);
		            
		            $(o_tb[o_tb.length-2]).css("margin-right", 0);
		            
		            var iThumbWidth = a+b+c;
		            
		            if(iThumbWidth==0) iThumbWidth = 152;
		            
				    scrollable.css({width: completed*iThumbWidth+20 +'px'});
				
				    scrollable.slideDown('slow', function(){
					    enableH();	
				    });	
				}
				else
				    scrollable.slideDown('slow', function(){
					    enable();	
				    });					
			}, 1000);	
		}
		
		compl_counter++;
		
	}, 100);
	
	
	function enable(){
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 99;	
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		// Create a invisible tooltip
	
		var tooltip = $('<div class="sc_menu_tooltip"></div>')
			.css('opacity', 0)
			.appendTo(wrapper);
	
		// Save menu titles
		scrollable.find('a').each(function(){				
			$(this).data('tooltipText', this.title);				
		});
		
		// Remove default tooltip
		scrollable.find('a').removeAttr('title');		
		// Remove default tooltip in IE
		scrollable.find('img').removeAttr('alt');	
		
		var lastTarget;
		//When user move mouse over menu			
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;

			var wrapperOffset = wrapper.offset();
		
			var tooltipLeft = e.pageX - wrapperOffset.left+20;
			// Do not let tooltip to move out of menu.
			// Because overflow is set to hidden, we will not be able too see it 
			tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());
			
			var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;
			// Move tooltip under the mouse when we are in the higher part of the menu
			if (e.pageY - wrapperOffset.top < wrapperHeight/2){
				tooltipTop += 80;
			}				
			tooltip.css({top: tooltipTop, left: tooltipLeft});				
			
			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
			if (top < 0){
				top = 0;
			}			
			wrapper.scrollTop(top);
		});
		
		// Setting interval helps solving perfomance problems in IE
		var interval = setInterval(function(){
			if (!lastTarget) return;	
										
			var currentText = tooltip.text();
			
			if (lastTarget.nodeName == 'IMG'){					
				// We've attached data to a link, not image
				var newText = $(lastTarget).parent().data('tooltipText');

				// Show tooltip with the new text
				if (currentText != newText) {
					tooltip
						.stop(true)
						.css('opacity', 0)	
						.text(newText)
						.animate({opacity: 1}, 1000);
				}					
			}
		}, 200);
		
		// Hide tooltip when leaving menu
		wrapper.mouseleave(function(){
			lastTarget = false;
			tooltip.stop(true).css('opacity', 0).text('');
		});			
		
		/*
		//Usage of hover event resulted in performance problems
		scrollable.find('a').hover(function(){
			tooltip
				.stop()
				.css('opacity', 0)
				.text($(this).data('tooltipText'))
				.animate({opacity: 1}, 1000);
	
		}, function(){
			tooltip
				.stop()
				.animate({opacity: 0}, 300);
		});
		*/	
		
		//** added on 6 july 2010 by stef
		//** check if this is the itemsList of the current loaded Chapter(the collapsed one) and then use its position number in the list of chapters
		//** scroll the vertical strip to the thumbansil of the active chapter
		//** the active chapter has itemList ID which is saved in the variable iCollapsedChapter --> its value comes from the image template
		//** this is used only in image template view
		if(typeof iCollapsedChapter != 'undefined'){ //** use this check as a condition to execute or not this block
		    var o_tb = $('div.itemsList_menu_wrapper', scrollable);
		    for (var a=-1; ++a<o_tb.length;){
		        if (o_tb[a].id == iCollapsedChapter){

		            var oTH = $('div.th_bg', scrollable);
		            if (oTH.length > 0){
                      var itemH = parseInt( $(oTH[0]).css('height') );
                      if (itemH == 0) itemH = ChapterThumbBoxHeight;
                      itemH += parseInt($(oTH[0]).css('marginBottom')) -1;

		              wrapper.scrollTop(itemH * a)
                    }
		            break;
		        }
	        }
	   } // end of if (iCollapsedChapter)
	}
	
	function enableH(){
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 5;					
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		var scrollableWidth = scrollable.outerWidth() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		
		
		var lastTarget;
		//When user move mouse over menu			
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;

			var wrapperOffset = wrapper.offset();
			// Scroll menu
			var left = (e.pageX -  wrapperOffset.left) * (scrollableWidth - wrapperWidth) / wrapperWidth - inactiveMargin;
			if (left < 0){
				left = 0;
			}			
			wrapper.scrollLeft(left);
		});
		
		wrapper.mouseleave(function(){
			lastTarget = false;
		});			
		
	}
}

$(function(){	
	makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
});


/**** place the horizontal thumbs *********************/
var COLLESCTS = function(){
	var _P = {
		init : function( params ) {
			_P.params = params;
			_P.loadXml();
		},
		params : null,
		data : null,
        itemsAssArr :  new Array,
		
		loadXml : function() {
			jQuery.ajax({
				type : "GET",
				url : _P.params.xmlPath,
				dataType : "asp",
				success : function( data1 ) {
		                 
				    jQuery.ajax({
				        type : "GET",
				        url : data1,
				        dataType : "xml",
				        success : function( data ) {
				            _P.data = data;
					        _P.max = _P.params.perView;
					        _P.count = jQuery( "gallery", data ).length;
					        _P.setSelItemsArr();
					        _P.preloadBooks();
				        }
				    });
			    
				}
			});
		},
		first : 0,
		max : 0,
		count : 0,
		preloadBooks : function() {
		    var k=0;
		    var arTmp=new Array();
			jQuery( "#collections" ).empty();
			jQuery( "gallery", _P.data ).each(function( i ) {
			
			    _P.params.selItems = ','+_P.params.selItems+',';
			    var gID = jQuery( this ).attr( "ID" ).replace('g_', '');
			   //** 1st if
			   if (_P.params.selItems.indexOf(','+gID+',') > -1){
			   
			    //** 2nd if
		         if ( jQuery( this ).attr( "isFlashIntro" )!=1 && 
		                jQuery( this ).attr( "isLibrary" )!=1 &&jQuery( this ).attr( "gallerytype" )!=2  ){
		                //** we remove the '&& jQuery( this ).attr( "isStack" )!=1 ' from the if clause because of the new logic with the Collections  type of galleriea --> 04 jan 2010
    			
		            var title = jQuery( this ).attr( "gName" )
			        var href = jQuery.trim( jQuery( "gLink", this ).text() )+"&AKey="+ jQuery.trim( jQuery( "Artist", _P.data ).attr("sSecKey") );
    				/*
        			arTmp[_P.itemsAssArr[gID]]=  "<div class=\"th_bg\"><div class=\"thumbholder\">"+
        			    "<a href=\"javascript:initSV("+
				        gID+",[750,500], 1"+
				        ")\" class='thumb'><img  src=\""+
				        jQuery( this ).attr( "gImageTh" )+
				        "\"/>"+
				        "</a></div>"+
				        "<span class='name'>"+ title + "</span>"+
				        "</div>";
				    */
				    arTmp[_P.itemsAssArr[gID]]=  "<div class=\"th_bg\"><div class=\"thumbholder\">"+
        			    "<a href=\"javascript:initSV("+
				        gID+",[750,500], 1"+
				        ")\" class='thumb' style=\"background:url('"+
				        jQuery( this ).attr( "gImageTh" )+
				        "') center no-repeat;\"><img  src=\"Commonfiles/spacer.gif\"/>"+
				        "</a></div>"+
				        "<span class='name'>"+ title + "</span>"+
				        "</div>";
			        k++;
    				    
				    } // end 2nd if
				
				} // end 1st if
				
			});
			_P.count = k;
			
			for (i=0; i< arTmp.length;i++){
			    jQuery( "#collections" ).append([arTmp[i]].join( "" ));
			}
			
			
			//*** show 'books' if there are galleries
			if (_P.count > 0) {
			    jQuery( "#collections" ).css( "visibility", "visible" );
			    jQuery( "#gallerybox" ).css( "display", "block" );
			    jQuery( "#coll_label" ).css( "display", "block" );
			    makeScrollable("div.sc_menu_wrapper_h", "div.sc_menu_h", 'horizontal');
		   }   
	         
 
			 
			    
		},
		setSelItemsArr : function(){
		    
		    //itemsArr
		    //_P.params.selItems
		    var z = _P.params.selItems.split(',');
		    for (var i=0;i<z.length;i++){
		        _P.itemsAssArr[z[i]] = i;
		    }
		    
		
		    
		}
	};
	return {
		init : function( params ) {
			_P.init( params );
		}
	};
}();

