// Declare Global Namespace
if(typeof(gallery) == 'undefined') {
	var gallery = {};	
}


//gallery
gallery.curr = 1;
gallery.sec = 1;
gallery.lastSection = 1;
gallery.slideTime = 2;
gallery.firstTime = true;
gallery.startDelay = 0;
gallery.preloadTotal = 0;
gallery.autoStart=true;
gallery.useSlideShow=true;
gallery.iv=null;
gallery.id=0;
gallery.off=false;
gallery.src='';
gallery.atINFO=false;
//external functions
gallery._onReady=null;


$.extend(gallery, {
	init: function() {
		gallery.db = $('#gallery');
		var s =1;
		gallery.getSection(s);
		gallery.setSection(s);
		gallery.loadImage(1);
		
	},
	onReady: function() {
		if(gallery._onReady!=null) gallery._onReady(); //call external
		gallery.preload(1,{start:2});
	},
	secDB: function() {
		return $('#section-'+gallery.sec);
	},
	secTotal: function() {
		return $('#section-'+gallery.sec+' div').size();
	},
	loading: function(on) {
		if(on){
			$("#loading").show();
		}else{
			$("#loading").hide();
		}
		
	},
	//bio
	loadImage: function(id,customCB) {
		
		gallery.loading(true);
		$('#subnav-'+gallery.id).attr('class','in-active');
		gallery.id= id;
		$('#subnav-'+id).attr('class','active');
		
		var useID= id-1;
		var fc = $('#section-'+gallery.sec+' :eq('+useID+')');
		var src = fc.attr('data-src');
		var caption = fc.html();
		
			if(src==gallery.src){
				var randomNumber = Math.floor(Math.random()*10000);
				src=src+'?='+randomNumber;
			}
			gallery.src =src;
			$('#measure')
			    .attr('src',src)
			    .load(function(a){
					
					gallery.loading(false);
					//why not?
					gallery.preloadNext();
					//set stuff
					prefs.imageWidth = $('#measure').width();
					prefs.imageHeight = $('#measure').height();
							//$('#fullscreen').attr('src',src);
							//do we need to watch it on this load??
							$('#fullscreen').attr('src',src);
							
						if(!customCB){
							if(gallery.firstTime){
								gallery.firstTime=false;
								max.resizeSite();
								gallery.onReady();
							}else{
								max.resizeSite();
								gallery.imageReady(useID);
							}
						}else{
							customCB();
							delete customCB;
							customCB=false;
						}
						
						//set cap
						gallery.setCaption();
			 });
		
	},
	
	imageReady: function(divID) {
		max.resizeSite();
		
		if(max.isIE()){
			$("#viewer").show();
		}else{
			$("#viewer").stop().animate({opacity:1}, prefs.gallerySpeed,prefs.easeOut,function(){
				//preload next etc.
				//if(gallery.useSlideShow) gallery.start();
			});
		}
	
	},
	loadSection: function(sec,cb) {
		gallery.curr = 1;
		if(gallery.sec!=sec){
			
			gallery.setSection(sec);
			if(sec==0){
			
				
					max.controlsOFF();
	
					if(max.isiPad() || max.isiPhone()){
						max.initFix("#info",function(){
							var myScroll = new iScroll('scroll-content', {desktopCompatibility:true,scrollbarColor:'#6ED1EE',fadeScrollbar:false});
						});
					}else{
						
						if(prefs.scrollset==false){
								max.initFix("#info",function(){
									prefs.scrollset=true;
									max.initEmail();
									setupScrollbar();
								});
						}else{
							max.initFix("#info",function(){
								max.initEmail();
								resetScrollbar();
							});
						}
					}
					
					
				//500,'easeInOutCubic'
				max._fadeIn("#info");
				//$('#info').fadeIn(500,'easeInOutCubic');
				gallery.atINFO=true;
			}else{
				max.controlsON();
				gallery.loadImage(1,cb);
				gallery.atINFO=false;
			}
			
		
		}
	},
	setCaption: function() {
		var aid = gallery.curr;
		var sec = gallery.sec;
		var useDIV = $('#section-'+sec+' #photo-'+aid);
		var meta1 = useDIV.attr('data-meta1');
		var meta2 = useDIV.attr('data-meta2');
	
		meta1 = (meta1==undefined) ? '': meta1;
		meta2 = (meta2==undefined) ? '': meta2;
		if(meta1 && meta2){
			preA = '<span class="pre">L</span>';
			preB = '<span class="pre">R</span>';
		}else{
			preA="";preB="";
		}
			
		if(prefs.controls==false){
			$("#meta").hide();
		}
		$("#meta #info-1").html(preA+meta1);
		$("#meta #info-2").html(preB+meta2);
		
	},
	setSection: function(sid) {
		$('#nav-'+gallery.sec).attr('class','in-active');
		gallery.sec= sid;
		$('#nav-'+sid).attr('class','active');
		//subnav
		var s = gallery.secTotal();
		var sHTML ='';
		for(a=1;a<=s;a++){
			
			if(a==s){
				sHTML += '<div class="hold last-subnav"><div class="ds1"><a id="subnav-'+a+'" href="javascript:gallery.get('+a+');">'+a+'</a></div></div>';
			}else{
				class2 = (a <=9) ? 'ds2' : 'ds';
				sHTML += '<div class="hold"><div class="ds1"><a id="subnav-'+a+'" href="javascript:gallery.get('+a+');">'+a+'</a></div><div class="'+class2+'">/</div></div>';
				
			}
			
		}
		//CUSTOM
		$("#subnav").html( sHTML);
	},
	setImageID: function(sid) {
		$('#nav-'+gallery.sec).attr('class','in-active');
	},
	getSection: function(sec) {
	
		if(gallery.sec!=sec){
			
			if(gallery.atINFO==true){
				
				max._fadeOut("#info",function(){
					gallery.loadSection(sec,function(){
						if(max.isIE()){
							$("#viewer").show();
						}else{
							$("#viewer").stop().animate({opacity: 1}, prefs.speed,prefs.easeOut);
						}
						
					});
				});
				/*
				$('#info').fadeOut(500,'easeInOutCubic',function(){
						gallery.loadSection(sec,function(){
							$("#viewer").stop().animate({opacity: 1}, prefs.speed,prefs.easeOut);
						});
				});
				*/
	
			}else{
				if(max.isIE()){
					$("#viewer").hide();
					gallery.loadSection(sec,function(){
						$("#viewer").show();
					});
				}else{
					$("#viewer").stop().animate({opacity: 0}, prefs.speed,prefs.easeIn,function(){
						//when it's done loading it will come u
						gallery.loadSection(sec,function(){
							$("#viewer").stop().animate({opacity: 1}, prefs.speed,prefs.easeOut);
						});
					});
				}
			}
		}
	},
	start: function(delay) {
		gallery.off=false;
		var useDelay = (delay) ? delay:0;
		var useTime = gallery.timer() + (useDelay * 1000);
		window.clearTimeout(gallery.iv);
		gallery.iv = window.setTimeout(function() {
		    if(gallery.off==false) gallery.next();
		}, useTime );
	},
	next: function() {
		gallery.curr++;
		if(gallery.curr > gallery.secTotal()){
			gallery.curr=1;
			//can do auto advance sections :  TODO LIST
		}
	
		gallery.nextID = gallery.curr;
		gallery.transition();
	},
	back: function() {
		gallery.curr--;
		if(gallery.curr == 0){
			gallery.curr=gallery.secTotal();
		}
		gallery.nextID = gallery.curr;
		gallery.transition();
	},
	get: function(id) {
		if(gallery.curr!=id){
			
			gallery.curr=id;
			gallery.nextID = gallery.curr;
			gallery.transition();
		}
	},
	transition: function() {
		//need preload checking right here
		if(max.isIE()){
			$("#viewer").hide();
			gallery.loadImage( gallery.curr );
		}else{
			$("#viewer").stop().animate({opacity: 0}, prefs.gallerySpeed,prefs.easeIn,function(){
				//load next image
				gallery.loadImage( gallery.curr );
			});
		}
	},
	timer: function() {
		return  gallery.slideTime * 1000;
	},
	stop: function() {
		gallery.curr=1;
		gallery.off=true;
		window.clearTimeout(gallery.iv);
		$("#viewer").stop();
	},
	preloadNext: function() {
		var options = {start:gallery.curr+1,total:1};
		gallery.preload(gallery.sec,options,true);
	},
	preload: function(sec,options,ask) {
		
		var defaults = {total:2,start:1};
		var options = $.extend(defaults, options);
		var end = options.start+options.total-1;
		var a = 1;
		var c =1;
		$('#section-'+sec+' div').each(function(){
			var src = $(this).attr('data-src');
			if($(this).attr('data-preloaded')!="yes"){
				if((a >= options.start) && (a <= end)){
					$(this).attr('data-preloaded','yes');
					gallery.preloadIMG(src);
				}
			}
			a++;
		});
	},
	preloadIMG: function(src) {
		$('<img />')
		    .attr('src',src)
		    .load(function(){
		        gallery.preloadTotal++;
		 });
	},
	getNow: function() {
	//	alert(gallery.curr);
	}
});
