function loadPage(page,initSlide){
	$('div.content-container').load('WebContent/page/'+page+'.html',function(){
		$('#menu a').removeClass('active');
		$('#menu a#'+page).addClass('active');
		if('home'== page){
			if(initSlide){
				$(window).load(function(){
					$('#slider').nivoSlider({
						 animSpeed: 2000,
						 pauseTime: 6000
					});
				});
			}else{
				$(this).ajaxStop(function(){
					$('#slider').nivoSlider();
				});
			}
			$('.fb .fb_logo, .fb span').click(function(event){
				window.open("https://www.facebook.com/groups/333506276682436/");
			});
			
		}
		if('gallery'== page){
			var settings = { 
        			containerResizeSpeed: 350,
        			// Configuration related to images
                    imageLoading: 'WebContent/images/loading.gif',
                    imageBtnPrev: 'WebContent/images/prev.png',
                    imageBtnNext: 'WebContent/images/next.png',
                    imageBtnClose: 'WebContent/images/close.png',
                    imageBlank: 'WebContent/images/lightbox-blank.gif',
                    imageBtnBottomPrev: 'WebContent/images/btm_prev.gif',
                    imageBtnBottomNext: 'WebContent/images/btm_next.gif',
                    imageBtnPlay: 'WebContent/images/start.png',
                    imageBtnStop: 'WebContent/images/pause.png',
                    // Configuration related to texts in caption. For example: Image 2 of 8. You can alter either "Image" and "of" texts.
                    txtImage: 'Immagine',
                    txtOf: 'di',
                    txtPrev: '&nbsp;Prec.',
                    txtNext: '&nbsp;Succ.'
         		};
            $('#gallery a, #gallery iframe').lightBox(settings);
            
            $("ul.demo2").ytplaylist({
            			addThumbs:true, 
            			autoPlay: false, 
            			holderId: 'ytvideo2',
            			playerHeight: 400,
            			playerWidth: 895
            		});
		}
		if('news'== page){
			$('div.pdf').click(function(event){
				window.open("WebContent/media/volantino2011_2012.pdf");
			});
			$('div.pdf2').click(function(event){
				window.open("WebContent/media/volantino_01-2012.pdf");
			});
			$('.screenshot').click(function(event){
				event.preventDefault();
			});
			screenshotPreview();
		}
		if('corsi'== page){
			$('h2.news').click(function(event){
				loadPage('news');
			});
		}
		if('collaboratori'== page){
			$('.screenshot').click(function(event){
				event.preventDefault();
			});
			screenshotPreview();
		}
		if('contatti' == page){
			initialize();
			$('#sede_legale').click(function(event){
				event.preventDefault();
				var address = 'Via C.na San Giuseppe,3 20030 Senago (Milano)';
				codeAddress(address);
			});
			$('#sede_corsi').click(function(event){
				event.preventDefault();
				var address = 'Via San Carlo Borromeo, 46/a Cesano Maderno (Monza e Brianza)';
				codeAddress(address);
			});
			
		}
	});
}

//MAPPE GOOGLE
var geocoder;
var map;
var position;
var positionCorsi;

function initialize() {
  geocoder = new google.maps.Geocoder();
  setDefaultAddress('Via C.na San Giuseppe,3 20030 Senago (Milano)');
}

function setDefaultAddress(address_){
	var address = address_;
	geocoder.geocode( { 'address': address}, function(results, status) {
	    if (status == google.maps.GeocoderStatus.OK) {
	      position = results[0].geometry.location;
	      loadMap();
	    } else {
	      alert("Geocode was not successful for the following reason: " + status);
	    }
	  });
}

function loadMap(){
  var latlng = new google.maps.LatLng(position.lat(), position.lng());
  var myOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  //SEDE LEGALE
  var image = ' WebContent/images/favicon.png';
  var markerSedeLegale = new google.maps.Marker({
      map: map,
      position: position,
      title:"Sede Legale",
      icon: image
  });
  //SEDE CORSI
  var addressCorsi = 'via San Carlo Borromeo, 46/a Cesano Maderno (Monza e Brianza)';
  geocoder.geocode( { 'address': addressCorsi}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
		  positionCorsi = results[0].geometry.location;
		  var markerSedeCorsi = new google.maps.Marker({
		      map: map,
		      position: positionCorsi,
		      title:"Corsi",
		      icon: image
		  });
	  }else {
	      alert("Geocode was not successful for the following reason: " + status);
	  }
  });
  
}

function codeAddress(address) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      lat = results[0].geometry.location.lat();
      lng = results[0].geometry.location.lng();
      map.setCenter(results[0].geometry.location);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
}
//FINE


$(document).ready(function() {
	loadPage('home',true);
	$('#menu a').click(function(event){
		event.preventDefault();
		var page = $(this).attr('id');
		if(!$(this).hasClass('active')){
			loadPage(page,false);
		}
	});
	
});

