var map = null;
var	prev;
var	next;
var	currentDiv;
var	shapeCount;
var	count;
var	divCount;
var	liCount;
var	LEGEND;
var currentshape;
function truncLegend(i){
map.HideInfoBox();
$("#pagesNav a#p"+currentDiv).removeClass("currentDiv");
$("#pagesNav a#p"+i).addClass("currentDiv");

var myVal = "";
$("#legend .list").hide();
$("#legend #u"+i).show();
currentDiv = i;

switch (currentDiv){
case divCount:
$("#next").addClass("inactive");
$("#prev").removeClass("inactive");
break;
case 1:
$("#prev").addClass("inactive");
$("#next").removeClass("inactive");
break;
default:
$("#next").removeClass("inactive");
$("#prev").removeClass("inactive");
};

if(count <=3){
$("#legend").css("height","40px");
} else if ((count >3) && (count <=6)){
$("#legend").css("height","74px");
}
}
function nextClick(){
if (currentDiv < divCount){
truncLegend(currentDiv + 1);
}
}
function prevClick(){
if (currentDiv > 1){
truncLegend(currentDiv - 1);
}
}
function showLegend(sFL){
if (divCount > 1){
$("#navContainer").show();
}
var sPageNav = "<span>Page&nbsp;</span>";

$("#legend").html(sFL);

for(var i=1; i<=Math.ceil(count/LEGEND); i++)
{
sPageNav += "<a id=\"p"+i+"\" href=\"javascript:truncLegend("+i+");\">"+i+"</a>";
}

$("#pagesNav").html(sPageNav);
}
function genLegend(){
var myList = "";

jQuery.each(myAirports.location, function(){
count += 1;

myList += genLegendMarkup(count, this.title, this.shapeID, "flight");
});

jQuery.each(myPointsOfInterest.location, function(){
count += 1;

myList += genLegendMarkup(count, this.title, this.shapeID, "sightseeing");
});

if(liCount != 0){
myList += "</div>";
}


return myList;
}
function genLegendMarkup(count, title, shapeID, classType){
liCount++;
sLegend = "";

if (liCount == 1){
divCount++;
sLegend += "<div id=\"u"+divCount+"\" class=\"list\">";
}
sLegend += "<a title=\"show/hide the information box for "+title+"\" name=\"i"+count+"\" class=\"legendIcon "+classType+" s"+count+"\" href=\"javascript:showInfoBox('"+shapeID+"','s"+count+"');\"><span>"+count+"</span></a>";
sLegend += "<a title=\"show/hide the information box for "+title+"\" class=\"legendTitle "+classType+"\" href=\"javascript:showInfoBox('"+shapeID+"','s"+count+"');\">"+title+"</a>";


if (liCount == LEGEND){
sLegend += "</div>";
liCount = 0;
}
return sLegend;
}
function CreateLayer(myJSON, myIcon)
{
var layer = new VEShapeLayer();

jQuery.each(myJSON.location, function(){
shapeCount++;	

this.index = shapeCount;
var myLocation = new VELatLong(this.lat, this.long, 0, VEAltitudeMode.RelativeToGround);
var myShape = new VEShape(VEShapeType.Pushpin, myLocation);
var indexedIcon = "<div class=\"icon i"+this.index+"\" style=\"background:url("+myIcon+") no-repeat; \"><span>"+this.index+"</span></div>";
myShape.SetCustomIcon(indexedIcon);
myShape.SetTitle("<div class=\"icon\" style=\"background:url("+myIcon+") no-repeat; font-weight:normal\"><span>"+this.index+"</span></div><h3>"+this.title+"</h3>");
myShape.SetDescription(this.description);
layer.AddShape(myShape);
this.shapeID = myShape.GetID();
});

map.AddShapeLayer(layer);
}
var mapcenterpanstarted = false;
var maponchangevieweventattached = false;
function showInfoBox(shapeID, index)
{
$(".legendIcon").removeClass("selected");
map.HideInfoBox();
if ( !maponchangevieweventattached)
{
map.AttachEvent("onchangeview",onchangeview);
maponchangevieweventattached=true;
}
if($("."+index).hasClass("selected") == false){
$("."+index).addClass("selected");
currentshape = map.GetShapeByID(shapeID);
mapcenterpanstarted = false;
map.SetCenter(currentshape.GetPoints()[0]);
}
}
function onchangeview(e){
if ( !mapcenterpanstarted )
{
mapcenterpanstarted = true;
map.Pan(1, 1);
map.Pan(-1, -1);
setTimeout('map.ShowInfoBox(currentshape)',500);
}
}

