Gallery = function(){
//private vars
var defaults={path:'/galleryWorx/',
			  pathImages:'photo/',
			  jsonPath:'json/',
			  pathTemplate:'template/',
			  pageIndex:true,
			  imageInSet:10,
			  currentPage:0,
			  pageSize:9
			  }	
var hasNextPage=true;
var hasPrevPage=true;
this.galleryIndex = new Array();
_templateGalleryIndex=new TemplateReader();	
				_templateGalleryIndex.read(defaults.path+defaults.pathTemplate+"gallery.html","#galleryIndexContainer");
//things to do when creating an object	
 _this=this;	

_init=function(){	
	//reads the json file
	readJson=function(data) { 
			for(i in data.gallery){
				o=data.gallery[i];
			 	_this.galleryIndex.push(o);
		}
		};
		
		jQuery.ajax({
  		url: defaults.path+defaults.jsonPath+'gallery-index.json',
  		dataType: 'json',
  		success: readJson,
		async:false
		});
}
_init();

this.showCurrentPage=function(){
			//get the template
			
			
			//update template 
			chainParams={'.galleryIndexLink': { href: '/index.php?id={id}'},'.img':{src:defaults.path+defaults.pathImages+'{id}/cover.jpg'},container:'#galleryIndex'};
			_data = _this.galleryIndex.slice(defaults.currentPage*defaults.pageSize,(defaults.currentPage*defaults.pageSize)+defaults.pageSize);
			
			_templateGalleryIndex.update(_data,"#galleryIndexContainer", chainParams);

			//check if prev/next page exists
			if(defaults.currentPage < (_data.length-1)){ hasNextPage=true;jQuery('#galleryIndexContainer .nextIndex').removeClass('disabled'); }else{ hasNextPage=false;jQuery('#galleryIndexContainer .nextIndex').addClass('disabled'); }
			if(defaults.currentPage!=0){ hasPrevPage=true; jQuery('#galleryIndexContainer .prevIndex').removeClass('disabled');}else{ hasPrevPage=false;jQuery('#galleryIndexContainer .prevIndex').addClass('disabled') }
}
this.showCurrentPage();

//load next page of the gallery Index
this.showNextPage=function(){
						if(hasNextPage){
							defaults.currentPage++;
							this.showCurrentPage();
						}
				}

//load previous page of gallery index
this.showPrevPage=function(){
						if(hasPrevPage){
							defaults.currentPage--;
							this.showCurrentPage();
						}
               }

//add events to next and previous link
jQuery('#galleryIndexContainer .prevIndex').live('click', function(event) {
																				
  			_this.showPrevPage();
		});
		
jQuery('#galleryIndexContainer .nextIndex').live('click', function(event) {
																					
  			_this.showNextPage();
		});
	}