/*
	Lobertini Mattia
	Script per il sito panighina.it
*/

//Load pagine
function load(x){
    $.ajax({
  		url: "getPages.php", 
		context: document.body,
		dataType: "string",
		type: "POST", 
		data: ( "page=" + x ),   
        success: function( data ){
			$("#pageContent").html(data);
            if( $("#pageContent").height() > 400 ){
		 		$("#content").animate({
					height: $("#pageContent").height()+80+"px"
				}, 750);   	
		    }else{
		 		$("#content").animate({
					height: "500px"
				}, 750); 		    	
		    }
		}      
    });  
}

//Startup
$(document).ready(function(){
	$("#loginB").click(function(){
		try{
			if( document.getElementById("html5Audio") )
				document.getElementById("html5Audio").play();
			else
				$("#container").append("<embed id=\"mpPlayer\" type=\"application/x-shockwave-flash\" src=\"http://www.google.com/reader/ui/3523697345-audio-player.swf\" flashvars=\"audioUrl=intro.mp3&autoPlay=true\" width=\"0\" height=\"0\" quality=\"best\"></embed>");			
			$('body').data('playStatus', 1);
			$("#audioControl").fadeIn(750);
		}catch(e){
			alert(e);
		}
		$("#loginB").fadeOut(750, function(){
			$("#content").animate({
				left: "449px"
			}, 1000);
			$("#container").animate({
				marginTop: "25px"
			}, 1000, "", function(){
				$("#header").animate({
					width: "1024px",
					left: "0px",
					backgroundPosition: "0px 0px"
				}, 1250, "");
				$("#content").animate({
					width: "1024px",
					height: "500px",
					left: "0px"
				}, 1250, "", function(){
					$("#menu").show("slide", {direction: "left"}, 1500);
					$("#page").show(0, function(){
						$("#pageContent").fadeIn(2000);	
			        	if( $("#pageContent").height() > 500 ){
							$("#content").animate({
								height: ($("#pageContent").height()+80+"px")
							}, 750);			        		
			        	}else{
							$("#content").animate({
								height: ("500px")
							}, 750);			        		
			        	}
											
					});
				});
			});
		});	
	});
});

function playPause(){
	try{
		if( document.getElementById("html5Audio") ){
			if( $('body').data('playStatus') == 1 ){
				document.getElementById("html5Audio").pause();
				document.getElementById("html5Audio").currentTime=0;
				$('body').data('playStatus', 0);
				$("#audioControl").removeClass("playerStop");
				$("#audioControl").addClass("playerPlay");
			}else{
				document.getElementById("html5Audio").play();
				$('body').data('playStatus', 1);
				$("#audioControl").removeClass("playerPlay");
				$("#audioControl").addClass("playerStop");		
			}
		}else{
			if( $('body').data('playStatus') == 1 ){
				$("#mpPlayer").remove();
				$('body').data('playStatus', 0);
				$("#audioControl").removeClass("playerStop");
				$("#audioControl").addClass("playerPlay");
			}else{
				$("#container").append("<embed id=\"mpPlayer\" type=\"application/x-shockwave-flash\" src=\"http://www.google.com/reader/ui/3523697345-audio-player.swf\" flashvars=\"audioUrl=intro.mp3&autoPlay=true\" width=\"0\" height=\"0\" quality=\"best\"></embed>");
				$('body').data('playStatus', 1);
				$("#audioControl").removeClass("playerPlay");
				$("#audioControl").addClass("playerStop");		
			}			
		}
	}catch(e){
		alert(e);
	}
}

//Begin photogallery
function photogallery(){
	$("#menu").hide("slide", {direction: "left"}, 1500);
	$("#pageContent").fadeOut(1500, function(){
		$("#page").hide(0, function(){
			$("#photogallery").show("slide", {direction: "up"}, 1500, function(){
				$.ajax({
			  		url: "getPhotos.php", 
					context: document.body,
					dataType: "string",
					type: "POST",  
					data: ( "what=showAlbums" ),
			        success: function( data ){
			        	$("#gallery").fadeOut(750, function(){
		   					$("#exit").html( "<div class=\"exit\" onclick=\"photogallery_exit()\" /></div>");
			        		$("#gallery").html(data);
			        		$("#content").animate({
								height: (($("#gallery").height()+40)+"px")
							}, 750);
							$("#gallery").fadeIn(750);	
			        	});					
			        } 				
				});				
			});
		});
	});	
}

//Show album
function showAlbum(x){
	$.ajax({
		url: "getPhotos.php", 
		context: document.body,
		dataType: "string",
		type: "POST",  
		data: ( "what=album&id=" + x ),
	    success: function( data ){
		   	$("#gallery").fadeOut(750, function(){
		   		$("#exit").html( "<div class=\"exit\" onclick=\"photogallery_exit()\" /></div><div class=\"back\" onclick=\"back()\" /></div>");
				$("#gallery").html(data);
			    $("#content").animate({
					height: (($("#gallery").height()+40)+"px")
				}, 750);				
				$("#gallery").fadeIn(750);	
			});					
		} 				
	});	
}

//Back
function back(){
	$.ajax({
  		url: "getPhotos.php", 
		context: document.body,
		dataType: "string",
		type: "POST",  
		data: ( "what=showAlbums" ),
        success: function( data ){
        	$("#gallery").fadeOut(750, function(){
        		$("#gallery").html(data);
 		   		$("#exit").html( "<div class=\"exit\" onclick=\"photogallery_exit()\" /></div>");       		
        		$("#content").animate({
					height: (($("#gallery").height()+40)+"px")
				}, 750);
				$("#gallery").fadeIn(750);	
        	});					
        } 				
	});		
}

//Show photo
function showPhotos(a, f){
	$.ajax({
  		url: "getPhotos.php", 
		context: document.body,
		dataType: "string",
		type: "POST",  
		data: ( "what=photo&a="+a+"&f="+f ),
        success: function( data ){
        	$("#gallery").fadeOut(250, function(){
        		$("#gallery").html(data);
 		   		$("#exit").html( "<div class=\"exit\" onclick=\"photogallery_exit()\" /></div><div class=\"back\" onclick=\"showAlbum("+a+")\" /></div>");
        		FB.init({appId: '186015164778656', status: true, cookie: true, xfbml: true});
				FB.XFBML.parse();
		        $("#content").animate({
					height: ( $("#gallery").height()+"px" )
				}, 500);
				$("#gallery").fadeIn(250);					
			});					
		}	
	});				
} 				

//End photogallery
function photogallery_exit(){
	$("#photogallery").hide("slide", {direction: "up"}, 1500, function(){
		$("#gallery").html("<div id=\"loader\">Loading photo<br /><img src=\"style/loader.gif\" /></div>");
		$("#page").show(0, function(){
			$("#menu").show("slide", {direction: "left"}, 1500);
			$("#pageContent").fadeIn(1500);
            if( $("#pageContent").height() > 400 ){
		 		$("#content").animate({
					height: $("#pageContent").height()+80+"px"
				}, 750);   	
		    }else{
		 		$("#content").animate({
					height: "500px"
				}, 750); 		    	
		    }
		});
	});
}
