var gallerySeries = new Array('photography/flesh-soul/midnight', 'photography/flesh-soul/chimera', 'photography/flesh-soul/the-purge', 'photography/flesh-soul/elysian-fields', 'photography/flesh-soul/flesh-structures', 'photography/flesh-soul/enigma', 'photography/exoteric-glimpses/hallucinations', 'photography/exoteric-glimpses/metaphysical-synapses', 'photography/flesh-soul/fie-set-1', 'photography/flesh-soul/deva-set-1', 'photography/flesh-soul/fluid-form', 'artwork/experiments/discarded', 'artwork/experiments/dream-shards', 'photography/flesh-soul/interaction', 'photography/flesh-soul/flesh-landscapes', 'photography/flesh-soul/jasmin', 'photography/exoteric-glimpses/while-they-sleep');
var galleries = new Array('photography/prints/test', 'artwork/corpus-transfiguratio/duality', 'photography/flesh-soul/midnight', 'photography/flesh-soul/chimera', 'photography/flesh-soul/the-purge', 'photography/flesh-soul/elysian-fields', 'photography/flesh-soul/flesh-structures', 'artwork/twisted-flesh/twisted-flesh-01', 'artwork/twisted-flesh/twisted-flesh-02', 'artwork/twisted-flesh/twisted-flesh-03', 'artwork/twisted-flesh/twisted-flesh-04', 'artwork/twisted-flesh/twisted-flesh-05', 'artwork/twisted-flesh/twisted-flesh-06', 'artwork/twisted-flesh/twisted-flesh-07', 'artwork/twisted-flesh/twisted-flesh-08', 'photography/flesh-soul/enigma', 'photography/exoteric-glimpses/hallucinations', 'photography/exoteric-glimpses/metaphysical-synapses', 'artwork/corpus-transfiguratio/unbound', 'photography/flesh-soul/fie-set-1', 'artwork/corpus-transfiguratio/aphrodisia', 'photography/flesh-soul/deva-set-1', 'photography/flesh-soul/fluid-form', 'artwork/experiments/digital-dreams', 'artwork/experiments/binary-reflux', 'artwork/experiments/used-to', 'artwork/experiments/discarded', 'artwork/experiments/untitled', 'artwork/experiments/the-one-voice', 'artwork/experiments/technical-difficulties', 'artwork/experiments/silence', 'artwork/experiments/self-in-static', 'artwork/experiments/sanity-test', 'artwork/experiments/paint-me-a-beauty', 'artwork/experiments/memory-fades', 'artwork/experiments/loneliness-as-a-cure-for-love', 'artwork/experiments/experience', 'artwork/experiments/elegantly-wasted', 'artwork/experiments/dream-shards', 'artwork/experiments/convenience', 'artwork/corpus-transfiguratio/mating-ritual', 'photography/flesh-soul/interaction', 'photography/flesh-soul/flesh-landscapes', 'photography/flesh-soul/jasmin', 'photography/exoteric-glimpses/while-they-sleep');
var galleryIds = new Array(110, 109, 108, 107, 106, 96, 86, 105, 104, 103, 102, 101, 100, 99, 98, 95, 94, 93, 91, 90, 85, 30, 84, 67, 65, 83, 82, 81, 80, 79, 77, 76, 75, 74, 73, 72, 70, 69, 68, 66, 45, 26, 24, 22, 8); 
var galleryFadeDuration = 500;
var galleryUrl = '/gallery-thumbs/';

function galleryThumbs(sender, galleryId, idx, hashAction) {
	hoverImg(sender.getElements('img')[0]);
	var scrollerDiv = $(sender).getParent().getParent().getParent();
	scrollerDiv.store('original-content', scrollerDiv.innerHTML);
	galleryRequest(sender, 'thumbs', galleryId, galleryId, idx, hashAction);
}

function galleryRestoreContent(scrollerDiv) {
	if( ! scrollerDiv.retrieve('original-content', false) ) return;
	scrollerDiv.innerHTML = scrollerDiv.retrieve('original-content');
	updateLinks(scrollerDiv);
	scrollerDiv.eliminate('original-content');
}

function galleryCategMain(sender, galleryId, categoryId, hashAction) {
	var scrollerDiv = $(sender).getParent().getParent().getParent();
	scrollerDiv.eliminate('original-content');
	galleryRequest(sender, 'categMain', galleryId, categoryId, 0, hashAction);
}

function galleryRequest(sender, action, galleryId,  param, jparam, hashAction) {
	pausePolling = true;

	if(hashAction == undefined) hashAction = function() { pausePolling = false; }
	var scrollerDiv = $(sender).getParent().getParent().getParent();
	scrollerDiv.set('morph', { duration: galleryFadeDuration, transition: Fx.Transitions.linear, 
		onComplete: function() {
			scrollerDiv.innerHTML = '';
			var workingDiv = new Element( 'div', { 'class': 'element-loading', 'styles': { 'height': '200px' } } );
			scrollerDiv.set('morph', { duration: 500, transition: Fx.Transitions.linear, 
				onComplete: function() {
					var data = 'sessid=' + sessid;
					data += '&action=' + action;
					data += '&param=' + ( param == undefined ? '' : param );
					var ajaxGallery = getNewXMLHttp();
					ajaxGallery.open("POST", galleryUrl, true);
					ajaxGallery.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	    
					ajaxGallery.onreadystatechange = function() {
						if(ajaxGallery.readyState != 4) return;
						//alert(ajaxGallery.responseText);
						
						scrollerDiv.set('morph', { duration: galleryFadeDuration, transition: Fx.Transitions.linear,
							onComplete: function() {
								workingDiv.dispose();
								scrollerDiv.innerHTML = ajaxGallery.responseText;
								updateLinks(scrollerDiv);
								var initObj = { duration: galleryFadeDuration, transition: Fx.Transitions.linear };
								if(action == 'thumbs' && parseInt(jparam) > 0) {
									initObj.onComplete = function() {
										var obj = scrollerDiv.getElements('.thumbs .image a')[jparam - 1];
										overlaySlideShow(obj);
										hashAction.run();
									}	
								} else {
									initObj.onComplete = hashAction;
								}
								scrollerDiv.set('morph',  initObj );
								scrollerDiv.morph( { opacity: [0, 1] } );
								
								$$('#element-' + currentIdx + ' .scrolling-content')[0].style.height = scrollerDiv.getSize().y + 'px';
								
								$('element-' + currentIdx).set('morph', { duration: galleryFadeDuration, transition: Fx.Transitions.linear } );
								$('element-' + currentIdx).morph( { height: scrollerDiv.getSize().y } );
							} 
						} );
						scrollerDiv.morph( { opacity: [ 1, 0 ] } );
					};
					ajaxGallery.send(data);
				}
			} );
			scrollerDiv.grab(workingDiv);
			scrollerDiv.morph( { opacity: [0, 1] } );
		} 
	} );
	scrollerDiv.morph( { opacity: [1, 0] } );
}

function galleryIsSeries(hash) {
	if(hash.split('/').length == 4) {
		hash = hash.split('/'); 
		hash.pop();
		hash = hash.join('/');
	}
	return gallerySeries.contains(hash);
}

function galleryGetIdFromHash(hash) {
	if(hash.split('/').length == 4) {
		hash = hash.split('/'); 
		hash.pop();
		hash = hash.join('/');
	}
	id = parseInt( galleryIds[ galleries.indexOf( hash ) ] );
	if ( isNaN(id) ) id = 0;
	return id;
}
