var urlAttuale
var urlAttualeSenzaWWW
$(document).ready(function(){
						   
	//LIGHTBOXGALLERY
	$(".mediaGallery").fancybox();
	
	//slider gallery
	$("#slider").easySlider();
	
	//gallery delle tmb
	$('.prettyGallery').prettyGallery({
		'navigation':'bottom',
		'itemsPerPage':3
	});

  	

	
	////////////////////////////////////////////////////////////////////////////////
	//FACEBOOK - like btn
	////////////////////////////////////////////////////////////////////////////////
   	urlAttuale=window.document.location.href;
	urlAttualeSenzaWWW = urlAttuale.replace("http://www.","http://");

	if (document.getElementById('fb-root')) {
    	window.fbAsyncInit = function() {
    	FB.init({url:urlAttualeSenzaWWW, appId: 'af559a96910a3628507a828c5683dd3f', status: true, cookie: true, xfbml: true});
  	};
	
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    //aggiunta per il sito fleka.. controlla che ci sia fb-root    
  
    	document.getElementById('fb-root').appendChild(e);
		
		createComments()
	  }());


      }
	  
  //posizionamento absolute
	SI.ClearChildren.initialize();
});

function createComments(){
	var chi = $('.blog_title_of_article').html()
	
	
	var cosa= '<fb:comments xid="'+escape(chi)+'" url="'+urlAttualeSenzaWWW+'" width="620" height="20"  style="margin-bottom:20px"></fb:comments>'
	if (document.getElementById('fb-comments')){
		//document.getElementById('fb-comments').appendChild(cosa);
		$('#fb-comments').html(cosa)
	}
}

function showSpreadExtra(){
	/*$('#spread_extra').fadeIn();*/
//	alert("CAZZO")
//	$('#spread_extra').children().delay(1500).fadeIn();
//.fadeIn();
	$('#header').children().fadeIn();
	$(this).unbind();
}


function fadeInMouseMove(){
	$('#header').children().hide();
	$('#spread_extra').children().hide();
	$(document).mousemove(showSpreadExtra);
}



function verifyPosition(){
	var initialLocation;
	var browserSupportFlag =  new Boolean();
	
	// Try W3C Geolocation (Preferred)
	if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position) {
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			$('#hidden').val(initialLocation);
			return initialLocation;
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		});
		// Try Google Gears Geolocation
	} else if (google.gears) {
		browserSupportFlag = true;
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function(position) {
			initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
			$('#hidden').val(initialLocation);
			return initialLocation;
		}, function() {
			handleNoGeoLocation(browserSupportFlag);
		});
		// Browser doesn't support Geolocation
	} else {
		browserSupportFlag = false;
		handleNoGeolocation(browserSupportFlag);
	}

	function handleNoGeolocation(errorFlag) {
		if (errorFlag == true) {
			return false;
		} else {
			return false;
		}
	}
}

/*--------------------------------------------------------------------------------
 SI.ClearChildren v1.0
 ---------------------------------------------------------------------------------*/

if (!SI) {var SI = new Object(); };

SI.ClearChildren =
{
	control			: null,
	watchInterval	: 50,
	height			: 0,
	initialize		: function()
	{
		// IE Mac chokes on this (width and height assignments in particular). Go fish.
		if (document.createElement && !(document.all && !window.print))
		{
			var c = document.createElement('div'), s = c.style;
			s.position		= 'fixed';
			s.top			= '0';
			s.visibility	= 'hidden';
			s.width			= '1.em';
			s.height		= '1.em';
			this.control = document.body.appendChild(c);
			this.height = 0;
			window.setInterval('SI.ClearChildren.monitor()', this.watchInterval);
		};
		
		this.clear();
	},
	
	monitor	: function()
	{
		var o = this.height;
		this.height = this.control.offsetHeight;
		if (o != this.height) { this.clear(); };
					
	},
	
	clear : function()
	{
		if (!document.getElementsByTagName && !document.all) { return; }

		var elems = (document.all) ? document.all : document.getElementsByTagName('*');
		for (var i = elems.length-1; i >= 0; i--)
		{
			var elem = elems[i];

			if (!elem.className.match(/\bclear_children\b/)) {continue; };
			var container = elem;
			var tallest;
			var maxHeight = 0;
			for (var j = 0; j < container.childNodes.length; j++)
			{
				var contained = container.childNodes[j];
				if (contained.nodeType == 1)
				{
					if (contained.offsetHeight > maxHeight)
					{
						maxHeight	= contained.offsetHeight;
						tallest		= contained;
					};
					contained.className = contained.className.replace(/\bcc_tallest\b/, '');
				};
			};
			// Add to the front of the existing classes to appease IE Mac. Save me Jeebus.
			tallest.className = 'cc_tallest' + ((tallest.className == '') ? '' : ' ' + tallest.className);
		};
	}
};

// Just do it.
	////////////////////////////////////////////////////////////////////////////////
	//
	//	SPREAD gestione
	//	Empty text field and change .class when focus in and out
	//
	////////////////////////////////////////////////////////////////////////////////
	
	function spreadInputManage(_activeManageFocus){
		
		$("#send_id").click(verifyContent)
		$('.spread_textInput').focus(); 
		
	
	
		if (_activeManageFocus == "true") {	
		//avoid use of space bar
	$('.spread_textInput').keypress(function(event) {
											 
		  if (event.keyCode == '32') {
			 event.preventDefault();
		   }
		/*var msg = 'Handler for .keypress() called ' + xTriggered + ' time(s).';
	  $.print(msg, 'html');
	  $.print(event);*/
	});
			$("#spread_facebook_btn").hide()
			$("#send_id").hide()	
		
			$(".spread_textInput").focusin(whenEnterIntTextField)
			$(".spread_textInput").focusout(whenOutFromTextField)
		}
	}

	function whenEnterIntTextField(){ 
		//
		var actualValue		=	$(this).val()
		var defaultValue	=	$(this).attr('defaultValue')
				
    	if (actualValue == defaultValue) {
			$(this).val(''); 
			$("#spread_facebook_btn").fadeIn()
			$("#send_id").fadeIn()
		} 
  	}
  
 	function whenOutFromTextField(){	
		//
		var actualValue	=	$(this).val()
		var defaultValue=	$(this).attr('defaultValue')

		if(actualValue == '') { 
			makeAnimation()
			$(this).val(defaultValue); 
			$("#spread_facebook_btn").fadeOut()
			$("#send_id").fadeOut()
		} 
		$(this).toggleClass(".spread_textInput");
				
  	};
	

	function verifyContent(e){
		//
		var actualTextFromUser = $(".spread_textInput").val()
		var defaultValue = $(".spread_textInput").attr('defaultValue')
		
		if (actualTextFromUser == defaultValue) {
			//alert("actualTextFromUser: "+actualTextFromUser+"/ defaultValue: "+defaultValue)
			//$(this).animate({ backgroundColor: "#68BFEF" }, 500);
			makeAnimation()
			return false;
		}
		//se hai risposto si a share on facebook allora return true
		shareOnFacebook()
		return true;
	}
	
	function makeAnimation(){
		$(".spread_textInput").animate({color: '#f00'}, 500, returnColorBlack);
		function returnColorBlack() {
			$(".spread_textInput").animate({color: '#333'}, 500);
		}
	}
	
	function shareOnFacebook(){
		//fare il check sul radio button
		if ($('.spread_facebook').attr("checked")){
		var loc = encodeURIComponent($(location).attr('href'));
		var userInput = $('.blog_social_your_comment_input_normal').val();
		var tit = encodeURIComponent(userInput);
		window.open('http://www.facebook.com/sharer.php?u='+loc+'&t='+tit,'sharer','toolbar=0,status=0,width=626,height=436');
		} 
	}

