
var currentPositionThumb = 0;
var currentGrid=0;
var showPerGrid=9;
var groups=0;
var lengthOfGrid = 444 //Add width of grid and margins  342+51+51
window.addEvent('domready', function(){
	if (location.href) {
		if (location.hash != "#") {
			LoadGallery(location.href.split('#').pop());
		}
	}
	$$('.OptionActiveLink').each(function(item){
		var div = item.getParent();
		item.addEvent('click',function(a){
		$$('.OptionActiveLink').each(function(item){ item.getParent().removeClass('Active')})
		div.addClass('Active')
		if(item.href)
			LoadGallery(item.href.split('#').pop())    			
    		})  
	})
})
function bottomsVisible(){

if((currentGrid+1)<groups){
		//alert($('downgallery')[0].innerHTML);
		$('downgallery').removeClass('downgallery_disable');
		$('downgallery').addClass('downgallery');
		//$('downgallery').innerHTML = '[<]';
	}else{
		$('downgallery').removeClass('downgallery');
		$('downgallery').addClass('downgallery_disable');
		//$('downgallery').innerHTML = '[-]';
	}
	if(currentGrid!=0){
		$('upGallery').removeClass('upGallery_disable');
		$('upGallery').addClass('upGallery');
		//$('upGallery').innerHTML = '[>]';
	}else{
		$('upGallery').removeClass('upGallery');
		$('upGallery').addClass('upGallery_disable');
		//$('upGallery').innerHTML = '[-]';
	}
}
function movethumbs(way){
	
	currentPositionThumb = -1*(currentGrid*lengthOfGrid) 
	scrollito =  new Fx.Styles('thumbMove', {duration: 500, transition: Fx.Transitions.quadOut});
	if(way=='plus' && currentGrid < groups-1)
		currentGrid++;
	if(way=='minus' && currentGrid != 0)
		currentGrid--;
	scrollito.start({ left: [currentPositionThumb,(currentPositionThumb=-1*(currentGrid*lengthOfGrid)) ]});
	if($('groupInfoActual'))
		$('groupInfoActual').innerHTML = currentGrid+1;
	bottomsVisible();
	
}

function movethumbsToGrid(grid) {

	if(grid >= 0 && grid < groups) {
		var currentPositionThumb = -1*(currentGrid*lengthOfGrid);
		var scrollito =  new Fx.Styles('thumbMove', {duration: 500, transition: Fx.Transitions.quadOut});
		currentGrid = grid;
		scrollito.start({ left: [currentPositionThumb,(currentPositionThumb=-1*(currentGrid*lengthOfGrid)) ]});
	}
	$('groupInfoActual').innerHTML = currentGrid+1;
	bottomsVisible();
	
}

function LoadGallery(number){

	if(number==null)
		prefix='';
	else
		prefix='?idGallery='+number;
	$('thumbMove').style.top=0;
	$('thumbMove').style.left=0;
	$('thumbMove').innerHTML = '<span id="loading"> </span>';
	$('groupInfo').innerHTML = '';
	currentGrid = 0;
	
	var JsonPetition = new Json.Remote(
			document.getElementById('jsonURL').value+prefix,{
				onComplete:function(g){
	 				currentPositionThumb=0;
					root = g.gallery
					total = root.image.length
					thumbsNum=total;
					images = root.image
					raizCarga = $('thumbMove')
					raizCarga.innerHTML=''
					groups=Math.ceil(total/showPerGrid);
					divGroups=new Array();
					
					var groupInfo = $('groupInfo');
					if(groups > 0) {
						groupInfo.innerHTML = 'Group ';
						var groupInfoActual = new Element('span');
						groupInfoActual.setProperty('id','groupInfoActual');
						groupInfoActual.injectInside(groupInfo);
						groupInfo.innerHTML += ' of ' + groups;
						groupInfoActual = null;
						$('groupInfoActual').innerHTML = 1;	
					}
					
					var divGroupMoveTo = $('pagesGroup');
					$('pagesGroup').innerHTML='';
					//divGroupMoveTo.setProperty('id','groupMoveTo');
					//divGroupMoveTo.injectInside(groupInfo);
					
					if(groups>1){
						$('downgallery').removeClass('downgallery_disable');
						$('downgallery').addClass('downgallery');
					}
					
					for(i=0;i<groups;i++){
						var aMoveToGrid = new Element('a');
						aMoveToGrid.setProperty('href','javascript:movethumbsToGrid(' + i +')');
						aMoveToGrid.innerHTML = i+1;
						aMoveToGrid.injectInside(divGroupMoveTo);
						
						divGroups[i]=new Element('div');
						divGroups[i].addClass('grid');
						divGroups[i].setProperty('id','group'+i);
						divGroups[i].injectInside(raizCarga)
					}
					for(i=0;i<total;i++){
						if(images[i]){
							thumbaild = new Asset.image(root.thumbBase+images[i].src, {onload: function() { }})
							//splitDim = images[i].rev_t.split('x')
							//thumbaild.setProperty('style','width:' +  splitDim[0] + 'px;height:'+splitDim[1]+'px;');
	 						link = new Element('a').setProperty('href',root.imageBase+images[i].src)
							link.setProperty('rel','galeria')
							link.setProperty('rev',images[i].rev)
							link.setProperty('class','smoothbox')
							link.injectInside(divGroups[Math.floor(i/showPerGrid)])
							thumbaild.injectInside(link)
	           			}
			  		}
        					box.loadLinks();
       				 }
     		})
	JsonPetition.send();
}

