var Step = 10; // step in pixels
var IntervalSlow = 100; // intervel between next move
var IntervalFast = 10;
var SmallTimer = 0;
var Timer = 0

// galeries
var ImageWidth = 200;
var ImageCount = 5;
var ImageId = 1;	//set in scrollImages method
var ImageSide = 'center'; // left or right
var ImageLeft = 0;
var ImageRight = 0;
var StopLeft = false;
var StopRight = false;

var VisibleAreaWidth = 700;
var VisibleAreaLeft = ( VisibleAreaWidth / 2 ) - ( ImageWidth / 2 );
var VisibleAreaHalfLeft = VisibleAreaLeft;
var VisibleAreaHalfRight = ( VisibleAreaWidth / 2 ) + ( ImageWidth / 2 );

// images from gallery

var ImagesAreaLeft = 0;
var ImagesAreaRight = 0;
var ImagesAreaWidth = 0;

var SmallImageWidth = 171;
var SmallImageId = 1;	//set in scrollImages method
var SmallImageCount = 11;
var SmallImageSide = 'center'; // left or right
var SmallImageLeft = 0;
var SmallImageRight = 0;
var SmallStopLeft = false;
var SmallStopRight = false;


var SmallVisibleAreaWidth = 820;
var SmallVisibleAreaLeft = ( SmallVisibleAreaWidth / 2 ) - ( SmallImageWidth / 2 );
var SmallVisibleAreaHalfLeft = SmallVisibleAreaLeft;
var SmallVisibleAreaHalfRight = ( SmallVisibleAreaWidth / 2 ) + ( SmallImageWidth / 2 );

var SmallImagesAreaLeft = 0;
var SmallImagesAreaRight = 0;
var SmallImagesAreaWidth = 0;


var SmallPhotsTitle = new Array();
SmallPhotsTitle[ 1 ] = '';
SmallPhotsTitle[ 2 ] = '';
SmallPhotsTitle[ 3 ] = '';
SmallPhotsTitle[ 4 ] = '';
SmallPhotsTitle[ 5 ] = '';
SmallPhotsTitle[ 6 ] = '';
SmallPhotsTitle[ 7 ] = '';
SmallPhotsTitle[ 8 ] = '';
SmallPhotsTitle[ 9 ] = '';
SmallPhotsTitle[ 10 ] = '';
SmallPhotsTitle[ 11 ] = '';

var SmallPhotsTxt = new Array();
SmallPhotsTxt[ 1 ] = '';
SmallPhotsTxt[ 2 ] = '';
SmallPhotsTxt[ 3 ] = '';
SmallPhotsTxt[ 4 ] = '';
SmallPhotsTxt[ 5 ] = '';
SmallPhotsTxt[ 6 ] = '';
SmallPhotsTxt[ 7 ] = '';
SmallPhotsTxt[ 8 ] = '';
SmallPhotsTxt[ 9 ] = '';
SmallPhotsTxt[ 10 ] = '';
SmallPhotsTxt[ 11 ] = '';

	function CalculateVisibleArea(){
	
		ImagesAreaLeft = parseFloat( document.getElementById("all_images").style.left );
		ImagesAreaWidth = ImageWidth * ImageCount;
		ImagesAreaRight = ImagesAreaWidth + ImagesAreaLeft;

		ImageLeft = ImagesAreaLeft + ( ( ImageId - 1 ) * ImageWidth );
		ImageRight = ImageLeft + ImageWidth;

		// go to left or right
		if( ImageLeft > VisibleAreaLeft )
			ImageSide = 'left';
		else if(ImageLeft < VisibleAreaLeft )
			ImageSide = 'right';
		else
			ImageSide = 'center';
		
		StopLeft = false;
		StopRight = false;
		// check left or right limited
		if( ImagesAreaLeft >= 0)
			StopRight = true;
		
		if( ImagesAreaRight <= VisibleAreaWidth )
			StopLeft = true;

		Interval = IntervalSlow;
		if( ImageRight < VisibleAreaHalfLeft || ImageLeft > VisibleAreaHalfRight )
			Interval = IntervalFast;
	}
	
	function SmallCalculateVisibleArea(){
		SmallImagesAreaWidth = SmallImageWidth * SmallImageCount;
		SmallImagesAreaLeft = parseFloat( document.getElementById("small_all_images").style.left );
		SmallImagesAreaRight = SmallImagesAreaWidth + SmallImagesAreaLeft;

		SmallImageLeft = SmallImagesAreaLeft + ( ( SmallImageId - 1 ) * SmallImageWidth );
		SmallImageRight = SmallImageLeft + SmallImageWidth;

		// go to left or right
		if( SmallImageLeft > SmallVisibleAreaLeft )
			SmallImageSide = 'left';
		else if(SmallImageLeft < SmallVisibleAreaLeft )
			SmallImageSide = 'right';
		else
			SmallImageSide = 'center';
		
		SmallStopLeft = false;
		SmallStopRight = false;
		// check left or right limited
		if( SmallImagesAreaLeft >= 0)
			SmallStopRight = true;
		
		if( SmallImagesAreaRight <= SmallVisibleAreaWidth )
			SmallStopLeft = true;
			
		Interval = IntervalSlow;
		if( SmallImageRight < SmallVisibleAreaHalfLeft || SmallImageLeft > SmallVisibleAreaHalfRight )
			Interval = IntervalFast;
	}

	function MoveToRight(){
		CalculateVisibleArea();

		if( ImageSide != 'center' && !StopRight ){
			move_images( document.getElementById("all_images"), (Step * -1) );
			Timer = setTimeout("MoveToRight()", Interval);
		}
		else
			StopMoving();
	}
	
	function SmallMoveToRight(){
		SmallCalculateVisibleArea();

		if( SmallImageSide != 'center' && !SmallStopRight ){
			move_images( document.getElementById("small_all_images"), (Step * -1) );
			SmallTimer = setTimeout("SmallMoveToRight()", Interval);
		}
		else
			SmallStopMoving();
	}
	
	function MoveToLeft(){
		CalculateVisibleArea();

		if( ImageSide != 'center' && !StopLeft ){
			move_images( document.getElementById("all_images"), Step );
			Timer = setTimeout("MoveToLeft()", Interval);
		}
		else
			StopMoving();
	}
	
	function SmallMoveToLeft(){
		SmallCalculateVisibleArea();

		if( SmallImageSide != 'center' && !SmallStopLeft ){
			move_images( document.getElementById("small_all_images"), Step );
			SmallTimer = setTimeout("SmallMoveToLeft()", Interval);
		}
		else
			SmallStopMoving();
	}
	
	function scrollStart(id){
		showDescription( id );
		ImageId = id;

		CalculateVisibleArea();

		switch( ImageSide ){
			case 'left':
				MoveToLeft();
				break;
			case 'right':
				MoveToRight();
				break;
		}
	}
	
	function scrollStop(){
		StopMoving();
	}
	
	function SmallscrollImages(id){
		SmallImageId = id;
		SmallshowDescription(id);

		SmallCalculateVisibleArea();

		switch( SmallImageSide ){
			case 'left':
				SmallMoveToLeft();
				break;
			case 'right':
				SmallMoveToRight();
				break;
		}
	}
	
	function move_images( element, step ){
		current_position = parseFloat( element.style.left );
		new_possition = current_position - parseInt( step );
		element.style.left = new_possition+'px';
	}
	
	function StopMoving(){
		clearTimeout(Timer);
	}
	
	function SmallStopMoving(){
		clearTimeout(SmallTimer);
	}
	
	function showDescription(id){
		fadeTargetId = 'gal_description_txt';
		fadeInit();
		
		$('gal_description_title').update( GaleryTitles[ id ] );
		$('gal_description_txt').update( GaleryTexts[ id ] );
	}
	
	function SmallshowDescription(id){
		$('small_gal_description_title').update( SmallPhotsTitle[ id ] );
		$('small_gal_description_txt').update( SmallPhotsTxt[ id ] );
		fadeTargetId = 'small_gal_description_txt';
		fadeInit();
	}
	
	function loadGalery( id ){
        
		new Ajax.Request('../getphotosforgallery.php?gallery_id='+id, {
				method: 'get',
                    onSuccess: function(transport, json) {
                    document.getElementById("small_all_images").style.left = "0px";
					var photos_area = $('visible_images_small');
					//alert(transport.responseText);
                    images = new Array();
					date = new Array();
                    date = transport.responseText.evalJSON();
					images = date['images'];
                    SmallImageCount = date['image_count'];
					//alert(Object.keys(images).length);
					html = preparePhotosForGallery( images );
					$('small_all_images').update( html );

                    //to_removed = $('lightbox');
                    //to_removed.parentNode.removeChild(to_removed);
					initLightbox();
				}
			});
	}
	
	function preparePhotosForGallery( images ){
		html = '';
		SmallPhotsTxt.clear();
		SmallPhotsTitle.clear();
		for( i = 1; i <= (Object.keys(images).length); i++){
			html += '<a href="'+ images[ i ]['big'] +'" rel="lightbox[roadtrip]">';
			html += '<img class="gal" src="'+ images[ i ]['src'] +'" onclick="SmallshowDescription('+ i +');" onmouseout="SmallStopMoving();" onmouseover="SmallscrollImages('+ i +');" />';
			html += '</a>';
            //alert(html);
			SmallPhotsTxt[i] = images[ i ][ 'des' ];
			SmallPhotsTitle[i] = images[ i ][ 'title' ];
		}
		return html;
	}
	
	function GotoStartPosition(){
		MoveToRight();
	}
	
	function SmallGotoStartPosition(){
		SmallMoveToRight();
	}
