var baseUrl = getURLBase();
function getURLBase() {
	var dominio = "http://" + document.domain + "/";	
	return dominio;
}

function filtraPhoto(iniziale) {
	$(".inizialeSelezionata").removeClass("inizialeSelezionata")
		.addClass("inizialeEsistente");	
	//$("#tab" + cat).addClass("active");  		
	$("#"+iniziale).addClass("inizialeSelezionata")
    .removeClass("inizialeEsistente");	
	var myElement = $('#pannelloFoto li');
	var classRequest=iniziale;
	var i =0;
  myElement.each(function() { 
		var myIcon = $('#icona'+i);   	 	
  	//alert(myIcon.attr('class'));	
		if( myIcon.attr('class') == classRequest ) {
			$('#icona'+i + '.'+iniziale).fadeIn(800);						
		} else {
			if (iniziale == null) {
				$('#icona'+i).fadeIn(500);
			} else {
				$('#icona'+i).fadeOut(500);
			}
		}
		i++;
  });  
  $('#portrait').fadeOut(500);	  
  $('#bio').fadeOut(500);	  
	var oldIniziale = 1;

}


function iconAppear(val) {
  $('#icona'+ val).fadeIn(100*val);  	
}

function backToSection() {
  $('#subMenu').animate({
    width: '100%',
    opacity:'1',
    height: '400px'
  }, 1500, function() {	
  });		
  $('#portrait').fadeOut(500);	  
  $('#bio').fadeOut(500);	   	
}

function showDetail(img, titolo, testo) {
  $('#bio').hide();  	
	$('#portrait').hide();	
	$('#portrait').html('<img src="'+img+'"/>');
	$('#portrait').append ('<a href="javascript:backToSection()">Torna alle immagini di scelta </a>');
	$('html, body').animate({"scrollTop": $("#portrait").scrollTop() + 330});

	/*$('#portrait img').animate({
	    marginTop: $(window).scrollTop()+'px'
	  }, 300, function() {	
	  });*/
	$('#bio').html('<h2>' + titolo+ '</h2>' + testo);
  $('#portrait').fadeIn(200);	
  $('#bio').fadeIn(600);	  
  $('#subMenu').animate({
    width: '100%',
    opacity:'1',
    height: '1px'
  }, 800, function() {	
  });	
}

function OLDshowResults(cat) {
	url = baseUrl + "phpajax/paginaVoto.php?"; // The server-side script
	url = url + "cat=" + cat + "&results=true";
	$(document).ready(function(){
	     $("#classifica").load(url);
	     
	});

	/*$("div#elenco").scrollTo( 'li:eq(15)', 520, {axis:'x'} );//only scroll on this axis (can be x, y, xy or yx)*/
  $('#popContainer').html('');
  $('#popContainer').hide();
	
}


function registraVoto(rifFile,cat,imgUrl,nomeImg) {
	url = baseUrl + "phpajax/registraVoto.php?rifFile=" + rifFile; // The server-side script
	url = url + "&cat=" + cat;

	$.get(url, function(data) {
		//alert(data);
		if (data == "true") {
			pubblicaVoto(imgUrl,nomeImg,cat);
			$('#esitoVoto').html("Gentile utente il tuo voto &egrave; stato registrato");
		} else  {
			$('#esitoVoto').html("Oggi hai gi&agrave; votato per questa categoria. Riprova domani.");
		}
		$('#esitoVoto').hide();
	  //$('#esitoVoto').html(data);
		  $('#esitoVoto').animate({
		    width: '100%',
		    opacity:'1',
		    height: 'toggle'
		  }, 800, function() {
		  });	 	  
	});

}


function apriPopupImg(idfile){
  $('#popContainer').hide();
	$('#elenco').fadeOut(300);  
	$('html, body').animate({scrollTop:0}, 'slow');		  	
	$('#popContainer').load(baseUrl + 'templates/~popup.php?id='+idfile, function() {			
		  $('#popContainer').fadeIn(500);	
		  	$('#elenco').hide;		
	
	  //alert('Load was performed.');
		  /*$('#popContainer').animate({
		    width: '100%',
		    //opacity:'1',
		    height: 'toggle'
		  }, 800, function() {
		  });	 */

	});
}	
function togglePopUp(){
		  $('#popContainer').fadeOut(300);	
		  $('#elenco').fadeIn(500);	
	
	  //alert('Load was performed.');
		  /*$('#popContainer').animate({
		    width: '100%',
		    opacity:'0',
		    height: 'toggle'
		  }, 800, function() {
		  });	 */
}	
function pubblicaVoto(imgUrl,nomeImg,cat) {
	var body = 'Ho appena votato su Miss Arte Italiana';
	var url = 'http://www.facebook.com/pages/uabred/142634749142177?sk=app_208071269216144';
	var caption = nomeImg;
	var picture = 'http://fb.marilenaferrari-fmr.it/uploads/'+cat+'/'+imgUrl;
	FB.api('/me/feed', 'post', { message: body, link: url, caption: caption, picture: picture }, function(response) {
		  if (!response || response.error) {
		    //alert('Error occured');
		  } else {
		    //alert('Post ID: ' + response.id);
		  }
		});
}
function pubblicaVotoDialog() {
	 FB.ui(
   {
     method: 'feed',
     name: 'Facebook Dialogs',
     link: 'http://developers.facebook.com/docs/reference/dialogs/',
     picture: 'http://fbrell.com/f8.jpg',
     caption: 'Reference Documentation',
     description: 'Dialogs provide a simple, consistent interface for applications to interface with users.',
     message: 'Facebook Dialogs are easy!'
   },
   function(response) {
     if (response && response.post_id) {
       alert('Post was published.');
     } else {
       alert('Post was not published.');
     }
   }
 );
}

function switchPage(qualePage) {
	/*$('#pagina').show();*/
	url = baseUrl + "templates/"+qualePage; // The server-side script
	$(document).ready(function(){
     $("#pagina").load(url);	
		 /*$("#elenco").animate({ marginLeft: "-525px"}, 300 );  */  
		 $('#elenco').fadeOut(200);		    
		 $('#pagina').fadeIn(300);	
		 /*$("#pagina").animate({ marginLeft: "0px"}, 300 )*/;     	     			
	});	
}
function backHome() {
	$(document).ready(function(){
     $("#pagina").load(url);	
		 /*$("#elenco").animate({ marginLeft: "0"}, 300 ); */
		 $('#elenco').fadeIn(300);		  
		/* $("#pagina").animate({ marginLeft: "520px"}, 300 ); */
		 $('#pagina').fadeOut(200);	    	     			
	});
		/*$('#pagina').hide();*/	
}
