﻿
var map;
var arrItems = [ {name: "Sentinel", description: "Sentinel", xCoord: -1, yCoord: -1} ];
            
function initMap()
{
    // create the cmap object            
    map = new CMap();
    addItemList();
    
    // optional - attach listeners to update xCoord and yCoord text when clicking on the map
    //attachMapClickedListeners();
}

function initMap2()
{
    map = new CMap();
    addItemListWithText();
}

function attachMapClickedListeners()
{
    // get elements to be tied as listeners
    var xcoord = document.getElementById("FeatureXCoord");
    var ycoord = document.getElementById("FeatureYCoord");
    
    var objMap = map.getMap();
    
    // change x-coordinate value to clicked area
    xcoord.mapClicked = function(objMap, coord)
    {
        if (coord.x)		       
            xcoord.value = coord.x;
    }
    
    // change y-coordinate  value to clicked area
    ycoord.mapClicked = function(objMap, coord)
    {
        if (coord.y)
            ycoord.value = coord.y;
    }
    
    // attach listeners to map
    objMap.addListener(xcoord);
    objMap.addListener(ycoord);            
}

// clears the item array that contains the nodes to be added to the map
function clearItemList()
{
    arrItems.length = 1;
}

// adds an item to the end of the array containing the nodes to be added to the map
function addItem(featName, featDescription, featXCoord, featYCoord, iconSource)
{
	if (featXCoord != 0 && featYCoord != 0)
	{
        arrItems[arrItems.length] = {name: featName, description: featDescription, xCoord: featXCoord, yCoord: featYCoord, iconSrc: iconSource};		     
	}	
}

function addItemList()
{
    // add the array of items to the map
    map.loadMapPoints(arrItems, 1);
}

function addItemListWithText()
{
    map.loadMapPointsWithText(arrItems, 1);
}

// temporary functions for demonstration only

// generate a random number between the lowerBound and upperBound values
function RandomNumber(lowerBound, upperBound)
{    
    return Math.floor((upperBound - lowerBound + 1) * Math.random()) + lowerBound;
}

function buildFakeItemList(intCount)
{
    for (var i = 1; i <= intCount; i++)
    {
        // add an item to the list, creating name/description based on the index
        addItem("Test " + i.toString(),
                "Test " + i.toString() + " Description",
                RandomNumber(2018000, 2960080),
                RandomNumber(5449238, 6727280));
    }
}

function addFakeFeatures(intCount)
{
    // clear the current list
    clearItemList();
    
    // add an amount of 'random' items to the list
    buildFakeItemList(intCount);
    
    // add the array of items to the map
    map.loadMapPoints(arrItems, 1);
}

function zoomIt(x,y)
{
	if (x != 0 && y != 0)
	{
		map.zoomMapToPoint(x,y);
	}
	else
	{
		alert("Can not find this address on the map.");
	}
}

function matchColumns ( colOneID, colTwoID, diff )
{ 
	var colOne = document.getElementById(colOneID);
	var colTwo = document.getElementById(colTwoID);
	var colOneH, colTwoH;
	if (colOne && colTwo)
	{
		colOneH = (colOne.offsetHeight ? colOne.offsetHeight : colOne.style.pixelHeight);
		colTwoH = (colTwo.offsetHeight ? colTwo.offsetHeight : colTwo.style.pixelHeight);
		if(colOneH + diff > colTwoH)
			colTwo.style.height = eval(colOneH + diff) + "px";
		
		else 
			colOne.style.height = eval(colTwoH - diff) + "px";		
	}
}

function emailInfo(postUrl,obj) 
{
	 var url = window.location.toString();
	 url.match(/(.*)\//);
	 obj.href = "mailto:?body=" + RegExp.$1 + "/" +postUrl;
}

