/*
 * $Name: MQSA_Samples_v2 $
 */

/*
 * Browser detection code
 */
function Browser ()
{
    var name = navigator.appName;
    if (name == "Netscape")
        this.name = "ns";
    else if (name == "Microsoft Internet Explorer")
        this.name = "ie";

    this.version = navigator.appVersion;
    this.vMajor  = parseInt(this.version);
    this.isNS    = (this.name =="ns" && this.vMajor >= 4);
    this.isNS4   = (this.name =="ns" && this.vMajor == 4);
    this.isNS6   = (this.name =="ns" && this.vMajor == 5);
    this.isIE    = (this.name =="ie" && this.vMajor >= 4);
    this.isIE4   = (this.version.indexOf ('MSIE 4')   >0);
    this.isIE5   = (this.version.indexOf ('MSIE 5')   >0);
    this.isDOM   = (document.createElement
                    && document.appendChild
                    && document.getElementsByTagName) ? true : false;

    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf ("win") > - 1)
        this.platform = "win";
    else if (ua.indexOf("mac") > -1)
        this.platform = "mac";
    else
        this.platform="other";
}

var browser = new Browser;

/*
 * Map control code
 */
function Map (id, level, sessionId)
{
    this.id        = id;
    this.level     = level;
    this.sessionId = sessionId;
    this.element   = document.getElementById (id);
    this.offset_x  = 0;
    this.offset_y  = 0;
    //this.inout     = inout;
 
    if (this.element) {
        var el = this.element;
        this.url = el.src;
        if (browser.isDOM && !browser.isIE) {
            while (el.offsetParent) {
                this.offset_x += el.offsetLeft;
                this.offset_y += el.offsetTop;
                el = el.offsetParent;
            }
        }
    }

    this.recenter = function (x, y)
    {
        if (this.element) {
            //this.element.src = this.url + "&mapbrowse=center&x=" + (x - this.offset_x) + "&y=" + (y - this.offset_y) + "&rand=" + Math.random ();
            //this.element.src = "/louisiana1st/web/mapquest/MQServlet?xc="+(x - this.offset_x) +"&yc=" +(y - this.offset_y);
            document.simpleSearchForm.useMap.value = "true";
			document.simpleSearchForm.xcenter.value = x - this.offset_x;
			document.simpleSearchForm.ycenter.value = y - this.offset_y;
			document.simpleSearchForm.submit();
        }
    };
    
    this.zoom = function (level)
    {    	
    	 if (this.element) {
            //this.element.src = "/louisiana1st/web/mapquest/MQServlet?zoomlevel="+level ;     
             this.level = level;
             document.simpleSearchForm.useMap.value = "true";
		     document.simpleSearchForm.zoomLevel.value = level;
		      //alert(">--- USE MAPs.level->"+document.simpleSearchForm.useMap.value);
  			 document.simpleSearchForm.submit();            
        } 
	};
    
    this.pan = function (dirx,diry)
    {
        if (this.element) {
            //this.element.src = this.url + "&mapbrowse=pan_" + dir + "&rand=" + Math.random();xval=-100&yval=-100
            //this.element.src = "/louisiana1st/web/mapquest/MQServlet?xval=" + dirx +"&yval=" +diry;
            
            document.simpleSearchForm.useMap.value = "true";
			document.simpleSearchForm.xcnt.value = dirx;
			document.simpleSearchForm.ycnt.value = diry;
			
			document.simpleSearchForm.submit();
        }
    };

    this.recenterzoom = function (x, y,level)
    {
        if (this.element) {
       
            if (map.level < 14) {
                ++this.level;
             
               // showVendor ();
               level = this.level;
            
            //alert(">---Q--level-->"+level);
		    //alert(">---W--this.level->"+this.level);
            document.simpleSearchForm.useMap.value = "true";
			document.simpleSearchForm.xcenter.value = x - this.offset_x;
			document.simpleSearchForm.ycenter.value = y - this.offset_y;
			document.simpleSearchForm.zoomLevel.value = level;
			document.simpleSearchForm.submit();
               
           }
          // this.element.src = "/louisiana1st/web/mapquest/MQServlet?xc="+(x - this.offset_x) +"&yc=" +(y - this.offset_y)+"&zoomlevel="+level ;		 	
        }
    };

    this.identify = function (x, y)
    {
       
      // alert('Zoom level:2 ');
       //this.element.src = "/louisiana1st/web/mapquest/GetMapAction?xid=" + x +"&yid=" + y;
       document.location.href="/louisiana1st/web/locate/mapquest/GetFeatureAction.do?xid=" +(x - this.offset_x) +"&yid=" +(y - this.offset_y);
       //document.location.href="/louisiana1st/web/locate/mapquest/GetFeatureAction.do"
       
       // url = window.location.protocol + "//"
         //   + window.location.host + window.location.pathname
          //  + '?template=identify&transaction=locMap&identifyIcon='
          //  + this.sessionId
          //  + '&x=' + (x - this.offset_x) + '&y=' + (y - this.offset_y);

       // var width  = 255;
       // var height = 255;
       // infoWindow = window.open (url,
         //          Math.floor (Math.random () * 100000),
           //        "height=" + height + ",width=" + width + ",toolbar=no,scrollbars=no,resize=yes");
        //infoWindow.moveTo ((screen.width - width) / 2, (screen.height - height) / 2);
    };

	this.identify1 = function(x,y)
    {
    	//alert(x + " " +  y + " " + this.offset_x + " " + this.offset_y);
    	this.element.src="/louisiana1st/web/mapquest/MQServlet?xid=" +(x - this.offset_x) +"&yid=" +(y - this.offset_y);
    	
    };
}

var map;

function mqaMapClick (e)
{
    var x, y;
    if (browser.isIE) {
        if (!(event.button == 1 || (browser.platform == "mac" && event.button == 0))) return;
        x = event.offsetX;
        y = event.offsetY;
    } else if (browser.isDOM) {
        if (e.which != 1) return;
        x = e.pageX;
        y = e.pageY;
    }    

    if (document.mapClick.clickAction[0].checked) {
        // Zoom       
        mqaMapZoomIn ();
        //map.zoom (102030);
    } else if (document.mapClick.clickAction[1].checked) {
        // Recenter        
        map.recenter (x, y)
    } else if (document.mapClick.clickAction[2].checked) {
        // Identify Icon
        if (document.mapClick.clickAction[2].value == 'identify') {
            map.identify (x, y);
        } else {        	
	    	//alert("Value:"+document.simpleSearchForm.useMap.value);
            map.recenterzoom (x, y,map.level);           
        }
    }else if (document.mapClick.clickAction[3].checked) {    	
    	map.identify (x, y);
    }
}

function mqaMapClickSearch (e)
{
   	//alert("Inside identify features");
    var x, y;
    if (browser.isIE) {
        if (!(event.button == 1 || (browser.platform == "mac" && event.button == 0))) return;
        x = event.offsetX;
        y = event.offsetY;
    } else if (browser.isDOM) {
        if (e.which != 1) return;
        x = e.pageX;
        y = e.pageY;
    }    

    if (document.mapClick.clickAction[0].checked) {
        // Zoom
        document.simpleSearchForm.useMap.value = "true";
    	//alert("Value:"+document.simpleSearchForm.useMap.value);
    	box = document.simpleSearchForm.region;
	   	box.selectedIndex = 0;
   	
   		box = document.simpleSearchForm.parish;
   		box.selectedIndex = 0;
   	
  	 	box = document.simpleSearchForm.city;
   		box.selectedIndex = 0;
   	
   		document.simpleSearchForm.zip.value = "";
        mqaMapZoomInSearch ();
        //map.zoom (102030);
    } else if (document.mapClick.clickAction[1].checked) {
        // Recenter
        document.simpleSearchForm.useMap.value = "true";
    	//alert("Value:"+document.simpleSearchForm.useMap.value);
    	box = document.simpleSearchForm.region;
	   	box.selectedIndex = 0;
   	
   		box = document.simpleSearchForm.parish;
   		box.selectedIndex = 0;
   	
  	 	box = document.simpleSearchForm.city;
   		box.selectedIndex = 0;
   	
   		document.simpleSearchForm.zip.value = "";
        map.recenter (x, y)
    } else if (document.mapClick.clickAction[2].checked) {
        // Identify Icon
        //alert("Inside identify features");
        if (document.mapClick.clickAction[2].value == 'identify') {
            map.identify (x, y);
        } else {
        	document.simpleSearchForm.useMap.value = "true";
	    	//alert("Value:"+document.simpleSearchForm.useMap.value);
            map.recenterzoom (x, y,map.level);
            box = document.simpleSearchForm.region;
	   	box.selectedIndex = 0;
   	
   		box = document.simpleSearchForm.parish;
   		box.selectedIndex = 0;
   	
  	 	box = document.simpleSearchForm.city;
   		box.selectedIndex = 0;
   	
   		document.simpleSearchForm.zip.value = "";
        }
    }else if (document.mapClick.clickAction[3].checked) {
    	//alert("Inside identify features");
    	//map.identify (x, y);
    	callToServer(x, y);
    }
}

function handleResponse(msg,x,y) {
	//alert('Hi'+msg);
	if(msg == "true")
	{
		map.identify (x, y);
	}
}

var IFrameObj; // our IFrame object
function callToServer(x, y) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  var URL = "/louisiana1st/web/mapquest/CheckFeature?xid=" +(x - map.offset_x) +"&yid=" +(y - map.offset_y);
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','RSIFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['RSIFrame'];
      }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\<iframe id="RSIFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout('callToServer()',10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
  
  IFrameDoc.location.replace(URL);
  return false;
}

function mqaMapClick1 (e)
{
    var x, y;
    if (browser.isIE) {
        if (!(event.button == 1 || (browser.platform == "mac" && event.button == 0))) return;
        x = event.offsetX;
        y = event.offsetY;
    } else if (browser.isDOM) {
        if (e.which != 1) return;
        x = e.pageX;
        y = e.pageY;
    }
    	map.identify1 (x, y);
   
}

function mqaMapInit (id, level, sessionId)
{
	//alert("Inside init");
    map = new Map (id, level, sessionId);
    if (map.element) {
    	 if (browser.isIE) {
            map.element.onmouseup = mqaMapClick;
        } else if (browser.isDOM) {
            map.element.addEventListener ("mouseup", mqaMapClick, true);
        }
        showZoomState ();
        showVendor ();
    }
}

function mqaMapInit1 (id, level, sessionId)
{
    map = new Map (id, level, sessionId);
    //alert('Zoom level: ' + level);
    if (map.element) {
        if (browser.isIE) {
            map.element.onmouseup = mqaMapClick1;
        } else if (browser.isDOM) {
            map.element.addEventListener ("mouseup", mqaMapClick1, true);
        }
        showZoomState ();
        showVendor ();
    }
}


function mqaMapInitSearch (id, level, sessionId)
{
	
    map = new Map (id, level, sessionId);
    if (map.element) {
    	 if (browser.isIE) {
            map.element.onmouseup = mqaMapClickSearch;
        } else if (browser.isDOM) {
            map.element.addEventListener ("mouseup", mqaMapClick, true);
        }
        if(level < 15)
        {
    
        showZoomState ();
        showVendor ();
        }
    }
}

function mqaMapZoomSearch (level)
{
   //alert("Inside hi zoom"+level);
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
    	//alert("Inside hi zoom"+level);
    	document.simpleSearchForm.useMap.value = "true";
    	//alert("Inside hi zoom"+document.simpleSearchForm.useMap.value);
    	box = document.simpleSearchForm.region;
	   	box.selectedIndex = 0;
   	
   		box = document.simpleSearchForm.parish;
   		box.selectedIndex = 0;
   	
  	 	box = document.simpleSearchForm.city;
   		box.selectedIndex = 0;
   	
   		document.simpleSearchForm.zip.value = "";
        map.zoom (level);
        showZoomState ();
        //showVendor ();
		//alert('Zoom: ' + level);
		//document.getElementById('zoomLevel').value=level;
		//alert('Current value: ' + document.getElementById('zoomLevel').value);
    }
}

function mqaMapZoom (level)
{
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
    	//alert("Inside hi zoom"+level);
        map.zoom (level);
        showZoomState ();
        showVendor ();
		//alert('Zoom: ' + level);
		//document.getElementById('zoomLevel').value=level;
		//alert('Current value: ' + document.getElementById('zoomLevel').value);
    }
}

function clearLoction ()
{
	//alert("Inside clear");	
}

function mqaMapZoomIn (name)
{
    if (map.level < 14)
        mqaMapZoom (map.level + 1);
}
function mqaMapZoomInSearch (name)
{
    if (map.level < 14)
        mqaMapZoomSearch(map.level + 1);
}
function mqaMapZoomOut (name)
{
    if (map.level > 1)
        mqaMapZoom (map.level - 1);
}
function mqaMapZoomOutSearch (name)
{
    if (map.level > 1)
        mqaMapZoomSearch (map.level - 1);
}

function showZoomState ()
{
    //alert('Zoom level: ' + map.level);
    for (var i = 1; i <= 14; ++i) {
        var image = i == map.level ? 'zoom_on.gif' : 'zoom.gif';
        eval ("document.zoom" + i + ".src='/louisiana1st/web/MapQuest/images/" + image + "'");
    }
}

function showVendor ()
{
    if (! document.vendor) return;

    if ((map.level <= 10) && (map.level >= 6))
    {
      var vendImage = 'navteq.gif';
    } else {
      var vendImage = 't.gif';
    }
    eval ("document.vendor.src='images/" + vendImage + "'");
}

function mqaMapPan (dirx,diry)
{
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {
        map.pan (dirx,diry);
    }
}

function mqaMapPanSearch (dirx,diry)
{   
    if (! map) {
        alert ('you must call mqaMapInit(mapurl) before you can call this function');
    } else {  
        document.simpleSearchForm.useMap.value = "true";
    	box = document.simpleSearchForm.region;
	   	box.selectedIndex = 0;
   	
   		box = document.simpleSearchForm.parish;
   		box.selectedIndex = 0;
   	
  	 	box = document.simpleSearchForm.city;
   		box.selectedIndex = 0;
   	
   		document.simpleSearchForm.zip.value = "";
    
        map.pan (dirx,diry);
    }
}
