Handler.add(window, "load", function(e_){	
	var search_div = Utils.ge("search_bar");
	var search_field = new SearchField(search_div);
	Utils.applyDefault(search_div.childNodes[2], "search members", "text");
	
	Utils.applyDefault(Utils.ge("email"), "email", "text");
	//Utils.applyDefault(Utils.ge("password"), "password", "password");
	var placeholder = Utils.ge("password_placeholder");
	placeholder.innerHTML = "password";
	placeholder.onclick = function(){
		this.style.display = "none";
		Utils.ge("password").focus();
	}
	Utils.ge("password").onfocus = function(){
		placeholder.style.display = "none";
	}
	
	Utils.applyEnter(Utils.ge("password"), function(){doLogin();});
	
	if(Utils.ge("map")){
		makeMap();
		/*self.mc = new MouseCapture();
		if(!self.MouseTrack){		
			self.mc.start(e_);
		} */	
	}
});

/*Handler.add(window, "unload", function(){
	if(self.mc && !self.MouseTrack){
		self.mc.stop();		
		Ajax.post("save_mouse_track", self.mc.getXML());		
	}		
});*/

function doLogin(){
	var email 		= Utils.ge("email");
	var password 	= Utils.ge("password");
	if(!email || email.value == "email" || !password)
		return;
	email = email.value;
	password = password.value;
	Utils.displayProgress();
	Ajax.get("get_login_challenge", {email: email}, function(json){
		var res = hex_md5(password+ "" + json.challenge);
		Ajax.get("login_user", {email: email, password: res}, function(json){
			Utils.hideProgress();
			window.location = Ajax.HOST+"?f=display_page&member_id="+json.user_id;
		});
	});
}

var map = null;
var last_id = 0;
var first_id = 0;
var new_content = null;
var bubble = new Bubble();

function makeMap(){		
	var lat = 59.92199;
	var lon = 10.70068359;
	var zoom = 12;
	if (GBrowserIsCompatible()) {
		map = new GMap2(Utils.ge("map"));
		var overlayControl = new GOverviewMapControl(); 
		//map.addControl(overlayControl);		
		map.setCenter(new GLatLng(lat*1.5, lon*1.5	), zoom);		
    	map.setMapType(G_PHYSICAL_MAP);   
    	//setTimeout("checkOverview()",100);    	
    	pullNextImage();	
	}    	
}



function pullNextImage(){
	Ajax.get("pull_frontpage_image", {last_id: last_id, first_id: first_id}, function(json){
		var new_photo = false;
		if(last_id != 0 && json.last_id > json.last_id)
			new_photo = true;
		if(json.last_id)
			last_id = json.last_id;
		if(json.first_id)
			first_id = json.first_id;
		if(first_id*1 == 0)
			first_id = last_id;			
		var img = new Image();
		img.src = json.file.replace(".jpg", "_180.jpg");		
		var point = new GLatLng(json.lat, json.lng);
		var icon = getMarker(point, new_photo);
		var inner_div = document.createElement("div");
		map.clearOverlays();
		var inner = "";
			inner 	+= "<div style='font-size: 10px; width: 100px; text-align: center; color: #FFF; '>"+json.title+"</div>";
		inner += "<img src='"+img.src+"'  style='width: 100px;'></img>";
		inner_div.innerHTML = inner;		
		bubble.remake(inner_div, icon, img);
		map.addOverlay(icon);
		map.panTo(new GLatLng(point.lat()*1+0.01, point.lng()));					
		map.addOverlay(bubble);
		pullNextImage();	
	});
}

function getMarker(point, new_photo){
	var picture_icon = new GIcon();
	picture_icon.iconSize = new GSize(22, 22);
    picture_icon.iconAnchor = new GPoint(8, 8);
    picture_icon.image = (new_photo ) ? ("/img/map/icon_yellow2_photo.png") : ("/img/map/icon_yellow2_photo.png");
    picture_icon.iconSize = new GSize(16, 16); 
    picture_icon.shadow="/img/map/icon_shadow.png"; 
    picture_icon.shadowSize = new GSize(20, 20);
    var markerOptions = { icon:picture_icon };
    return new GMarker(new GLatLng(point.lat(), point.lng()), markerOptions);     
}

function Bubble(){
	this.content_div = document.createElement("div");
}
Bubble.prototype.remake = function(content_div_, icon_, image_){
	this.content_div = content_div_;
	this.icon = icon_;
	this.height = 0;
	var self = this;
	image_.onload = function(){		
		if(self.bubble && self.bubble.childNodes[0].offsetHeight != self.height){
			self.height	= self.bubble.childNodes[0].offsetHeight;
			self.redraw(true); 
		} 									
	}
}
Bubble.prototype.redraw = function(force){
	if (!force) 
		return;
	var icon_point = this.icon.getPoint();
  	var left = this.map.fromLatLngToDivPixel(icon_point).x;
  	var top = this.map.fromLatLngToDivPixel(icon_point).y;
	this.bubble.style.left = (left-51)+"px";
	this.bubble.style.top = (top-this.height-10)+"px";
	this.bubble.style.width = "102px";
	this.bubble.style.visibility = "visible";
}
Bubble.prototype.initialize = function(map_){
    var inner = "<div class='b_body clear_fix'></div>"+
    			"<div class='b_close2 news_icon icon_close'></div>"+    			
    			"<div class='b_bottom'>" +
    				"<div class='b_dip'></div>" +
    			"</div>";
    
   	var bubble_ = document.createElement("div");
   	bubble_.style.cssText = "position: absolute; visibility: hidden";
   	bubble_.innerHTML = inner;
   	bubble_.childNodes[0].appendChild(this.content_div);
   	map_.getPane(G_MAP_FLOAT_PANE).appendChild(bubble_);
	this.height 	= bubble_.childNodes[0].offsetHeight;	
	var center 		= map_.getCenter();
	var bounds_ 	= map_.getBounds();
	var delta 		= bounds_.getNorthEast().lat()-bounds_.getSouthWest().lat();
	var icon_pos 	= new GLatLng(center.lat()+(delta/4), center.lng());
	
	var southWest = bounds_.getSouthWest();
	var northEast = bounds_.getNorthEast();
	var lngDelta = (northEast.lng() - southWest.lng()) / 4;
	var latDelta = (northEast.lat() - southWest.lat()) / 4;
	var RectBounds_ = new GLatLngBounds(
    	new GLatLng(southWest.lat() + latDelta, southWest.lng() + lngDelta),
    	new GLatLng(northEast.lat() - latDelta, northEast.lng() - lngDelta)
    );
	this.map = map_;
	this.bubble = bubble_;
    this.bounds = RectBounds_;
}

Bubble.prototype.remove = function() {
	if(this.bubble && this.bubble.parentNode)
  		this.bubble.parentNode.removeChild(this.bubble);
}
Bubble.prototype.copy = function() {
	return new Bubble(this.htmlText);
}
Bubble.prototype = new GOverlay();

