/**
 * geosignet.js
 */

BG.util.GeoSignet = function ( signetConfig )
{	
	this.init ( signetConfig );
	this.id = BG.util.GeoSignet.ID;
	BG.util.GeoSignet.ID ++;
};

BG.util.GeoSignet.ID = 0;
BG.util.GeoSignet.mapSignet = new YAHOO.ext.util.MixedCollection ( true );

BG.util.GeoSignet.prototype = 
{
	id : 0,
	x : 0,
	y : 0,
	width : 0,
	height : 0,
	sName : null,
	
	sJSON : "{sName:'Equipe 1',x:548698.326589,y:987546.265778,width:12548,height:5487}",
	
	/*initObject : function ( obj )
	{
		this.x = obj.x;
		this.y = obj.y;
		this.width = obj.width;
		this.height = obj.height;
		this.sName = obj.sName;
	},*/
	init : function ( signetConfig )
	{
		BG.util.ApplyConfig( signetConfig, this );
	},
	
	ToJSON : function (  )
	{
		return "{sName:'" + BG.util.EscapeCote ( this.sName ) + "', x:" + this.x + ",y:" + this.y + ",width:" + this.width + ",height:" + this.height + "}";
	},
	
	ToCookie : function ()
	{
		var x = new String ( this.x );
		x = x.substring ( 0, x.indexOf (".") + 3);
		
		var y = new String ( this.y );
		y = y.substring ( 0, y.indexOf (".") + 3);
		
		var w = new String ( this.width );
		w = w.substring ( 0, w.indexOf (".") + 3);
		
		var h = new String ( this.height );
		h = h.substring ( 0, h.indexOf (".") + 3);
		
		return BG.util.EscapeCote ( this.sName ) + "|" + x + "|" + y + "|" + w + "|" + h;
	},
	
	Show : function ()
	{
		console.info ( "Il faut rafraichir la carte avec la bonne emprise" );
	},
	
	ConstructDiv : function ( objParentDiv )
	{
		var theDiv = document.createElement( 'div' );
		theDiv.setAttribute ( "id", BG.util.ToId ( this.sName ) );
		var theTable = document.createElement( 'table' );
		var theTBody = document.createElement( 'tbody' );		
		var theRow = document.createElement( 'tr' );		
		
		var cell1 = document.createElement( 'td' );		
		cell1.appendChild ( document.createTextNode ( this.sName ) );
		cell1.className = "tdSignet";
		
		var cell2 = document.createElement( 'td' );		
		cell2.setAttribute( "indice", this.id );
		cell2.setAttribute( "sname", this.sName );
		cell2.onclick = function(){ BG.util.ShowGeoSignet ( this.getAttribute ( 'sname' ), this.getAttribute ( 'indice' ) ); };		
		cell2.appendChild ( document.createTextNode ( ' ' ) );
		cell2.className = "iconVoir main";
		cell2.setAttribute ( "title", "Montrer le géosignet" );
		
		var cell3 = document.createElement( 'td' );		
		cell3.setAttribute( "indice", this.id );
		cell3.setAttribute( "sname", this.sName );
		cell3.onclick = function(){ BG.util.DeleteGeoSignet ( this.getAttribute ( 'sname' ), this.getAttribute ( 'indice' ) ); };
		cell3.appendChild ( document.createTextNode ( ' ' ) );
		cell3.className = "iconDelete main";
		cell3.setAttribute ( "title", "Supprimer le géosignet" );
		
		objParentDiv.appendChild ( theDiv );
		theDiv.appendChild ( theTable );
		theTable.appendChild ( theTBody );
		theTBody.appendChild ( theRow );	
		theRow.appendChild ( cell1 );		
		theRow.appendChild ( cell2 );
		theRow.appendChild ( cell3 );
	}
};



BG.util.AddGeoSignet = function ()
{
	var sGeosignet = prompt ( 'Saisir le nom du nouveau geosignet.', '' );
	
	if ( sGeosignet == null || sGeosignet == "" || sGeosignet == "undefined"   )
	{
		return;
	}
	if ( sGeosignet.indexOf ("&") >= 0 || sGeosignet.indexOf ("|") >= 0 )
	{
		alert ("Le nom d'un signet ne doit pas contenir les caractères & ou |.");
		return;
	}
	//console.log ( "#" + sGeosignet + "#" );

	if ( sGeosignet.replace ( " ", "" ) == "")
		return;
	
	//console.log ( "OK#" + sGeosignet + "#" );
	
	var signet = new BG.util.GeoSignet ();
	signet.sName = sGeosignet;
	signet.x = mapleft;
	signet.y = mapbottom;
	signet.width = mapright-mapleft;
	signet.height = maptop - mapbottom;
	
	
	
	//BG.util.GeoSignet.arraySignet.push ( signet );	
	BG.util.GeoSignet.mapSignet.add ( signet.sName, signet );
	BG.util.SaveGeoSignets ( );
	
	signet.ConstructDiv ( document.getElementById( 'geosignet' ) );
}


BG.util.SaveGeoSignets = function ( )
{
	//var sSave = "arraySignetTemp=[";//
	var sSave = "";
	
	for ( var i=0; i<BG.util.GeoSignet.mapSignet.getCount(); i++ )
	{
		//sSave += BG.util.GeoSignet.mapSignet.get ( i ).ToJSON() + ",";
		sSave += BG.util.GeoSignet.mapSignet.get ( i ).ToCookie() + "&";
	}
	
	/*for ( i=0; i<BG.util.GeoSignet.arraySignet.length; i++ )
	{
		sSave += BG.util.GeoSignet.arraySignet[i].ToJSON() + ",";
	}*/
	if ( BG.util.GeoSignet.mapSignet.getCount() > 0 )
	{
		sSave = sSave.substr ( 0, sSave.length -1 );
	}
	//sSave += "]";
	
	

	
	
	var cp = new YAHOO.ext.state.CookieProvider({path:'/',expires: new Date(new Date().getTime()+(1000000*60*60*24*7)),domain: null,secure:false});
	YAHOO.ext.state.Manager.setProvider(cp);
	
	
	
	cp.setCookie ( "GEOSIGNETS", escape(sSave) );
				
	
};


BG.util.LoadGeoSignets = function (  )
{
	//console.info ( BG.util.Cookies.Get( "GEOSIGNETS") );
	
	var parentDiv = document.getElementById( 'geosignet' );
	
	var cp = new YAHOO.ext.state.CookieProvider()
	var cookieSignet =  unescape ( cp.readCookies()[ "GEOSIGNETS" ] ) ;
	
	if ( cookieSignet == "" || cookieSignet == "undefined" )
	{
		return ;
	}
	
	//eval ( "var "+cookieSignet );
	var arraySignetTemp = cookieSignet.split ( "&" );
	for ( i=0; i<arraySignetTemp.length; i++ )
	{
		var obj = arraySignetTemp [ i ].split ( "|" );
		//var signet = new BG.util.GeoSignet ( obj );
		var signet = new BG.util.GeoSignet ( );
		signet.sName = obj[0];
		signet.x = obj[1];
		signet.y = obj[2];
		signet.width = obj[3];
		signet.height = obj[4];
		
		signet.ConstructDiv ( parentDiv );
		BG.util.GeoSignet.mapSignet.add ( signet.sName, signet );
	}
	/*for ( i=0; i<arraySignetTemp.length; i++ )
	{
		var obj = arraySignetTemp [ i ];
		var signet = new BG.util.GeoSignet ( obj );
		
		signet.ConstructDiv ( parentDiv );
		BG.util.GeoSignet.arraySignet [ signet.id ] = signet;
	}*/
	
	arraySignetTemp = null;
};

BG.util.ShowGeoSignet = function ( sName, idSignet )
{
	//alert( "Implementer ShowGeoSignet " + sName );
	var signet = BG.util.GeoSignet.mapSignet.get ( sName );
	DrawMap ( signet.x, signet.y, signet.width, signet.height );
}

BG.util.DeleteGeoSignet = function ( sName, idSignet )
{
	if ( confirm ( "Etes vous certain de voulir supprimer le signet : " + sName + "?" ) )
	{
		document.getElementById( 'geosignet' ).removeChild ( document.getElementById(BG.util.ToId ( sName )) );
		BG.util.GeoSignet.mapSignet.removeKey ( sName );
		
		BG.util.SaveGeoSignets ( );	
	}
}





