function initial(n)
{
    if (n==1)
        plotLocs(0);
}

function plotLocs(n)
{
    removeChilds();
    // retrieves all locations
    mid = document.getElementById('mapid').value;
    
    if (n==0){ 
        lid = document.getElementById('legendid').value;
    }
    else{
        lid = n;
    }
    dest_main.SS_getLocations(mid, lid, plotLocs_Callback);
}

function plotLocs_Callback(response)
{   
	var ds = response.value;
	if (response.error != null){
			alert(response.error);
			return;
	}
	// locations info retrieved
	if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0)) {
        // plot all locations
	    for (i=0; i<ds.Tables[0].Rows.length; i++) {
            plotDot(ds.Tables[0].Rows[i]["group_id"],ds.Tables[0].Rows[i]["location_id"],'dot' + ds.Tables[0].Rows[i]["location_id"],ds.Tables[0].Rows[i]["location_name"],ds.Tables[0].Rows[i]["legend_file"],ds.Tables[0].Rows[i]["location_posx"],ds.Tables[0].Rows[i]["location_posy"],ds.Tables[0].Rows[i]["location_legendpos"],ds.Tables[0].Rows[i]["group_desc"]);
        }
    }
}

function plotDot(gid,id,dotid,dotname,legend,x,y,legendpos,tooltip)
{
    // textpos: 0 - top, 1 - right, 2 - bottom, 3 - left

    // if dot don't exist, create
    // else update position    
    
    x = x + findPosX('mappix');
    y = y + findPosY('mappix');
        
    if (document.getElementById(dotid) != null)
        document.getElementById(dotid).parentNode.removeChild(document.getElementById(dotid));

	if (document.getElementById(dotid) == null)
	{	           
		elem = document.getElementById('dotscontainer');
		newdot = document.createElement('div');
		newdot.id = dotid;
		newdot.style.cssText = 'position:absolute;top:' + y + 'px;left:' + x + 'px;z-index:990';
        newdot.onmouseover =  function(){showDetail(id);};
        dotcontent = createDot(gid,id,dotid, dotname,legend,legendpos,tooltip);
        newdot.appendChild(dotcontent);
		elem.appendChild(newdot);
	}
}

function createDot(gid,id,dotid, dotname, legend, legendpos,tooltip)
{
    // css style for the box enclosing the location name. change if necessary.
    textstyle = 'color:white;background-color:#454545;border:1px solid #f8f8f8;z-index:990;text-align:center;padding: 0px 3px 0px 3px';

    table = document.createElement('table');
    tbody = document.createElement('tbody');
    table.appendChild(tbody);
    row = document.createElement('tr');
    cell = document.createElement('td');
    img = document.createElement('img');
    img.src = legend;
    img.setAttribute('border','0');
    img.setAttribute('title',tooltip);
    imglink = document.createElement('a');
    imglink.setAttribute('href','dest_detail.aspx?id='+gid);
    imglink.onmouseover =  function(){showDetail(id);};
    imglink.appendChild(img);
    
    if (legendpos == 0) // top
    {
        cell.appendChild(imglink);
        cell.setAttribute('align','center');
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);
        row = document.createElement('tr'); // new row
        cell = document.createElement('td'); // new cell
        cell.style.cssText = textstyle;        
        dotlink = document.createElement('a');
        dotlink.style.cssText = 'text-decoration:none;color:#ffffff';
        dotlink.setAttribute('href','dest_detail.aspx?id='+gid);
        dotlink.onmouseover =  function(){showDetail(dotid);};
        dotlink.appendChild(document.createTextNode(dotname));
        cell.appendChild(dotlink);
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);
    }
    else if (legendpos == 1) // right
    {
        div = document.createElement('div');
        div.style.cssText = textstyle;
        dotlink = document.createElement('a');
        dotlink.style.cssText = 'text-decoration:none;color:#ffffff';
        dotlink.setAttribute('href','dest_detail.aspx?id='+gid);
        dotlink.onmouseover =  function(){showDetail(dotid);};
        dotlink.appendChild(document.createTextNode(dotname));
        div.appendChild(dotlink);
        cell.appendChild(div);
        row.appendChild(cell);
        cell = document.createElement('td'); // new cell
        cell.appendChild(imglink);
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);        
    }
    else if (legendpos == 2) // bottom
    {
        dotlink = document.createElement('a');
        dotlink.style.cssText = 'text-decoration:none;color:#ffffff';
        dotlink.setAttribute('href','dest_detail.aspx?id='+gid);
        dotlink.onmouseover =  function(){showDetail(dotid);};
        dotlink.appendChild(document.createTextNode(dotname));
        cell.appendChild(dotlink);
        cell.style.cssText = textstyle;
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);
        row = document.createElement('tr'); // new row
        cell = document.createElement('td'); // new cell
        cell.appendChild(imglink);
        cell.setAttribute('align','center');
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);
    }
    else if (legendpos == 3) // left
    {
        cell.appendChild(imglink);
        row.appendChild(cell);
        table.tBodies[0].appendChild(row);        
        cell = document.createElement('td'); // new cell
        div = document.createElement('div');
        div.style.cssText = textstyle;
        dotlink = document.createElement('a');
        dotlink.style.cssText = 'text-decoration:none;color:#ffffff';
        dotlink.setAttribute('href','dest_detail.aspx?id='+gid);
        dotlink.onmouseover =  function(){showDetail(dotid);};
        dotlink.appendChild(document.createTextNode(dotname));
        div.appendChild(dotlink);
        cell.appendChild(div);
        row.appendChild(cell);                     
    }
    return table;
}

function findPosX(id)
{
    obj = document.getElementById(id);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(id)
{
    obj = document.getElementById(id);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}        
  
function removeChilds(){    
    var element = document.getElementById("dotscontainer");
    while(element.hasChildNodes())
        element.removeChild(element.firstChild);
}

function showDetail(id){    
    dest_main.SS_getDetail(id, showDetail_Callback);    
}

function showDetail_Callback(response)
{       
	var ds = response.value;
	if (response.error != null){
			alert(response.error);
			return;
	}
		
	if ((ds.Tables != null) && (ds.Tables.length > 0)) {        
        if(ds.Tables[0].Rows[0]["group_img"].length > 0){
            
            var image = document.getElementById("img_desc");
            image.style.width = '180px';
            image.style.height = '100px';
            image.style.border = '1px solid #f0edc4';
            image.src = ds.Tables[0].Rows[0]["group_img"];
            document.getElementById('img_desc').style.display = 'block';  
        }
        else
        {            
            document.getElementById('img_desc').style.display = 'none';            
        }           
        var descp = document.getElementById("desc");
        descp.innerHTML = ds.Tables[0].Rows[0]["location_desc"]        
    }
    
}

function escapeVal(sText,replaceWith){ 

	sText = escape(sText); 

	for(i=0; i<sText.length; i++){ 
		if(sText.indexOf("%0D%0A") > -1){ 
			//Windows encodes returns as \r\n hex
			sText=sText.replace("%0D%0A",replaceWith);
		}
		else if(sText.indexOf("%0A") > -1){ 
			//Unix encodes returns as \n hex
			sText=sText.replace("%0A",replaceWith);
		}
		else if(sText.indexOf("%0D") > -1){ 
			//Macintosh encodes returns as \r hex
			sText=sText.replace("%0D",replaceWith);
		}

	}
	sText=unescape(sText); //unescape all other encoded characters
	
	return sText;
}
