/**
 *  Author:     Ahmed Lacevic (alacevic@socialexplorer.com)
 *  Date:       2003.09.10
 *  
 *  Updated: 2005.06.05 Removed png support and moved to swf files handled by flash directly.
 *  Updated: 2006.09.30 Added reportExtentChangesToUrl parameter and also updated code to test for undefined parameters.
 */


var userAgent      = navigator.userAgent;
var isIE_FC        = (navigator.appName.indexOf("Microsoft") != -1);
var isNav_FC       = (navigator.appName.indexOf("Netscape") != -1);
var isOpera_FC     = (userAgent.indexOf("Opera") != -1);
var isOSWin_FC     = (userAgent.indexOf("Windows") != -1);
var isOSMac_FC     = (userAgent.indexOf("Mac") != -1);
var isOSUnix_FC    = (userAgent.indexOf("Unix") != -1);
var isOSLinux_FC   = (userAgent.indexOf("Linux") != -1);

if(isOpera_FC)
{ 
  //Opera pretends to be IE, but behaves more like Nav...
  isIE_FC = false;
  isNav_FC = 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(showResizeGrip)=="undefined") showResizeGrip = "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 = "";



//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;





//--------------------------------------------------------------------------------------//
//  COMPATIBILITY FUNCTIONS 
//--------------------------------------------------------------------------------------//

function getObject_FC(obj)
{ 
  var theObj = null;
  
  if(typeof obj == "string")
  { 
  	if(isNav_FC)
  	{ 
      theObj = eval("document." + obj);
      
      if(theObj == null)
        theObj = document.getElementById(obj);
    }
    else 
      theObj = document.getElementById(obj);
  }
  else
    theObj = obj;
  
  return theObj;
}

function getStyleObject_FC(obj)
{ 
  var theObj = null;
  
  
  if(typeof obj == "string")
  { 
    
  	if(isNav_FC)
  	{ 
      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_FC(obj, vis)
{ 
  var theobj = getStyleObject_FC(obj);
  if(theobj == null) return;
  
  if(vis == "true" || vis == true || vis == "True")
      theobj.visibility = "visible";
  else
    theobj.visibility = "hidden";
}


function getObjectWidth_FC(obj)
{ 
  var theobj = getStyleObject_FC(obj);
  if(isNav_FC)
    return parseInt(theobj.width);
  else
    return theobj.pixelWidth;
	
}

function getObjectHeight_FC(obj)
{ 
  var theobj = getStyleObject_FC(obj);
  if(isNav_FC)
    return parseInt(theobj.height);
  else
    return theobj.pixelHeight;
  
}


function createLayer_FC(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()
{ 
  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()
{ 
  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_FC("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) 
{ 
  if(command == "SetInitialValues")
  { 
    //save a reference to mapClient object...
    mapClient = getObject_FC("MapClientObject");
    
    //set properties...
    //setFlashMapClientProperties(mapClient);
    
    //when all properties are set, start the client.
    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_FC("txtDLTimeStatus")!=null)
      getObject_FC("txtDLTimeStatus").value = "Production time: " + processingTime + "ms";
  }
  
  //reportStatus(command + ": " + args)
  
}

//var statusTextAreaON = true;

function reportStatus(val)
{ 
  if(!statusTextAreaON)
    startStatusTextArea();
  
  var txtObj = getObject_FC("txtStatus");
  txtObj.value = val + "\n" + txtObj.value;
  
}

function startStatusTextArea()
{ 
  setVisibility_FC("spanTxtStatus", "true");
  statusTextAreaON = true;
}




function urlEncode(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(c, 16));
    else
      encodedURL += url.charAt(i);
  }
  
  return encodedURL;
}


var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                        "A", "B", "C", "D", "E", "F");

function decToHex(num, radix)
{ 
  var hexString = "";
  while(num >= radix)
  { 
    temp = num % radix;
    num = Math.floor(num / radix);
    hexString = hexVals[temp] + hexString;
  }
  
  hexString = hexVals[num] + hexString;
  return hexString;
}


function getQueryString()
{ 
  var sRet = "?";
  if(typeof(mapServiceName)!="undefined") sRet += "&mapServiceName=" + mapServiceName;
  if(typeof(initMapIndex)!="undefined") sRet += "&initMapIndex=" + initMapIndex;
  if(typeof(initThemeIndex)!="undefined") sRet += "&initThemeIndex=" + initThemeIndex; 
  if(typeof(initRndIndex)!="undefined") sRet += "&initRndIndex=" + initRndIndex;
  if(typeof(initStyleset)!="undefined") sRet += "&initStyleset=" + 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=" + sidebarWidth;
  if(typeof(clientMaps2)!="undefined") sRet += "&clientMaps2=" + urlEncode(clientMaps2);
  if(typeof(findHostUrl)!="undefined") sRet += "&findHostUrl=" + urlEncode(findHostUrl);
  if(typeof(primaryHostUrl)!="undefined") sRet += "&primaryHostUrl=" + urlEncode(primaryHostUrl);
  if(typeof(mapGroup)!="undefined") sRet += "&mapGroup=" + urlEncode(mapGroup);
  if(typeof(maxMapHistoryCount)!="undefined") sRet += "&maxMapHist=" + maxMapHistoryCount;
  if(typeof(loadInitialSlideshow)!="undefined") sRet += "&loadInitialSlideshow=" + urlEncode(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;
  sRet += "&startClient=true";
  
  if(typeof(reportExtentChangesToUrl)!="undefined" && reportExtentChangesToUrl!="") 
    sRet += "&reportExtentChangesToUrl=" + urlEncode(reportExtentChangesToUrl);
  
  return sRet;
  
}



//available width and height is the space we have within the window to fit the client!
var availWidth = availWindowWidth() - horizPadding;
var availHeight = availWindowHeight() - vertPadding;



function GetMaxWidth()
{ 
  var nwidth = availWindowWidth() - 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()
{ 
  var nheight = availWindowHeight() - vertPadding;
  if(nheight > maxMapHeight)
    nheight = maxMapHeight;
  
  return nheight;
}


var autoClientResize = true;
if(initClientWidth == "automax")
  initClientWidth = GetMaxWidth();
else
  autoClientResize = false;

if(initClientHeight == "automax")
  initClientHeight = GetMaxHeight();


/*
//this function is called from within our map client to open a pop-up window. 
//it allows us to test whether a popup-blocker blocked our window, something 
//that the getUrl function in flash does not provide.this 
function OpenPopUpWindow(url, wname)
{ 
  alert("Popup will be opened now!");
  
  var w = window.open(url, wname);
  
  alert("w: " + w);
  
  if (!w)
    alert("A popup blocker did not allow a new window to be opened! Please disable your pop-up blocker and try again.");
}
*/


document.writeln('<OBJECT id="MapClientObject" codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ' + 
                 'height="' + initClientHeight + '" width="' + initClientWidth + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">');
document.writeln('<PARAM NAME="Movie" VALUE="http://www.socialexplorer.com/pub/maps/MapClient_v11.swf' + getQueryString() + '">');
document.writeln('<PARAM NAME="Src" VALUE="http://www.socialexplorer.com/pub/maps/MapClient_v11.swf' + getQueryString() + '">');
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 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="' + initClientWidth + '" height="' + initClientHeight + '"' + 
                 ' align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' + 
                 ' src="http://www.socialexplorer.com/pub/maps/MapClient_v11.swf' + getQueryString() + '"/>');
document.writeln('</OBJECT>');







