Ext.onReady(function(){
	Home.init({ interval: 8000, animation: 0.5, autoStart: true });
});


Home=(function(){	return {
	init: function(conf){
		conf=conf||{};
		this.dur = conf.animation?conf.animation:0.3; 
		this.interval = conf.interval&&(conf.interval>1999)?conf.interval:3000
		this.img = Ext.get('wrapper').child('.home-banner').down('.home-banner-illustration')
		this.txt = Ext.get('wrapper').child('.home-banner').down('.home-banner-text')
		this.nav = Ext.get('wrapper').child('.home-banner-nav'); this.navList = this.nav.child('ul');
		
		this.moreBtn = this.txt.next('.btn-large').dom;
		this.imgLnk = this.img.child('a').dom;	this.imgY = this.img.getY(); this.imgX = this.txt.getX()+this.txt.getWidth()+16; 
		this.n = 0;	
		this.banners = [
			{
				projectID: 24,
				title: 'Golden Spider Award for Xwerx',
				url: 'www.primaryscience.ie',
				img: 'primaryscience', // image folder name
				text: 'has won the Golden Spider Award for The Best Education, Research &amp; Training website.' 
			},
			{
				projectID: 31,
				title: 'Increased Hire Rate',
				url: 'www.skip.ie',
				img: 'skipie',
				text: 'A redesign to Thorntons Recycling\'s skip.ie website helped to increase hire rate by almost 800% in the first six weeks following launch.' 
			},
			{
				projectID: 28,
				title: 'Communicate Your Message',
				url: 'www.ecocem.ie',
				img: 'ecocem',
				text: 'A strong new website for Ireland\'s leading provider of green cement is playing a central role in communicating Ecocem\'s message to a diverse set of customers.' 
			},
			{
				projectID: 21,
				title: 'Signs of Spring',
				url: 'www.greenwave.ie',
				img: 'greenwave',
				text: 'This award winning site allows primary schools from all over Ireland to join in a mass experiment to officially track the arrival of Spring to these shores.' 
			}	
			//{
			//	projectID: 29,
			//	title: 'Secure Financial Data',
			//	url: 'www.alpro-fund.com',
			//	img: 'alprofunds',
			//	text: 'An international group of fund investors now have a user-centred, secure space to view monthly updates on a broad range of financial data.' 
			//}	
		]
		this.showNav();
		if (conf.autoStart) this.run()
	}
	
	,hideNav: function(){ this.nav.fadeOut() }
	,showNav: function(){
		var list=new Array(), len=this.banners.length;
		for (var i=0; i<len; i++) list.push('<li'+(!i?' class="active"':'')+'><a href="#" onclick="Home.show('+i+');return false"></a></a></li>');
		if (list.length>1){ this.navList.update(list.join(' ')); this.nav.fadeIn() }
	}
	,selectNavDot: function(x){ this.navList.down('li:nth-child('+x+')').radioClass('active') }
	
	,run: function(interval){
		var self=this; 
		clearInterval(this.repeater); 
		interval=interval?interval:this.interval; interval+=(this.dur*4*1000); // increase interval with duration of all animations
		this.repeater = setInterval(function(){ self.changeBanner() }, interval) 
	}
	,stop: function(){ clearInterval(this.repeater) }
	,prev: function(){ this.stop(); if (!this.img.hasActiveFx()) this.changeBanner('prev') }
	,next: function(){ this.stop(); if (!this.img.hasActiveFx()) this.changeBanner('next') }
	,show: function(x){ this.stop(); if (!this.img.hasActiveFx()) this.changeBanner(x) }
	
	,getBanner: function(n){
		var bnr=this.banners[n]; if (!bnr) return ;
		return '<h1>'+bnr.title+'</h1><h2><a href="http://'+bnr.url+'">'+bnr.url+'</a>&nbsp;&nbsp;'+bnr.text+'</h2>';
	}
	
	,changeBanner: function(x){
		var banlen=this.banners.length-1; // x must be lesser than the length
		if(typeof x=='string'){if(x=='next')this.n=(this.n<banlen?++this.n:0); if(x=='prev')this.n=(this.n>0?--this.n:banlen)} 
		else if (typeof x=='number') this.n = x<banlen+1?x:0;
		else this.n=(this.n<banlen?++this.n:0);
		var banner=this.banners[this.n];		
		this.selectNavDot(this.n+1);
		this.imgX = this.txt.getX()+this.txt.getWidth()+16;
		
		var self = this, dur  = this.dur, imgX = this.imgX, imgY = this.imgY
		if (!banner.imgObj){
			banner.imgObj=new Image(); 
			banner.imgObj.src='img/projects/'+banner.img+'/spotlight.jpg';
		}
		this.txt.fadeOut({ duration: dur, scope: this, callback: function(){}})
		this.img.applyStyles('position:absolute;top:'+imgY+'px;left:'+imgX+'px;');
		this.img.shift({ x: document.body.offsetWidth, duration: dur, easing:'easeIn', opacity: 0, scope: this, callback: function(){
			this.moreBtn.href=this.imgLnk.href='projects,'+banner.projectID+'.htm';
			this.img.child('img').dom.onload=function(){ 
				self.img.shift({ x: imgX, duration: dur*1.5, easing:'easeOut', opacity: 1, scope: self, callback: function(){
					this.img.applyStyles('position:relative;top:0px;left:0px;');
				}})
			}
			if (banner.imgObj) this.img.child('img').dom.src=banner.imgObj.src;
			this.txt.update(this.getBanner(this.n), false, function(){ self.txt.pause(dur).fadeIn({ duration: dur }) });
		}})
	}
	
}})()
