/**
 * Core
 *
 * @author Tobias Schibler <tobias@tujo.no>
 * @package JavaScript
 * @copyright 2011 tujo ANS
 */


function Core(){}
Core.prototype.init = function(){
	var target = this;

	this.tools = new Tools();
	this.logo = new Logo();
	this.fullscreen = new Fullscreen();
	this.fadeSlider = new FadeSlider();
	this.easyGallery = new EasyGallery();

};
Core.prototype.prepareImagesList = function(){
	for(var i=0,l=images.length;i<l;++i){
		if(images[i].src)
			images[i].src = './images/'+images[i].src;
	}
};
Core.prototype.refresh = function(){
	this.tools.refresh();
	this.logo.refresh();
	this.fullscreen.refresh();
	this.fadeSlider.setLiWidth(function(liEl){
		return $('img', liEl).width();
	});
	this.fadeSlider.refresh();
};
Core.prototype.startup = function(){
	var target = this;

//	alert('onorientationchange' in window);
	if(!('onorientationchange' in window))
	$(window).resize(function(){
		target.refresh();
//		alert('resize');
	});

//		if($.browser.SafariMobile){
//			alert($.screen().w);
//			alert(self.innerWidth+' || '+document.documentElement.clientWidth+' || '+document.body.clientWidth);
//		}

//	alert($('body').length);

//	$('body').bind('ongesturechange', function(e){
//		target.refresh();
//		alert('refresh');
//	});
//	$('body').bind('onorientationchange', function(e){
//		alert('refresh');
//		target.refresh();
//	});
//	$('onorientationchange' in window?window:document).bind('onorientationchange', function(e){
//	$('body').get(0).addEventListener('onOrientationChange', function(e){
//		alert('refresh');
//		target.refresh();
//	}, false);
//	window.addEventListener('onorientationchange', function(e){
//	$(window).bind('orientationchange', function(e){

	if('onorientationchange' in window)
		$(window).get(0).addEventListener('orientationchange', function(e){
//			if($.browser.SafariMobile){
//				var orientation = window.orientation;
//				alert(orientation);
//				alert(self.innerWidth+' || '+document.documentElement.clientWidth+' || '+document.body.clientWidth);
//			}
			target.refresh();
		}, false);


//Value  |  Description
//-------+-------------------------------------------------------------------------------
// 0     |  Portrait orientation. This is the default value.
//-90    |  Landscape orientation with the screen turned clockwise.
// 90    |  Landscape orientation with the screen turned counterclockwise.
// 180   |  Portrait orientation with the screen turned upside down. This value is currently not supported on iPhone.

//	window.onorientationchange = function(e){
//		var rotation = e.rotation;
//		alert(rotation);
//		target.refresh();
//	};

//	Pad.ongesturechange = function(e){
//		var rotation = e.rotation;
//		if (rotation < -180.0) rotation += 360.0 ;
//		if (rotation >  180.0) rotation -= 360.0 ;
//		Command = {"action": "spin", "speed": rotation / 180.0} ;
//	};

	this.prepareImagesList();

	this.fullscreen.init();
	this.fadeSlider.init($('div.images'));
	this.easyGallery.init($('div.images'));
	this.easyGallery.setImages(images, function(data){
		if(!data.src)
			return null;
		return {
			src: data.src
		};
	});
	this.fadeSlider.onNavigate(function(res){
		setTimeout(function(){
			$('div.tools a.prev').show().fadeTo('slow', res.isFirst?0.2:1);
			$('div.tools a.next').show().fadeTo('slow', res.isLast?0.2:1);
		}, 100);
	});
	this.easyGallery.onLoad(function(img, i, l){
//		fire('current: '+i+' total: '+l)
//		fire(img);

//		target.easyGallery.setLiWidth(function(){
//		});

		/** make the images to fullscreen */
		target.fullscreen.startup(img);

		/** run this when the image is complete */
		target.fadeSlider.setLiWidth(function(liEl){
			return $('img', liEl).width();
		});

		/** makes the size correct, so we get the items side by side */
		target.fadeSlider.refresh();

//		if(i===l)
//			$('div.tools a.next').trigger('click');
	});
	this.fadeSlider.onSlide(function(left){
//		target.easyGallery.setCurrentByLeft(left);
	});

	target.step1();

	setTimeout(function(){
		target.step2();
	}, 2e3);
	setTimeout(function(){
		target.step3();
	}, 2e3+400);
	setTimeout(function(){
		target.step4();
	}, 4e3);
};
Core.prototype.step1 = function(){
	var target = this;
	this.logo.init();
};
Core.prototype.step2 = function(){
	var target = this;
	$('div.images').fadeIn({duration:2000, easing: 'easeOutQuad'});
};
Core.prototype.step3 = function(){
	var target = this;
	this.tools.init();
};
Core.prototype.step4 = function(){
	var target = this;
	$('div.footer').fadeIn('fast');
	this.isDone = true;
};


function Tools(){}
Tools.prototype.init = function(){
	var target = this;
	this.assign();
	this.refresh();
	this.showTools();
//	if($.browser.SafariMobile)
//		setTimeout(function(){
//			$('div.tools').fadeOut('slow');
//		}, 3e3);
};
Tools.prototype.assign = function(){
	var target = this;
	$('div.tools a.prev').click(function(){
		_core.fadeSlider.prev();
//		_core.easyGallery.prev(function(elem){
//			_core.fadeSlider.toElem(elem);
//		});
	}).block().hide();//.fadeTo('fast', 0.2)
	$('div.tools a.next').click(function(){
		_core.fadeSlider.next();
//		_core.easyGallery.next(function(elem){
//			_core.fadeSlider.toElem(elem);
//		});
	}).block();

	$('ul', $('div.images')).bind('touchstart', function(e){
//		alert('touchstart');
	}).bind('touchend', function(e){
//		alert('touchend');
	}).bind('touchmove', function(e){
//		alert(e);
	});

//	$(document).swipe({
//		swipeStatus: function(event, phase, direction, distance){
//
//			if(phase === 'move' && (direction === 'left' || direction === 'right')){
//			}
//			else
//			if(phase === 'end'){
//				switch(direction){
//					case 'left':
//						$('div.tools a.next').trigger('click');
//						break;
//					case 'right':
//						$('div.tools a.prev').trigger('click');
//						break;
//				}
//			}
//		},
//		triggerOnTouchEnd: true,
//		allowPageScroll: 'vertical',
//		threshold: 200
//
//	});

	$(document).bind('keydown', function(e){
//		fire(e.keyCode);
		switch(e.keyCode){
			case 37://left
				$('div.tools a.prev').trigger('click');
				break;
			case 39://right
				$('div.tools a.next').trigger('click');
				break;
		}
	});
};
Tools.prototype.refresh = function(){
	var screen = $.screen();
	var top = screen.centerY($('div.tools'));
	if(_core.isDone)
		$('div.tools').show().stop().animate({'top': top}, 500);
	else
		$('div.tools').css({'top': top});
};
Tools.prototype.showTools = function(){
	$('div.tools').fadeIn('fast');
};

function Logo(){}
Logo.prototype.init = function(){
	var target = this;
	this.assign();
	$('div.logo img:first').load(function(){
		target.showLogo();
	});
	var iv = setInterval(function(){
		if($('div.logo img:first').prop('width') > 0){
			target.showLogo();
			setTimeout(function(){
				clearInterval(iv);
			}, 1e3);
		}
	}, 100);
};
Logo.prototype.assign = function(){
	var target = this;
};
Logo.prototype.refresh = function(){
	var screen = $.screen();

//	if(!this.offset){
//		this.offset = parseFloat(top)+parseFloat($('div.logo').css('marginTop'));
//		$('div.logo').css('marginTop', 0);
//		-this.offset
//	}

	this.logoWidth = this.logoWidth||$('div.logo img:first').prop('width');
	this.logoRatio = this.logoRatio||($('div.logo').width()/$('div.logo').height());
	if(this.logoWidth > 0 && (screen.w < this.logoWidth || this.logoWidth > screen.w)){
		var totalWidth = screen.w/1.5;
		$('div.logo img').css({'width': totalWidth, 'height': totalWidth/this.logoRatio});
	}
	else if(this.logoWidth > 0) {
		$('div.logo img').css({'width': this.logoWidth, 'height': this.logoWidth/this.logoRatio});
	}

	var top = screen.centerY($('div.logo'));
	var left = screen.centerX($('div.logo'));
	if(left<0)
		left = 0;
	if(top<0)
		top = 0;

	if(_core.isDone)
		$('div.logo').show().stop().animate({'top': top, 'left': left}, 500);
	else
		$('div.logo').css({'top': top, 'left': left});

};
Logo.prototype.showLogo = function(){
	this.refresh();
	$('div.logo').fadeIn({duration:2000, easing: 'easeOutQuad'});

//	$('div.logo').show();
//	$('div.logo img.light').hide().fadeIn({duration:2000, easing: 'easeOutQuad'});
//	$('div.logo img.dark').show().fadeOut({duration:2000, easing: 'easeOutQuad'});
};


/** Start App */
var _core = new Core();
_core.init();

//window.addEventListener('load', setOrientation, false);
//window.addEventListener('orientationchange', setOrientation, false);
//
//function setOrientation(){
//	alert('refresh');
//	var orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';
//	var cl = document.body.className;
//	cl = cl.replace(/portrait|landscape/, orient);
//	document.body.className = cl;
//}

//$('body').get(0).addEventListener('onOrientationChange', function(e){
//	alert('refresh');
////	target.refresh();
//}, false);

$(document).ready( function(){
	if($.browser.SafariMobile)
		setTimeout(function(){
			window.scrollTo(0, 1)
			_core.startup();
		}, 100);
	else
		_core.startup();
});


