var allImagesLoaded = false;

function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

function isiPad(){
	return navigator.userAgent.match(/iPad/i) != null;
}

function isAndroid(){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.indexOf("android") > -1 ||  ua.indexOf("mobile") > -1) {
		return true;
	}
	else{
		return false;
	}
}

function calculateGaleryHeight(){
	return $(window).height() - 20;
}

function calculateGaleryWidth(){
	var width =  (4/3) * calculateGaleryHeight();
	
	if((width + 140 + 30) >  $(window).width()){
		width = $(window).width() - (140 + 30);
	}
	
	return width;
}

function orientationChanged(){	
	setPageClass();
	
	var myGalleria = Galleria.get(0);
	if(myGalleria !== null){	
		var index= myGalleria.getIndex();
		
		if(window.orientation == -90 || window.orientation == 90){
			initialImages = initialImagesSmall;
		}
		else{
			initialImages = initialImagesSmallVertical;	
		}
		
		reloadImages();	
		updateGalleriaFSSize();
		
		if(isiPad() || isiPhone()){
			setTimeout ( 'Galleria.get(0).show('+index+')', 200 );
		}
		
	}
	
}

function updateGalleriaFSSize(){
	var myGalleria = Galleria.get(0);
	
	if(myGalleria !== null){	
		
		if(isiPad() || isiPhone()){
			myGalleria.$('container').width($(window).width());
			myGalleria.$('container').height($(window).height());
		}
		else{
			myGalleria.$('container').width("100%");
			myGalleria.$('container').height("100%");
		}
		
		
		myGalleria.rescale();
	}
	
}

function reloadImages(){
	var myGalleria = Galleria.get(0);
	
	if(myGalleria !== null){
		var initialLength = myGalleria.getDataLength();
		
		for(var i=0; i < initialImages.length; i++){
			myGalleria.push(initialImages[i]);
		}
		
		myGalleria.splice(0,initialLength);	
		
	}
}


function pageResized(){
	var myGalleria = Galleria.get(0);
	var newHeight = calculateGaleryHeight();
	var newWidth = calculateGaleryWidth();
	
	$("#gallery").height(newHeight);
	$("#gallery").width(newWidth);
	myGalleria.$('container').width(newWidth);
	myGalleria.$('container').height(newHeight);
	myGalleria.rescale();
	
	$("#header_top").css("right",newWidth+10);
	$("#header_bottom").css("right",newWidth+10);
}

function pageBlogResized(){
	var newWidth = calculateGaleryWidth();
	
	$("#blog").width(newWidth);
	
	$("#header_top").css("right",newWidth+10);
	$("#header_bottom").css("right",newWidth+10);
}

function setPageClass(){
	// remove all existing classes
	$("body").removeClass("ipad");
	$("body").removeClass("iphone");
	$("body").removeClass("ipad_horizontal");
	$("body").removeClass("ipad_vertical");
	
	if(isiPad()){
		$("body").addClass("ipad");
		if(window.orientation == -90 || window.orientation == 90){
			$("body").addClass("ipad_horizontal");
		}
		else{
			$("body").addClass("ipad_vertical");
		}
	}
	else if(isiPhone()){
		$("body").addClass("iphone");
		if(window.orientation == -90 || window.orientation == 90){
			$("body").addClass("iphone_horizontal");
		}
		else{
			$("body").addClass("iphone_vertical");
		}
	}
	else if(isAndroid()){
		$("body").addClass("android");
	}
}


function setPageImages(){
	if(isiPad()){
		if(window.orientation == -90 || window.orientation == 90){
			initialImages = initialImagesSmall;
		}
		else{
			initialImages = initialImagesSmallVertical;
		}
	}
	else if(isiPhone()){
		if(window.orientation == -90 || window.orientation == 90){
			initialImages = initialImagesSmall;
		}
		else{
			initialImages = initialImagesSmallVertical;
		}
	}
	else if(isAndroid()){
		initialImages = initialImagesSmallVertical;
	}

}

function galleriaInit(){ 
	setPageClass();
	setPageImages();
	
	// set initial sizes depending on device
	if(isiPad() || isiPhone() || isAndroid()){
		$("#header_top_ios").removeClass("hidden");
	}
	else{
		var newWidth = calculateGaleryWidth();
		$("#gallery").width(newWidth);
	
		// set header position
		$("#header_top").css("right",newWidth+10);
		$("#header_bottom").css("right",newWidth+10);
		$("#header_bottom").removeClass("hidden");
		$("#header_top").removeClass("hidden");
	}
	
	// init galleria
	$('#gallery').galleria({
         showInfo:false,
         imageCrop: true,
         thumbnails: false,
         dataSource: initialImages,
         clicknext:true,
         queue:false,
         debug: false,
         fullscreenDoubleTap:false
   	});
	
	// load the rest of the images after the first set has loaded
	var myGalleria = Galleria.get(0);
	if(myGalleria !== null){
		
		// attach keyboard functions
		myGalleria.attachKeyboard({
		    left: myGalleria.prev, // applies the native prev() function
		    right: myGalleria.next		    
		});
	}

	// init for resize if its not ipad, iphone, android
	if(!isiPad() && !isiPhone() && !isAndroid()){
		$(window).resize(pageResized);
	}
	// listen for orientation change if it is an ipad, iphone, 
	else if(isiPad() || isiPhone()){
	    window.onorientationchange = function(){  
	    	orientationChanged();
	    };
	}
}

function blogInit(){
	if(isiPad()){
		$("body").addClass("ipad");
		$("#header_top_ios").removeClass("hidden");
	}
	else if(isiPhone()){
		$("body").addClass("iphone");
		$("#header_top_ios").removeClass("hidden");
	}
	else if(isAndroid()){
		$("body").addClass("iphone");
		$("#header_top_ios").removeClass("hidden");
	}
	else{
		var newWidth = calculateGaleryWidth();
		$("#blog").width(newWidth);
		
		$("#header_top").css("right",newWidth+10);
		$("#header_bottom").css("right",newWidth+10);
		
		$("#header_top").removeClass("hidden");
		$("#header_bottom").removeClass("hidden");
		
		// init for resize
		$(window).resize(pageBlogResized);
	}
	
	

}
