/**
 *  Author:     Ahmed Lacevic (alacevic@socialexplorer.com)
 *  WARNING:    This is a self-contained file, all functions used here must also be defined here,
 *              so that other websites can include a single file to host our map client.
 */


var userAgent      = navigator.userAgent;
var isIE_MC        = (navigator.appName.indexOf("Microsoft") != -1);
var isNav_MC       = (navigator.appName.indexOf("Netscape") != -1);
var isOpera_MC     = (userAgent.indexOf("Opera") != -1);
var isOSWin_MC     = (userAgent.indexOf("Windows") != -1);
var isOSMac_MC     = (userAgent.indexOf("Mac") != -1);
var isOSUnix_MC    = (userAgent.indexOf("Unix") != -1);
var isOSLinux_MC   = (userAgent.indexOf("Linux") != -1);

if(isOpera_MC)
{ 
  //Opera pretends to be IE, but behaves more like Nav...
  isIE_MC = false;
  isNav_MC = false;
}


//default values if not specified by hosting script...
if(typeof(clientMaps)=="undefined") clientMaps = "";
if(typeof(clientMaps2)=="undefined") clientMaps2 = "";
if(typeof(mapListUrl)=="undefined") mapListUrl = "";
if(typeof(findHostUrl)=="undefined") findHostUrl = "";
if(typeof(primaryHostUrl)=="undefined") primaryHostUrl = "";
if(typeof(mapGroup)=="undefined") mapGroup = "0";
if(typeof(initMapIndex)=="undefined") initMapIndex = "0";
if(typeof(initThemeIndex)=="undefined") initThemeIndex = "0";
if(typeof(initRndIndex)=="undefined") initRndIndex = "1";
if(typeof(initStyleset)=="undefined") initStyleset = "";
if(typeof(initExtentLeft)=="undefined") initExtentLeft = "";
if(typeof(initExtentRight)=="undefined") initExtentRight = "";
if(typeof(initExtentTop)=="undefined") initExtentTop = "";
if(typeof(initExtentBottom)=="undefined") initExtentBottom = "";
if(typeof(loadInitialSlideshow)=="undefined") loadInitialSlideshow = "";
if(typeof(autoPlayInitSlideshow)=="undefined") autoPlayInitSlideshow = "";
if(typeof(legendBgColor)=="undefined") legendBgColor = "auto";
if(typeof(clientBgColor)=="undefined") clientBgColor = "auto";
if(typeof(sidebarWidth)=="undefined") sidebarWidth = "200";
if(typeof(initClientWidth)=="undefined") initClientWidth = "automax";
if(typeof(initClientHeight)=="undefined") initClientHeight = "automax";
if(typeof(maxMapWidth)=="undefined") maxMapWidth = 1280;
if(typeof(maxMapHeight)=="undefined") maxMapHeight = 1024;
if(typeof(reportEventsToBrowser)=="undefined") reportEventsToBrowser = "false";

if(typeof(vertPadding)=="undefined") vertPadding = 50;
if(typeof(horizPadding)=="undefined") horizPadding = 10;

if(typeof(keepViewOnMapChange)=="undefined") keepViewOnMapChange = "true"; 
if(typeof(showStatusbarAlways)=="undefined") showStatusbarAlways = "false"; 
if(typeof(showAnimationTool)=="undefined") showAnimationTool = "true"; 
if(typeof(maxMapHistoryCount)=="undefined") maxMapHistoryCount = 5; 
if(typeof(reportExtentChangesToUrl)=="undefined") reportExtentChangesToUrl  = ""; 

if(typeof(showInaccessibleItems)=="undefined") showInaccessibleItems = "true";
if(typeof(showExportSSMenuItem)=="undefined") showExportSSMenuItem = "true";
if(typeof(defaultStylesetTranslation)=="undefined") defaultStylesetTranslation = "";

if(typeof(hideColorSelection)=="undefined") hideColorSelection = "";
if(typeof(useTwoMapDropdowns)=="undefined") useTwoMapDropdowns = "";

//recode auto to snap, snap is the new parameter...
if(initClientWidth=="auto") initClientWidth = "automax";
if(initClientHeight=="auto") initClientHeight = "automax";


//if auto colors are specified use document background color...
if(legendBgColor=="auto") legendBgColor = document.bgColor;
if(clientBgColor=="auto") clientBgColor = document.bgColor;




function urlEncode_MC(url)
{ 
  var encodedURL = "";
  
  for(var i=0; i<url.length; ++i)
  { 
    //if it's not one of the acceptable character codes, encode it.
    var c = url.charCodeAt(i);
    if(!(( c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57)))
      encodedURL += ("%" + decToHex_MC(c, 16));
    else
      encodedURL += url.charAt(i);
  }
  
  return encodedURL;
}


var hexVals_MC = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");

function decToHex_MC(num, radix)
{ 
  var hexString = "";
  while(num >= radix)
  { 
    temp = num % radix;
    num = Math.floor(num / radix);
    hexString = hexVals_MC[temp] + hexString;
  }
  
  hexString = hexVals_MC[num] + hexString;
  return hexString;
}




//--------------------------------------------------------------------------------------//
//  COMPATIBILITY FUNCTIONS 
//--------------------------------------------------------------------------------------//

function getObject_MC(obj)
{ 
  var theObj = null;
  
  if(typeof obj == "string")
  { 
  	if(isNav_MC)
  	{ 
      theObj = eval("document." + obj);
      
      if(theObj == null)
        theObj = document.getElementById(obj);
    }
    else 
      theObj = document.getElementById(obj);
  }
  else
    theObj = obj;
  
  return theObj;
}

function getStyleObject_MC(obj)
{ 
  var theObj = null;
  
  
  if(typeof obj == "string")
  { 
    
  	if(isNav_MC)
  	{ 
      theObj = eval("document." + obj);
      
      if(theObj == null)
        theObj =  document.getElementById(obj);
    }
    else
      theObj =  document.getElementById(obj);
    

    if(theObj != null) 
      theObj = theObj.style;
    
  }
  else
    theObj = obj;
  
  return theObj;
  
}



function setVisibility_MC(obj, vis)
{ 
  var theobj = getStyleObject_MC(obj);
  if(theobj == null) return;
  
  if(vis == "true" || vis == true || vis == "True")
      theobj.visibility = "visible";
  else
    theobj.visibility = "hidden";
}


function createLayer_MC(name, left, top, visible, content) 
{ 
  return  '<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">' +
                  '\n' + content + '\n</div>';
  
}


function availWindowWidth_MC()
{ 
  var myWidth = 700;
  if(typeof(window.innerWidth) == 'number')
  { 
    //Non-IE
    myWidth = window.innerWidth;
  } 
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  { 
    //IE 6+ in 'standards compliant mode'
    myWidth = parseInt(document.documentElement.clientWidth);
  } 
  else if(document.body && (document.body.clientWidth || document.body.clientHeight))
  { 
    //IE 4 compatible
    myWidth = parseInt(document.body.clientWidth);
  }
  
  return myWidth;
  
}


function availWindowHeight_MC()
{ 
  var myHeight = 550;
  if(typeof(window.innerWidth) == 'number')
  { 
    //Non-IE
    myHeight = window.innerHeight;
  } 
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  { 
    //IE 6+ in 'standards compliant mode'
    myHeight = parseInt(document.documentElement.clientHeight);
  } 
  else if(document.body && (document.body.clientWidth || document.body.clientHeight))
  { 
    //IE 4 compatible
    myHeight = parseInt(document.body.clientHeight);
  }
  
  return myHeight;
  
}


//--------------------------------------------------------------------------------------//
// END OF COMPATIBILITY FUNCTIONS
//--------------------------------------------------------------------------------------//


var mapClient = null;

//document.writeln(createLayer_MC("spanTxtDLTimeStatus", 20, 850, true, "<INPUT class='xForm2' type=text size=100 id='txtDLTimeStatus' name='txtDLTimeStatus' value='time'></INPUT>"));


// Handle all the the FSCommand messages in a Flash movie
function MapClientObject_DoFSCommand(command, args) 
{ 
  //alert("MapClientObject_DoFSCommand: cmd: " + command + "args: " + args);
  
  if(command == "SetInitialValues")
  { 
    mapClient = getObject_MC("MapClientObject");
    mapClient.SetVariable("startclient", "true");
  }
  else if(command == "openreportwindow")
  { 
    //figure what's the biggest window we can open...
    wwidth=screen.availWidth+2;
    wheight=screen.availHeight+2;
    winparam="screenx=0,screeny=0,left=0,top=0,width=" + wwidth + ",height=" + wheight + ",toolbar,location,directories,status,menubar,scrollbars,copyhistory,resizable";
    
    window.open(args,'',winparam);
    
  }
  else if(command == "ProcessingTime")
  { 
    processingTime = parseInt(args);
    if(getObject_MC("txtDLTimeStatus")!=null)
      getObject_MC("txtDLTimeStatus").value = "Production time: " + processingTime + "ms";
  }
  else if(command == "openMetaWindow")
  { 
    
    openMetadataWindow(args);
    
  }
  
}



function getQueryString_MC()
{ 
  var sRet = "?";
  if(typeof(mapServiceName)!="undefined") sRet += "&mapServiceName=" + mapServiceName;
  if(typeof(initMapIndex)!="undefined") sRet += "&initMapIndex=" + urlEncode_MC(initMapIndex);
  if(typeof(initThemeIndex)!="undefined") sRet += "&initThemeIndex=" + urlEncode_MC(initThemeIndex); 
  if(typeof(initRndIndex)!="undefined") sRet += "&initRndIndex=" + initRndIndex;
  if(typeof(initStyleset)!="undefined" && initStyleset != "") sRet += "&initStyleset=" + urlEncode_MC(initStyleset);
  if(typeof(initExtentLeft)!="undefined") sRet += "&initUsrExtentLeft=" + initExtentLeft;
  if(typeof(initExtentRight)!="undefined") sRet += "&initUsrExtentRight=" + initExtentRight;
  if(typeof(initExtentTop)!="undefined") sRet += "&initUsrExtentTop=" + initExtentTop;
  if(typeof(initExtentBottom)!="undefined") sRet += "&initUsrExtentBottom=" + initExtentBottom;
  if(typeof(keepViewOnMapChange)!="undefined") sRet += "&keepViewOnMapChange=" + keepViewOnMapChange;
  if(typeof(showStatusbarAlways)!="undefined") sRet += "&showStatusbarAlways=" + showStatusbarAlways;
  if(typeof(showAnimationTool)!="undefined") sRet += "&showAnimationTool=" + showAnimationTool;
  if(typeof(sidebarWidth)!="undefined") sRet += "&sidebarWidth=" + urlEncode_MC(sidebarWidth);
  if(typeof(clientMaps2)!="undefined") sRet += "&clientMaps2=" + urlEncode_MC(clientMaps2);
  if(typeof(findHostUrl)!="undefined") sRet += "&findHostUrl=" + urlEncode_MC(findHostUrl);
  if(typeof(primaryHostUrl)!="undefined") sRet += "&primaryHostUrl=" + urlEncode_MC(primaryHostUrl);
  if(typeof(mapGroup)!="undefined") sRet += "&mapGroup=" + urlEncode_MC(mapGroup);
  if(typeof(useTwoMapDropdowns)!="undefined") sRet += "&UseTwoMapDropdowns=" + useTwoMapDropdowns;
  if(typeof(maxMapHistoryCount)!="undefined") sRet += "&maxMapHist=" + maxMapHistoryCount;
  if(typeof(loadInitialSlideshow)!="undefined") sRet += "&loadInitialSlideshow=" + urlEncode_MC(loadInitialSlideshow);
  if(typeof(autoPlayInitSlideshow)!="undefined") sRet += "&PlaySS=" + autoPlayInitSlideshow;
  if(typeof(reportEventsToBrowser)!="undefined") sRet += "&reportEventsToBrowser=" + reportEventsToBrowser;
  if(typeof(showInaccessibleItems)!="undefined") sRet += "&showInaccessibleItems=" + showInaccessibleItems;
  if(typeof(showExportSSMenuItem)!="undefined") sRet += "&showExportSSMenuItem=" + showExportSSMenuItem;
  if(typeof(defaultStylesetTranslation)!="undefined") sRet += "&defaultStylesetTranslation=" + defaultStylesetTranslation;
  if(typeof(hideColorSelection)!="undefined") sRet += "&hideColorSelection=" + hideColorSelection;
  sRet += "&startClient=true";
  
  if(typeof(reportExtentChangesToUrl)!="undefined" && reportExtentChangesToUrl!="")
    sRet += "&reportExtentChangesToUrl=" + urlEncode_MC(reportExtentChangesToUrl);
  
  return sRet;
  
}




function GetMaxWidth_MC()
{ 
  var nwidth = availWindowWidth_MC() - horizPadding; // - 15;  //used to be 40 here!
  if(nwidth - parseInt(sidebarWidth) > maxMapWidth)   //this 300 is the size of map explorer (ie. the legend and map chooser)
    nwidth = maxMapWidth + parseInt(sidebarWidth);
  
  return nwidth;
  
}

function GetMaxHeight_MC()
{ 
  var nheight = availWindowHeight_MC() - vertPadding;
  if(nheight > maxMapHeight)
    nheight = maxMapHeight;
  
  return nheight;
}



var _clientWidth = initClientWidth;
var _clientHeight = initClientHeight;

if (_clientWidth == "automax")
  _clientWidth = GetMaxWidth_MC();

if (_clientHeight == "automax")
  _clientHeight = GetMaxHeight_MC();






/* support for the fscommand in IE (uses vb script to call javascript ... will this ever go away!? !@$%#$%@#%!) */
if (isIE_MC)
{ 
  document.write("<script language=\"vbscript\">\n");
  document.write("On Error Resume Next\n");
  document.write("Sub MapClientObject_FSCommand(ByVal command, ByVal args)\n");
  document.write("  Call MapClientObject_DoFSCommand(command, args)\n");
  document.write("End Sub\n");
  document.write("</script>");
}


var swfUrl = "http://www.socialexplorer.com/pub/maps/MapClient_v12.swf";

document.writeln('<OBJECT id="MapClientObject" codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ' + 
                 'height="' + _clientHeight + '" width="' + _clientWidth + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">');
document.writeln('<PARAM NAME="name" VALUE="MapClientObject">');
document.writeln('<PARAM NAME="Movie" VALUE="' + swfUrl + getQueryString_MC() + '">');
document.writeln('<PARAM NAME="Src" VALUE="' + swfUrl + getQueryString_MC() + '">');
document.writeln('<PARAM NAME="WMode" VALUE="opaque">');
document.writeln('<PARAM NAME="Play" VALUE="0">');
document.writeln('<PARAM NAME="Loop" VALUE="0">');
document.writeln('<PARAM NAME="Quality" VALUE="High">');
document.writeln('<PARAM NAME="SAlign" VALUE="">');
document.writeln('<PARAM NAME="Menu" VALUE="0">');
document.writeln('<PARAM NAME="Base" VALUE="">');
document.writeln('<PARAM NAME="AllowScriptAccess" VALUE="always">');
document.writeln('<PARAM NAME="Scale" VALUE="NoScale">');
document.writeln('<PARAM NAME="DeviceFont" VALUE="0">');
document.writeln('<PARAM NAME="EmbedMovie" VALUE="0">');
document.writeln('<PARAM NAME="BGColor" VALUE="' + clientBgColor + '">');
document.writeln('<PARAM NAME="SWRemote" VALUE="">');
document.writeln('<embed wmode="opaque" id="MapClientObject" name="MapClientObject" ' + 
                 ' quality="high" bgcolor="#ffffff" loop="false" menu="false" align="middle" menu="0" scale="NoScale" DeviceFont="0"' + 
                 ' EmbedMovie="0" allowScriptAccess="always" width="' + _clientWidth + '" height="' + _clientHeight + '"' + 
                 ' align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' + 
                 ' src="' + swfUrl + getQueryString_MC() + '"/>');
document.writeln('</OBJECT>');











