// JavaScript Document
var featuredInt;
var previousItem = null;

var annaSlide = function(toggler, wrapper){
				
		var mySlider = new Fx.Slide(wrapper, {
			duration:1000,
			trasition: Fx. Transitions.Pow.easeOut
		}).hide();
		
		wrapper.setStyle('display', 'block');
		
		toggler.addEvent('click', function(){
			mySlider.toggle('vertical');
		});	
};

var thumbPath = function(thumbnail, thumbPath, gallery, index){
	thumbnail.addEvent('click', function(){
		window.clearInterval(featuredInt);
		if(previousItem != null){
			previousItem.setStyle('display', 'none');
			$$('body').adopt(previousItem);
		}
		$('featured').empty();
		new Element('img', {'src':thumbPath}).inject($('featured'));
		showImgInfo(thumbnail, gallery, index);
	});
};

var showImgInfo = function(thumbnail, gallery, index){
	$('imgDesc').set('html', '<p>'+imgDescs[gallery][index]+'</p>');
	$('imgDesc').setStyle('display', 'block');
}

var popGalleries = function(){
	for(a=0;a<myGalleries.length;a++){
		var category = new Element('dt', {'id':myGalleries[a][0]}).inject($('navigation'), 'bottom');
		var thumbWrapper = new Element('dd', {'id':myGalleries[a][0]+'Thumbs'}).inject(category, 'after');
		
		category.set('text', myGalleries[a][0]);
		
		var parent = this;
		
		for(b=0;b<myGalleries[a][1];b++){
			var thumbCategory = myGalleries[a][0];
			var thumbSource = 'artwork/'+thumbCategory+'/thumbnails/'+thumbCategory+[b+1]+'.jpg';
			var thumbPath = 'artwork/'+thumbCategory+'/'+thumbCategory+[b+1]+'.jpg';
			
			var thumbnail = new Element('img', {'id':thumbCategory+[b+1], 'src':thumbSource}).inject(thumbWrapper, 'bottom');
								
			parent.thumbPath(thumbnail, thumbPath, a, b);
			
		}
		
		annaSlide(category, thumbWrapper);
	}
	
	popLinks();
		var imgDesc = new Element('div', {'id':'imgDesc'}).inject($('navigation'), 'bottom');

};

var popLinks = function(){
	if(otherLinks.length != 0){
		for(d=0;d<otherLinks.length;d++){
			var navLink = new Element('dt', {'id':otherLinks[d]+'Link'}).inject($('navigation'), 'bottom');
			navLink.set('text', otherLinks[d].replace('-', ' & '));
			
			linkFunctions(navLink, otherLinks[d]);
		}
	}
};

var linkFunctions = function(navLink, itemName){
	navLink.addEvent('click', function(){
		$('imgDesc').set('html','');
		$('imgDesc').setStyle('display', 'none');
		if(previousItem != null){
			previousItem.setStyle('display', 'none');
			$$('body').adopt(previousItem);
		}
		$('featured').empty();
		$(itemName).setStyle('display', 'inherit');
		$('featured').adopt($(itemName));
		window.clearInterval(featuredInt);
		previousItem = $(itemName);
	});
}

var featuredImages = function(){
	for(c=0;c<featuredNum;c++){
		var featuredImg = new Element('img',{'id':'featured'+[c+1], 'src':'artwork/featured/featured'+[c+1]+'.jpg'});
		featuredImg.setStyles({
			'opacity':'0',
			'position':'absolute'
		});
		featuredImg.inject($('featured'));
	};

	$('featured1').addEvent('load', $('featured1').fade.bind($('featured1'), [1]));
	
	var cycleNum = 1;
	var cycleTimeout = 0;
	
	featuredInt = window.setInterval(function(){

		$('featured'+cycleNum).fade(0);
		if(cycleNum<featuredNum){
			$('featured'+[cycleNum+1]).fade(1);
			cycleNum++;
		}else{
			cycleNum = 1;
			$('featured'+cycleNum).fade(1);
			cycleTimeout++;
		}
		
		if(cycleTimeout==2) window.clearInterval(featuredInt);
	}, 8000);

};

var featuredSlideShow = function(){
	

	$('featured'+cycleNum).fade(0);
	if(cycleNum<featuredNum){
		$('featured'+[cycleNum+1]).fade(1);
		cycleNum++;
	}else{
		cycleNum = 1;
		$('featured'+cycleNum).fade(1);
		cycleTimeout++;
	}
		
	if(cycleTimeout==1) window.clearInterval(featuredSlideShow());
};

window.addEvent('domready', function(){
	
	featuredImages();
	
	popGalleries();

});