var map;
var bounds;
var curLatitude=0;
var curLongitude=0;
var infowindow, marker, i, markerArray, bisignanoMarker;

$(document).ready(function() {
	init();
});

function init(){
	if ($('#slideshow').length!=0) slideshow();	
	if($('#map').length!=0) initMaps();
	if($('.calendar').length!=0) calInit();
	
	if($('#photoblock').length!=0) { 
		setTimeout("scrollerGallery($('#photoblock'))", 100); 
	}
	
	if ($('body#pg_things_to_see_and_do_in_florence').length!=0) {
		scrollPane();
	};
	
	if ($('.lightbox').length!=0) {
		$('.lightbox li a').lightBox({
			imageLoading:  siteURL+'js/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: siteURL+'js/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev:  siteURL+'js/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext:  siteURL+'js/lightbox/images/lightbox-btn-next.gif',
			iamgeBlank:    siteURL+'js/lightbox/images/lightbox-blank.gif'
		   });
	};
	
	if ($('.lightboxURL').length!=0) {
		$('.lightboxURL').lightBox({
			imageLoading:  siteURL+'js/lightbox/images/lightbox-ico-loading.gif',
			imageBtnClose: siteURL+'js/lightbox/images/lightbox-btn-close.gif',
			imageBtnPrev:  siteURL+'js/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext:  siteURL+'js/lightbox/images/lightbox-btn-next.gif',
			iamgeBlank:    siteURL+'js/lightbox/images/lightbox-blank.gif'
		   });
	};
}

function slideshow(){
	$('#slideshow').cycle({
		speed: 3000,
		timeout: 3000,
		random: true
	});
}

function initMaps(){	
	var myOptions = {
	    zoom: 14,
		mapTypeControl: false,
		scrollwheel: false,
		panControl: false,
		streetViewControl: false,
	    center: new google.maps.LatLng(43.770443,11.259828),
	    mapTypeId: google.maps.MapTypeId.TERRAIN
	  }
	
	map = new google.maps.Map(document.getElementById('map'), myOptions);
	bounds = new google.maps.LatLngBounds();
	
	renderMap(mapData);	
	map.fitBounds(bounds);
	
	$('#city_list section').each(function(){
		var latitude = $(this).attr('data-latitude');
		var longitude = $(this).attr('data-longitude');
		$(this).hover(function(){
			centerOnMap(latitude, longitude, $(this).attr('data-entry-id'));
		}, function(){
		});
	});
}

function renderMap(data){
	markerArray = new Array();
	infowindow = new google.maps.InfoWindow();
	
	bisignanoMarker = new google.maps.Marker({
		position: new google.maps.LatLng(43.770147,11.262043),
		title: 'Via Ghibellina, 96',
		map: map
	});
	bisignanoMarker.setIcon(siteURL+'img/marker_96.png');

	for (i=0; i<data.length; i++) {  
		marker = new google.maps.Marker({
			position: new google.maps.LatLng(data[i][1], data[i][2]),
			map: map
		});
		marker.setIcon(siteURL+'img/marker.png');
		markerArray.push(marker);

		google.maps.event.addListener(marker, 'click', (function(marker, i) {
			return function() {
				var str = '<div class="bisInfo"><strong>'+data[i][3]+'</strong><br>'+data[i][4];
				if(data[i][5]!="") str += '<br>T: '+data[i][5];
				if(data[i][6]!="") str += '<br>Notes: '+data[i][6];
				str += '</div>';
				
				infowindow.setContent(str);
				infowindow.open(map, marker);
			}
		})(marker, i));

		bounds.extend(new google.maps.LatLng(data[i][1],data[i][2]));
	}
}

function centerOnMap(latitude, longitude, id){		
	if (curLatitude!=latitude && curLongitude!=longitude) {	
		
		var str = '<div class="bisInfo"><strong>'+mapData[id][3]+'</strong><br>'+mapData[id][4];
		if(mapData[id][5]!="") str += '<br>T: '+mapData[id][5];
		if(mapData[id][6]!="") str += '<br>Notes: '+mapData[id][6];
		str += '</div>';
		
		infowindow.setContent(str);
        infowindow.open(map, markerArray[id]);
		
		curLatitude=latitude;
		curLongitude=longitude
		
		map.panTo(new google.maps.LatLng(latitude,longitude));
		map.setZoom(21);
	}
	
}

function scrollPane(){
	$('#city_list').jScrollPane({
		verticalDragMinHeight: 20,
		verticalDragMaxHeight: 20,
		horizontalDragMinWidth: 20,
		horizontalDragMaxWidth: 20
	});
}

function scrollerGallery(scrollObj){		
	scrollObj.find('img').each(function(){ $(this).wrap('<div></div>');	});	
	startGallery(scrollObj);
}

function startGallery(ele){
	var offset = '0px';
	galleryInterval = window.setInterval(function(){
		var curWidth = ele.find('div:first-child').width();		
		ele.animate({'left': '-='+(curWidth)+'px' }, 3000, 'easeOutQuad', function(){		
			ele.find('ul').append(ele.find('li:first-child'));
			ele.css({ 'left': offset });
		});		
	}, 6000);
}

function calInit(){	
	if($('#calNav').length!=0) calNav();
	
	$('a#prev_month, a#next_month').click(function(e){
		$('.calendar').load($(this).attr('href'), function(){
			calInit();
		});
		e.preventDefault();
	});
}

function calNav(){	
	$('#calNav select').change(function(){
			$('.calendar').load(siteURL+'ajax/calendar_lg/'+$('#calNav select[name$="year"]').val()+'/'+$('#calNav select[name$="month"]').val());
	});
}
