var SlideShow = new Class({	
	initialize:function(container,slides,nav,interval){

		this.container = $(container);
		this.container.set('tween', {duration: 'short'});
		
		this.slides = $$(slides);
		this.nav = $$(nav);

		this.interval = interval;
		this.currentIndex = 0;
		this.addEvents();

		// For a fade effect
		this.slides.set('opacity',0);
		
		// for a vertical slide effect
		//this.slides.setStyle('margin-top','440px');
		
		this.show(this.currentIndex);
		this.play();
	},	
	addEvents:function(){
		/*this.container.addEvent('mouseenter', function() {
			this.pause();
		}.bind(this));
		
		this.container.addEvent('mouseleave', function() {
			this.play();
		}.bind(this));	*/
		var ss = this;
		this.nav.each(function(el,index){
			
			el.addEvent('click',function(e){
				e.stop();
				ss.pause();
				ss.show(index);
			});
			
			el.addEvent('mouseenter',function(e){
				ss.pause();
				ss.show(index);
			});
			
			el.addEvent('mouseleave',function(e){
				ss.play();
			});
			
		}.bind(this));
	},	
	pause:function(){
		this.isPlaying = false;
		//this.playButton.set('class','play');
		$clear(this.timer);
	},
	play:function(){
		this.isPlaying = true;
		//this.playButton.set('class','pause');
		this.timer = this.showNext.periodical(this.interval, this);
	},
	toggle:function(){
		if(this.isPlaying){
			this.pause();
		}
		else{
			this.play();
		}
	},
	show:function(index){
		this.nav[this.currentIndex].removeClass('active');
		this.slides[this.currentIndex].fade('out');
		//this.slides[this.currentIndex].tween('margin-top','440px');
	
		this.currentIndex = index;
		this.nav[this.currentIndex].addClass('active');
		this.slides[this.currentIndex].fade('in');
		//this.slides[this.currentIndex].tween('margin-top','0px');
		
	},	
	showNext:function(){
		if(this.currentIndex < this.slides.length-1 )
		{
			this.show(this.currentIndex+1);
		}
		else
		{
			this.show(0);
		}
	}
});

	
	Gallery = new Class({
	
		initialize:function(container){	
			//console.log("Gallery.initialize");
			
			this.currentPage = 1;
			this.container = $(container);
			this.nav = $('vids-nav');
			this.slider = this.container.getElement('.slider');
			this.items = this.container.getElements('.vid');		
			this.buttonBack = this.nav.getElement('a[href$=back]');
			this.buttonNext = this.nav.getElement('a[href$=next]');
			
			this.viewable = 12;
			this.viewable_height = this.container.getStyle('height').toInt();
			this.pageCount = Math.ceil(this.items.length/this.viewable);
			
			this.slider.set('tween', {duration: 'short'});
			
			this.addEvents();
			this.show(this.currentPage);
			
	
		},
		
		addEvents:function(){
			//console.log("Gallery.addEvents");
			this.buttonBack.addEvent('click', function(e) {
				e.stop();
				this.showPrev();
			}.bind(this));	
			
			this.buttonNext.addEvent('click', function(e) {
				e.stop();
				this.showNext();
			}.bind(this));	
			
		},
		
		show:function(page){
	
			//console.log("Gallery.show(",page,")");
			this.currentPage = page;
			this.slider.tween('margin-top', - ((this.currentPage-1)*this.viewable_height));
		
			if(this.currentPage >= this.pageCount)
			{
				this.buttonNext.setOpacity(0.2);
				this.buttonNext.setStyle('cursor','default');
			}
			else
			{
				this.buttonNext.setOpacity(1);
				this.buttonNext.setStyle('cursor','pointer');
			}
			if(this.currentPage <=1 )
			{
				this.buttonBack.setOpacity(0.2);
				this.buttonBack.setStyle('cursor','default');
			}
			else
			{
				this.buttonBack.setOpacity(1);
				this.buttonBack.setStyle('cursor','pointer');
			}
		},
		
		showNext:function(){
			//console.log("Gallery.showNext");
			if(this.currentPage < this.pageCount )
			{
				this.show(this.currentPage+1);
			}
		},
		
		showPrev:function(){
			//console.log("Gallery.showPrev");
			if(this.currentPage > 1 )
			{
				this.show(this.currentPage-1);
			}
		}
	});
	// EOF Gallery Class
	
	var Site = {
		
		slidesToJson: function(slides){
			var obj = [],i=0;
			slides.each(function(a){
				img = a.getElement('img');
				obj[i++] = {image:img.get('src'),href:a.get('href'),title:img.get('alt')};
			});
			return JSON.encode(obj);
		},
		slidesToFlashvars: function(slides){
			var obj = {},i=0;
			slides.each(function(a){
				img = a.getElement('img');
				obj['image'+i] = img.get('src');
				obj['href'+i] = a.get('href');
				obj['title'+i] =  img.get('alt');
				i++;
			});
			return obj;
		},
		loadHomeFlash: function()
		{
			var params = {
				quality: "high",
				scale: "noscale",
				wmode: "transparent",
				allowscriptaccess: "always",
				bgcolor: "#F9F9F9"
			};
			
			//var flashvars = Site.slidesToFlashvars($$('#home_feature li a'));
			var flashvars =  {
				urls:$$('#home_feature li a').get('href').join(','),
				titles:$$('#home_feature li a img').get('alt').join(','),
				images:$$('#home_feature li a img').get('src').join(',')
			}
			//console.log(flashvars);
	
			/*
			var flashvars =  {
				   //contentXML: escape($('home_feature').clone().toString())
				  //contentXML: escape($('home_feature').get('html').replace( /<(img[^>]*)>/gi , "<$1 />"))
				  contentJSON: Site.slidesToJson($$('#home_feature li a'))
				   
			};
			//console.log(flashvars);
			*/
			var attributes = {id:"flashmovie"};
			
			swfobject.embedSWF("swf/home.swf", "home_feature", "960", "540", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
		}
		
	};




