var map, map2;
var layer, layer2;
var features = new Array();
var hasFeatures = false;
var isFeatureBefore = false;
function initPage() 
{
	map = null;
	if (Utils.$("myMainMap"))
	{
		map = new GSMap("myMainMap", {centerOnDblClick:false});
		map.addControl(GSMap.ZOOM_CONTROL);
		layer = map.createLayer('main_display');
		if(isFirstPage){
			map.centerOnNewZealand();
			
		}
		map.updateSize();
		map.addListener(window);
	}
	
	

	map2 = new GSMap("myMap", {centerOnDblClick:false});
	map2.addControl(GSMap.ZOOM_CONTROL);
	layer2 = map2.createLayer('display');

	var lmnts = Utils.$$('map_alink');
	lmnts.reverse();
	hasFeatures = lmnts.length > 0 ? true : false;
	for (lmnt in lmnts)
		{	
			Event.attach(lmnts[lmnt], 'mousedown', moveMap, lmnts[lmnt]);
		
		}
		
	map2.addListener(window);
	
	window.currentMap = 0;
}

function mapDblClicked(map, object) 
{
  if(object instanceof GSPoint) 
  {
    map.zoom( map.getZoomLevel() - 1 );
  }
}

function moveMap()
{
	
	var tmp = this.id.split('_');
	moveMapContent(tmp);
}
function moveMapContent(tmp)
{
	var listing_id = tmp[0];
	var tail = '';
	if(tmp[tmp.length-1] =='featuredListing'){
		tail = 'featured_';
	}
	if (window.currentMap == listing_id){
		if((tail=='' && !isFeatureBefore) ||(tail == 'featured_' && isFeatureBefore)){
			return;
		}
	}
	var floatingMap = Utils.$("myMap");
	var targetMap = Utils.$("myMap_"+tail + listing_id);

	if (!targetMap || !floatingMap) return;

	var floatingParent = floatingMap.parentNode;
	floatingParent.removeChild(floatingMap);

	var targetParent = targetMap.parentNode;
	targetParent.removeChild(targetMap);

	if (window.currentMap != 0)
	{
		// Put the current map back in its place.
		if(isFeatureBefore){
			var currentMap = Utils.$("myMap_featured_"  + window.currentMap);
		}
		else{
			var currentMap = Utils.$("myMap_" + window.currentMap);
		}
		var currentParent = currentMap.parentNode;
		currentParent.removeChild(currentMap);
		floatingParent.appendChild(currentMap);
	}

	targetParent.appendChild(floatingMap);
	Utils.$('floatingMap_wrap').appendChild(targetMap);

	var geocode_x = tmp[1];
	var geocode_y = tmp[2];
	var zoom = tmp[3];
	zoom = zoom && (zoom >= 0 && zoom <= 11) ? zoom : 0;
	
	var name = urldecode(tmp[4]);
	name = name && name != '' ? name : '';

	layer2.clear();

	var coord = new GSPoint(geocode_x, geocode_y);
	map2.centerAndZoom(coord, zoom);

	var params2 = {"name" : name, "coordinate" : coord};
	var feature2 = new GSPointFeature(params2);
	layer2.addFeature(feature2);
	
	window.currentMap = listing_id;
	if(tail == ''){
		isFeatureBefore = false;
	}
	else{
		isFeatureBefore = true;
	}
}
//onclick
clickHandler = function( oEvent ) {
	//ddaccordion.collapseall("submenuheader2");
	
	var listing_id = this.data.listing_id;
	var url = window.location+"";
	var index = url.indexOf('#');
	var scroll = false;
	var new_index = this.data.index;
	if(index >0){
		url = url.substring(0,index);
	}
	var href = url+'#'+listing_id;
	var lmnts = Utils.$$('map_alink');
	
	var old_index = 0;
	if(window.currentMap > 0){
		var i = 0;
		for (lmnt in lmnts)
		{ 
			var note = lmnts[lmnt];
			var tmp = note.id.split('_');
			var note_listing_id = tmp[0];
			if(window.currentMap == note_listing_id){
				
				old_index = i; 
				break;
			}
			i = i + 1;
			
		}
		if(new_index > old_index){
			scroll = true;
		}
	}
	

	
	for (lmnt in lmnts)
	{ 
		var note = lmnts[lmnt];
		var tmp = note.id.split('_');
		var note_listing_id = tmp[0];
		if(tmp[tmp.length-1] !='featuredListing'){
			if(listing_id == note_listing_id){
				//isFeatureBefore = false;
				moveMapContent(tmp);
				break;
			}
		}
	}
	
	
	ddaccordion.expandone("submenuheader2", new_index);
	window.location.href = href;
	//window.location.hash = listing_id;
	if(scroll){
		window.scrollBy(0,-230);
	}

};
function delay(){
}

//GSMap.prototype.addLogo = function() {return;};

var current = window.onload;
window.onload = null;
if(isFirstPage){
	Event.onloadEvent(function () { current(); initPage(); loadMainFeatures(); callSearchResultsMapFunction();}, this);
}
function loadMainFeatures()
{
	if (map && layer){
		var lmnts = Utils.$$('map_alink');
		if (!lmnts || lmnts.length <= 0) return;
	
		map.centerOnNewZealand();
		map.updateSize();
		layer.clear();
		var constructPois = new Array();
		var i=  parseInt(lmnts.length,10)-1;
		lmnts.reverse();
		for (lmnt in lmnts){
			var tmp = lmnts[lmnt].id.split('_');
			var listing_id = tmp[0];
			var geocode_x = tmp[1];
			var geocode_y = tmp[2];

			var name = urldecode(tmp[4]);
			name = name && name != '' ? name : '';

			var coord = new GSPoint(geocode_x, geocode_y);

			var params = {"name" : name, "coordinate" : coord};
			var feature = new GSPointFeature(params);
			
			feature.data = {"listing_id":listing_id, 'index':i};
			i = i-1;
			if(tmp[tmp.length-1] !='featuredListing'){
				feature.addEventHandler('click', clickHandler);
				layer.addFeature(feature);
			}
		}
		
		map.updateSize();
		map.centerOnLayer("main_display");

		var zl = map.getZoomLevel();
		if (typeof onmapzoomtolayer != 'undefined') map.zoom(onmapzoomtolayer(zl));
		map.updateSize();
	}
}

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 40;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

