var setThumbs = Class.create()
setThumbs.prototype = {
	initialize:function(parent, frame){
		this.parent = $(parent);
		this.frame = $(frame);
		this.lastframed = null;
		this.init();
	},

	init:function(){
		var imgs = $A(this.parent.getElementsByTagName('img'));
		imgs.each(
			function(item){
				Event.observe(item, 'click', this.click.bindAsEventListener(this));
				if(item.className.match('loaded'))
				{
					this.lastframed = item;
				}
			}.bind(this)
		)
	},

	click:function(e){		
		var item = Event.element(e);
		if(this.lastframed == item) return;
		this.frame.style.display = 'none';
		this.lastframed.className = this.lastframed.className.replace('loaded','');
		item.className += ' loaded';
		this.lastframed = item;
		new resizeImage(item, this.frame, {width:430, height:430});
	}
}

function init()
{	
	try
	{
		new resizeImage($('default_foto'),'default_foto_holder', {width:430, height:430});
		new setThumbs('thumbsholder','default_foto_holder');
	}
	catch(e){}
}
var map; 
var info = '';
var marker = null;
var language = null;
var shortInfos = new Array();
var mapTrayShadow;
var mapTrayFloat;
var mapTryMarker;

function createInfoLabel()
{
	var label=document.createElement('div');
	label.setAttribute('id','MAP_infoLabel');
	label.style.display = 'none';
	document.body.appendChild(label);
	label.style.position = 'absolute';
	label.style.color = '#000';
	label.style.zIndex = 2;
	label.style.background = "url('szurke_hatter.gif') top repeat-x #ddd";
	label.style.height = '15px';
	label.style.fontSize = '7pt';
	label.style.whiteSpace = 'nowrap';	
	label.style.border = '#777 1px solid';
	label.style.padding = '2px 8px 0px 8px';
	return label;
}

function showInfo(id, top, left, content)
{	
	var div = $(id);
	var txt = content.split('|');
	div.innerHTML =  txt[1] + ' ' + (txt[2] != ''? ' - ' + txt[2] : '') + ' (' + txt[3] + ' méterre)';
	div.style.top = top + 'px';
	div.style.left =left + 'px';
	div.style.display = 'block';
}

function hideInfo(id)
{
	var div = $(id);
	div.style.display = 'none';					
}

// Alkalmazás inicializálása
function load() 
{
   if (GBrowserIsCompatible()) 
   {	  
      map = new GMap2(document.getElementById("al_inframap"));
      map.setCenter(new GLatLng(47.50143120349543, 19.044113159179688), 13); 
	  map.addControl(new GLargeMapControl());
   	  map.enableDoubleClickZoom();
	  map.enableContinuousZoom(); 

	  // infoLabel hozzáadása
	  mapTrayShadow = map.getPane(G_MAP_FLOAT_SHADOW_PANE);
	  mapTrayShadow.appendChild(createInfoLabel());	 	  
	  showAddress(lat, lng, map.getZoom());	   
   }
}

function load_points(data, spec)
{	 
   if(typeof spec == 'undefined') return;
   var datalength = data.length;	  
   var info = '';
	   
   bounds = new GLatLngBounds(new GLatLng(spec.maxlat, spec.minlng), new GLatLng(spec.minlat, spec.maxlng));
   var zoom = map.getBoundsZoomLevel(bounds);
   zoom = (zoom < 6)? 6 : zoom-2;
   map.setZoom(zoom-1);  

   for(var i=0; i<datalength; i++)
   {			 
	  smallicon = new GIcon();  		 		 		  
	  smallicon.image = Utils.ROOT + '/css/troli.gif';
	  smallicon.iconSize = new GSize(10,10);
	  smallicon.iconAnchor = new GPoint(5,5);		 		
		  		
	  info = data[i].id + '|' + data[i].type + '|' + data[i].optname + '|' + data[i].d;	 
	  shortInfos.push(info);

	  smallMarker = new GMarker(new GLatLng(data[i].lat , data[i].lng),{icon:smallicon});	
	  smallMarker.num = i;

	  GEvent.addListener(smallMarker, 'mouseover', function(){
		  var point = this.getLatLng();
		  var coords = map.fromLatLngToDivPixel(point);		  		  		  
	      hideInfo('MAP_infoLabel');
		  showInfo('MAP_infoLabel', parseInt(coords.y)-15, parseInt(coords.x)+15, shortInfos[this.num]);			  
	  });

	  GEvent.addListener(smallMarker, 'mouseout', function(){
		  hideInfo('MAP_infoLabel');
	  });

	  map.addOverlay(smallMarker);
   }	  	  
}

function showAddress(lat, lng, zoom) 
{  	
	var point = new GLatLng(lat,lng); 
	map.clearOverlays();
	map.setCenter(point, zoom);					
	if(marker)
	{
		map.removeOverlay(marker);
		marker = null;
	}
	smallicon = new GIcon();  		 		 		  
	smallicon.image = icon;
	smallicon.iconSize = new GSize(23,24);
	smallicon.iconAnchor = new GPoint(8,24);	
	marker = new GMarker(point, {icon:smallicon});   
	map.addOverlay(marker);								
	var lat = point.y;
	var lng = point.x;
	var oXML = new FCKXml();		    
	GDownloadUrl(Utils.ROOT + '/getpoints.php?lat=' + lat + '&lng=' + lng, function(txt){
		var data = eval(txt);
		//load_points(data[0], data[1][0]);
	});	
}

function setCloseButton()
{
	$('closeDetails').onclick = function()
	{
		window.close();
	}

	$('closeDetails').onmouseover = function()
	{
		this.className = this.className.replace('closer_off' , 'closer_on');
	}

	$('closeDetails').onmouseout = function()
	{
		this.className = this.className.replace('closer_on' , 'closer_off');
	}

	$('printDetails').onclick = function()
	{
		print();
	}

	$('sendPerEmail').onclick = function()
	{	
		alert($lang('underDevelopment'));
	}
}

window.onload = function()
{  
   var oXML = new FCKXml();
   oXML.LoadUrl(Utils.ROOT + '/data/language_hu.xml',function(xml){ 	 
	  language = xml;	  
	  init();	  
	  createInfoLabel();	
	  setCloseButton();
	  load();	  
   })
}

window.onunload = function()
{
	GUnload();
}
