
var firstLoad = true;
var indexMap;
var geodataString;
var currentTheme;
var pageColumn;
var firstLoad;
var showRightColumn;
var selection;
var actualtheme;
var nrowsColumn = 1;
var allowcolumnhover=true;
var zoomSliderValue = false;

//
//	Page events
//

// page init
Event.observe(window, "load", initIndex, false);
Event.observe(window, "resize", indexResize, false);
//Event.observe('indexMap', "mouseover", allowhover, false);


var showRightColumn = false;

function initIndex()
{
	// Current theme selected
	currentTheme = 0;

	indexResize();

	if(selection == 'eventos')
		initWeekCalendar();
	else
		initIndexMap();
	
	firstLoad = false;
	
	// PNG hacks for IE6
	hackPNGs();
	
	// Refresh anatips
	new PeriodicalExecuter(function(pe) {
		ANAEMA.tips.setupTooltips();
	}, 1);
}

function initIndexMap()
{
		initMap();
		refreshMap();		
}

function indexResize()
{
		
	var pageHeight = getPageSizes('height');
	var pageWidth = getPageSizes('width');

	
	if(showRightColumn == true || selection == 'eventos')
	{
		$('rightCol').show();
		$('indexMap').style.width = ( pageWidth - 260 ) + "px";
		$('indexMap').style.height = ( pageHeight - 112 ) + "px";
		$('rightCol').style.width = ( 260 ) + "px";
		$('rightCol').style.height = ( pageHeight -112 ) + "px";
		
		// Menu buttons
		$('btn1').style.right="430px";
		$('btn2').style.right="280px";
		
		// Column rows number
		if(selection == 'eventos' )
			nrowsColumn = ( pageHeight -240 ) / 84;
		else
			nrowsColumn = ( pageHeight -180 ) / 60;
	}
	else
	{
		$('rightCol').hide();
		$('indexMap').style.width = ( pageWidth ) + "px";
		$('indexMap').style.height = ( pageHeight -112 ) + "px";
		
		// Menu buttons
		$('btn1').style.right="190px";
		$('btn2').style.right="30px";
		
		// Column rows number
		if(selection == 'eventos' )
			nrowsColumn = ( pageHeight -240 ) / 84;
		else
			nrowsColumn = ( pageHeight -180 ) / 60;
	}
	
	if(firstLoad == false)
		loadRightColumn(false);
}


//
//	Geodata String and parameters
function setGeoData()
{
    
	if(selection == 'eventos' ) {
		if(currentTheme != 0)
			geodataString = 'index.php?q=geoData&data=activities&theme=' + currentTheme + dateCalendarStr();
		else
			geodataString = 'index.php?q=geoData&data=activities' + dateCalendarStr();
	}
	else
	{
		if(currentTheme != 0)
			geodataString = 'index.php?q=geoData&data=locations&theme=' + currentTheme;
		else
			geodataString = 'index.php?q=geoData&data=locations'
	}
	
}

function initMap(){
	
	setGeoData();
	
	//
	// Create point Types
	//
	
	var points = Array();
	
	points[0] = new MapPointType('espazo', new GSize(28, 34),new GSize(46, 34),new GPoint(23, 17), new GPoint(10, 2), new GPoint(14, 17));

	///
	/// Map
	///
	var position = Array();
	position[0] = 43.37436;
	position[1] = -8.430462;
	iniZoom = 14;
	
	
	indexMap = new Map('indexMap', geodataString, position, iniZoom, 0, 1, points);
	
    indexMap.changeMapType(G_SATELLITE_MAP);
	// Create dialog on the map
	indexMap.CreateDialogBoxes();

	// Click point when load is finished
	if(selection == 'eventos' && showloc >= 0)
	{
		if(showloc==0) Modalbox.show('Contents/messages/noevents.html', {title: 'Actividades non encontradas' , width: 500});
		else indexMap.OpenOnload(showloc);
	}	

	//
	//	Zoom Bar events
	//
	indexMap.zoomSetLimits(13, 20);
	Event.observe('mapzoomin', "click", ZoomIn, false);
	Event.observe('mapzoomout', "click", ZoomOut, false);
	
	// Zom custom Bar
	mapZoomBar = new Control.Slider('mapzoombarhandle','mapzoombar',{
					axis:'vertical',
					range: $R(0, 7)
				});
		
	mapZoomBar.options.onChange = function(value) {
		var zoomValue = Math.round(20-value);
		indexMap.zoomSet(zoomValue);
		zoomSliderValue = zoomValue;

	}.bind(this);

	// Bar value listener
	new PeriodicalExecuter(function(pe) {
		var Zoomvalue = indexMap.zoomLevel;
		var realBarvalue = (Zoomvalue-20)*-1
	
		if(realBarvalue != Math.round(mapZoomBar.value))
				mapZoomBar.setValue(realBarvalue);
	}.bind(this), 0.5);

}

function ZoomIn(){
	indexMap.zoomIn();
}

function ZoomOut(){
	indexMap.zoomOut();
}

function refreshMap()
{
	var theme = currentTheme;
	$$("div.mapFilterEach").each(function(e){e.hide(); }.bind(this));
	$("mapFilterText").innerHTML = $("mapFilter"+theme).innerHTML;
	$("mapFilterText").style.cursor = "pointer";
	
	if (theme == 0 && selection != 'eventos') {
		showRightColumn = false;
		$("rightColContent").innerHTML="";
	}
	else {
		showRightColumn = true;

		// load column content
		actualtheme = theme;
		loadRightColumn(false);

	}
	indexResize();
	
	// Map and interface data load
	currentTheme = theme;
	setGeoData();
	indexMap.dataXML = geodataString;	
	indexMap.LoadPoints();
	
	
	
}

function loadRightColumn(page)
{
	// Calculo de número de filas en base al tamaño de la página.

	if(selection == 'eventos' ) 
		var columnsource = 	'index.php?q=columlist&list=events' + dateCalendarStr();
	else
		var columnsource = 	'index.php?q=columlist&list=locations';
	
	
	if(page == false)
		pageColumn = 1;
	else 
		pageColumn = page;

	new Ajax.Updater('rightColContent', columnsource, {
		  method: 'get',
		  parameters: {id: actualtheme, page: pageColumn, rowseachpage: nrowsColumn}

		});
}

function showThemeList()
{
	$$(".mapFilterEach").each(function(e){e.show()});
	$("mapFilterText").style.cursor = "default";
}



// show map point and show map.
function showMapPoint(point)
{
	indexMap.PointAction(point, "click");
	allowcolumnhover = false;
}


// set position on map
function centerOnPoint(point)
{
	if (allowcolumnhover == true) {
		var ptt = indexMap.GetPointbyID(point);
		indexMap.map.panTo(ptt['point']);
	}
}

// allow make center when hoven on column row
function allowhover()
{
	allowcolumnhover = true;
}



//show Event on an already open globe of the map.
function showEventMapPoint(loc,event,start, end, theme)
{	
	new Ajax.Updater('globeContent'+loc, 'index.php', {
		  method: 'get',
		  parameters: {q: 'globemap',
		               type: 'activity',
		               id: loc,
		               event: event,
		               back: 'y',
		               evini: start,
		               evend: end,
		               theme: theme
		              }
		});	
		
}

//show EventList on an already open globe of the map.
function showEventListMapPoint(loc,start, end, theme)
{
	new Ajax.Updater('globeContent'+loc, 'index.php', {
		  method: 'get',
		  parameters: {q: 'globemap',
		               type: 'activity',
		               id: loc,		               		               
		               evini: start,
		               evend: end,
		               theme: theme
		              }
		});	
}



function dateCalendarStr()
{
	var dateStr = "";
	
	if(showdate != null)
		dateStr = '&datestart=' + showdate +'&dateend=' + showdate;
	else
	if(shorangestart != null && showrangeend != null)
		dateStr = '&datestart=' + shorangestart +'&dateend=' + showrangeend;

	return dateStr;	
}

function hackPNGs()
{
	
}



