
var SEE_LOCATION='see_location';var CHANGE_LOCATION='change_location';var DEFAULT_LOCATION=SEE_LOCATION;function GLocation(objName,GMap,projectURL,currentUserId,viewUserId)
{this.objName=objName;this.projectURL=projectURL;this.ajaxGetPHPScript=this.projectURL+'ajax.get.php';this.currentUser=[];this.viewUser=[];this.currentUser.id=currentUserId;this.viewUser.id=viewUserId;this.GMap=GMap;this.markerCluster=new ClusterMarker(this.GMap);this.massageDOM=$('messageDiv');this.GMapCenter={lat:45.4419,lng:-122.1419};this.GoogleExtension();this.setIcons();this.MSGWait=new MSGWait(this.objName+'.MSGWait',this.projectURL);this.runObject();}
GLocation.prototype.SendRequest=function(usrTo,method,answerFunction,data,event)
{var responseText=null;var responseData=null;var referenceThis=this;var dataRequest=[];if(data)
{dataRequest=data;}
if(event)
{dataRequest['event']=event;}
funct=function(text,hash)
{eval('referenceThis.'+answerFunction+'(text, hash)');};new Ajax.Request(usrTo,{method:method,parameters:dataRequest,onSuccess:function(transport)
{responseText=transport.responseText;try
{responseData=responseText.evalJSON();}catch(e){}
funct(responseText,responseData);},onFailure:function(transport)
{alert('Please, check your connection!');}});}
GLocation.prototype.runObject=function()
{this.MSGWait.showDiv();this.SendRequest(this.ajaxGetPHPScript,'get','initMap',{userid:this.viewUser.id},'LoadLocation');}
GLocation.prototype.initMap=function(text,data)
{this.GMapDiv='GoogleMap';this.GMap.addControl(new GLargeMapControl());this.GMap.addControl(new GMapTypeControl());GEvent.addListener(this.GMap,"click",this.onClickMap(this));this.viewUser.data=data;this.reset();this.showUserLocation();this.MSGWait.hideDiv();}
GLocation.prototype.reset=function()
{this.GMap.clearOverlays();this.tempPosition=null;this.updateMessage('');this.status=DEFAULT_LOCATION;}
GLocation.prototype.setIcons=function()
{this.goofyIcon=new GIcon(G_DEFAULT_ICON,this.projectURL+'media/images/goofy.png');this.goofyIcon.iconSize=new GSize(26,35);this.goofyIcon.shadow=this.projectURL+'media/images/shadow.png';this.goofyIcon.shadowSize=new GSize(39,35);this.regularIcon=new GIcon(this.goofyIcon,this.projectURL+'media/images/regular.png');}
GLocation.prototype.onClickMap=function(objectRef)
{return function(marker,point)
{if(point&&objectRef.status==CHANGE_LOCATION)
{objectRef.setNewPosition(point);}
if(marker&&objectRef.status==SEE_LOCATION)
{objectRef.showUserWindow(marker);}};}
GLocation.prototype.setNewPosition=function(point)
{if(this.tempPosition)
{this.GMap.removeOverlay(this.tempPosition);this.tempPosition=null;}
icon=(this.viewUser.data.stance=='Goofy'?this.goofyIcon:this.regularIcon);this.tempPosition=new GMarker(point,icon);this.GMap.addOverlay(this.tempPosition);this.updateMessage('Now you can <a class="textlink" href="save your location" onclick="'+this.objName+'.saveLocation(); return false;">save your location</a>'+' or <a class="textlink" href="cancel" onclick="'+this.objName+'.cancelActions(); return false;">cancel</a>.');}
GLocation.prototype.showUserWindow=function(marker)
{if(marker.getUserId&&marker.getUserId()>0)
{this.SendRequest(this.ajaxGetPHPScript,'get','onSuccessShowUserWindow',{userid:marker.getUserId()},'LoadUserForLocation');}}
GLocation.prototype.onSuccessShowUserWindow=function(text,data)
{if(data)
{var html='<table width="300">'+'<tr><td style="padding: 10px; height: 110px; border: 1px solid #aaa;" align="center" valign="middle">';if(data.userPhoto!=null)
{html+='<img src="'+data.userPhoto+'" />';}
else
{html+='<div style="font-size: 14px;">No photo</div>';}
html+='</td><td align="center" valign="top">'+'<div style="font-size: 14px;"><b>'+data.username+'</b></div>'+'<div style="padding: 10px;">'+'<div style="padding: 10px;">Rating: '+data.rating+'</div>'+'<a class="minilink" href="'+this.projectURL+'profiles/'+data.username+'"> Go to user\'s profile</a>'+'</div>'+'</td></tr>';html+='</table>';this.GMap.openInfoWindowHtml(new GLatLng(data.lat,data.lng),html);}}
GLocation.prototype.GoogleExtension=function()
{GMarker.prototype.setUserId=function(id)
{this.userId=id;}
GMarker.prototype.getUserId=function()
{return(this.userId?this.userId:0);}}
GLocation.prototype.updateMessage=function(text)
{this.massageDOM.update(text);}
GLocation.prototype.isHaveLocation=function()
{return(this.viewUser.data&&this.viewUser.data.lat!=this.viewUser.data.lng&&this.viewUser.data.lat!=0)}
GLocation.prototype.isCanChange=function()
{return(this.viewUser.id==this.currentUser.id&&this.currentUser.id>0);}
GLocation.prototype.showUserLocation=function()
{if(this.isHaveLocation())
{point=new GLatLng(this.viewUser.data.lat,this.viewUser.data.lng);this.GMap.setCenter(point,3);icon=(this.viewUser.data.stance=='Goofy'?this.goofyIcon:this.regularIcon);this.GMap.addOverlay(new GMarker(point,icon));if(this.isCanChange())
{this.updateMessage('You have marked your location. To change your location <a class="textlink" href="change your location" onclick="'+this.objName+'.changeLocation(); return false;">click here</a>');}}
else
{this.GMap.setCenter(new GLatLng(this.GMapCenter.lat,this.GMapCenter.lng),2);if(this.isCanChange())
{this.changeLocation();}
else
{if(this.viewUser.id>0)
{this.updateMessage('This user has not added their location yet.');}
this.loadAllUsers();}}}
GLocation.prototype.loadAllUsers=function()
{this.SendRequest(this.ajaxGetPHPScript,'get','onSuccessLoadAllUsers',{userid:this.viewUser.id},'LoadUsersLocation');}
GLocation.prototype.onSuccessLoadAllUsers=function(text,data)
{if(data)
{var markers=[];for(var i=0;i<data.getSize();i++)
{if(!(data[i].lat==data[i].lng&&data[i].lat==0))
{icon=(data[i].stance=='Goofy'?this.goofyIcon:this.regularIcon);marker=new GMarker(new GLatLng(data[i].lat,data[i].lng),icon);marker.setUserId(data[i].id);markers.push(marker);}}
this.markerCluster.removeMarkers();this.markerCluster.addMarkers(markers);this.markerCluster.fitMapToMarkers();}}
GLocation.prototype.saveLocation=function()
{point=this.tempPosition.getLatLng();this.GMap.removeOverlay(this.tempPosition);this.SendRequest(this.ajaxGetPHPScript,'get','onSuccesseSaveLocation',{userid:this.viewUser.id,lat:point.lat(),lng:point.lng()},'SaveLocation');}
GLocation.prototype.onSuccesseSaveLocation=function(text,data)
{this.viewUser.data=data;this.reset();this.showUserLocation();this.MSGWait.hideDiv();}
GLocation.prototype.changeLocation=function()
{if(this.isCanChange())
{this.reset();this.status=CHANGE_LOCATION;if(this.isHaveLocation())
{icon=(this.viewUser.data.stance=='Goofy'?this.goofyIcon:this.regularIcon);this.tempPosition=new GMarker(new GLatLng(this.viewUser.data.lat,this.viewUser.data.lng),icon);this.GMap.addOverlay(this.tempPosition);}
if(this.isHaveLocation())
{this.updateMessage('Please, choose your location by click on the map or <a class="textlink" href="cancel" onclick="'+this.objName+'.cancelActions(); return false;">cancel</a>');}
else
{this.updateMessage('Please choose your location by clicking on the map');}}}
GLocation.prototype.cancelActions=function()
{this.reset();this.showUserLocation();}