/*
window.addEvent('domready', function()
{
	loadDefaultMap();
});
*/
function loadDefaultMap(mapUid, itemUid)
{
	myBox = new HpoBox(450, 300); 
	if ( mapUid )
	{
		if ( !itemUid )
			itemUid = 0;
	
		myBox.loadDatas(myBox.datasUrl + '&tx_guyanepresta_pi3[mapUid]='+ mapUid +'&tx_guyanepresta_pi3[uid]='+ itemUid);
	}
	else
		myBox.loadDatas(myBox.datasUrl);
}

var HpoBoxObj;

var HpoBox = new Class({
	border: 10,
	datasUrl : '?type=65',

	initialize: function(boxWidth, boxHeight) 
	{
		HpoBoxObj = this;
		
		this.boxWidth = boxWidth;
		this.boxHeight = boxHeight;
		
		this.win = $$('.presta-window');
		this.winContent = $$('.presta-window-content');
		this.mapContainer = $$('.presta-window-content .mapContainer');
		this.detailsContainer = $$('.presta-window-content .detailsContainer');
		this.closeLink = $$('.presta-window-content .detailsContainer .close');
		this.catContainer = $$('.presta-window-content .detailsContainer .catContainer');
		this.backLinkContainer = $$('.presta-window-content .detailsContainer .backLink');
		
		this.setPositions();
	},
	
	setPositions: function() 
	{
		$$('select').setStyle('display', 'none');
		
		this.win.setStyle('display', 'block');
		this.win.setStyle('position', 'absolute');
		this.win.setStyle('top', (window.getSize().scroll.y-30));
		this.win.setStyle('left', '0px');
		this.win.setStyle('height', (window.getSize().size.y+30));
		this.win.setStyle('width', window.getSize().size.x);
		this.win.setStyle('opacity', '.7');
		this.win.setStyle('zIndex', '9000');
		
		this.boxTop  = ((window.getSize().size.y - this.boxHeight) / 2);
		this.boxLeft = ((window.getSize().size.x - this.boxWidth) / 2);
		
		this.winContent.setStyle('display', 'block');
		this.winContent.setStyle('position', 'absolute');
		this.winContent.setStyle('top', (window.getSize().scroll.y + this.boxTop));
		this.winContent.setStyle('left', this.boxLeft-this.border);
		this.winContent.setStyle('height', this.boxHeight);
		this.winContent.setStyle('width', this.boxWidth);
		this.winContent.setStyle('zIndex', '9001');
		
		this.win.addEvent('click', function()
		{
			HpoBoxObj.close();
		});
		this.closeLink.addEvent('click', function()
		{
			HpoBoxObj.close();
		});
		window.addEvent('keydown', function(e)
		{
			if ( HpoBoxObj.win.getStyle('display') == 'none' )
				return false;
				
			e = new Event(e);
			if ( e.key == 'esc' )
				HpoBoxObj.close();
		});
		window.addEvent('scroll', function()
		{
			if ( HpoBoxObj.win.getStyle('display') == 'none' )
				return false;
				
			HpoBoxObj.onScroll();
		});
		window.addEvent('resize', function()
		{
			if ( HpoBoxObj.win.getStyle('display') == 'none' )
				return false;
				
			HpoBoxObj.setPositions();
		});
	},
	
	onScroll: function()
	{
		this.win.setStyle('top', (window.getSize().scroll.y - 30));
		this.winContent.setStyle('top', (window.getSize().scroll.y + this.boxTop));
	},
	
	close: function()
	{
		$$('select').setStyle('display', 'block');
		this.win.setStyle('display', 'none');
		this.winContent.setStyle('display', 'none');
	},
	
	loadDatas: function(url)
	{
		var request = new Json.Remote(url, 
		{
			onComplete: function(jsonObj) 
			{
				HpoBoxObj.boxWidth = jsonObj.map.width;
				HpoBoxObj.boxHeight = jsonObj.map.height;
				
				var map = new Element('img');
				map.src = jsonObj.map.src;
				
				HpoBoxObj.mapContainer.empty();
				HpoBoxObj.mapContainer.adopt(map);
				
				var i = 0;
				while ( jsonObj.submaps )
				{
					++i;
					eval('var submap = jsonObj.submaps._'+ i +';');
					if ( !submap )
						break;
						
					var submapLink = new Element('a');
					submapLink.setStyle('display', 'block');
					submapLink.setStyle('position', 'absolute');
					submapLink.setStyle('top', submap.top);
					submapLink.setStyle('left', submap.left);
					submapLink.setStyle('height', submap.height);
					submapLink.setStyle('width', submap.width);
					submapLink.addClass('submap');
					submapLink.setStyle('opacity', '.5');
					
					submapLink.setAttribute('href', '#');
					submapLink.setAttribute('rel', submap.uid);
					
					submapLink.addEvent('click', function(e)
					{
						e = new Event(e);
						e.stop();
						HpoBoxObj.loadDatas( HpoBoxObj.datasUrl +'&tx_guyanepresta_pi3[mapUid]='+ this.getAttribute('rel'))
					});
				
					HpoBoxObj.mapContainer.adopt(submapLink);
				}
				
				if ( jsonObj.map.parent_uid > 0 )
				{
					HpoBoxObj.backLinkContainer.setStyle('display', 'block');
					HpoBoxObj.backLinkContainer.getElement('.parent').removeEvents('click');
					HpoBoxObj.backLinkContainer.getElement('.parent').addEvent('click', function(e)
					{
						e = new Event(e);
						e.stop();
						HpoBoxObj.loadDatas(HpoBoxObj.datasUrl + '&tx_guyanepresta_pi3[mapUid]='+ jsonObj.map.parent_uid);
					});
					HpoBoxObj.backLinkContainer.getElement('.general').removeEvents('click');
					HpoBoxObj.backLinkContainer.getElement('.general').addEvent('click', function(e)
					{
						e = new Event(e);
						e.stop();
						HpoBoxObj.loadDatas(HpoBoxObj.datasUrl);
					});
				}
				else
					HpoBoxObj.backLinkContainer.setStyle('display', 'none');
				
				if ( jsonObj.details == 1 )
				{
					HpoBoxObj.detailsContainer.setStyle('display', 'block');
					HpoBoxObj.boxHeight += HpoBoxObj.detailsContainer.getSize()[0].size.y;
					
					if ( jsonObj.cat )
					{
						HpoBoxObj.catContainer.empty();
						
						i = 0;
						while ( jsonObj.cat )
						{
							++i;
							eval('var cat = jsonObj.cat._'+ i +';');
							if ( !cat )
								break;
								
							var label = new Element('label');
							
							var checkbox = new Element('input',
													{
														type: 'checkbox',
														value: cat.uid,
														checked: 'checked'
													});
													
							checkbox.addEvent('change', function()
							{
								checkboxList = this.getParent().getParent().getElements('input[type=checkbox]');
								checkboxListValue = '';
								checkboxList.each(function(item) 
								{
									if ( item.checked )
									{
										if ( checkboxListValue )
											checkboxListValue = checkboxListValue +',';
										checkboxListValue = checkboxListValue + item.value;
									}
								});
								
								urlPoiList = url + '&tx_guyanepresta_pi3[themes]='+ checkboxListValue;
								
								var requestPoi = new Json.Remote(urlPoiList, 
								{
									onComplete: function(jsonObj) 
									{
										$$('.poi').each(function(item)
										{
											item.remove();
										});
										if ( jsonObj.poi )
											HpoBoxObj.listPoi(jsonObj.poi);
									}
								}).send();
							});
													
							label.adopt(checkbox);
							
							var text = new Element('span').setText(cat.name);
							label.adopt(text);
							
							HpoBoxObj.catContainer.adopt(label);
						}
						
					}
					
					if ( jsonObj.poi )
						HpoBoxObj.listPoi(jsonObj.poi);
						
					
					if ( jsonObj.otherMaps )
					{
						HpoBoxObj.buildArrow(jsonObj.otherMaps.top, 'top');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.topLeft, 'top-left');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.topRight, 'top-right');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.left, 'left');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.right, 'right');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.bottom, 'bottom');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.bottomLeft, 'bottom-left');
						HpoBoxObj.buildArrow(jsonObj.otherMaps.bottomRight, 'bottom-right');
					}
					
				}
				else
					HpoBoxObj.detailsContainer.setStyle('display', 'none');
					
				HpoBoxObj.setPositions();
			}
		}).send();
	},
	
	buildArrow: function(uid, position)
	{
		if ( uid <= 0 )
			return false;
			
		var arrow = new Element('a', {'rel': uid})
		arrow.addClass('arrow');
		arrow.addClass('arrow-'+ position);
		
		arrow.addEvent('click', function(e) 
		{
			e = new Event(e);
			e.stop();
			HpoBoxObj.loadDatas(HpoBoxObj.datasUrl + '&tx_guyanepresta_pi3[mapUid]='+ this.getAttribute('rel'));
		});
				
		this.mapContainer.adopt(arrow);
	},
	
	listPoi: function(jsonPoi)
	{
		var i = 0;
		while ( 1 )
		{
			++i;
			eval('var poi = jsonPoi._'+ i +';');
			if ( !poi )
				break;
				
			var img = new Element('img', 
								{
									'src': poi.icon,
									'alt': poi.name,
									'rel': poi.link
								});
			img.addClass('poi');
			img.setStyle('position', 'absolute');
			img.setStyle('top', poi.top);
			img.setStyle('left', poi.left);
			img.setStyle('width', '14px');
			img.setStyle('height', '14px');
			img.setStyle('border', poi.cssBorder);
			
			this.mapContainer.adopt(img);
			
			img.addEvent('click', function()
			{
				window.document.location = '/'+ this.getAttribute('rel');
			});
			img.addEvent('mouseover', function()
			{
				if ( !this.getAttribute('alt') )
					return ;
			
				var div = new Element('div').setText(this.getAttribute('alt'));
				div.addClass('bulle');
				div.setStyle('position', 'absolute');
				div.setStyle('top', (this.getStyle('top').toInt()-20));
				div.setStyle('left', this.getStyle('left'));
				
				HpoBoxObj.mapContainer.adopt(div);
				
				this.addEvent('mouseout', function()
				{
					div.remove();
				});
			});
		}
	}
	

});