
var HomeContent = function(){};
	
	HomeContent.prototype = {
	
		init:function(){
			
			this.body   = $("#siteContent");
			this.fwElements = $("#featuredWork").find('.item');
			
			this.attachEvents();
			
			Framework.initIconMovments($(".iconic"), {marginTop:50}, {marginTop:0});
			
			return this;	
		}
		
		,attachEvents:function() {
			
			this.body.delegate("mouseover", {
				 selector:"[key]"
				,handler:this.loadQuote
				,context:this
				,realTimeEvent:{
					 type:"mouseout"
					,handler:this.removeQuote
					,context:this
				}	
			});
			
			this.FWModule = new FeaturedWork({
				 elements:this.fwElements.get()
				,incr:450
			});
			
			Framework.setInterval( function(){ 
					this.FWModule.next(); 
			}.Context(this), 8000);
			
			$("#featuredWork_leftScroll").addEvent({
				 type:'click'
				,handler:this.toggleWork
				,context:this
				,data:'previous'
			});
			
			$("#featuredWork_rightScroll").addEvent({
				 type:'click'
				,handler:this.toggleWork
				,context:this
				,data:'next'
			});
			
			var links = $("#featuredWork a[identifier]");
			
				links.addEvent({type:'click', handler:this.goToFolio, context:this});
			
			Framework.logEvents('home', {
				 element:links
				,type:'click'
				,fn:this.goToFolio	
			});
				
			Framework.logEvents('home', {
				 element:this.body
				,type:'mouseover'
				,fn:this.loadQuote	
			});
			
			Framework.logEvents('home', {
				 element:$("#featuredWork_leftScroll")
				,type:'click'
				,handler:this.toggleWork
			});
			
			Framework.logEvents('home', {
				 element:$("#featuredWork_rightScroll")
				,type:'click'
				,fn:this.toggleWork
			});
			
		}
		
		,toggleWork:function(e, $el, el, dir) {
			
		
			Framework.clearInterval();	
			
			this.FWModule[dir]();
			
			Framework.setInterval(function(){
				this.FWModule.next();
			}.Context(this), 8000);	
		}
		
		,goToFolio:function(e, $el){
			
			e.preventDefault();
			
			var id = $el.attr('identifier');
			var type = $el.attr('subsection');
			
			var data = {initImage:id, folioType:type};
			
			Framework.fireComponentMethod("header", "handleFWMap", data);
		}
		
		,loadQuote:function(e, $el) {
			
			this.origTop = this.origTop || parseFloat($el.css('marginTop'));
			
			Framework.animate($el, {marginTop:50});

			this.waiting = setTimeout(function(){
				
				var key  = $el.attr('key');
				var next = $el.attr('next');
	
				this.currentElement = $el;
				
				if(!this.quote) {
					
					var elem = $([
						'<div class="quotehover none fontw">'
							,'<div class="top bg"></div>'
							,'<div class="content bg">'
								,'<div class="title bold fontdb font120"></div>'
								,'<div class="quote"></div>'
							,'</div>'
							,'<div class="tail bg"></div>'
						,'</div>'
					].join('')).appendTo('#siteContainer');
					
					this.quote = elem;
				}
				
				var h  = $('#siteHeader').height();
					h += this.origTop + 20;	
					
				var total = $("#siteContainer").height();
				
				var pos = $el.position();
				var w	= $el.width() / 2;
				
				this.quote.css('bottom',total - h)
					.removeClass("none");
					
				this.quote.find('.quote')
				    	  .html('Loading Quote...');
				    	  
				this.quote.find('.title')
				    	  .html('');
				  
				Framework.animate(this.quote, {left:pos.left + w});
				
				Framework.loadRemoting('quote', {
					 url:'common/php/pages/home/quotes.data.php'
					,onload:this.renderQuote
					,context:this
					,data:{key:key, next:next}
				});
				
			}.Context(this), 300);
		}
		
		,renderQuote:function( data ){
			
			setTimeout(function(){
			
				var el = this.currentElement;
					el.attr('next', data.next);
					
				this.quote
					.removeClass('none')
					.find('.title')
					.html(data.title);
				this.quote
					.find('.quote')
					.html(data.quote)
					
			}.Context(this), 500);
		}
		
		,removeQuote:function(e, $el) {
			
			if(Framework.remotes['quote']) {
				Framework.remotes['quote'].abort();	
			}
			
			if(this.waiting) {
				clearTimeout(this.waiting);
				this.waiting = null;	
			}
			
			if(this.quote) {
			  Framework.animate(this.quote, {left:-1000});
			}
			
			Framework.animate($el, {marginTop:this.origTop});
		}
	};
	
FeaturedWork = function(args){
	
	this.incr  = args.incr;
	this.elements = args.elements;
	this.total = this.elements.length - 1;
};
	
FeaturedWork.prototype = {
			
		 index:0
		,timer:null
		
		,next:function(e, $el){
		
			this.index++;
			this.handle();
		}
		
		,previous:function(){
			
			this.index--;
			this.handle();
		}
		
		,handle:function() {
			
			if(this.index > this.total) {
				this.index = 0;	
			}
			
			if(this.index < 0) {
				this.index = this.total;
			}
			
			if(this.timer) {
				clearTimeout( this.timer );
				this.timer = null;
			}
			
			var i   = this.index;
			var els = this.elements;
			var o   = els[i - 1] || els[i + 1];
			var inc = this.incr;

			var oEl  = $(o);
			var oimg = oEl.find('.imageBody');
			var parent = $("#featuredWork_hidden");
			
			var nEl  = $(els[i]);
			var nimg = nEl.find(".imageBody");
			
			this.timer = setTimeout(function(){
				
				Framework.animate(oimg, {height:0}, function(){
					
					oimg.find('img')
						.css('display', 'none');
					
					oimg.colorShift("highlightTolb", null, "borderColor");
					oEl.colorShift("highlightTolb", null, "borderColor");
					
					Framework.animate(parent, {left:(inc * i) * -1}, function(){

						var img = nimg.find('img');
							img.css('display', 'block');
						
						Framework.animate(nimg, {height:img.height()})
						nEl.colorShift("lbToHighlight", null, "borderColor");
						nimg.colorShift("lbToHighlight", null, "borderColor");
						
					});
				});
			}, 400);	
		}
};
