/*
 * Copyright  2006 The Apache Software Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
 
/**
 * EventMco class contains all the business logic for the EMS applications.
 * responsible for handling events in applications.
 *
 */
function Mco() {
}

Mco.prototype.onCreateGoogle = function(){
	this.bGoogleCreated = true;
}

Mco.prototype.onCreateYahoo = function(){
	this.bYahooCreated = true;
}

Mco.prototype.onCreateMicrosoft = function(){
	this.bMicrosoftCreated = true;
}


Mco.prototype.onMapClick = function(clientEvent) {
    if (document.getElementById("latitude") != null){
	    document.getElementById("latitude").value = clientEvent.latitude;
	    document.getElementById("longitude").value = clientEvent.longitude;
	}
}

Mco.prototype.onMapTypeChange = function(clientEvent){

	var uiDoc  = clientEvent.session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");
    
	if (clientEvent.mapType == "BIRDS_EYE" ||
		clientEvent.mapType == "UNKNOWN") {
    	microsoftMap.setAttribute("mapType", clientEvent.mapType);
		return;
	}
    if (clientEvent.source != googleMap && 
		this.bGoogleCreated == true)
    	googleMap.setAttribute("mapType", clientEvent.mapType);
    if (clientEvent.source != yahooMap && 
		this.bYahooCreated == true)
    	yahooMap.setAttribute("mapType", clientEvent.mapType);
    if (this.bMicrosoftCreated == true)
    	microsoftMap.setAttribute("mapType", clientEvent.mapType);
}

Mco.prototype.onZoomEnd = function(clientEvent) {

	var uiDoc  = clientEvent.session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");
    
	if (microsoftMap.getAttribute("mapType") == "BIRDS_EYE" ||
		microsoftMap.getAttribute("mapType") == "UNKNOWN") return;

    if (clientEvent.source != googleMap && this.bGoogleCreated == true)
    	googleMap.setAttribute("zoom", ""+clientEvent.zoom);
    if (clientEvent.source != yahooMap && this.bYahooCreated == true)
    	yahooMap.setAttribute("zoom", ""+clientEvent.zoom);
    if (clientEvent.source != microsoftMap && this.bMicrosoftCreated == true)
    	microsoftMap.setAttribute("zoom", ""+clientEvent.zoom);
    
    if (document.getElementById("zoom") != null){
    	document.getElementById("zoom").value = clientEvent.zoom;
    }
    
}

Mco.prototype.onMoveEnd = function(clientEvent) {
	
	var uiDoc  = clientEvent.session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");
    
	if (microsoftMap.getAttribute("mapType") == "BIRDS_EYE" ||
		microsoftMap.getAttribute("mapType") == "UNKNOWN") return;

    if (clientEvent.source != googleMap && this.bGoogleCreated == true)
    	googleMap.setAttribute("center", clientEvent.center);
    if (clientEvent.source != yahooMap && this.bYahooCreated == true)
    	yahooMap.setAttribute("center", clientEvent.center);
    if (clientEvent.source != microsoftMap && this.bMicrosoftCreated == true)
    	microsoftMap.setAttribute("center", clientEvent.center);
    
    if (document.getElementById("latitude") != null){
	    document.getElementById("latitude").value = clientEvent.latitude;
	    document.getElementById("longitude").value = clientEvent.longitude;
	}
}

Mco.prototype.centerAndZoom = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");
    
    var center = document.getElementById("latitude").value + ", " +
    			 document.getElementById("longitude").value;
    
    if (clientEvent.source != googleMap && this.bGoogleCreated == true){
    	googleMap.setAttribute("center", center);
	    googleMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
    if (clientEvent.source != yahooMap && this.bYahooCreated == true){
    	yahooMap.setAttribute("center", center);
	    yahooMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
    if (clientEvent.source != microsoftMap && this.bMicrosoftCreated == true){
    	microsoftMap.setAttribute("center", center);
		microsoftMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
}

Mco.prototype.centerAndZoomHtml = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");
    
    var center = document.getElementById("latitude").value + ", " +
    			 document.getElementById("longitude").value;
    
    if (this.bGoogleCreated == true){
    	googleMap.setAttribute("center", center);
	    googleMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
    if (this.bYahooCreated == true){
    	yahooMap.setAttribute("center", center);
	    yahooMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
    if (this.bMicrosoftCreated == true){
    	microsoftMap.setAttribute("center", center);
		microsoftMap.setAttribute("zoom", document.getElementById("zoom").value);
	}
}

Mco.prototype.openInfoWindow = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var googleMap = uiDoc.getElementById("googleMap");
    var yahooMap = uiDoc.getElementById("yahooMap");
    var microsoftMap = uiDoc.getElementById("microsoftMap");

	this.openMapInfoWindow(uiDoc, 
							googleMap, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.google.com"
						);
	this.openMapInfoWindow(uiDoc, 
							yahooMap, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.yahoo.com"
						);
	this.openMapInfoWindow(uiDoc, 
							microsoftMap, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.microsoft.com"
						);
}

Mco.prototype.openMapInfoWindow = function(uiDocument, element, xPos, yPos, text, namespace){
	//remove the old window.
	this.removeInfoWindow(element);

	var infoWindow = uiDocument.createElement("infoWindow", namespace);
	infoWindow.setAttribute("latitude", xPos);
    infoWindow.setAttribute("longitude", yPos);
    infoWindow.setAttribute("text", text);
    element.appendChild(infoWindow);                   
}

	
Mco.prototype.addMarker = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var googleMarkers = uiDoc.getElementById("googleMarkers");
    var yahooMarkers = uiDoc.getElementById("yahooMarkers");
    var microsoftMarkers = uiDoc.getElementById("microsoftMarkers");

	this.openMapMarkerWindow(uiDoc, 
							googleMarkers, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.google.com"
						);
	this.openMapMarkerWindow(uiDoc, 
							yahooMarkers, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.yahoo.com"
						);
	this.openMapMarkerWindow(uiDoc, 
							microsoftMarkers, 
							document.getElementById("latitude").value, 
							document.getElementById("longitude").value, 
							document.getElementById("message").value, 
							"http://www.microsoft.com"
						);
}

Mco.prototype.openMapMarkerWindow = function(uiDocument, element, xPos, yPos, text, namespace){
	var marker = uiDocument.createElement("marker", namespace);
	marker.setAttribute("latitude", xPos);
    marker.setAttribute("longitude", yPos);
    marker.appendChild(uiDocument.createTextNode(text));
    element.appendChild(marker);                   
}
	
Mco.prototype.removeMarker = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var googleMarkers = uiDoc.getElementById("googleMarkers");
    var yahooMarkers = uiDoc.getElementById("yahooMarkers");
    var microsoftMarkers = uiDoc.getElementById("microsoftMarkers");

	this.removeAllMarkers(googleMarkers);
	this.removeAllMarkers(yahooMarkers);
	this.removeAllMarkers(microsoftMarkers);
}
	
Mco.prototype.removeAllMarkers = function(element){
	while (element.childNodes.length != 0){
		element.removeChild(element.childNodes[0]);
	}
}

Mco.prototype.removeInfoWindow = function(element){
	for (index = 0; index < element.childNodes.length; index++){
		if (element.childNodes[index].getLocalName() == "infoWindow"){
			element.removeChild(element.childNodes[index]);
		}
	}
}

Mco.prototype.lookUpAddress = function(session){
	var uiDoc  = session.getDocumentContainer().getUiDocument();
    var microsoftMap = uiDoc.getElementById("microsoftMap");
	var bridge = session.getUiDocumentHandler().getHandlerForElement(microsoftMap);	
	try{
    	bridge.map.Find(null, document.getElementById("address").value);
    }catch(e){
    	alert(e.message);
    }
}
