$(function() {    
	Projects.init();
});

var Projects = {
	
	jOEOverlay: null,
	jOESpinner: null,
	jOEScrollableMain: null,
	jOEPlayer: null,
	jOEDetails: null,
	jOEListLinks: null,
	jOEPreview: null,
	
	init : function() {
		var cc = this;
		cc.jOESpinner = $('#Spinner');
		cc.jOEOverlay = $('.overlay');	
		// view
		if ($('body.projects-view').length) {
			cc.view();
		} 
		// or index
		else {
			// navigation
			$('#NavPrimary li.work').addClass('selected');
			cc.fonts();
			cc.scrollable();
			cc.list();
		}	
	},
	fonts : function() {
		Cufon.replace('#Main h1', { fontFamily: 'Helvetica Neue Light' });
		Cufon.replace('.carousel-thumb div.thumb, .carousel-list .item li, .filters li, .view li', { fontFamily: 'Helvetica Neue' });	
	},
	scrollable : function() {
		var cc = this;
		cc.jOEScrollableMain = $("div#Main div.scrollable");
		
		// scrollable required?
		if(cc.jOEScrollableMain.find('div.item').size()>4) {
			cc.jOEScrollableMain.scrollable({ 
		        size: 4,
				clickable: false,
				navi: '#Navi .wrapper'
		    });
		}
		// hide scrollable items
		else {
			cc.jOEScrollableMain.parent().find('.prev').hide();
			cc.jOEScrollableMain.parent().find('.next').hide();
			cc.jOEScrollableMain.parent().find('.navi').hide();
		}
		
		$('a[rel=#overlay]').click(function(){
			var link = this.href;
			cc.jOESpinner.show();
			cc.jOEOverlay.show();
			$.ajax({
				url: link,
				cache: false,
				success: function(html){
					cc.jOESpinner.hide();
					cc.jOEOverlay.html(html);
					// close
					cc.jOEOverlay.find('.close a').click(function(){
						cc.jOEOverlay.html('');
						cc.jOEOverlay.fadeOut('fast', function(){					
							cc.jOEPlayer = null;
						});
						return false;
					})
					cc.view();
				}
			});
			return false;
		})
		
	},
	view : function() {
		var cc = this;
		
		cc.jOEDetails = cc.jOEOverlay.find('div.details div.wrapper');
		
		// fonts
		Cufon.replace('.overlay h2, .overlay div.close', { fontFamily: 'Helvetica Neue Light' });
		Cufon.replace('.overlay div.details p, .overlay div.details li', { fontFamily: 'Helvetica Neue' });
		
		// hide details
		cc.jOEDetails.hide();
		
		// show details
		cc.jOEOverlay.find('div.title h2 a').click(function(){
			if(cc.jOEPlayer!=null) {
				cc.jOEPlayer.unload();
			}
			cc.jOEDetails.find('span').show();
			cc.jOEDetails.slideDown({"duration": 300, "easing": "easeInOutQuart"});
			cc.jOEDetails.mouseenter(function(){
			      cc.jOEDetails.mouseleave(function(){
					cc.jOEDetails.find('span').hide();
				      cc.jOEDetails.slideUp({"duration": 300, "easing": "easeInOutQuart"});
				});
			});
		});
		
		// videos
		flowplayer("a.player", "/flash/flowplayer.commercial-3.1.2.swf", {
			key: '$66aa232e1b5759ef53b', 
			onLoad: function() { 
				cc.jOEPlayer = this;
			},
			plugins:  { 
		        controls: {
				      timeBgColor: '#242424',
				      bufferGradient: 'none',
				      backgroundGradient: 'none',
				      borderRadius: '0',
				      sliderColor: '#000000',
				      buttonColor: '#242424',
				      progressGradient: 'none',
				      sliderGradient: 'none',
				      durationColor: '#ffffff',
				      tooltipColor: '#5F747C',
				      tooltipTextColor: '#ffffff',
				      backgroundColor: '#000000',
				      timeColor: '#b91a20',
				      volumeSliderGradient: 'none',
				      progressColor: '#b91a20',
				      volumeSliderColor: '#000000',
				      bufferColor: '#242425',
				      buttonOverColor: '#070707',
				      height: 24,
				      opacity: 1.0,
					  all:false, 
					  scrubber:true,
					  fullscreen:true,
					  play:true
				}
		    },
			clip: {
				scaling: 'fit',
				accelerated: false,
				bufferLength: 5,
			    autoBuffering: true
			},
			api: true
		});
		
		// scrollable
		var overlayScroller = cc.jOEOverlay.find('div.scrollable');
		var scroller = overlayScroller.scrollable({ 
	        size: 1,
			clickable: false,
			api: true,
			navi: '#NaviOverview .wrapper',
			onBeforeSeek: function() {
				if(cc.jOEPlayer!=null) {
					cc.jOEPlayer.unload();
				}
			}
	    });	
		if(overlayScroller.find('div.item').size()<2) {
			overlayScroller.parent().find('.prev').hide();
			overlayScroller.parent().find('.next').hide();
			overlayScroller.parent().find('.navi').hide();
		}
	},
	list : function() {
		var cc = this;
		cc.jOEListLinks = $('.carousel-list li a');
		cc.jOEPreview = $('#Preview');
		cc.jOEListLinks.each(function(){
			$(this).mouseenter(function(){
				var position = $(this).position();
				var image = $(this).find('img');
				cc.jOEPreview.css('top',position.top);
				cc.jOEPreview.css('left',(position.left-102));
				cc.jOEPreview.html(image.clone());
				cc.jOEPreview.show();
				$(this).mouseleave(function(){
					cc.jOEPreview.hide();
				});
			});
			
		});
		
	}
}