// WEBKOLM

$(document).ready(function()
{
	
	// EFFETTO SULLE IMMAGINI NELL'HEADER
	$("#fotohead li").click(function(){
		$("#fotohead li").css({"z-index":1});
		$(this).css({"z-index":3});
		});
		
	
	
	function RandomDegree() {
		$val2=-15;
		$val1=15;
		$r = Math.random() * ($val2 - $val1) + $val1;
		$r_rounded = Math.round($r);   
       return $r_rounded;}
		
	// GESTIONE ROTAZIONE CASUALE DELLE IMMAGINI NELLE FOTOGALLERY
	$(".fotogallery li a").each(function(){  
	   $grado=RandomDegree();
       $(this).rotate($grado);
    });	
	
	// GESTIONE HOVER 
	
	$(".fotogallery li a").hover(
		function(){
		$(this).rotate(0);}, 
		function(){
		$grado=RandomDegree();
       $(this).rotate($grado);	
		});	
	
// PULSANTE ADMIN
 $("#admin").toggle(function(){								   
	$("#footer span.admin").animate({width:'560px'});							   
	}, function(){		
	$("#footer span.admin").animate({width:'35px'});					
});	
	
	
	//COLORBOX
	$('a[rel^="cb_"]').colorbox();
	
	//PLACEHOLDER E EMAIL
	if($('.richiesta').length)
	{
		$('.richiesta form input,textarea').placeholder({css:'test'});
		
		//invio email richiesta info
		$('.richiesta').bind('submit',function()
		{
			$.ajax(
			{
				url		: BASE+'email/message/mail/',
				type	: "POST",
				dataType: 'json',
				data	: 
					{
						'email':$('input[name="email"]').val(),
						'nome':$('input[name="nome"]').val(),
						'testo':$('textarea[name="testo"]').val(),
						'subject':$('input[name="oggetto"]').val()
					},
				success	: function(data){$('.richiesta').hide(); if(data.ok) $('#infook').fadeIn(); else $('#infoer').fadeIn();},
				error	: function(){$('.richiesta').hide(); $('#infoer').fadeIn();}
			});
			return false;
		});		
	}
	
	
	// MAPPA GOOGLE
	//google maps
	if($('#map').length)
	{
		$('#map').gmap3
		(
			{
				action:'init',
				options:
				{
					center:MAP_CENTER,
					zoom: 10
				}
			},
			{
				action: 'addMarkers',
				markers:
				[
					{
						lat: MAP_MARKER_COORD[0],
						lng: MAP_MARKER_COORD[1],
						data : MAP_MARKER_TEXT,
						options: { icon: new google.maps.MarkerImage(BASE+'application/assets/img/map-icon.png') }
					}
				],
				events:
				{
					click: function(marker, event, data)
					{
						var map = $(this).gmap3('get'),
						infowindow = $(this).gmap3({action:'get', name:'infowindow'});
			            if(infowindow)
			            {
			            	infowindow.open(map, marker);
			            	infowindow.setContent(data);
			            }
			            else
			            {
			            	$(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: data}});
			            }
					}
				}
			}
		);
	}
	
	//GESTIONE TABS
	if($('#menu_interno li').length)
	{
		//handler click su menu
		$('#menu_interno li').bind('click',function(e)
		{
			e.preventDefault();
			activateTab($(this).attr('data-master'));
			return false;
		});
		
		//apre tab giusto su refesh pagina
		if(window.location.hash.length)
		{
			activateTab(window.location.hash);
		}
	}
});

function activateTab(x)
{
	x = x.replace('#','');
	if($('[data-master="'+x+'"]').length)
	{
		$('[data-master]').find('a').removeClass('active');
		$('[data-slave]').hide();
		$('[data-master="'+x+'"]').find('a').addClass('active');
		$('[data-slave="'+x+'"]').show();
	}
	
//	var currentUrl = window.location.toString();
//	var baseUrl = currentUrl.split("#",1)[0];
	
	window.location.hash = x;
}
