function initProjects() {
	
	
	var projects = $("projects");
	
	var project  = null;
	var overlays = null;
	var overlay  = null;
	
	projects_list = projects.getElementsBySelector("div");

	for ( var i = 0; i < projects_list.length; i++) {
		project = projects_list[i];
		link = project.getElementsBySelector("div");
		if (link != "") {
			link[0].observe('mouseover', showProjectImage);
		}
	}
}

function showProjectImage(event) {
	link = Event.element(event);
	
	project = link.ancestors()[0];
	overlay = project.getElementsBySelector("img#overlay");
	
	link.stopObserving('mouseover', showProjectImage);
	link.observe('mouseout', hideProjectImage);
	
	if (overlay != "") {
		overlay[0].style.display = "none";
	}
}

function hideProjectImage(event) {
	link = Event.element(event);
	
	project = link.ancestors()[0];
	overlay = project.getElementsBySelector("img#overlay");
	
	link.stopObserving('mouseout', hideProjectImage);
	link.observe('mouseover', showProjectImage);
	
	if (overlay != "") {
		overlay[0].style.display = "block";
	}
}

function initGallery() {
	
	
	var gallery = $("gallery");
	
	var gallery_image  = null;
	var overlays = null;
	var overlay  = null;
	
	gallery_list = gallery.getElementsBySelector("div");

	for ( var i = 0; i < gallery_list.length; i++) {
		gallery_image = gallery_list[i];
		link = gallery_image.getElementsBySelector("div");
		if (link != "") {
			link[0].observe('mouseover', showGalleryImage);
		}
	}
}

function showGalleryImage(event) {
	link = Event.element(event);
	
	gallery = link.ancestors()[0];
	overlay = gallery.getElementsBySelector("img#overlay");

	link.stopObserving('mouseover', showGalleryImage);
	link.observe('mouseout', hideGalleryImage);
	
	if (overlay != "") {
		overlay[0].style.display = "none";
	}
}

function hideGalleryImage(event) {
	link = Event.element(event);
	
	gallery = link.ancestors()[0];
	overlay = gallery.getElementsBySelector("img#overlay");
	
	link.stopObserving('mouseout', hideGalleryImage);
	link.observe('mouseover', showGalleryImage);
	
	if (overlay != "") {
		overlay[0].style.display = "block";
	}
}

function showTwitters() {
	users = ['etruta', 'lupeke', 'malungo'];
	rand = (Math.random() * 100) / 34;
	getTwitters('twitter_message', { 
	  id: users[parseInt(rand)], 
	  count: 1, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  withFriends: true,
	  enableLinks: true,
		//template: '%text% <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">[ %user_screen_name% ]</a>'
		template: '%text% <a href="http://twitter.com/%user_screen_name%/">[ %user_screen_name% ]</a>'
	});
}

function init(page) {
	
	if(page == "projects") {
		initProjects();
	}
	
	if(page == "gallery") {
		initGallery();
	}
	//mjt.run();
	init_google_maps();
	load_locations();
	/*parse_locations([
    {
      latitude: "-23.575079641392875",
      longitude: "-46.63794994354248",
      weight: "200"
    },
    {
      latitude: "-23.536251748768812",
      longitude: "-46.675286293029785",
      weight: "1300"
    },
	]);*/
}


function load_locations() {
  $.getJSON("http://dalata.laboratorio.us/devices", function(data) {
    parse_locations(data);
  }, "text");
}

function parse_locations(locations) {
  $(locations).each(function() {
    //console.log(this);
    add_marker(this.latitude, this.longitude, this.weight, this.location);
  });
}

function init_google_maps() {
  if (!GBrowserIsCompatible()) return;
  
  map = new GMap2(document.getElementById("embed"));
  geocoder = new GClientGeocoder();
  GEvent.addListener(map, "click", function(overlay, latlng, overlaylatlng) {
    //console.log(overlaylatlng);
    if (overlay) {
      //map.setCenter(overlaylatlng, 12);
      map.openInfoWindowHtml(overlaylatlng, 
          //document.createTextNode(overlay.getTitle() + " em latas neste endereço.")
          overlay.getTitle()
          );
		}
  });
  //map.enableGoogleBar();
  var customUI = map.getDefaultUI();
	//customUI.controls.maptypecontrol = false;
	map.setUI(customUI);
	//var spLatLng = new google.maps.LatLng(-23.547778, -46.635833);
	var spLatLng = new google.maps.LatLng(-23.564499,-46.65679);
  map.setCenter(spLatLng, 14);
}

function add_marker(latitude, longitude, weight, loc) {
  if (!map) return;
  
  var icon = new GIcon();
  if (weight < 500) icon.image = "http://dalata.laboratorio.us/images/pin_pouco.png";
  else if (weight < 1000) icon.image = "http://dalata.laboratorio.us/images/pin_medio.png";
  else icon.image = "http://dalata.laboratorio.us/images/pin_muito.png";
  
  icon.size = new GSize(25,25);
  icon.iconAnchor = new GPoint(10,0);
  icon.infoWindowAnchor = new GPoint(10,0);
  
  var position = new GLatLng(latitude, longitude);
  var marker = new GMarker(position, {
    icon: icon,
        "draggable": false,
		//title: "<b>" + loc + "</b><br /><br />" + "Weight: " + weight + " grams <br />" + "Units: " + (weight / 14.5) + " cans"
		title: "<b>" + loc + "</b><br /><br />" + "Units: " + (weight / 14.5) + " cans <br/>Weight: " + weight + "g"
	});
	map.addOverlay(marker);
}