

var Artists = {

	category_id:0,
	page:1,
	cache:{},
	cachep:{},
	get:function(category_id)
	{
		Artists.category_id = category_id;
		// Check the cache 
		if( !Artists.cache[Artists.page+"-"+Artists.category_id] )
		{
			// Start the fadeout
			new Effect.Fade('artists-inner',{duration:.3,
				afterFinish:function(){
			
					// SHow the loadbar
					Element.show('loader');
					
					// Set the query parameters
					var _q = "action=getArtists&category="+category_id+"&page="+Artists.page;
					
					// Send the ajax request
					var ajax = new Ajax.Request('/ajax',
							{
									onComplete:function(t){
										try {
											eval(t.responseText);
										} catch(e) {
											alert("error loading artists:"+e);
										}
										
										// Update the cache
										Artists.cache[Artists.page+"-"+Artists.category_id] = data.artists.stripSlashes();
										Artists.cachep[Artists.page+"-"+Artists.category_id] = data.pagination.stripSlashes();
										
										// Update the main content and pagination areas
										Element.update('artists-inner',data.artists.stripSlashes());
										Element.update('pagination',data.pagination.stripSlashes());
										// Update the category text 
										var cat = $('category')
										Element.update('category_display',cat.options[cat.selectedIndex].text);
																
										// Hide the loader 
										Element.hide('loader');
										
										// Fade in the elements
										new Effect.Appear('artists-inner',{
											duration:.4,
											afterFinish:function(){
												Element.show('artists-inner');
											}
										});
										
									},
									postBody:_q,
									method:'post',
									evalScripts:false
							}); 
				}	
			}); // Close fadeout
		} else {
			// Populate from cache
			//alert("cache!"+Artists.page+"-"+Artists.category_id+" "+Artists.cache[Artists.page+"-"+Artists.category_id]);
			var cat = $('category')
			Element.update('category_display',cat.options[cat.selectedIndex].text);
			Element.update('artists-inner',Artists.cache[Artists.page+"-"+Artists.category_id]);
			Element.update('pagination',Artists.cachep[Artists.page+"-"+Artists.category_id]);
		}
		
	},
	update:function(category_id){
		Artists.page = 1;
		Artists.get(category_id);
		
	},
	getPage:function(category_id,page,max)
	{
		Artists.category_id = category_id;
		Artists.page = page;
		Artists.get(category_id);
	},
	nextPage:function(category_id,max){
		if(Artists.page<max){
			Artists.category_id = category_id;
			Artists.page++;
			Artists.get(category_id);
		}
	}

}

var Collection = {

	page:1,
	cache:{},
	cachep:{},
	get:function()
	{
		
		// Check the cache 
		if( !Artists.cache[Collection.page+"-"+Collection.category_id] )
		{
			// Start the fadeout
			new Effect.Fade('artist-inner',{duration:.3,
				afterFinish:function(){
			
					// SHow the loadbar
					Element.show('loader');
					
					// Set the query parameters
					var _q = "action=getCollection&page="+Collection.page;
					
					// Send the ajax request
					var ajax = new Ajax.Request('/ajax',
							{
									onComplete:function(t){
										try {
											eval(t.responseText);
										} catch(e) {
											alert("error loading artists:"+e);
										}
										
										// Update the cache
										Collection.cache[Collection.page] = data.collection.stripSlashes();
										Collection.cachep[Collection.page] = data.pagination.stripSlashes();
										
										// Update the main content and pagination areas
										Element.update('artist-inner',data.collection.stripSlashes());
										Element.update('pagination',data.pagination.stripSlashes());
										// Update the category text 
										//var cat = $('category')
										//Element.update('category_display',cat.options[cat.selectedIndex].text);
																
										// Hide the loader 
										Element.hide('loader');
										
										// Fade in the elements
										new Effect.Appear('artist-inner',{
											duration:.4,
											afterFinish:function(){
												Element.show('artist-inner');
											}
										});
										
									},
									postBody:_q,
									method:'post',
									evalScripts:false
							}); 
				}	
			}); // Close fadeout
		} else {
			// Populate from cache
			Element.update('artist-inner',Collection.cache[Collection.page]);
			Element.update('pagination',Collection.cachep[Collection.page]);
		}
		
	},
	getPage:function(page,max)
	{
		
		Collection.page = page;
		Collection.get();
	},
	nextPage:function(max){
		if(Collection.page<max){
			Collection.page++;
			Collection.get();
		}
	}

}

var Products = {

	page:1,
	artist_id:0,
	collection_id:0,
	exhibition_id:0,
	max:0,
	done:false,
	cache:{},
	cachep:{},

	
	get:function(){

		cacheKey = Products.artist_id+"-"+Products.collection_id+Products.exhibition_id+Products.page;
		if( !Products.cache[cacheKey] )
		{
			new Effect.Fade('artist-inner',{duration:.3,
			afterFinish:function(){

				Element.show('loader');
				
				_q = "action=getProducts&page="
				+Products.page+"&artist_id="+Products.artist_id
				+"&collection_id="+Products.collection_id 
				+"&exhibition_id="+Products.exhibition_id;
				
				//_q = "action=getProducts&page="+Products.page+"&collection_id="+Products.collection_id;

				var ajax = new Ajax.Request('/ajax',
				{
						onComplete:function(t){

							try{
								eval(t.responseText);
							}
							catch(e){
								alert("error with js string="+e);
							}

							Products.cache[cacheKey] = data.products.stripSlashes();
							Products.cachep[cacheKey] = data.pagination.stripSlashes();

							Element.hide('loader');

							Element.update('artist-inner',Products.cache[cacheKey]);
							Element.update('pagination',Products.cachep[cacheKey]);

							new Effect.Appear('artist-inner',{
								duration:.4,
								afterFinish:function(){
									Element.show('artist-inner');
								}
							});
							initLightbox() ;
						},
						postBody:_q,
						method:'post',
						evalScripts:false
				});

			}});
		}
		else
		{
			Element.update('artist-inner',Products.cache[cacheKey]);
			Element.update('pagination',Products.cachep[cacheKey]);
			initLightbox();
		}
	},
	update:function(artist_id,collection_id,exhibition_id)
	{
		Products.artist_id = artist_id;
		Products.collection_id = collection_id;
		Products.exhibition_id = exhibition_id;
		Products.get();
	},
	getPage:function(artist_id,collection_id,exhibition_id,page,max)
	{
		Products.artist_id = artist_id;
		Products.collection_id = collection_id;
		Products.exhibition_id = exhibition_id;
		Products.page = page;
		Products.get();
	},
	nextPage:function(artist_id,collection_id,exhibition_id,max)
	{
		if(Products.page<max){
			Products.artist_id = artist_id;
			Products.collection_id = collection_id;
			Products.exhibition_id = exhibition_id;
			Products.page++;
			Products.get();
		}
	}

}