(function(){
	
	var gallery = window.gallery = {
			
		options: {},
		 
		init: function(){
			
			gallery.currentPosition = 0;
  			gallery.galleryWidth = 600;
  			gallery.gallerys = $('.gallery');
  			gallery.numberOfGallerys = gallery.gallerys.length;

  			// remove scrollbar in js
  			$('#galleryContainer').css('overflow', 'hidden');

  			// wrap all .gallerys with #galleryInner div
  			gallery.gallerys
    			.wrapAll('<div id="galleryInner"></div>')
			// float left to display horizontally, readjust .gallerys width
			.css({
      			'float' : 'left',
				'width' : gallery.galleryWidth
    		});

  			// set #galleryInner width equal to total width of all gallerys
  			$('#galleryInner').css('width', gallery.galleryWidth * gallery.numberOfGallerys);

  			// Insert controls in the DOM
		  	$('#galleryShow')
				.prepend('<span class="control" id="leftControl">next</span>')
				.append('<span class="control" id="rightControl">previous</span>');
		
		  	// hide left arrow control on first load
		  	gallery.manageControls(gallery.currentPosition);
		
		  	// create event listeners for .controls clicks
		  	$('.control').bind('click', function(){
												 
				// determine new position
				gallery.currentPosition = ($(this).attr('id')=='rightControl') ? gallery.currentPosition+1 : gallery.currentPosition-1;
			
				// hide / show controls
				gallery.manageControls(gallery.currentPosition);
			
				// move galleryInner using margin-left
				$('#galleryInner').animate({
			  		'marginLeft' : gallery.galleryWidth*(-gallery.currentPosition)
				});
				
		  	});

  				//setTimeout("gallery.next()", 5000);

		 },
		 
		 // auto scroll
		next: function(){
		
			if(gallery.currentPosition < (gallery.numberOfGallerys-1)){
				$('#rightControl').click();
			}else{
				gallery.currentPosition = 0;
				gallery.manageControls(gallery.currentPosition);
				$('#galleryInner').animate({
				  'marginLeft' : gallery.currentPosition
				});
			}
			
				//setTimeout("gallery.next()", 5000);
  		},
	
		// hides and shows controls depending on currentPosition
		manageControls: function(position){
		
			// hide left arrow if position is first gallery
			if(position==0){ $('#leftControl').hide(); } else{ $('#leftControl').show(); }
			
			// hide right arrow if position is last gallery
			if(position==gallery.numberOfGallerys-1){ $('#rightControl').hide(); } else{ $('#rightControl').show(); }
	  	
		}	
		  
	};
	
})();

