var photos = new Array;
var delai;
var iconurl;
var highlightCircle;
var currentMarker;

/******
 * Init
 ******/

window.addEvent('domready', function() {
	reachJson();
	handleResize();
});

window.onresize = handleResize;

/***************
 * auto-resize *
 ***************/

function windowHeight(){
	if(self.innerHeight)
		return self.innerHeight;
		
	if(document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
		
	if(document.body)
		return document.body.clientHeight;return 0;
}

function handleResize(){
	var height = windowHeight()-20;
	$('map').style.height = height+'px';
}


/***********
 * markers *
 ***********/

function createMarker(point, id, complex, category, prefot_ot){

	var icon = new GIcon();
	iconurl = "img/pi_"+complex+"_"+category+"_"+prefot_ot+".png";
	
	icon.image = iconurl;
	icon.iconSize = new GSize(40, 40);
	icon.iconAnchor = new GPoint(30, 30);
	icon.infoWindowAnchor = new GPoint(50, 8);
	
	var marker = new GMarker(point, icon);
	map.addOverlay(marker);

}


function reachJson(){
	var json = new Json.Remote("php/getfilters.php?o=true&f=diapo", {
		onComplete: function(o){
			readJson(o);
		}
	}).send();
}

function readJson(o){
	o.each(function(e) {
		var point = new GLatLng(e.lat, e.lon);
		var marker = createMarker(point, e.id_e, e.id_cx, e.id_c, e.id_pot);
	});      
}


/*************
 * diaporama *
 *************/

function showPhotos() {
	
	var p;
	
	/*if (typeof photos != 'object')
		return;*/
	
	if(photos.length == 0) {
		loadData();
		return;
	}
	
	if( p = photos.pop() ) {
	
		var point= new GLatLng(p[0],p[1]);
		var mk = new GMarker(point);
		$('ariane').setText(p[3] + ' > ' + p[4]);
		
		ewindow = new EWindow(map, E_STYLE_3);
		
		map.addOverlay(ewindow);
		map.panTo(point);
		highlight(mk);
		
		ewindow.openOnMarker(mk,p[2])
		
		delai = setTimeout(function(){
			ewindow.hide();
			showPhotos();
		}, 20000);
	}
}


function addPhoto(lat, lon, infos, nom_complex, nom_category){
	photos.push(arguments);
}


function highlight(p){
	
	var markerPoint = p.getPoint();
	var polyPoints = Array();
	
	if (highlightCircle)
		map.removeOverlay(highlightCircle);
	
	var mapNormalProj = G_NORMAL_MAP.getProjection();
	var mapZoom = map.getZoom();
	var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);
	var polySmallRadius = 20;
	var polyNumSides = 20;
	var polySideLength = 18;
	
	for (var a = 0; a<(polyNumSides+1); a++) {
		var aRad = polySideLength*a*(Math.PI/180);
		var polyRadius = polySmallRadius; 
		var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
		var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
		var polyPixel = new GPoint(pixelX,pixelY);
		var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
		polyPoints.push(polyPoint);
	}
	
	// Using GPolygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
	highlightCircle = new GPolygon(polyPoints,"#000000",2,0.75,"#000000",.5);
	map.addOverlay(highlightCircle);
}


/*****
 * MAP
 *****/

function loadData() {
	var jx = new Ajax('php/diapo.php', {method: 'get', evalScripts:true}).request();
}