
;(function( $ ) {
//

var Position = {
	TOP_LEFT: 'TOP_LEFT',
	TOP_CENTER: 'TOP_CENTER',
	TOP_RIGHT: 'TOP_RIGHT',
	BOTTOM_LEFT: 'BOTTOM_LEFT',
	BOTTOM_CENTER: 'BOTTOM_CENTER',
	BOTTOM_RIGHT: 'BOTTOM_RIGHT',
	CENTER_LEFT: 'CENTER_LEFT',
	CENTER: 'CENTER',
	CENTER_RIGHT: 'CENTER_RIGHT'
};

function img_onload( event ) {
	
	var img = $( this );
	var parent = img.parent();
	var ratio = img.width() / img.height();
	
	if ( ( parent.width() / parent.height() ) > ratio ) {
		
		img.css({
			height: 'auto',
			width: '100%',
			right: 0,
			left: 0
		});
		
		switch ( event.data.position )
		{
			case Position.TOP_LEFT:
			case Position.TOP_CENTER:
			case Position.TOP_RIGHT:
				img.css( 'top', 0 );
				break;
			
			case Position.BOTTOM_LEFT:
			case Position.BOTTOM_CENTER:
			case Position.BOTTOM_RIGHT:
				img.css( 'bottom', 0 );
				break;
			
			case Position.CENTER_LEFT:
			case Position.CENTER:
			case Position.CENTER_RIGHT:
			default:
				img.css( 'top', ( parent.height() - img.height() ) / 2 )
		}
	}else {
		
		img.css({
			height: '100%',
			width: 'auto',
			top: 0,
			bottom: 0
		});
		
		switch ( event.data.position )
		{
			case Position.TOP_LEFT:
			case Position.BOTTOM_LEFT:
			case Position.CENTER_LEFT:
				img.css( 'left', 0 );
				break;
			
			case Position.TOP_RIGHT:
			case Position.BOTTOM_RIGHT:
			case Position.CENTER_RIGHT:
				img.css( 'right', 0 );
				break;
			
			case Position.TOP_CENTER:
			case Position.BOTTOM_CENTER:
			case Position.CENTER:
			default:
				img.css( 'left', ( parent.width() - img.width() ) / 2 )
		}
	}
	
	setTimeout(function() {
		parent.find( '.overlay' ).animate( {opacity: 0}, 450 );
	}, 1000)
}

$.fn.bgImage = function( options ) {
	
	var img = new Image();
	$( this ).append( img );
	$( img ).bind( 'load', options, img_onload ).attr( 'src', options.url );
	
	$( window ).bind( 'resize', options, function( event ) {
		img_onload.call( img, event )
	})

}

$.bgImage = Position;

})( jQuery );


/*
//scroller
	var node = $( '#scroller' );
	var scroller = node.find( '.handler' );
	var dif = 0;
	var $pos = 0;
	var w = scroller.width();
	
	var u = node.innerWidth() / 100;
	
	node.bind( 'mousedown', function( e ) {
		$pos = node.offset().left + ( w/2 );
		$max = node.innerWidth() - w;
		
		var z = Math.min( Math.max( 0, e.clientX - $pos ), $max );
		scroller.animate({left: z}, 200);
		z = z/u;
		air.SoundMixer.soundTransform = new air.SoundTransform(z/100, 0);
		//move( e );
		$( document ).mousemove( move ).bind( 'mouseup', function() {
			$( document ).unbind( 'mousemove', move ).unbind( 'mouseup', arguments.callee );
		});
	});
	
	var d = scroller.get(0);
	function move( e ) {
		e.preventDefault();
		var z = Math.min( Math.max( 0, e.clientX - $pos ), $max );
		//d.style.left = z + 'px';
		scroller.stop().css({left: z});
		z = z/u;
		air.SoundMixer.soundTransform = new air.SoundTransform(z/100, 0);
	}
*/
