// Current left visible thumb
// Used for thumbs scrolling
var bottomThumbCurrent = 1; 

var bottomThumbSelected = 1;

// Global accessed ajax object
// Used to stop loading
var ajax;

// Current selected collection
var collectionSelected = 0;
 
// Scroller
var scroll;

var totalBottomThumbs;

function initScroller()
{
	scroll = new Fx.Scroll('bottomScroller', {
			wait: false,
			duration: 1000,
			//offset: {'x': -200, 'y': -50},
			transition: Fx.Transitions.Back.easeInOut//Fx.Transitions.Quad.easeInOut
		});	

	totalBottomThumbs = 1;
	while ($('tp' + totalBottomThumbs) != null) totalBottomThumbs++;
	totalBottomThumbs--;
			
	scroll.toElement('tp' + bottomThumbCurrent);

}

function arrowLeftOver()
{
	if (bottomThumbCurrent == 1) return;
	$('left').src = 'pics/Arrow-L-over.jpg';
}

function arrowLeftOut()
{
	$('left').src = 'pics/Arrow-L.jpg';
}

function arrowRightOver()
{
	if (bottomThumbCurrent > totalBottomThumbs - 6) return;
	$('right').src = 'pics/Arrow-R-over.jpg';
}

function arrowRightOut()
{
	$('right').src = 'pics/Arrow-R.jpg';
}



window.addEvent('domready', function() 
	{
		Element.Events.extend({
			'wheelup': {
				type: Element.Events.mousewheel.type,
				map: function(event){
					event = new Event(event);
					if (event.wheel >= 0) this.fireEvent('wheelup', event)
				}
			},
		 
			'wheeldown': {
				type: Element.Events.mousewheel.type,
				map: function(event){
					event = new Event(event);
					if (event.wheel <= 0) this.fireEvent('wheeldown', event)
				}
			}
		});
	
		rigthClick(phpGalerie); // When document first loads, loading needed collection, usually first
	
		$('left').addEvent('click', function(event) {
			event = new Event(event).stop();
			if (bottomThumbCurrent > 1)
			{
				bottomThumbCurrent--;
				scroll.toElement('tp' + bottomThumbCurrent);
				if (bottomThumbCurrent == 1) arrowLeftOut();
			}
			//scroll.scrollTo($('left').getPosition().x + 40,0);
		});
		
		$('right').addEvent('click', function(event) {
			event = new Event(event).stop();
			/*bottomThumbCurrent++;
			if ($('tp' + bottomThumbCurrent) == null)
				bottomThumbCurrent--;
			else
				scroll.toElement('tp' + bottomThumbCurrent);*/
				
			if (bottomThumbCurrent <= totalBottomThumbs - 6)
			{
				bottomThumbCurrent++;
				scroll.toElement('tp' + bottomThumbCurrent);
				if (bottomThumbCurrent > totalBottomThumbs - 6) arrowRightOut();
			}
				
			//scroll.scrollTo($('left').getPosition().x + 40,0);
		});
		
		//initScroller();
		
		$('bottomScroller').addEvent('wheelup', function(event) {
			event = new Event(event).stop();
			if (bottomThumbCurrent > 1)
			{
				bottomThumbCurrent--;
				scroll.toElement('tp' + bottomThumbCurrent);
				if (bottomThumbCurrent == 1) arrowLeftOut();
			}
			//scroll.scrollTo($('left').getPosition().x + 40,0);
		});
		
		$('bottomScroller').addEvent('wheeldown', function(event) {
			event = new Event(event).stop();
			/*bottomThumbCurrent++;
			if ($('tp' + bottomThumbCurrent) == null)
				bottomThumbCurrent--;
			else
				scroll.toElement('tp' + bottomThumbCurrent);*/
				
			if (bottomThumbCurrent <= totalBottomThumbs - 6)
			{
				bottomThumbCurrent++;
				scroll.toElement('tp' + bottomThumbCurrent);
				if (bottomThumbCurrent > totalBottomThumbs - 6) arrowRightOut();
			}
				
			//scroll.scrollTo($('left').getPosition().x + 40,0);
		});
		
		/*var Tips2 = new Tips([$('botanique'),$('chats'),$('oiseaux')], 
		{
			showDelay:100,
			initialize:function()
			{
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
			},
			onShow: function(toolTip) 
			{
				this.fx.start(1);
			},
			onHide: function(toolTip) 
			{
				this.fx.start(0);
			}
		});*/
	
	});
	
	
	
// Preloading right thumbs text mouse over images
preloadImage('collections/c1/text.jpg');
preloadImage('collections/c2/text.jpg');
preloadImage('collections/c3/text.jpg');
preloadImage('collections/c4/text.jpg');
preloadImage('collections/c1/text-over.jpg');
preloadImage('collections/c2/text-over.jpg');
preloadImage('collections/c3/text-over.jpg');
preloadImage('collections/c4/text-over.jpg');


function rightOver(c)
{

	if (c < 1 || c == collectionSelected) return;
	document.getElementById('c' + c + 'pic').className = "rightImageOver"; //style.border = '3px solid #ee4d9b';
	document.getElementById('c' + c + 'text').src = 'collections/c' + c + '/text-over.jpg';

}

function rightOut(c)
{

	if (c < 1 || c == collectionSelected) return;
	document.getElementById('c' + c + 'pic').className = "rightImage"; //style.border = '3px solid #6b726b';
	document.getElementById('c' + c + 'text').src = 'collections/c' + c + '/text.jpg';

}

function loadBottomThumbs(cnum)
{
	if (ajax != null) ajax.cancel();
	var url = base + "ajax/bottom-thumbs/cnum/" + cnum;
	var el = $('bottomScroller');
	ajax = new Ajax(url, 
				{
					update : el,
					method : 'get',
					onComplete : function()
					{
						el.removeClass('loading');
						bottomThumbCurrent = 1;
						bottomThumbSelected = 0;
						bottomClick(1);
						initScroller();
					}
				});
	el.empty().addClass('loading');
	$('mainImage').src = 'pics/ajax-loader.gif';		
	ajax.request();
}

function rigthClick(c)
{

	if (c == collectionSelected) return;
	rightOver(c);
	var prev = collectionSelected;
	collectionSelected = c;
	rightOut(prev);
	loadBottomThumbs(c);

}

function bottomOver(p)
{
	if (p < 1 || bottomThumbSelected == p) return;
	$('p' + p).className = 'bottomThumbOver';
}

function bottomOut(p)
{
	if (p < 1 || bottomThumbSelected == p) return;
	$('p' + p).className = 'bottomThumb';
}

function bottomClick(p)
{
	bottomOver(p);
	var prev = bottomThumbSelected;
	bottomThumbSelected = p;
	bottomOut(prev);
	
	var tmp = new Image();
	tmp.onload = function() 
		{
			$('mainImage').src = 'collections/c' + collectionSelected + '/p' + p + '/image.jpg';
		}
	tmp.src = 'collections/c' + collectionSelected + '/p' + p + '/image.jpg';
}

// Main image zoomer cursor functions
function zoomerOver()
{
	var div = document.getElementById("zoomer");
	div.style.display = "block";
}

function zoomerOut()
{
	var div = document.getElementById("zoomer");
	div.style.display = "none";
}

document.onmousemove = zoomerMove;

function zoomerMove(e)
{
	if (!e) e = window.event;
	var div = document.getElementById("zoomer");
	div.style.left = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 15 + 'px';//(document.layers) ? e.pageX : document.body.scrollLeft+event.clientX+15;
	div.style.top = e.clientY + document.body.scrollTop + document.documentElement.scrollTop + 'px';//(document.layers) ? e.pageY : document.body.scrollTop+event.clientY;
}

// MainImage mouse click
function mainClick()
{
	window.open(base + 'collections/c' + collectionSelected + '/p' + bottomThumbSelected + '/big.jpg');
}
