$.extend(Site, {
	Faces: {
	
		/* Attributes */
		config: {
			'persons': ['eric', 'fabian', 'lievin', 'nico', 'pascale'],
			'nbpositions': 8,
			'heightimage': 480
		},
		currentImage: null,
		
		
		/* Methods */
		
		angle: function (cursor) {
			var angle =  Math.atan2(0-cursor.y,0-cursor.x)*(180 / Math.PI);
			if(angle < 0) { angle += 360;}
			return angle;
		},
		
		distance: function(cursor) { 
		    return Math.sqrt( Math.pow((0 - cursor.y),2) + Math.pow((0 - cursor.x),2) ); 
		},
	
		getCenterFace: function() {
			var offsetFace = $("#face").offset();
			offsetFace.left += $("#face").width()/2;
			offsetFace.top += $("#face").height()/2;
			return offsetFace;
		},
		
		init: function(config) {
			if (typeof config!='undefined') {
				$.extend(this.config, config);
			}
			if (typeof this.config.person=='undefined') {
				 this.config.person = this.config.persons[Math.floor(Math.random()*this.config.persons.length)];
			}
			/* 
				To force a face, use this...
				this.config['person'] = 'fabian';
			*/
			if (Shin.isMobile()) { 
				Site.Faces.preloadImage(Shin.href(';img/site/faces/'+this.config['person']+'/mixed.jpg'));
				var offset = null;
				var cursor = new Array();
				var centerFace = this.getCenterFace();
				centerFace.top -= 45;
				$('body').mousemove(function(e) {
					cursor['x'] = (e.pageX - $('#face').offset().left - centerFace.left);
					cursor['y'] = (e.pageY - $('#face').offset().top - centerFace.top);
					Site.Faces.manageImage(cursor, 'mousemove');
				});
				$('a, #face').bind('mousedown',function() {
					Site.Faces.showImage(10)
				});
			} else {
				$('#face').css('background-image','url('+Shin.href(';img/site/faces/'+this.config['person']+'/mobile.jpg')+')');
				$('#face , #faces p span.'+this.config['person']).fadeIn();
			}
			
		},
		
		manageImage: function(cursor, event) {
			if (event == 'mousemove') {
				if (this.distance(cursor)<100) {	
					if(Site.Faces.currentImage != 9 && Site.Faces.currentImage != 10) {
						Site.Faces.showImage(9);
					}
				} else {
					var angleByPosition = 360/this.config['nbpositions'];
					var numImage = Math.ceil(( this.angle(cursor) + (angleByPosition/2))/angleByPosition);
					numImage = (numImage>this.config['nbpositions'])?1:numImage;
					if (Site.Faces.currentImage != numImage) {
						Site.Faces.showImage(numImage);						
					}
				}
			}
		},
		
		preloadImage: function(src) {
            var img = new Image();
			img.onload = function () {
				$('#face').css('background-image','url('+Shin.href(';img/site/faces/'+Site.Faces.config['person']+'/mixed.jpg')+')');
				$('#face , #faces p span.'+Site.Faces.config['person']).fadeIn();
			};
            img.src = src;
        },
		
		showImage: function(num) {
			Site.Faces.currentImage = num;
			$('#face').css({backgroundPosition: '0px '+(this.config['heightimage']*num)+'px'}, 1000);
		}
	
	}
});
