/*
 * Created on October 15, 2009
 *
 * 
 * @copyright 2009 Capraro Technologies, Inc.
 * @author Dan Klockowski
 *
 */
var BrowserInternetExplorer = false;
var AjaxRequest;
var map;
var DefaultMapZoom = 8;
var BusinessMarkers = new Array(0);

//Initialize the navigation engine	
if (navigator.appName == 'Microsoft Internet Explorer'){
	BrowserInternetExplorer = true;	
}

function createMap(){
	try{
		var myLatlng = new google.maps.LatLng(43.084669, -75.265209);		
		var myOptions = {
			zoom: DefaultMapZoom,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
	    
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		getColleges();
	}
	catch(e){
		alert(e.message);
	}
}

/**
 * Runs the map ajax requests
 */
function doMapRequest(url, func){
	try{
		if(BrowserInternetExplorer){
			AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if (window.XMLHttpRequest)
		{
		 	AjaxRequest = new XMLHttpRequest();
			if (AjaxRequest.overrideMimeType){
				AjaxRequest.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject){
			AjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
	   
		AjaxRequest.open("GET", url, true);
		AjaxRequest.onreadystatechange = func;
		AjaxRequest.send(null);
	}
	catch(e){
		alert("Error: doMapRequest(): "+e.message);
	}
}

/**
 * Retrieves all the colleges from the database.
 */
function getColleges(){
	doMapRequest("/AJAX/Colleges.cdf.php", processColleges);
}

/**
 * Retrieves all the businesses from the database.
 */
function getBusinesses(){
	doMapRequest("/AJAX/Businesses.cdf.php", processBusinesses);
}

/**
 * This function is called when the AJAX call in
 * changePage has completed.
 */
function processColleges() {
    if (AjaxRequest.readyState == 4) {
        if (AjaxRequest.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            var xmlDoc = AjaxRequest.responseXML;
			var colleges = xmlDoc.getElementsByTagName("record");		
                       
            
			for (var i = 0; i < colleges.length; i++) {
				//var id = colleges[i].getElementsByTagName("jsxid")[0].firstChild.nodeValue;
				var atts = colleges[i].attributes;
				var marker = new CollegeMarker(atts);
				marker.addMarker();
			}
			
			getBusinesses();
        }
        else{
        	alert("Error loading colleges");
        }
    }
}

/**
 * This function is called when the AJAX call in
 * changePage has completed.
 */
function processBusinesses() {
    if (AjaxRequest.readyState == 4) {
        if (AjaxRequest.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            var xmlDoc = AjaxRequest.responseXML;
			var businesses = xmlDoc.getElementsByTagName("record");		
			BusinessMarkers = new Array(0);
			            
			for (var i = 0; i < businesses.length; i++) {
				var atts = businesses[i].attributes;
				var marker = new BusinessMarker(atts);
				marker.addMarker();
				BusinessMarkers.push(marker);
			}
        }
        else{
        	alert("Error loading businesses");
        }
    }
}

/**
 * Event when the college dropdown is changed
 */
function changeCollege(el){
	var id = el.value;
	
	if(id > 0){
		map.setZoom(DefaultMapZoom+4);
		doMapRequest("/AJAX/Colleges.cdf.php?id="+id, processCollegeDropdownChange);		
	}
}

/**
 * This function is called when the AJAX call in
 * changeCollege has completed.
 */
function processCollegeDropdownChange() {
    if (AjaxRequest.readyState == 4) {
        if (AjaxRequest.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            var xmlDoc = AjaxRequest.responseXML;
			var college = xmlDoc.getElementsByTagName("record");
			var atts = college[0].attributes;

			var latitude = parseFloat(atts.getNamedItem("jsxlocationlatitude").value);
			var longitude = parseFloat(atts.getNamedItem("jsxlocationlongitude").value);
			
			var myLatlng = new google.maps.LatLng(latitude, longitude);	
			map.panTo(myLatlng);
        }
        else{
        	alert("Error loading page");
        }
    }
}

/**
 * Event when the filter dropdown is changed
 */
function changeFilter(el){
	var id = el.value;
	
	//First, remove the original markers
	for(var i=0; i < BusinessMarkers.length; i++){
		var marker = BusinessMarkers[i];
		marker.remove();
	}
	
	doMapRequest("/AJAX/Businesses.cdf.php?type="+id, processBusinesses);		
}

/**
 * Returns the HTML for this business
 */
function openBusinessInformation(id){
	var height = "300";
	var width = "400";
	var wName = "businessInformation";
	var wPage = "/college_town/business.php?id="+id;
	
	if (window.showModalDialog) {
		window.showModalDialog(wPage,wName,"dialogWidth:"+width+"px;dialogHeight:"+height+"px");
	}
	else{
		window.open(wPage,wName,"menubar=no,scrollbars=no,resizable=no, directories=no,status=no, width="+width+", height="+height+", modal=yes");
	}
}

/**
 * Returns the HTML for this business Mobile style
 */
function openBusinessInformationMobile(id){
	alert("Mobile");
	var wName = "businessInformationMobile";
	var wPage = "/college_town/businessMobile.php?id="+id;
	

	window.open(wPage,wName,"menubar=no,scrollbars=no,resizable=no, directories=no,status=no, modal=no");
}

