// JavaScript Document

//General Overlay Collection
var overlays;

overlays = new OverlayCollection('overlays',25);
//Settings
overlays.useLabels = true;
overlays.labelType= 'Numeric';


overlays.hidePlacemarkListingsContainer = function(){
	var container = $('placemarkContainer');
	var list = $('placemarkList');
	if(container == null)
		container = list;
		
	if(container != null){
		container.style.display = 'none';
	}
}

overlays.showPlacemarkListingsContainer= function(){
	var container = $('placemarkContainer');
	var list = $('placemarkList');
	if(container == null)
		container = list;
		
	if(container != null){
		container.style.display = '';
	}
}

//Clear Placemarks
overlays.clearPlacemarkListings = function(){
	var list = $('placemarkList');
	this.hidePlacemarkListingsContainer();	
	if(list != null){
		list.innerHTML = '';
	}
}

//Show Placemarks
overlays.showPlacemarkListings = function(){
	this.clearPlacemarkListings();

	if(this.length > 0){
		var placemarks = this.getPage(this.currPage);
		for (var i = 0; i < placemarks.length; i++){
			this.addToList(placemarks[i]);
		}
		this.showPlacemarkListingsContainer();		
	}

	this.showPaging();
}


//Show Placemarks
overlays.markPlacemarkListings = function(){
	if(this.length > 0){
		var placemarks = this.getPage(this.currPage);
		for (var i = 0; i < placemarks.length; i++){
				var spot = $('placemark_' + placemarks[i].prikey);
				if(spot != null){ 				
					spot.innerHTML = parsePlacemarkData('<img src="placemark:iconimage" hspace=\"3\" width=\"24\" height=\"19\"  style="position: absolute; margin-top: -5px">',placemarks[i]);
				}
		}
	}
}

//Clear Placemarks
overlays.unmarkPlacemarkListings = function(){
	if(this.length > 0){
		var placemarks = this.getPage(this.currPage);
		for (var i = 0; i < placemarks.length; i++){
				var spot = $('placemark_' + placemarks[i].prikey);
			if(spot != null){				
				spot.innerHTML = '';
			}
		}
	}
}

overlays.addToList = function(placemark){
	var list = $('placemarkList');
	if(list != null){
		var d= document.createElement('div');
		d.innerHTML += parsePlacemarkData('<div class=\"Explorer_Listing">' + (placemark.logofile.length ? '<img class=\"PropertyPhoto\" src=\"../includes/images/listings/placemark:logofile\"  width="160" height="106" align="left">' : '<img class=\"PropertyPhoto\" src="../includes/images/blank.gif" width="160" height="106" align="left" />') + '<a href="javascript:goToPlacemark(placemark:prikey)" class="explorerListingTitle">placemark:name</a><br><div class="explorerListingAddress">placemark:address</div><br><img src="placemark:iconimage" align="right" width="24" height="19"><div class=\"explorerListingDescription\">placemark:description</div></div>',placemark);
		list.appendChild(d);
	}
}

/*
overlays.showPaging = function(){
	var page = this.currPage;
	var numPages = this.numPages();
	var overlayCollection = this;

	//Clear Paging Links
	$('paging_top').innerHTML = $('paging_bottom').innerHTML  = '';

	//Show/Hide Paging Divs
	$('paging_top').style.display = $('paging_bottom').style.display = (numPages > 1 ? '' : 'none');

	for (var i = 1; i < numPages; i++){
		var pageidtop = 'page_top_' + i;
		var pageidbottom = 'page_bottom_' + i;
		//Output Regular pages
		if(i != page){
			var a1 = document.createElement('A');
			var a2 = document.createElement('A');

			a1.id  = pageidtop;
			a2.id = pageidbottom;
			a1.href = a2.href = '##page' + i;
			a1.innerHTML = a2.innerHTML = i;
			a1.className = a2.className = 'explorer_pagelink';
			a1.page_num = a2.page_num = i;
			var _this = this;
			Event.observe(
				a1,
				'click',
				function(){
					_this.map.showPlacemarks(overlayCollection,''+this.page_num+'')
				}
			);
			Event.observe(
				a2,
				'click',
				function(){
					_this.map.showPlacemarks(overlayCollection,''+this.page_num+'')
				}
			);
			
			$('paging_top').appendChild(a1);
			$('paging_bottom').appendChild(a2);

		}
		//Selected Page
		else{
			var s1 = document.createElement('SPAN');
			s1.id = pageidtop;
			var s2 = document.createElement('SPAN');
			s2.id = pageidbottom;
			s1.className = 'explorer_selpage';
			s2.className = 'explorer_selpage';
			s1.innerHTML = i;
			s2.innerHTML = i;
			
			$('paging_top').appendChild(s1);
			$('paging_bottom').appendChild(s2);			
		}
	}
}
*/

overlays.showPaging = function(){
	var page = this.currPage;
	var numPages = this.numPages();
	var overlayCollection = this;
	var pagingstr = '<div style=\"float: left; width: 95px\">Page ' + this.currPage + ' of ' + numPages + '</div>';
	pagingstr += '<div style=\"float: right; text-align: right; width: 375px; margin: 0px auto;\">';
	if(page > 1){
		pagingstr += '<a href="#" class=\"page_first\">First</a>';
		pagingstr += ' | <a href = "#" class=\"page_previous\">Previous</a>';
	}
	else{
		pagingstr += 'First';
		pagingstr += ' | Previous';
	}

	if(page < numPages){
		pagingstr += ' | <a href="#" class=\"page_next\">Next</a>';
		pagingstr += ' | <a href="#" class=\"page_last\">Last</a>';
	}
	else{
		pagingstr += ' | Next';
		pagingstr += ' | Last';
	}
	pagingstr += '</div>';

	$('paging_top').innerHTML = pagingstr;
	$('paging_bottom').innerHTML  = pagingstr;

	$('paging_top_container').style.display = $('paging_bottom_container').style.display = (numPages >= 1 ? '' : 'none');

	if(page > 1){
		var pagefirst = $$('.page_first');
		pagefirst.each(
			function(a){
				Event.observe(a,'click',function(e){
						overlayCollection.map.showPlacemarks(overlayCollection,1);
						Event.stop(e);
					}
				);
			}
		);

		var pageprevious = $$('.page_previous');
		pageprevious.each(
			function(a){
				Event.observe(a,'click',function(e){
						overlayCollection.map.showPlacemarks(overlayCollection,overlayCollection.currPage-1);
						Event.stop(e);
					}
				);
			}
		);
	}


	if(page < numPages){
		var pagenext = $$('.page_next');
		pagenext.each(
			function(a){
				Event.observe(a,'click',function(e){
						overlayCollection.map.showPlacemarks(overlayCollection,overlayCollection.currPage+1);
						Event.stop(e);
					}
				);
			}
		);


		var pagelast = $$('.page_last');
		pagelast.each(
			function(a){
				Event.observe(a,'click',function(e){
						overlayCollection.map.showPlacemarks(overlayCollection,overlayCollection.numPages());
						Event.stop(e);
					}
				);
			}
		);
	}



}

overlays.getInfoHTML = function(placemark){
	var addr = new Address();
	try{
		addr.loadFromObject(placemark);
	}
	catch(ex){
	}
	var str = new String();
	str = '<div class=\"infoWindowTitle\">placemark:name</div>';
	str += '<div class=\"infoWindowAddress\">' + addr.toHTMLString() + '</div>';
//	str += '<span onclick=\"svp();" class=\"fakelink\">Streetview</span>';
	str += '<ul class=\"infoWindowOptions\">';
	str += placemark.weburl.length > 0 ? '<li><a href=\"' + this.root +'/includes/redirects/webcount.cfm?listingID=placemark:prikey\" target=\"_blank\">Visit website</a></li>' : '';	
	str += '<li><a href=\"./index.cfm?action=display&listingid=placemark:prikey&hit=1">More Info</a></li>';
	str += '<li><a href=\"javascript:getDirections(placemark:prikey)">Directions</a></li>';
//	str += '<li><a href=\"javascript:directions.directionsFrom(placemark:prikey)\">Directions From Here</a></li>';
	str += '</ul">';
	if(placemark.distance){
			str += '<div class=\"infoWindowSection\">Distance: ' + placemark.distance.toFixed(1) + ' miles</div>';
	}

	return parsePlacemarkData(str,placemark);
}

overlays.updateBubble = function(args){
}