/**
 * Mapa rejonów na elementach AREA;
 * event:
 * - onSelect( rejon_id )	- zdarzenie wywołane na mousedown na rejonie;  
 *
 * @author Piotr Kusztal
 * @refaktoring Tomasz Łukawski
 */
TMapa = function()
{
	var param =
		{
			className:'mapa_sz',
			//img1	:'/images/01/80p/mapka3.png',
			//img1	:'/images/01/80p/mapaglowna3.png',
			img1	:'http://cache1.anonse.com/images/01/80p/mapa-glowna-kropki.png',
			img2	:'http://cache1.anonse.com/images/01/80p/mapka_alfa2.png',
			AJAXsrc	:'/strona_v2/ajax/mapa/',
			multiselect_on	:false,
			legenda_on		:false,
			legenda	: undefined,
			send_on	: true
		}
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];

	this.obj		= _c( 'div', param );
	this.obj.lista	= [];
	this.obj.rejony_do_wyslania	= [];
	
	this.obj.rysuj	= function( dane )
	{
		this.mapa_miejsce	= _c( 'div', {className:'obszar_mapy'} );
		this.mapa_sz		= _c( 'img', {src:this.img1, title: this.name} ); 
		this.mapa_alfa		= _c( 'img', {src:this.img2, useMap:'#polska', className:'alfa'} );

		//mapa główna szara (tło)
		this.mpolska	= _c( 'map', {name:'polska'} );
	
		this.apC( this.mpolska );
		this.apC( this.mapa_miejsce );
		this.apC( this.mapa_sz ); 
		this.apC( this.mapa_alfa );
		
		//-- legenda
		if (this.legenda_on) this.createLegenda();
		//--		
		this.update( dane );
	}
	
	this.obj.createLegenda	= function()
	{
		this.legenda	= new TMapaLegenda( {parent:this} );
		this.apC( this.legenda );
		//-- event z legendy //-- overload;
		this.legenda.sendRejonId	= function( rejon_id ){ this.parent.unsetRejon( rejon_id ); }	
	}
	
	this.obj.unsetRejon = function( v )
	{
		for(var i in this.lista)
		{
			if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
			//--
			if (this.lista[i].rejon_id == v )
			{
				this.lista[i].set(false);
				this.lista[i].sendValue( this.rejon_id );
			}
		}
	}
	
	this.obj.setRejon = function( v )
	{
		for(var i in this.lista)
		{
			if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
			//--
			if (this.lista[i].rejon_id == v )
			{
				this.lista[i].set(true);
				this.lista[i].sendValue( this.rejon_id );
			}
		}
	}
	
	this.obj.showRejon = function( v )
	{
		for(var i in this.lista)
		{
			if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
			//--
			if (this.lista[i].rejon_id == v ) this.lista[i].set(true);
		}
	}
	
	this.obj.hideRejon = function( v )
	{
		for(var i in this.lista)
		{
			if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
			//--
			if (this.lista[i].rejon_id == v ) this.lista[i].set(false);
		}	
	}
	
	this.obj.deselectAll	= function()
	{
		for(var i in this.lista)
		{
			if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
			if ( this.lista[i].isSelected() )
			{
				this.lista[i].set(false);
				this.rejony_do_wyslania[ this.lista[i].rejon_id ] = 0; 
			}
		}
		this.sendValues();
	}
	
	/**
	 * dane - tablica asocjacyjna klucz=>wartość;
	 */	
	this.obj.sendValues = function()
	{	
		if (!this.send_on) return false;
	
		var dane = this.rejony_do_wyslania;
		var dane_out = '';
		if (dane == undefined) return false;
		//--
		for ( var i in dane )
		{
			if (typeof(dane[i])=='function') continue;
			dane_out += i + '_'+dane[i]+'_';
		}
		setByAjax( '/strona_v2/ajax/set/', this, {_set_action:'rejony_z_mapy', value:dane_out }	);
		//--
		this.rejony_do_wyslania = [];		
	}	
	
	this.obj.load = function( dane ){ this.update( dane ); }
	this.obj.update = function( dane )
	{
		if (dane == undefined) return false;
		if (dane['mapa_glowna'] == undefined) return false;		
		
		//-- usuń poprzednie elementy legendy;
		if (this.legenda != undefined) this.legenda.usunAll();
		
		//-- area dla mapy głównej
		if (this.lista.length == 0)
		for(var i in dane['mapa_glowna'])
		{
			var d = dane['mapa_glowna'][i];
			if (typeof(d)=='function') continue; //-- jquery patch;
			if (d==undefined) continue;
			//--			
			var area = new TArea({parent:this, alt:i});
			area.update( dane['mapa_glowna'][i] );
			//--
			this.lista[ this.lista.length ]	= area;
			//--
			this.mpolska.apC( area );
		}
		
		//-- ustawienie rejonów na mapie;
		if (dane['values']!= undefined)
		if (dane['mapa_glowna']!= undefined)
		{
			//-- odznaczenie poprzednich;
			for (var i in this.lista)
			{
				if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
				if ( this.lista[ i ].active ) this.lista[ i ].set(false);
			}

			for (var i in this.lista)
			{
				if (typeof(this.lista[i])=='function') continue; //-- jquery patch;
				//-
				if (dane['values']['mapa_glowna'][this.lista[i].rejon_id] != undefined)
					this.lista[ i ].set(true);
			}
		}
		$("map > area").tooltip({ positionRight: true });	
	}
	//--
	this.obj.rysuj();
	return this.obj;
}





/**
 * Klikany obszar;
 */
TArea = function()
{
	var param = { shape:'poly',active:false,rejonOver:false, title: this.name };
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	//--
	this.obj	= _c('area',param);
	this.obj.rysuj = function( dane ){ this.update( dane ); }
	this.obj.onclick = function( )
	{
		try { motyw.play( 'klik' ) } catch( e ){ };
		//--
		if (!this.parent.multiselect_on)	this.parent.deselectAll();
		//--		
		this.set();
		this.sendValue( this.rejon_id );
		var host = window.location.hostname;
		window.location = 'http://'+host + '/strona_v2/rej_id/'+ this.rejon_id ;
		//--
		if (typeof(this.parent.onSelect)=='function') this.parent.onSelect( this.rejon_id );
	}
	this.obj.onmouseover = function( )
	{	
		var akapit 			= _get("nazwamiasta");
		//akapit.innerHTML = TMapaZmienNazwePowiatuNaSkie(this.tytul); 
		akapit.innerHTML	= this.tytul;
		var kod_pocztowy 	= _get('inputFilter');
		kod_pocztowy.value	= '';//this.kod_pocztowy; 
		//this.showRejon();
		this.set();
		if (typeof(this.parent.onSelect)=='function') this.parent.onSelect( this.rejon_id );
	}
	this.obj.onmouseout = function( )
	{
		//this.showRejon();
		this.set();
		if (typeof(this.parent.onSelect)=='function') this.parent.onSelect( this.rejon_id );
		//_get("mapapolski").value=''; 
	}	
	this.obj.set = function(bool)
	{
		if (bool != undefined) this.active = !bool;
		if (!this.active)
		{
			this.active = true;
			this.parent.apC( this.rm );
			//--
			if (this.parent.legenda != undefined)
				this.parent.legenda.add( this.rejon_id, this.name );
		}
		else
		{
			this.active = false;
			this.parent.removeChild( this.rm );
			//--
			if (this.parent.legenda != undefined)
				this.parent.legenda.usun( this.rejon_id );
		}		
	}
	this.obj.showRejon = function()
	{
		if (bool != undefined) this.rejonOver = !bool;
		if (!this.rejonOver)
		{
			//console.log(this.name);
			this.rejonOver = true;
			this.parent.apC( this.rm );
		}
		else
		{
			this.rejonOver = false;
			this.parent.removeChild( this.rm );
		}		
	}
	
	this.obj.isSelected	= function(){ return this.active; }
	this.obj.sendValue = function()
	{	
		if (!this.parent.send_on) return false;
		
		setByAjax( '/strona_v2/ajax/set/', this, {_set_action:'rejon_z_mapy',
			_set:this.rejon_id, value:((this.active)?1:0) }
			);
	}
	this.obj.goLocation = function()
	{

	}
	this.obj.delay_goLocation = function(){ setTimeout('this.goLocation()',500); }
	this.obj.dalej = function()
	{
		var host = window.location.hostname;
		window.location = 'http://'+host + '/strona_v2/gazeta_v2/';
	}
			
	this.obj.onSetByAjaxEnd	= function()
	{
		if (typeof(this.parent.onSetByAjaxEnd) == 'function')
			this.parent.onSetByAjaxEnd();
	}
	this.obj.load = function( dane ){ this.update( dane ); }
	this.obj.update = function( dane )
	{
		if (dane==undefined) return false;
		if (dane['coords'] == undefined) return false;
		//--
		this.title      = dane['tytul']
		this.tytul      = dane['tytul']
		this.name		= dane['name'],
		this.rejon_id	= dane['id'],
		this.kod_pocztowy = dane['kod_pocztowy'],
		this.coords		= dane['coords'].toString();
		//-- rejon / obrazek;
		this.rm = new TRejonMap({parent:this});
		
		this.rm.update( dane );
	}
	return this.obj;
}


/**
 * Widoczny fragment mapy; 
 */
TRejonMap = function()
{
	var param = { katalog : '/images/01/80p/rejony12/' };
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	//--
	this.obj = _c('img',param);
	this.obj.rysuj = function( dane )
	{
		this.hint = _c('div');
		this.update( dane );
	}
	this.obj.load = function( dane ){ this.update( dane ); }	
	this.obj.update = function( dane )
	{
		if (dane==undefined) return false;
		if (dane['name']==undefined) return false;
		//--
		//this.src 			= this.katalog + dane['name']  + '.png';
		this.src 			= this.katalog + dane['name']  + '_kropka' +'.png';
		this.className 		= 'rejonmap ' + dane['name'];
		this.title			= dane['tytul'];
		this.tytul			= dane['tytul'];
		this.hint.innerHTML = dane['tytul'];
	}
	this.obj.rysuj();
	return this.obj;
}




/**
 * Legenda z rejonami z mapy;
 */
TMapaLegenda = function()
{
	var param = {
			className:'legenda'
		};
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	//--
	this.obj = _c('div',param);
	this.obj.rysuj = function( dane )
	{
		this.lista	= new Array();
		this.kontener	= _c('div',{className:'CheckBoxGroup'});
		this.apC( this.kontener );
		//--
		this.update( dane );
	}
	this.obj.load = function( dane ){ this.update( dane ); }	
	this.obj.update = function( dane )
	{
		if (dane == undefined) return false;
		this.usunAll();
		//--
		for ( var i in dane )
		{
			if (typeof( dane[i] )=='funciton') continue;
			//--
			this.add( dane[i] );
		}
	}
	//-- dodaje element do listy;
	this.obj.add	= function( rejon_id, name )
	{
		var c = new TMapaLegendaElement({parent:this});
		//--
		c.setOpis( name );
		c.setRejonId( rejon_id );
		//--
		this.lista[ this.lista.length ] = c;
		//-- dodanie do DOM;
		this.kontener.apC( c );
	}
	//-- usuwa element z listy po rejon id;
	this.obj.usun	= function( rejon_id )
	{
		for( var i in this.lista )
		{
			if ( typeof( this.lista[i] ) == 'function' ) continue;
			if ( this.lista[i].rejon_id == rejon_id )
			{
				//-- odłączenie od widoku;
				this.kontener.removeChild( this.lista[i] );
				//-- usunięcie z listy;
				this.lista.splice(i, 1);
			}
		}
	}
	this.obj.usunAll	= function()
	{
		while( this.lista.length > 0 )
			for( var i in this.lista )
			{
				if ( typeof( this.lista[i] ) == 'function' ) continue;
				this.usun( this.lista[i].rejon_id );
				break;
			}
	}
	this.obj.find	= function( param, value )
	{
		for( var i in this.lista )
		{
			if ( typeof( this.lista[i] ) == 'function' ) continue;
			if (this.lista[i][param]==value) return this.lista[i];
		}
		return undefined;
	}
	this.obj.sendRejonId	= function( rejon_id ){  }	// virtualna;
	//--
	this.obj.rysuj();
	return this.obj;
}



/**
 * Element legendy z rejonami z mapy;
 */
TMapaLegendaElement = function()
{
	var param = { rejon_id:0, className:'CheckBoxGroupEl' };
	for (var i in arguments[0]) if (typeof(arguments[0][i]!= 'function')) param[i]=arguments[0][i];
	//--
	var obj = _c('div',param);
	obj.rysuj = function( dane )
	{
		this.check		= _c('input',{type:'checkbox',parent:this,name:'rejon_id',className:'c'});
		this.b_check	= _c('input',{parent:this,type:'button',value:'',className:'a_checked'});
		this.sp			= _c('span',{parent:this,className:'opis'});
		//--
		this.check.hide();
		this.apC(this.check);
		this.apC(this.b_check);
		this.apC(this.sp);
		//--
		this.b_check.onclick	=	function(){ this.parent.sendRejonId(); }
	}
	obj.usunSelf	= function(){ this.parent.usun( this.rejon_id ); }
	obj.setOpis		= function( opis ){ this.sp.innerHTML = opis; }
	obj.setRejonId	= function( id ){ this.rejon_id = id; }
	obj.sendRejonId	= function(){ this.parent.sendRejonId( this.rejon_id ); }
	//--
	obj.rysuj(); 
	return obj;
}


