

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

//set basic prefs
prefs.heroImage = '#image-1';
prefs.sec = false;
prefs.assetsTotal=1; //image right side
prefs.assetsLoadedTotal=0;
prefs.fullscreen=true;
prefs.locked=false;
prefs.scrollset=false;
prefs.speed=580;
prefs.gallerySpeed=420;
prefs.easing='easeInOutQuart';
prefs.easeIn='easeInCubic';
prefs.easeOut='easeOutCubic';
prefs.imageWidth='';
prefs.imageHeight='';
prefs.controls = true;
prefs.offset=180;
prefs.contSpeed=400;
prefs.controlStop=0;

//setup max functions
$.extend(max, {
	assetsLoaded: function() {
		prefs.assetsLoadedTotal++;
		//max.resizeSite();
		if(prefs.assetsLoadedTotal==prefs.assetsTotal){
			//we're done
			max._fadeOut("#loading");
			
		}
	},
	// handles user agent checking
	isiPad: function() {
	return navigator.userAgent.match(/iPad/i) != null;
	},

	isiPhone: function() {
	return navigator.userAgent.match(/iPhone/i) != null;
	},
	//
	isOpen: function() {
		return prefs.fullscreen;
	},
	swipe:function(){
		
		var swipedElement = document.getElementById(triggerElementID);
		if ( swipeDirection == 'left' ) {
				if(!gallery.atINFO){
					gallery.back();
				}
		} else if ( swipeDirection == 'right' ) {
			
				if(!gallery.atINFO){
					gallery.next();
				}
		} else if ( swipeDirection == 'up' ) {
	
		} else if ( swipeDirection == 'down' ) {

		}
	},
	isIE: function() {
		return ($.browser.msie && $.browser.version < 9) ? true : false;
	},
	_fadeIn : function(el,callback){
		if($.browser.msie && $.browser.version < 9){
			$(el).show();
			//use callback
			if(callback) callback();
		}else{
			$(el).fadeIn(500,'easeInOutCubic',callback);
		}
		
	},
	_fadeOut : function(el,callback){
		if($.browser.msie && $.browser.version < 9){
			$(el).hide();
			//use callback
			if(callback) callback();
		}else{
			$(el).fadeOut(500,'easeInOutCubic',callback);
		}
	},
	winWidth : function(){
		return $(window).width();
	},
	winHeight : function(){
		return $(window).height();
	},
	loadImage : function(src){
		$('<img />')
		    .attr('src',src)
		    .load(function(){
		 });
	},
	initEmail : function(){
		$('#info-contact a').addClass('mail');
	},
	delayFade : function(el,mseconds){
		var runMe  =function(){
			//$(el).fadeOut();
			max._fadeOut(el);
		};
		setTimeout( runMe, mseconds );
	},

	initFix : function(el,fn){
		$(el).animate({opacity:0},0);
		$(el).show();
			//
			fn();
			//
		$(el).hide();
		$(el).animate({opacity:1},0);
	},
	controlsOFF:function(){
		$("#next").hide();
		$("#back").hide();
		//$('#arrow').attr('src','_img/arrow-bar-up.gif');
		$("#controls").stop().animate({bottom: -80},prefs.contSpeed,'easeInOutCubic');
	},
	controlsON:function(){
		$("#next").show();
		$("#back").show();
		//$('#arrow').attr('src','_img/arrow-bar.gif');
		$("#controls").stop().animate({bottom: prefs.controlStop},prefs.contSpeed,'easeInOutCubic');
	},
	toggleControls:function(){
	
		
		if(prefs.controls){
			$('#arrow').attr('src','_img/arrow-bar-up.gif');
			prefs.controls=false;
			//hide
			prefs.offset=140;
			prefs.offsetX=300;
			max.resizeSite(true);
			prefs.controlStop=-54;
			$("#controls").stop().animate({bottom: -54},prefs.contSpeed,'easeInOutCubic',function(){
				
			});
			max._fadeOut("#meta");
			max._fadeOut("#arrows");
			max._fadeOut("#subnav");
			//$("#meta").fadeOut();
		//	$("#arrows").fadeOut();
			//$("#subnav").fadeOut();
	//		//max.resizeSite();
			
			
		}else{
			$('#arrow').attr('src','_img/arrow-bar.gif');
			//$("#arrow-flip").stop().animate({rotate: '+=180deg'},300,'easeInOutCubic');
			$("#controls").stop().animate({bottom: 0},prefs.contSpeed,'easeInOutCubic',function(){
				
			});
			prefs.controls=true;
			prefs.offset=180;
			prefs.offsetX=0;
			max.resizeSite(true);
				max._fadeIn("#meta");
				max._fadeIn("#arrows");
				max._fadeIn("#subnav");
			//$("#meta").fadeIn();
			//$("#arrows").fadeIn();
			//$("#subnav").fadeIn();
			prefs.controlStop=0;
			//max.resizeSite();
		}
	},
	showoff : function(src){
				//getting this from gallery code using div "measure"
				var imageWidth= prefs.imageWidth;
				var imageHeight= prefs.imageHeight;
		
				winWidth=$('#viewer').width();
				winHeight=$('#viewer').height()-300;
				
			
				var picHeight = imageHeight / imageWidth;
				var picWidth = imageWidth / imageHeight;
				
				var tw = 0;
				var th = 0;
				//sizing the image
				if ((winHeight / winWidth) < picHeight) {
					tw = winWidth;
					th = picHeight*winWidth;
				} else {
					tw = picWidth*winHeight;
					th = winHeight;
				};	
				
				$("#fullscreen").stop().animate({width: tw,height:th},prefs.contSpeed,'easeInOutCubic');
			
		
	},
	resizeSite  : function(useIt){
		if(!prefs.locked){
			 	 	var animateIt = (useIt) ? true : false;
					//define divs
					var holder = '#holder';
					var full = '#fullscreen'; //img
					var intro = '#intro';
					
					//getting this from gallery code using div "measure"
					var imageWidth= prefs.imageWidth;
					var imageHeight= prefs.imageHeight;
					
					//var winWidth= max.winWidth();
					//var winHeight=max.winHeight()-prefs.offset;
					
					winWidth=$('#viewer').width();
					winHeight=$('#viewer').height()-prefs.offset;
					
				
					
					
					if(winHeight > prefs.imageHeight){
						winHeight=prefs.imageHeight;
						left =  (max.winWidth()/2)  - ($(full).width()/2);
						topPX =  (max.winHeight()/2)  - ($(full).height()/2);
						
					}else{
						
						left =  (max.winWidth()/2)  - ($(full).width()/2) ;
						topPX = '80px';
					}
					
					var picHeight = imageHeight / imageWidth;
					var picWidth = imageWidth / imageHeight;
					
					
					
					//info
					if(winHeight < 600){
						$('#info').css('top',topPX);
						$('#info').css('margin-top','0px');
					}else{
						$('#info').css('top','50%');
						$('#info').css('margin-top','-270px');
					}
					
					if(max.winWidth() < 968){
						$('#meta').hide();
						$('#info').css('left','38px');
						$('#info').css('margin-left','0px');
					}else{
						$('#meta').show();
						$('#info').css('left','50%');
						$('#info').css('margin-left','-450px');
					}
				
					
					

					if(!animateIt){
						$(full).css("height",winHeight);
						$(full).css("width",picWidth*winHeight);
						$('#viewer').css('top',topPX);
						$('#viewer').css('left',left);
					}else{	
						var w = (picWidth*winHeight);
						$(full).animate({height:winHeight,width:w},prefs.contSpeed,'easeInOutCubic');
						left =  (max.winWidth()/2)  - (w/2) ;
						
						$('#viewer').animate({top:topPX,left:left},prefs.contSpeed,'easeInOutCubic');
					}
					
					$('#arrow-back').css('left',left-100);
					$('#arrow-back').css('top','50%');
					//$('#arrow-back').show();
					
					$('#arrow-next').css('left',left+$(full).width()+5);
					$('#arrow-next').css('top','50%');
					//$('#arrow-next').show();
					
					$('#next').css('height',$('#viewer').height()-180+'px');
					$('#back').css('height',$('#viewer').height()-180+'px');


				

		}
	}
});



max.onReady = function() {	
	


		
	
	max.resizeSite();
	
	$("#next").click(function(e){
		if(!gallery.atINFO){
			gallery.next();
		}
	});
	$('#next').mouseenter(function(){
			if(!gallery.atINFO){
				max._fadeIn("#arrow-next");
				//$('#arrow-next').fadeIn();
				max.delayFade('#arrow-next',3000);
			}
	});
	$('#next').mouseleave(function(){
		if(!gallery.atINFO){
			max._fadeOut("#arrow-next");
			//$('#arrow-next').fadeOut();
		}
	});
	$("#back").click(function(e){
		if(!gallery.atINFO){
			gallery.back();
		}
	});
	$('#back').mouseenter(function(){
		if(!gallery.atINFO){
			max._fadeIn("#arrow-back");
			//$('#arrow-back').fadeIn();
			max.delayFade('#arrow-back',3000);
		}
	});
	$('#back').mouseleave(function(){
		if(!gallery.atINFO){
			max._fadeOut("#arrow-back");
			//$('#arrow-back').fadeOut();
		}
	});
	
	$('body').keyup(function(e) {	
		
		if(e.keyCode == 37) {
			if(!gallery.atINFO){
				gallery.back();
			}
		}
		if(e.keyCode == 39) {
			if(!gallery.atINFO){
				gallery.next();
			}
		}
	});
	
	
	
	//setup nav
	var t = $("#gallery .gallery").size();
	var sHTML ='';

	for(z=1;z<=t;z++){
	
		secName = $("#section-"+z).attr('data-title');
		
		sHTML +='<a id="nav-'+z+'" href="javascript:gallery.getSection('+z+');" >'+secName+'</a>';
	}
	sHTML +='<a id="nav-0" href="javascript:gallery.getSection(0);" >info</a>';
	sHTML +='<a href="http://blog.josemandojana.com/" target="_blank">blog</a>';
	$('#navigation').html(sHTML);
	

	//gallery will also call our max.assetsLoaded();
	gallery.init();
	//this is the first image gallery ready
	gallery._onReady = function(){
		max.resizeSite();
		max.assetsLoaded();
	};
	
	
};

max.onResize = function() {	
	max.resizeSite();
};

/*******************************
       Namespaced Functions
 *******************************/


$(document).ready(function(){
		
	//setupScrollbar();
	max.onReady();
});
$(window).resize(max.onResize);
