var qLay;


/* This function will move the map to the North or Downtown Campus, *
 * and will overlay a custom image based on a query string:         *
 * ex: uoit.ca?camp=d - will focus the map downtown                 *
 * ex: uoit.ca?camp=d&qm=eventid - will show a custom image downtown*/
 
function overlayCheck() {

	var loc=new String(window.location);
	
	//var qm=loc.lastIndexOf("?");
	var qm = getQuerystring('qm');
	var swBound = ""; // South-West corner of image overlay
	var neBound = ""; // North-East corner of image overlay
	var bounds = ""; // swBound & neBound for map initialization
	
		
		
		if (camp=="d") {
		 	swBound = new google.maps.LatLng(43.8969955945,-78.86379003525);
  		 	neBound = new google.maps.LatLng(43.90113,-78.85756);
			map.panTo(new google.maps.LatLng(43.89818230111724, -78.86193931102753));
			dtMarkers();
			$("#downRad").attr('checked',true);
			$("#northRad").attr('checked',false);
			$("#campuses").buttonset('refresh');
		 
		} else {
		 //set the boundaries for the  north oshawa campus overlay image
 	 	 	swBound = new google.maps.LatLng(43.9397,-78.90050);
  	 	    neBound = new google.maps.LatLng(43.9511,-78.8907);
		}
  	  	bounds = new google.maps.LatLngBounds(swBound, neBound);
	
		//var overlay = new google.maps.GroundOverlay(qImg,bounds);
		//overlay.setMap(map);
		
	// Check to see if a custom image is set in the query string
	if (qm != "") {
		var qImg = qm; // don't ask.
		if (qImg.length>0) {
			qImg="./overimg/"+qImg+".png";
			qLay = new UOITOverlay(bounds, qImg, map);
		}
	
	}
	
	if (!DetectIphoneOrIpod()) {
		// Initialize and display the north oshawa campus image overlay
		swBound = new google.maps.LatLng(43.9397,-78.90050);
		neBound = new google.maps.LatLng(43.9511,-78.8907);
		bounds = new google.maps.LatLngBounds(swBound, neBound);
		uoitNorthLayer = new UOITOverlay(bounds, "./overimg/over.png", map);
	
		// Initialize and display the downtown oshawa campus image overlay
		swBound = new google.maps.LatLng(43.897024,-78.864286);
		neBound = new google.maps.LatLng(43.90116,-78.85745);
		bounds = new google.maps.LatLngBounds(swBound, neBound);
		uoitDowntownLayer = new UOITOverlay(bounds, "./overimg/downtown_over.png", map);
	}
}


function UOITOverlay(bounds, image, map) {
	// initialize all properties
	this.bounds_ = bounds;
	this.image_ = image;
	this.map_ = map;
	this.div_ = null;
	
	//call setMap() on THIS overlay
	this.setMap(map);
}

UOITOverlay.prototype = new google.maps.OverlayView();
UOITOverlay.prototype.onAdd = function() {

// create the DIV and set some basic attributes.
var div = document.createElement('DIV');
div.style.border = "none";
div.style.borderWidth = "0px";
div.style.position = "absolute";
div.className = "theOverlays";

// create an IMG element and attach it to the DIV.
var img = document.createElement("img");
img.src = this.image_;
img.style.width = "100%";
img.style.height = "100%";
div.appendChild(img);

// set the overlay's div_ property to this DIV
this.div_ = div;

var panes = this.getPanes();
panes.overlayImage.appendChild(div);
}

UOITOverlay.prototype.draw = function() {

// Size and position the overlay. We use a southwest and northeast
// position of the overlay to peg it to the correct position and size.
// We need to retrieve the projection from this overlay to do this.
var overlayProjection = this.getProjection();

// Retrieve the southwest and northeast coordinates of this overlay
// in latlngs and convert them to pixels coordinates.
// We'll use these coordinates to resize the DIV.
var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());

// Resize the image's DIV to fit the indicated dimensions.
var div = this.div_;
div.style.left = sw.x + 'px';
div.style.top = ne.y + 'px';
div.style.width = (ne.x - sw.x) + 'px';
div.style.height = (sw.y - ne.y) + 'px';
}

UOITOverlay.prototype.hide = function() {
  if (this.div_) {
    this.div_.style.visibility = "hidden";
  }
}

UOITOverlay.prototype.show = function() {
  if (this.div_) {
    this.div_.style.visibility = "visible";
  }
}

UOITOverlay.prototype.toggle = function() {
  if (this.div_) {
    if (this.div_.style.visibility == "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}
