/**************************************************************

	Script	: BSI web scripts
	Date	: Feb 2011
	Author	: Max Johnson
	Desc	: Custom scripts for the Blue Shift Inc website
	License	: Open Source MIT License
	Dep		: Mootools version 1.3

**************************************************************/

var bsi_imageLoader = new Class({

	Implements: [Events, Options, Chain],

	options: {
		loaderImg	: './images/spinner.gif',
		spoofImg	: './images/spacer.gif', // image to use as spoof replacement for img
		elements	: [], // optionally feed specific elements instead of using selector
		morph		: true, // animate or not
		width		: '50px', // start width
		height		: '50px', // start height
		maxWidth	: '50px', // start width
		maxHeight	: '50px', // start height
		onStart		: $empty,
		onFinish	: $empty
	},
	initialize: function(selector, options){
		this.setOptions(options);
		//console.log(selector);
		
		this.selector = selector;
		this.elements = $$(selector) || this.options.elements;
		
		if( !this.elements.length ) return false;// no elements?
		
		this.elements.each( function(el){
			if( !el.complete ) {
				this.preload.attempt(el, this);
			}
		},this);
	},
	preload: function(el){
		var oSrc = el.get('src');
		var maxHeight = this.options.maxHeight.toInt();
		var maxWidth = this.options.maxWidth.toInt();
		
		var imageLoader = new Asset.image(oSrc, {
			onload : function(el) {
				if( imageLoader.width > maxWidth || imageLoader.height > maxHeight )
				{
					var scale = (imageLoader.width > imageLoader.height)? (maxWidth/imageLoader.width) : (maxHeight/imageLoader.height);
					imageLoader.width = Math.floor(imageLoader.width * scale);
					imageLoader.height = Math.floor(imageLoader.height * scale);
				}
				
				// resize to preloader's specs and swap back source image //
				if( this.options.morph ) {
					imageLoader.morpher = new Fx.Morph(el);
					imageLoader.morpher.start({
						'width' : imageLoader.width
					}).chain( function(){
						imageLoader.morpher.start({
							'height' : imageLoader.height,
							'opacity' : 0
						});
					}).chain( function(){
						el.set('src', oSrc);
						el.removeClass('loadImage');
						imageLoader.morpher.start({'opacity' : 1});
					});
				} else {
					el.set('src', oSrc);
					el.removeClass('loadImage');
					el.setStyles({
						'width' : imageLoader.width,
						'height' : imageLoader.height
					});
				}
				
				// preloader suicide //
				imageLoader.dispose();
			}.pass(el, this)
		});
		el.set('src', this.options.spoofImg);
		el.addClass('loadImage');
		el.setStyles( {
			//'background-image': ('url(' + this.options.loaderImg + ')'),
			'width' : this.options.width,
			'height' : this.options.height
		});		
	}
});

var BSI_ImageRing = new Class({
	Implements: [Options],
	options: {
		container: 'slider-container',
		speed: 0.005
	},
	initialize: function(options) {
		//console.log(options);
		this.count = 0;
		var op = this.options;
		op.radiusX = $(op.container).getSize().x/2;
		
		$(op.container).getElements('li').each( function(el, i, group){
			el.store('angle', (i * ( Math.PI * 2 ) / group.length));
			
			el.setStyles({
					display: 'block',
					float: 'none',
					position: 'absolute'
			});
		});
		
		this.animate.periodical(60, this);
	},
	animate: function(){
		
		//if(this.count >100)return;
		var op = this.options;
		var rX = op.radiusX;
		var cX = op.radiusX;
		$(op.container).getElements('li').each( function(el, i, group){
			//console.log(el,i,group);
			var numberOfElements =  group.length;
			a = el.retrieve('angle');
			
			posX = ( Math.sin( this.count * op.speed + a ) ) * cX + rX;
			co = ( Math.cos( this.count * op.speed + a ) );
			
			//if(i==1){console.log(cosine);}
			el.setStyles({
				'left': posX+'px',
				visibility: (co>0)?'visible':'hidden'
			});
			if( co > 0 ){
				el.getElement('img').setStyles({
				   'width': co*75,
				   'opacity': co
				});
			}
		},this);
		this.count++;
	}
});
				
				
window.addEvent('domready', function() {
									 //console.log($$('.content'));
	//$$('.content').each( function(el){
	//	el.fade('hide');
	//});		 
	//if( $(document.body).hasClass('splash') ){
	//	$('mainMenu').fade('hide');
	//}
	
	if ($('slider-container')){ var imgRing = new BSI_ImageRing();}

	if( typeof(XtLightbox) !== "undefined" ){
		new XtLightbox('.content .screenshot', {
			adaptorOptions: {
				Image: {
					lightboxCompat: false
				}
			},
			rendererOptions: {
				positionText: "Image {x} of {total}. Use arrows to navigate, Esc to close.",
				size: {
					x: (window.getSize().x*0.9),
					y: (window.getSize().y*0.9)
				}
			}
		});
	}
	
	
	$$('.content').each( function(el){
		el.set('tween', {duration: 4000, transition:'quad:inOut'}).tween('opacity', 0, 100);
	});
});

window.addEvent('load', function() {
						//console.log($(document.body));		 
	if( $(document.body).hasClass('splash') ){
		$('mainMenu').set('tween', {duration: 8000, transition:'quad:inOut'}).tween('opacity', 0, 100);
	}

});

