var IosGallery = {};

IosGallery.modal = {};
IosGallery.controlBar = {};
IosGallery.thumbs = {};
IosGallery.imgData = new Array();
IosGallery.slider = {}
IosGallery.stageWidth = 0;
IosGallery.stageHeight = 0;
IosGallery.imgCount = 0;
IosGallery.ios = false;
IosGallery.os = '';
IosGallery.homeUrl = '';


IosGallery.init = function( urlPrefix ){
	
	document.getElementsByTagName('html')[0].style.background = 'black';
	document.body.style.display = 'none';
	
	
	urlPrefix += 'client/ios/';
	LazyLoad.js( 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', function () {
		
		$( '#layout' ).hide();
		$( 'head' ).find( 'link' ).remove();
		
		console.debug( 'jquery loaded' );

		var required = [
		urlPrefix + "js/Callbacks.js",
		urlPrefix + "js/BrowserDetect.js",
		urlPrefix + "js/iscroll.min.js",
		urlPrefix + "js/gallery/IosThumbs.js",
		urlPrefix + "js/gallery/IosControlBar.js",
		urlPrefix + "js/gallery/IosSlider.js",
		urlPrefix + "js/gallery/IosImage.js",
		urlPrefix + "js/gallery/IosDetails.js"
		];

		LazyLoad.js( required, function () {
			
			console.debug( 'dependants loaded' );
			$( 'head' ).find( 'link' ).remove();
			LazyLoad.css( urlPrefix + 'css/ios_gallery.css' );
			IosGallery.build();
		});

	});

}


IosGallery.build = function(){
	
	$('body').show();
	IosGallery.os = BrowserDetect.OS;
	
	if( IosGallery.os == 'iPhone/iPod' || IosGallery.os == 'iPad' ){
		IosGallery.ios = true;	
	}

	
	this.homeUrl = $( '#menu a').first().attr( 'href' );
	
	this.imgCount = $( '#thumbs a' ).length;
	this.thumbs = new IosThumbs( $( '#thumbs' ) );	

	this.modal = $( '<div id="modal"></div>' );
	this.modal.hide();
	
	IosDetails.init();

	$( 'body' ).append( this.modal );
	$( '#layout' ).remove();
	
	$( 'head' ).append( '<meta name="viewport" content="initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>' );
	this.getData();

	this.slider = new IosSlider( this.imgData );
	this.controlBar = new IosControlBar();	

	$( window ).load(function(){
		IosGallery.resize();
	});
	
	
	
	if( this.ios ){
		setInterval( IosGallery.checkResize, 500 );
	}else{

		$( window ).resize( function(){
			IosGallery.resize();
		});
	}
	
	this.resize();
	setTimeout( IosGallery.resize, 1000 );
}


IosGallery.checkResize = function(){

	if( IosGallery.stageWidth != $( window ).width() ||
		IosGallery.stageHeight != $( window ).height() 
	){
		IosGallery.resize();
	}
}


IosGallery.resize = function(){	
	IosGallery.stageWidth = $( window ).width();
	IosGallery.stageHeight = $( window ).height();
	Callbacks.fireCallback( 'stageResize', {} );
	this.modal.width( this.stageWidth ).height( this.stageHeight );
}

IosGallery.getData = function(){

	$( '#thumbs img' ).each( function(){

		var obj = {};
		obj.largeImage = $(this).attr('src').replace( '_thumb', '' );
		obj.caption = this.alt;
		obj.title = this.title;

		IosGallery.imgData.push( obj );
	});
}
