
function deSpace(str) {
	return str.gsub(' ', '-').camelize();
}
var HpSlides={
	list: $A(),
	load: function() {
		if(Prototype.Browser.IE) {
			$(document.body).addClassName('ie')
		}
		HpSettings.cover=$$(HpSettings.coverSelector).first();
		HpSettings.viewElem=$(HpSettings.viewElement);
		HpSettings.dataElem=$(HpSettings.dataElement);
		var zzz = $('hpboxdata').select('li').map(function(em){
			return eval('new SlideDefinitions.'+deSpace(em.className)+'(em)')
		});
	},
	start: function() {
		var _llength=HpSlides.list.length;
		var _elems=HpSlides.list.map(function(hp, qi){return hp.getElement().setStyle('z-index:'+(_llength-qi))});
		HpSlides.currentElement=HpSlides.list.first();
		HpSlides.list.first().open();
	},
	getNext: function() {
		var currentIndex=HpSlides.list.indexOf(HpSlides.currentElement);
		if((currentIndex)<HpSlides.list.length) {
			return HpSlides.list[currentIndex+1];
		} else {
			return;
		}
	}
}
var HpSlide=Class.create({
	initialize: function(elm) {
		HpSlides.list.push(this);
		this.loadFromElement(elm);
	},
	slideDuration: function() {return 4},
	setLink: function() {
	},
	loadFromElement: function(elm) {
	},
	showElems: function(){},
	getElement: function(){
		if(typeof(this.elem)=='undefined') { this.elem=this.generateElement()}
		return this.elem;
	},
	generateElement: function() {
		return new Element('div').update('text');
	},
	placeElem: function() {
		if(!this.elem.descendantOf(HpSettings.viewElem)) {
			HpSettings.viewElem.insert(this.elem)
		}
	},
	advance: function() {
		var _next=HpSlides.getNext();
		if(_next) {
			HpSlides.currentElement=_next;
			this.close(_next.open.bind(_next))
		}
		_next=null;
	},
	open: function() {},
	close: function(cb) {
		this.elem.fade({
			afterFinish: cb,
			duration: 0.8
		})
	},
	blankSlide: function(){return new Element('div', {className: 'slide'})}
});
var SlideDefinitions={};
SlideDefinitions.youtube=Class.create(HpSlide, {
	loadFromElement: function(elm) {
		this.imgElem = elm.down('.feature');
		this.link = elm.down('a');
	},
	open: function() {
		this.setLink();
	},
	showFirstSlide: function() {
		HpSlides.list.first().elem.appear({duration:1});
	},
	hideElems: function() {

	},
	showElems: function() {
		this.imag.setStyle('display:block;position:absolute;top:0;left:0;');
	},
	advance: function() {
		var _next=HpSlides.list.first(); //CYCLES BACK TO INTRO SLIDE !!!WARNING!!! --this may be confusing down the line --- if we choose to go there
		HpSlides.currentElement=_next;
		this.close(_next.open.bind(_next))
		_next=null;
	},
	setLink: function() {
		if(this.link.hasClassName('lightview')) {
			HpSettings.cover.addClassName('lightview');
			HpSettings.cover.href=this.link.href;
			HpSettings.cover.title=this.link.textContent.strip() + ":: :: topclose:true,autosize:true,width:1000,height:600";
		}
	},
	slideDuration: function() {
		return 12;
	},
	generateElement: function() {
		if(typeof(this.elem)=='undefined') {
			this.elem=this.blankSlide();
			this.imag=this.imgElem.clone(true);
			this.elem.insert(this.imag);
		}
		this.placeElem();
		return this.elem;
	}
})
SlideDefinitions.landing=Class.create(HpSlide, {
	loadFromElement: function(elm) {
		this.title="Welcome to Millennium Promise"
		this.shown=false;
		this.imgElem=new Image().setStyle('opacity:0.0');
		this.imgElem.onload=this.imLoaded.bind(this);
		this.imgElem.src=elm.down('img').src;
		this.bgCover=new Element('div', {className: 'fullstretch'}).setStyle('background-color:#cf1313;');
	},
	imLoaded: function(evt) {
		this.imgElem.appear({duration: 1, from: 0.0, to: 1.0, afterFinish: this.hideScreen.bind(this)});
	},
	showAllElems: function() {
		HpSlides.list.each(function(zzq){zzq.showElems.call(zzq)})
	},
	hideScreen: function() {
		this.showAllElems();
		this.bgCover.setStyle('background-color:green')
	},
	generateElement: function() {
		this.elem=this.blankSlide();
		this.elem.insert(this.imgElem);
		this.elem.insert(this.bgCover);
		this.placeElem();
		return this.elem;
	},
	open: function() {
		if(!this.shown) {
			this.advance.bind(this).delay(10);
		}
		this.shown=true;
	}
});
SlideDefinitions.photo=Class.create(HpSlide, {
	loadFromElement: function(elm) {
		this.title=elm.title;
		this.captionBg=new Element('div', {className:'promiseBg'}).setStyle('opacity:0.8');
		this.aPromiseText=new Element('div', {className:'aPromiseText', style:'display:none'});
		this.caption=elm.down('.caption').setStyle('display:none');
		this.logo=elm.down('.boxLogo').setStyle('display:none');
		this.photo=elm.down('.feature');
		this.button=elm.down('a');
	},
	setLink: function() {
		if(typeof(this.button)!=='undefined') {
			HpSettings.cover.href=this.button.href;
			HpSettings.cover.title=String(this.button.textContent || '').strip()
		}
	},
	generateElement: function() {
		if(typeof(this.elem)=='undefined') {
			this.elem=new Element('div', {className: 'slide'});
			this.elem.insert(this.captionBg);
			this.elem.insert(this.aPromiseText);
			this.elem.insert(this.caption);
			this.elem.insert(this.logo);
			this.elem.insert(this.photo);
		}
		this.placeElem();
		return this.elem;
	},
	showElems: function() {
		this.photo.setStyle("display:block")
	},
	open: function() {
		this.setLink();
		this.darkenBg();
		this.showPromiseText.bind(this).delay(.25*HpSettings.heartbeat);
		this.showCaption.bind(this).delay(1*HpSettings.heartbeat);
		this.showLogo.bind(this).delay(2*HpSettings.heartbeat);
		this.advance.bind(this).delay(this.slideDuration()*HpSettings.heartbeat);
	},
	darkenBg: function() {
		new Effect.Appear(this.captionBg, {duration:.25})
	},
	showPromiseText: function() {
		this.aPromiseText.appear({
			duration: 0.5
		})
	},
	showCaption: function() {
		this.caption.appear({
			duration: 0.8
		});
	},
	showLogo: function() {
		this.logo.appear({
			duration: 0.8
		});
	},
	moveableLogo: function(evt) {
		$$('.cover').first().setStyle('display:none');
		new Draggable(evt.element, {
			onEnd: function(evt){
				var xxx=evt.element.positionedOffset();
			}
		})
	}
})
