//šš
var weather;
var ept=null;

function RefMin()
{
	var myFx = new Fx.Style('refmapdesk','width',
	{
		duration:10,
		onStart:function()
		{
			$('refmapdeskcls').onclick=null;
		},
		onComplete:function()
		{
			$('refmapwin').style.display='none';
			$('refmapdeskcls').src='http://mapy.atlas.sk/design/refmap/ref_max.gif';
			$('refmapdeskcls').onclick=RefMax;
			$('clickerref').onclick=RefMax;
			$('refmapdeskcls').title='Otvoriť';
			$('refmapdeskcls').alt='Otvoriť';
		}
	}).start(170,11);
	myFx = null;
}
function RefMax()
{
	var myFx = new Fx.Style('refmapdesk','width',
	{
		duration:10,
		onStart:function()
		{
			$('refmapdeskcls').onclick=null;
			$('refmapwin').style.display='block';
		},
		onComplete:function()
		{
			$('refmapdeskcls').src='http://mapy.atlas.sk/design/refmap/ref_min.gif';
			$('refmapdeskcls').onclick=RefMin;
			$('clickerref').onclick=RefMin;
			$('refmapdeskcls').title='Zatvoriť';
			$('refmapdeskcls').alt='Zatvoriť';
			var cor=$('mappa_in').ShCoord();
			//$('refmapwin_in').moveTo(cor.centerX,cor.centerY);
			$('refmapwin_in').refholderMove();
			cor=null;
		}
	}).start(11,170);
	myFx = null;
}

MD=({
	ajax:function(u,p,f)
	{
		if(this.htr!=null){document.htr.abort();document.htr=null;}
		this.htr=new Ajax(u,{
			method:'post',
			postBody:p,
			onComplete:function(res)
			{
				document.htr=null;
				document.ajaxexec(f,res);
			}}).request();
	},
	ajaxexec:function(fun,param)
	{
		//alert(fun); alert(param);
		eval(fun+'('+param+');');
	},
	resetTool:function()
	{
		return;
	},
	redrawmap:function()
	{
		weather.getPoints();
		return;
	},
	onmouseup:function()
	{
		if(this.mov)
		{
			$(this.mov).onmouseup();
			this.mov=null;
		}
		return;
	},
	onmousemove:function(e)
	{
		e=new Event(e);
		if(this.mov)
		{
			$(this.mov).drag(e);
		}
		e=null;
	},
	onmousedown:function()
	{
		return;
	}
});


window.addEvent('domready', function()
{
	var sz=$('mapmaindesk').getSize();
	weather=new _weather();
	$extend(document,MD);
	$extend($('mappa_in'),mapC);
	$extend($('refmapwin_in'),mapC);
	$('mapmaindesk').style.width=(sz.size.x)+'px';
	$('mappa').style.cssText='width:'+(sz.size.x)+'px;height:'+sz.size.y+'px';
	$('mappa_in').ini((sz.size.x),sz.size.y,false,'refmapwin_in');
	sz=null;
	$('refmapwin_in').ini(161,114,true,'mappa_in');
	$extend($('slider'),_slider);
	$extend($('slid'),_slidearea);
	$('slider').ini('mappa_in');
	$('slid').ini('slider');
	$('mappa_in').refmapmoved=false;
	$('mappa_in').bboxZoom({x:x_min,y:y_min,cS:cs},{x:x_max,y:y_max,cS:cs}); //nastavi sa defaultne mapa...
	RefMin();
});
function closSrWin()
{
	$('srout').style.display='none';
	$('norespt').style.display='none';
	$('respt').style.display='none';
}



//vrati cestu k aktualnemu skriptu !
function domena(){
	return 'http://'+document.location.host+'/';
}

_weather=new Class({
	results:{},
	POINTSM:[],
	POINTSD:{},
	POINTS:{},
	box1:null,
	box2:null,
	box3:null,
	mesto:null,
	text:null,
	initialize:function()
	{
		return;
	},
	getPoints:function()
	{
		var cor=$('mappa_in').ShCoord();
		var minx=cor.minX;
		var maxy=cor.maxY;
		
		if($('mappa_in').coordS!=1)
		{
			var pt={x:minx,y:maxy,cS:$('mappa_in').coordS};
			pt=ConvKlasTrans.doFoo(pt,1);
			minx=pt.x;
			maxy=pt.y;
			pt=null;
		}
		
		var maxx=minx+$('mappa_in').px2len($('mappa_in').viewportW);
		var miny=maxy-$('mappa_in').px2len($('mappa_in').viewportH);
		var poststr='';
		poststr+='xmin='+minx;
		poststr+='&xmax='+maxx;
		poststr+='&ymin='+miny;
		poststr+='&ymax='+maxy;
		poststr+='&cS='+$('mappa_in').coordS;
		poststr+='&scale='+$('mappa_in').scale;
		//alert(path()+"pocasie_ajax.php");
		document.ajax(domena()+"pocasie_ajax.php",poststr,'weather.writeRes');
		//kurwafix document.location je tam ako prefix, preto aby to fungovalo kdekolvek tak to nemente, sa potom budeme cudovat ze preco to nejde.
	},
	writeRes:function(points)
	{
		var tmppointsm = []; //pole prave ziskanych bodov
		var tmppoints = {};
		//natiahnu sa nove body z ajaxu
		for(i=0;i<points.length;i++)
		{
			tmppointsm[i]=points[i].pid;
			tmppoints[points[i].pid]=points[i];
			
		}
		
		//spravi sa rozdiel bodov ktore su uz nacitane a ktore sa prave nacitali.
		for(i=0;i<this.POINTSM.length;i++)
		{
			if(!tmppointsm.contains(this.POINTSM[i]))
			{
				this.removePoint(this.POINTSM[i]);
				this.POINTSM[i]= null; 
			}
		}
		this.POINTSM = tmppointsm;
		this.POINTS = tmppoints;
		
		tmppoints=null; tmppointsm=null; 
		this.drawPoints();
	},
	drawPoints:function()
	{
		if(this.POINTSM.length>0)
		{
			var cor=$('mappa_in').ShCoord();
			var cS=$('mappa_in').coordS;
			var xmin=cor.minX
			var xmax=cor.minX+$('mappa_in').px2len($('mappa_in').viewportW);
			var ymax=cor.maxY;
			var ymin=cor.maxY-$('mappa_in').px2len($('mappa_in').viewportH);
			var i;
			var x;
			var y;
			var ptid;
			var tmppoints=[];
			
			for(i=0;i<this.POINTSM.length;i++)
			{
				
				pid=this.POINTSM[i];
				
				if (pid){ //ak nema pid tzn. ze bod sa vymazal tak nech to taha do prec...
					
					if(this.POINTS[pid].coord.cS!=cS) this.POINTS[pid].coord=ConvKlasTrans.doFoo(this.POINTS[pid].coord,cS);
					x=this.POINTS[pid].coord.x;
					y=this.POINTS[pid].coord.y;
					if(x>=xmin && x<=xmax && y>=ymin && y<=ymax)
					{
						if(!this.POINTSD[pid])
						{
							tmppoints[pid]=this.drawPoint(this.POINTS[pid]);
						}
						else
						{
							tmppoints[pid]=this.POINTSD[pid];
						}
						tmppoints[pid].sredraw();
					}
					else
					{
						this.removePoint(pid); //vymaze ak je mimo dosahu
					}
				} else {
					this.removePoint(pid); //vymaze ak nema pid
				}
				
			}
			this.POINTSD=tmppoints;
			tmppoints=null;
		}
		return;
	},
	removeRes:function()
	{
		for(i=0;i<this.POINTSM.length;i++)
		{
			var ptid=this.POINTSM[i];
			this.results[ptid].destr();
			this.results[ptid].remove();
			this.results[ptid]=null;
		}
	},
	removePoint:function(ptid)
	{
		if(this.POINTSD[ptid])
		{
			this.POINTSD[ptid].destroy();
			this.POINTSD[ptid]=null;
		}
	},
	drawPoint:function(po)
	{
		var el=new _weatherPoint(po);
		return el;
	}
});

_weatherPoint=new Class({
	po:null,
	ico:({w:45,h:36}),
	initialize:function(po)
	{
		this.po=po;
		this.el=new Element('div');
		this.el.className='pointdiv';
		this.el.injectInside('mappa_in');
		//this.icn=new Element('img');
		//this.icn.src='images/spacer.gif';
		//this.el.width=this.ico.w;
		//this.el.height=this.ico.h;
		//this.icn.injectInside(this.el);*/
		//this.el.style.cssText='width:'+this.ico.w+'; height:'+this.ico.h+'; position:absolute;background-image: url('+domena()+'images/pocasie/listing/ico_'+this.po.icon+'.png);#background-image: url(images/spacer.gif);#filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+domena()+'images/pocasie/listing/ico_'+this.po.icon+'.png\',sizingMethod=\'crop\');';
		/*alert(this.icn.style.cssText);*/
		this.el.innerHTML='<img style="background-image: url('+domena()+'images/pocasie/listing/ico_'+this.po.icon+'.png);#background-image: url(images/spacer.gif);#filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+domena()+'images/pocasie/listing/ico_'+this.po.icon+'.png\',sizingMethod=\'crop\');" src="images/spacer.gif" alt="'+this.po.nm+'" title="'+this.po.nm+'" width="'+this.ico.w+'" height="'+this.ico.h+'" >';
		this.el.addEvent('click',this.fillBoxes.bind(this));
		this.el.addEvent('mouseenter',this.hilit.bind(this));
		this.el.addEvent('mouseleave',this.dehilit.bind(this));
		this.sredraw();
		return this;
	},
	hilit:function(e)
	{
		e=new Event(e);
		e.stop();
		e=null;
		this.el.style.zIndex='999';
	},
	dehilit:function(e)
	{
		e=new Event(e);
		e.stop();
		e=null;
		this.el.style.zIndex='50';
	},
	sredraw:function()
	{
		var cS=$('mappa_in').coordS;
		var tmp_coord=this.po.coord  
		if(tmp_coord.cS!=cS) tmp_coord=ConvKlasTrans.doFoo(tmp_coord,cS);  
		var pcor=$('mappa_in').coordToPos(tmp_coord.x,tmp_coord.y);
		/*if(this.po.coord.cS!=cS) this.po.coord=ConvKlasTrans.doFoo(this.po.coord,cS);
		var pcor=$('mappa_in').coordToPos(this.po.coord.x,this.po.coord.y);*/
		this.el.style.cssText='cursor:pointer;width:'+(this.ico.w)+'px;height:'+(this.ico.h)+'px;z-index:50;left:'+(pcor.px-Math.floor(this.ico.w/2))+'px;top:'+(pcor.py-Math.floor(this.ico.h/2))+'px;';
		return;
		
		
	},
	destroy:function()
	{
		this.el.empty();
		this.el.remove();
		this.el=null;
		this.po=null;
		return;
	},
	fillBoxes:function(e)
	{
		e=new Event(e);
		e.stop();
		e=null;
		
		var box_1 = $('box_1');
		var box_2 = $('box_2');
		var box_3 = $('box_3');
		if (!this.po.box1 && !this.po.box2 && !this.po.box3){
			box_1.style.display='none';
			box_2.style.display='none';
			box_3.style.display='none';
		} else {
			if (box_1.style.display=='none'){
				box_1.style.display='block';
				box_2.style.display='block';
				box_3.style.display='block';
			}
						
			box_1.innerHTML=this.po.box1;
			box_2.innerHTML=this.po.box2;
			box_3.innerHTML=this.po.box3;
		}
		
		$('mesto').innerHTML=this.po.mesto;
		$('text').innerHTML=this.po.text;
	}
});

