function Schools() {
	var me = this;
	me._allSchools = [];
	me._schools = [];
	me._markers = [];
	me._placemarks = [];
	me._campus = [];
	me._index;
	me.page=1;
	me._numRows;
	me._lastPage;
	me._pageLimit = 20;
}

Schools.prototype.getSchools = function() {
	return this._schools;
};

Schools.prototype.getNumberOfSchools = function() {
	return this._schools.length;
};

Schools.prototype.getSchoolAtIndex = function(index) {
	return this._schools[index];
};

Schools.prototype.getMarkers = function() {
	return this._markers;
};

Schools.prototype.getPlacemarks = function() {
	return this._placemarks;
};

Schools.prototype.setMarkers = function() {
	if(this._markers.length) {
		this._markers = [];
	}

    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = BASE_URL + 'assets_schools/_default/icon-school-red.png';
   	icon.iconSize = new GSize(14, 22);
	icon.shadowSize = new GSize(0, 0);
	icon.iconAnchor = new GPoint(7,11);

	for(var i = 0; i < this._schools.length; i++) {
		var latLng = new GLatLng(this._schools[i].getLat(), this._schools[i].getLng());
		var markerOpts = { title: this._schools[i].getIpeds(), icon: icon };
		var marker = new GMarker(latLng, markerOpts);
		
		this._markers.push(marker);
	}
};

Schools.prototype.setPlacemarks = function(ge) {
	if(this._placemarks.length) {
		this._placemarks = [];
	}

	// Placemark/Style
	var style = ge.createStyle('');

	// Placemark/Style/IconStyle
	var icon = ge.createIcon('');
	style.getIconStyle().setScale(0.7);
	style.getLabelStyle().setScale(0.65);

	for(var i = 0; i < this._schools.length; i++) { //
		var point = ge.createPoint('');
		icon.setHref(BASE_URL + 'assets_schools/_default/icon-school-red.png');
		var placemark = ge.createPlacemark('');

		point.setLatLng(Number(this._schools[i].getLat()), Number(this._schools[i].getLng()));
		style.getIconStyle().setIcon(icon);
		placemark.setStyleSelector(style);
		
		if(this._schools[i].getName() != null) {
			placemark.setName(this._schools[i].getName());
		}
		if(this._schools[i].getIpeds() != null) {
			placemark.setSnippet(this._schools[i].getIpeds());
		}
		placemark.setGeometry(point);

		var schoolPlacemark = new SchoolPlacemark(this._schools[i].getIpeds(), placemark, point);

		this._placemarks.push(schoolPlacemark);
	}
};

Schools.prototype.loadSchoolsJson = function(jsonObject) {
	for(var i = 0; i < jsonObject.length; i++) {
		var school = new School(jsonObject[i].ipeds, jsonObject[i].name, jsonObject[i].lat, jsonObject[i].lng, jsonObject[i].logo,
				jsonObject[i].city, jsonObject[i].state,
				Number(jsonObject[i].inTuitionAndFees), Number(jsonObject[i].outTuitionAndFees), Number(jsonObject[i].totalInState), 
				Number(jsonObject[i].totalOutState), Number(jsonObject[i].applicantsTotal), Number(jsonObject[i].enrolledTotal), 
				Number(jsonObject[i].percentAdmittedTotal));
		this._allSchools.push(school);
	}
	
	this._numRows = this._allSchools.length;
	this._lastPage = Math.ceil(this._numRows/this._pageLimit);	
	this.displaySchools();
};

Schools.prototype.changePage = function(change) {
	this.clearPageSchools();
	
	switch(change) {
		case 'first':
			this.page = 1;
			break;
		case 'last':
			this.page = this._lastPage;
			break;
		case 'prev':
			this.page -= 1;
			break;
		case 'next':
			this.page += 1;
			break;
	}
	
	this.displaySchools();
	control._loadData('schoolResultsList');
};

Schools.prototype.clearPageSchools = function() {
	map.getEarthInstance(function() {
		//------------ todo find original object --------------- //
		if(control._placemarks) {
			control._placemarks.destroy();
		}
		if(control._markers) {
			control._markers.clearMarkers();
		}
		if(control._schools) {
			control._schools.clearSchoolCampus();
		}
		if(control.camera) {
			if (map.getCurrentMapType().getName() == 'Earth') {
				removeSpin();
				control.camera.moveCamera(41.8489055052432, -95.90747171258352, 5000000, 0, 0);
			} else if(map.getCurrentMapType().getName() == 'Map' || map.getCurrentMapType().getName() == 'Hybrid') {
				map.setCenter(new google.maps.LatLng(41.8489055052432, -95.90747171258352), 3);
			}
		}
	});
};

Schools.prototype.sortBy = function(orderFn) {
	this.clearPageSchools();
	this.page = 1;

	control._schools._allSchools.sort(orderFn);
	control._schools.displaySchools();
	control._loadData('schoolResultsList');
};

Schools.prototype.displaySchools = function() {
	if(this.page > this._lastPage) {
		this.page = this._lastPage;
	}
	if(this.page < 1) {
		this.page = 1;
	}
	
	var slicePage = (this.page-1)*this._pageLimit;
	this._schools = this._allSchools.slice(slicePage, slicePage+this._pageLimit);
};

Schools.prototype.clearSchoolCampus = function() {
	map.getEarthInstance(function(object) {
		if(ge) {
			var c = ge.getFeatures().getFirstChild();
			while (c) {
				var s = c.getNextSibling();
				if(c.getSnippet() == '')
					ge.getFeatures().removeChild(c);
				c = s;
			}
		}
	});
	
	if(this._campus.length) {
		for(var i=0; i<this._campus.length; i++) {
			map.removeOverlay(this._campus[i][0]);
			this._campus[i][1] = null;
		}
		this._campus = [];
	}
};

Schools.prototype.loadSchoolCampus = function(ipeds) {
	this.clearSchoolCampus();
	
	var tableName = CAMPUS;
	var url = URL + 'php/ajaxDb.php';
	$.get(url, {"ipeds": ipeds, "table": tableName}, function(data) {
		var jsonObject = JSON.parse(data);
		if(jsonObject.length) {
			control._schools._loadSchoolCampusJson(jsonObject);
		} else {
			$("#campusBuildings").html('We\'re working with the school to incorporate a complete campus map. In the meantime, you can view school information using the tabs above, add directions, see local restaurants and hotels, add to your favorites and send to your friends on facebook.<br/><br/>Schools can claim your school with a custom map, custom data and 3D with Egiate.com. Send inquiry to <a href="mailto:mapmyschool@egiate.com">mapmyschool@egiate.com</a><br/><br/><b>Featured Campuses</b><br/><ul><li><a href="http://egiate.com/index.php?show=no&go=164988">Boston University</a></li><li><a href="http://egiate.com/index.php?show=no&go=126614">University of Colorado at Boulder</a></li></ul>');
		}
	});
};

Schools.prototype._loadSchoolCampusJson = function(jsonObject) {	
	var eIcon = new GIcon(G_DEFAULT_ICON);

	if(jsonObject[0].icon == 'y') {
		eIcon.iconSize = new GSize(12, 12);
		eIcon.shadowSize = new GSize(0, 0);
		eIcon.iconAnchor = new GPoint(6,12);
		var logoUrl = BASE_LOGO_URL+jsonObject[0].ipeds+'/icon.jpg';	
		eIcon.image = logoUrl;
	} else {
		eIcon.iconSize = new GSize(7, 11);
		eIcon.shadowSize = new GSize(0, 0);
		eIcon.iconAnchor = new GPoint(6,12);
		eIcon.image = BASE_LOGO_URL+'_default/icon-school-red.png';	
	}

	// Set up our GMarkerOptions object
	markerOptions = { title: 'campusIcon', icon: eIcon };

	var categoryNameOld = jsonObject[0].categoryName;
	var categoryNameNew = '';

	var subCategoryNameOld = jsonObject[0].subCategoryName;
	var subCategoryNameNew = '';
	
	var campusList = '<h3>'+categoryNameOld+'</h3><ul>';
	
	if(subCategoryNameOld != '') {
		campuseList += '<h3>'+subCategoryNameOld+'</h3><ul>';
	}

	for(var i = 0; i < jsonObject.length; i++) {
		markerBalloon(jsonObject[i], markerOptions, i);
		categoryNameNew = jsonObject[i].categoryName;
		subCategoryNameNew = jsonObject[i].subCategoryName;
		
		// alert(categoryNameOld + ' ' + categoryNameNew + ' ' + subCategoryNameOld + ' ' + subCategoryNameNew);
		
		if(categoryNameNew != categoryNameOld) {
			if(subCategoryNameNew == '') {
				campusList += '</ul>';
				subCategoryNameOld = subCategoryNameNew;
			} else {
				campusList += '</ul>';
				subCategoryNameOld = '';
			}
			categoryNameOld = categoryNameNew;
			campusList += '</ul><h3>'+categoryNameNew+'</h3><ul>';
		}
		
		if(subCategoryNameNew != subCategoryNameOld) {
			if(subCategoryNameNew == '') {
				campusList += '</ul>';
			} else if(subCategoryNameOld == '') {
				campusList += '<h3>'+subCategoryNameNew+'</h3><ul>';
			} else {
				campusList += '</ul><h3>'+subCategoryNameNew+'</h3><ul>' ;
			}
			subCategoryNameOld = subCategoryNameNew;

		}
		
		campusList += '<li><a href="#" onclick="if(ge){removeSpin(); ge.setBalloon(null);} control._schools.showMarkerBalloon('+i+');">' + jsonObject[i].name + '</a></li>';
	}
	campusList += '</ul>';
	
	$("#campusBuildings").html(campusList);
};


Schools.prototype.showMarkerBalloon = function(index) {
	var json = this._campus[index][1];
	var htmlContent = this.markerBalloonHtml(json.ipeds, json.name, json.description, json.icon);
	// this._campus[index][0].openInfoWindowHtml(htmlContent);
	startOpenBalloon(index, htmlContent);
};

Schools.prototype.markerBalloonHtml = function(ipeds, name, description, icon) {
	var logoUrl;
	if(icon == 'y') {
		logoUrl = BASE_LOGO_URL+ipeds+'/logo.jpg';
	} else {
		logoUrl = BASE_LOGO_URL+'_default/logo-default-egiate.png';
	}

	var htmlContent = "<div style=\"float:left; width:350px; height:100%; font-family:Trebuchet MS, Verdana, Arial, Helvetica, sans-serif; font-size:11px; color:#3f3f3f;\">"
					+ "		<div style=\"float:left; width:100%; margin-right:10px; margin-bottom:10px;\">"
					+ "			<div style=\"float:left; width:50px;\">"
					+ "				<div style=\"text-align:center;\">"
					+ "					<img src=\""+logoUrl+"\" />" 
					+ "				</div>"
					+ "			</div>"
					+ "			<div style=\"float:left; width:300px; color:#9C544B;\">"
					+ "				<div style=\"float:left; padding-left:5px; padding-top:20px;\">"
					+ "					<div style=\"float:left; width:100%;\"><b>" + name + "</b></div>"
					+ "				</div>"
					+ "			</div>"
					+ "		</div>"
					+ "		<div style=\"float:left; width:100%; margin-bottom:10px;\">"
					+			description
					+ "		</div>"
					+ "		<div style=\"float:left; width:100%;\">"
					//+ "			<div style=\"float:left; width:40%; padding:0 10px 0 10px;\">"
					//+ "				<a href=\"\">School Website</a>"
					//+ "				<br />"
					//+ "				<a href=\"\">Download Application</a>"
					//+ "			</div>"
					+ "			<div style=\"float:left; width:100%; padding:0 10px 0 0px;\">"
					+ "				<span style=\"color:#9C544B;\">Are you a School?</span> <a style=\"color:#274B68;\" href=\"" + BASE_URL + "/forschools.php\">Claim your School and have it mapped in 3D.</a>"
					+ "			</div>"
					+ "		</div>"
					+ "</div>";
	
	return htmlContent;
};

function markerBalloon(jsonObject, markerOptions, index) {
	var lat = jsonObject.lat;
	var lng = jsonObject.lng;
	
	var point = new GLatLng(Number(lat), Number(lng));
	
//	var marker = new GMarker(point, markerOptions);
//
//	var htmlContent = control._schools.markerBalloonHtml(ipeds, name, description);
//	// alert(htmlContent);
//	
//	GEvent.addListener(marker, "click", function() {
//		// marker.openInfoWindowHtml(htmlContent);
//		startOpenBalloon(index, htmlContent);
//	});

//	map.addOverlay(marker);
	map.addOverlay(createMarker(point, markerOptions, index, jsonObject));
//	var markerJsonArray = new Array(marker, jsonObject);
//	control._schools._campus.push(markerJsonArray);
}

function createMarker(point, markerOptions, index, jsonObject) {
	var ipeds = jsonObject.ipeds;
	var name = jsonObject.name;
	var description = jsonObject.description;

	var icon = jsonObject.icon;

//	var htmlContent = control._schools.markerBalloonHtml(ipeds, name, description, logoUrl);

	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
		startOpenBalloon(index);
	});
	var markerJsonArray = new Array(marker, jsonObject);
	control._schools._campus.push(markerJsonArray);
	  
	return marker;
}


function startOpenBalloon(index) {
	control._schools._index = index;
	var marker = control._schools._campus[index][0];
	
	if (map.getCurrentMapType().getName() == 'Earth') {
		google.earth.addEventListener(ge.getView(), 'viewchangeend', openBalloon);
		control.camera.moveCamera(marker.getLatLng().lat()+0.0004, marker.getLatLng().lng()+0.0007, 300, 45, 45);
	} else if(map.getCurrentMapType().getName() == 'Map' || map.getCurrentMapType().getName() == 'Hybrid') {
		openBalloon();
		map.setZoom(17);
	}
}

function openBalloon() {
	var index = control._schools._index;
	var marker = control._schools._campus[index][0];
	var json = control._schools._campus[index][1];
	var htmlContent = control._schools.markerBalloonHtml(json.ipeds, json.name, json.description, json.icon);
	// alert(htmlContent);
	
	marker.openInfoWindowHtml(htmlContent);
	
	if (map.getCurrentMapType().getName() == 'Earth') {
		google.earth.removeEventListener(ge.getView(), 'viewchangeend', openBalloon);
	}
}

/*
 * school object
 */
function School(ipeds, name, lat, lng, logo, city, state, 
		inTuitionAndFees, outTuitionAndFees, totalInState, totalOutState, applicantsTotal, enrolledTotal, percentAdmittedTotal) {
	var me = this;
	
	me._ipeds = ipeds;
	me._name = name;
	me._lat = lat;
	me._lng = lng;
	me._logo = logo;
	me._city = city;
	me._state = state;
	me._inTuitionAndFees = inTuitionAndFees;
	me._outTuitionAndFees = outTuitionAndFees;
	me._totalInState = totalInState;
	me._totalOutState = totalOutState;
	me._applicantsTotal = applicantsTotal;
	me._enrolledTotal = enrolledTotal;
	me._percentAdmittedTotal = percentAdmittedTotal;
	
}

School.prototype.getIpeds = function() {
	return this._ipeds;
};

School.prototype.getName = function() {
	return this._name;
};

School.prototype.getLat = function() {
	return this._lat;
};

School.prototype.getLng = function() {
	return this._lng;
};

School.prototype.getLogo = function() {
	return this._logo;
};

School.prototype.getCity = function() {
	return this._city;
};

School.prototype.getState = function() {
	return this._state;
};

School.prototype.getInTuitionAndFees = function() {
	return this._inTuitionAndFees;
};

School.prototype.getOutTuitionAndFees = function() {
	return this._outTuitionAndFees;
};

School.prototype.getTotalInState = function() {
	return this._totalInState;
};

School.prototype.getTotalOutState = function() {
	return this._totalOutState;
};

School.prototype.getApplicantsTotal = function() {
	return this._applicantsTotal;
};

School.prototype.getEnrolledTotal = function() {
	return this._enrolledTotal;
};

School.prototype.getPercentAdmittedTotal = function() {
	return this._percentAdmittedTotal;
};

/*
 * school placemark object
 */
function SchoolPlacemark(ipeds, placemark, point) {
	var me = this;
	
	me._ipeds = ipeds;
	me._placemark = placemark;
	me._point = point;
}
	
SchoolPlacemark.prototype.getPlacemark = function() {
	return this._placemark;
};

SchoolPlacemark.prototype.getPoint = function() {
	return this._point;
};

SchoolPlacemark.prototype.getLat = function() {
	return this._point.getLatitude();
};

SchoolPlacemark.prototype.getLng = function() {
	return this._point.getLongitude();
};

SchoolPlacemark.prototype.isSeen = function() {
	return this._placemark.getVisibility();
};

SchoolPlacemark.prototype.setSeen = function(visibility) {
	this._placemark.setVisibility(visibility);
};


/*
 * school list sorting
 */
Schools.prototype.sortNameAsc = function(thisObject, thatObject) {
	if(thisObject._name > thatObject._name) {
		return 1;
	} else if (thisObject._name < thatObject._name) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortNameDec = function(thisObject, thatObject) {
	if(thisObject._name < thatObject._name) {
		return 1;
	} else if (thisObject._name > thatObject._name) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortStateAsc = function(thisObject, thatObject) {
	if(thisObject._state > thatObject._state) {
		return 1;
	} else if (thisObject._state < thatObject._state) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortStateDec = function(thisObject, thatObject) {
	if(thisObject._state < thatObject._state) {
		return 1;
	} else if (thisObject._state > thatObject._state) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortPopAsc = function(thisObject, thatObject) {
	if(thisObject._applicantsTotal > thatObject._applicantsTotal) {
		return 1;
	} else if (thisObject._applicantsTotal < thatObject._applicantsTotal) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortPopDec = function(thisObject, thatObject) {
	if(thisObject._applicantsTotal < thatObject._applicantsTotal) {
		return 1;
	} else if (thisObject._applicantsTotal > thatObject._applicantsTotal) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortPriAsc = function(thisObject, thatObject) {
	if(thisObject._totalInState > thatObject._totalInState) {
		return 1;
	} else if (thisObject._totalInState < thatObject._totalInState) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortPriDec = function(thisObject, thatObject) {
	if(thisObject._totalOutState < thatObject._totalOutState) {
		return 1;
	} else if (thisObject._totalOutState > thatObject._totalOutState) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortAdmAsc = function(thisObject, thatObject) {
	if(thisObject._percentAdmittedTotal > thatObject._percentAdmittedTotal) {
		return 1;
	} else if (thisObject._percentAdmittedTotal < thatObject._percentAdmittedTotal) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortAdmDec = function(thisObject, thatObject) {
	if(thisObject._percentAdmittedTotal < thatObject._percentAdmittedTotal) {
		return 1;
	} else if (thisObject._percentAdmittedTotal > thatObject._percentAdmittedTotal) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortEnrAsc = function(thisObject, thatObject) {
	if(thisObject._enrolledTotal > thatObject._enrolledTotal) {
		return 1;
	} else if (thisObject._enrolledTotal < thatObject._enrolledTotal) {
		return -1;
	}
	return 0;
};

Schools.prototype.sortEnrDec = function(thisObject, thatObject) {
	if(thisObject._enrolledTotal < thatObject._enrolledTotal) {
		return 1;
	} else if (thisObject._enrolledTotal > thatObject._enrolledTotal) {
		return -1;
	}
	return 0;
};







