var FFMP_leftmsg
var FFMP_rightmsg
var FFMP_picturerow
var FFMP_map
var FFMP_geocoder
var FFMP_marker=[]

function FFMP_ShowGeoMap() {
  var lat=0
  var lon=0
  var zm=4
  try {FFMP_geocoder = new GClientGeocoder()}
  catch(err) {return}
  FFMP_map = new GMap2(document.getElementById("FFMP_GoogleMap"))
  if (document.getElementById("FFMP_PostCoords")) {
    var cd=document.getElementById('FFMP_PostCoords').innerHTML.split(':')
    lat=cd[0]
    lon=cd[1]
    zm=11
  }
  if (document.getElementById('FFMP_default')){
    if (document.getElementById('FFMP_default').lat) {
      lat=document.getElementById('FFMP_default').lat.value
      lon=document.getElementById('FFMP_default').lon.value
      zm=11
    }
  }
  if (lat==0&&lon==0) {lat=55;lon=-4;zm=4}
  FFMP_map.setCenter(new GLatLng(lat,lon),zm)
  FFMP_map.addControl(new GSmallMapControl)
  FFMP_geocoder = new GClientGeocoder()
  GEvent.addListener(FFMP_map, "click", FFMP_getAddress)
  GEvent.addListener(FFMP_map, "moveend", FFMP_NewView)
  FFMP_NewView()
}

function FFMP_NewView() {
  var mapzoom=FFMP_map.getZoom()
  if (mapzoom<4) {
    FFMP_map.clearOverlays()
    return
  }
  if (!document.getElementById("FFMP_GeoCoords")) {return}
  if (document.getElementById("FFMP_GeoCoords").innerHTML=='') {return}
  var bounds = FFMP_map.getBounds()
  var southWest = bounds.getSouthWest()
  var northEast = bounds.getNorthEast()
  var swlat=southWest.lat()
  var nelat=northEast.lat()
  var swlon=southWest.lng()
  var nelon=northEast.lng()
  var spanlat=nelat-swlat
  var spanlon=nelon-swlon
  var cdata=document.getElementById('FFMP_GeoCoords').innerHTML.split(':')
  var totalposts=cdata[1]
  if (totalposts<1) {return}
  var mapcenter=FFMP_map.getCenter()
  FFMP_map.clearOverlays()
  var p
  var latlng
  for (var m=0; m<totalposts; m++) {
    p=cdata[m+2].split('^')
    if (p[1]>swlat&&p[1]<nelat&&p[2]>swlon&&p[2]<nelon) {
      AddMarker(m,p[0],p[1],p[2],p[3])
    }
  }
}

function AddMarker(i, p, lat, lon, t) {
  latlng = new GLatLng(lat,lon)
  FFMP_marker[i]=new GMarker(latlng,{title:t})
  FFMP_map.addOverlay(FFMP_marker[i])
  GEvent.addListener(FFMP_marker[i], "click", function () {FFMP_GoToPost(p);})
}


function FFMP_FindLocation(adr) {
  if ((adr=='')||(adr=='i.e. London, UK')) {return}
  if (FFMP_geocoder) {
    FFMP_geocoder.getLatLng(adr,
    function(point) {
      if (!point) {
        FFMP_map.setCenter(new GLatLng(55,-4),5);
        window.alert('The location\n'+adr+'\nwas not found.\nPlease use the map to find the location.')
      } else {
        FFMP_map.setCenter(point, 11)
        FFMP_geocoder.getLocations(point, FFMP_showAddress)
        }
      }
    )
  }
}

function FFMP_getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng
    FFMP_geocoder.getLocations(latlng, FFMP_showAddress)
  }
}

function FFMP_showAddress(response) {
  if (!document.getElementById("FFMP_default")) {return}
  var obj=document.getElementById("FFMP_default")
  if (!obj.lat) {return}
  FFMP_map.clearOverlays()
  if (!response || response.Status.code != 200) {
    alert('Unable to determine location\nPlease try again')
  }
  else {
    var address=response.Placemark[0].AddressDetails.Country
    var area=''
    try {area=address.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName}
    catch(err) {area=''}
    if (area!='') {area+=', '}
    area+=address.AdministrativeArea.AdministrativeAreaName
    if (area!='') {area+=', '}
    if (address.CountryNameCode=='GB') {area+='UK'}
    else {area+=address.CountryNameCode}
    place = response.Placemark[0]
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0])
    FFMP_marker[0] = new GMarker(point)
    FFMP_map.addOverlay(FFMP_marker[0])
    obj.lat.value=place.Point.coordinates[1]
    obj.lon.value=place.Point.coordinates[0]
    obj.generalarea.value=area
  }
}

function FFMP_GoToPost(p) {
  document.location=p
}

function FFMP_ScrollWindow(n,s,d) {
  if (!document.getElementById(n)) {return}
  var thisobj=this
  this.speed=s
  this.direction=d
  this.stop=false
  var obj=document.getElementById(n)
  var temp=obj.innerHTML
  obj.innerHTML='<div id=\"'+n+'1\" style=\"position: absolute; left: 0px; top: 0px; width: auto; height: auto\" onmouseover=\"'+n+'.ScrollPause(true)\" onmouseout=\"'+n+'.ScrollPause(false)\"></div><div id=\"'+n+'2\" style=\"position: absolute; left: 0px; top: 0px; width: auto; height: auto\" onmouseover=\"'+n+'.ScrollPause(true)\" onmouseout=\"'+n+'.ScrollPause(false)\"></div>'
  this.content=document.getElementById(n+'1')
  this.trailer=document.getElementById(n+'2')
  this.content.style.textAlign=obj.style.textAlign
  this.trailer.style.textAlign=obj.style.textAlign
  if (d=='v') {
    this.content.innerHTML=temp
    this.content.style.width=obj.style.width
    this.trailer.style.width=obj.style.width
  }
  else {
    this.content.innerHTML=temp
    this.content.style.height=obj.style.height
    this.trailer.style.height=obj.style.height
  }
  if (d=='v') {
    this.containerheight=parseInt(obj.style.height)
    this.contentheight=this.content.offsetHeight
    if (this.contentheight>this.containerheight) {
      if (s>0) {
        this.trailer.style.top=this.contentheight+'px'
        this.trailer.innerHTML=temp
        setTimeout(function(){thisobj.ScrollUp()}, 3000)
      }
      else {
        this.content.style.height=obj.style.height
        this.content.style.overflow='scroll'
      }
    }
  }
  else {
    this.containerwidth=parseInt(obj.style.width)
    this.contentwidth=this.content.offsetWidth
    if (this.contentwidth>this.containerwidth) {
      if (s>0) {
        this.trailer.style.left=this.contentwidth+'px'
        this.trailer.innerHTML=temp
        setTimeout(function(){thisobj.ScrollUp()}, 3000)
      }
      else {
        this.content.style.width=obj.style.width
        this.content.style.overflow='scroll'
      }
    }
  }
}

FFMP_ScrollWindow.prototype.ScrollUp=function() {
  if (this.direction=='v') {
    if (parseInt(this.content.style.top)<=-this.contentheight) {
      this.content.style.top='0px'
      this.trailer.style.top=this.contentheight+'px'
    }
  }
  else {
    if (parseInt(this.content.style.left)<=-this.contentwidth) {
      this.content.style.left='0px'
      this.trailer.style.left=this.contentwidth+'px'
    }
  }
  var thisobj=this
  setTimeout(function(){thisobj.ScrollUp()}, 40)
  if (this.stop) {return}
  if (this.direction=='v') {
    this.content.style.top=parseInt(this.content.style.top)-this.speed+'px'
    this.trailer.style.top=parseInt(this.trailer.style.top)-this.speed+'px'
  }
  else {
    this.content.style.left=parseInt(this.content.style.left)-this.speed+'px'
    this.trailer.style.left=parseInt(this.trailer.style.left)-this.speed+'px'
  }
}

FFMP_ScrollWindow.prototype.ScrollPause=function(p) {
  this.stop=p
}

function FFMP_SearchField(n, t, v) {
  if (n.value==t) {n.value=v}
}

function FFMP_Validate(f) {
  var r=true
  var e='Required'
  var w='&nbsp;'
  var obj=document.getElementById('FFMP_search')
  if (f=='Find >>') {
    if (((obj.searchfirstname.value!='')&&(obj.searchfirstname.value!='First Name'))||((obj.searchsurname.value!='')&&(obj.searchsurname.value!='Surname'))) {obj.action.value='search'; obj.submit()}
    else {window.alert('Please enter a First Name or a Surname')}
  }
  if (document.getElementById('FFMP_default')==null) {return false}
  obj=document.getElementById('FFMP_default')
  if (f=='SendMessage') {
    if (obj.Email.value=='') {r=false; w=e}
    document.getElementById('FFMP_ErrEmail').innerHTML=w
    if (r==false) {window.alert('Please complete the fields marked \''+e+'\''); return false}
    if (obj.Message.value!='') {return true}
    else {window.alert('You have not written a message!'); return false}
  }
  if (f=='NewPost') {
    if (obj.category.selectedIndex==0) {r=false; w=e}
    document.getElementById('FFMP_Errcategory').innerHTML=w
    w='&nbsp;'
    if (obj.forename.value=='') {r=false; w=e}
    document.getElementById('FFMP_Errforename').innerHTML=w
    w='&nbsp;'
    if (obj.surname.value=='') {r=false; w=e}
    document.getElementById('FFMP_Errsurname').innerHTML=w
    w='&nbsp;'
    if ((obj.generalarea.value=='')||(obj.generalarea.value=='i.e. London, UK')) {r=false; w=e}
    document.getElementById('FFMP_Errgeneralarea').innerHTML=w
    if (r==false) {window.alert('Please complete the fields marked \''+e+'\''); return false}
    if (obj.message.value!='') {return true}
    else {window.alert('You have not written a message!'); return false}
  }
  if (f=='Log In') {
    if ((obj.UserEmail.value!='')&&(obj.UserPassword.value!='')) {return true}
    else {window.alert('Please enter your E-Mail Address and Password'); FFMP_SetFocus(); return false}
  }
  if (f=='Reset') {
    if (obj.UserEmail.value!='') {return true}
    else {window.alert('Please enter your E-Mail Address'); FFMP_SetFocus(); return false}
  }
  if (f=='Upload') {
    if (obj.picture.value!='') {return true}
    else {window.alert('Please browse for a file to upload'); FFMP_SetFocus(); return false}
  }
  if (f=='Register') {
    if (obj.UserFullName.value=='') {r=false; w=e}
    document.getElementById('FFMP_ErrUserFullName').innerHTML=w
    w='&nbsp;'
    if (obj.UserEmail.value=='') {r=false; w=e}
    document.getElementById('FFMP_ErrUserEmail').innerHTML=w
    return r
  }
  if (f=='ChangePassword') {
    if (obj.UserPassword.value=='') {window.alert('Please enter your current password!');obj.UserPassword.focus();return false}
    if (obj.NewPassword.value=='') {window.alert('Please enter a new password!');obj.NewPassword.focus();return false}
    if (obj.NewPassword.value!=obj.ConfirmPassword.value) {window.alert('The new and confirm paswords do not match!');obj.NewPassword.focus();return false}
    return true
  }
}

function FFMP_SetFocus() {
  if (document.getElementById('FFMP_default')!=null) {
    var obj=document.getElementById('FFMP_default')
    if (obj.FullName!=null) {obj.FullName.focus();return}
    if (obj.UserFullName!=null) {obj.UserFullName.focus();return}
    if (obj.UserEmail!=null) {
      if (obj.UserEmail.value=='') {obj.UserEmail.focus()}
      else if (obj.UserPassword!=null) {obj.UserPassword.focus()}
    return
    }
    if (obj.UserPassword!=null) {obj.UserPassword.focus();return}
    if (obj.picture!=null) {obj.picture.focus();return}
  }
}

function FFMP_UserPost(p,w) {
  if (w!='') {
    if (window.confirm(w)==false) {return}
  }
  document.location=p
}

function FFMP_ClearText(c, t, s) {
  if ((!s)&&(c.value==t)) {c.value=''}
  if ((s)&&(c.value=='')) {c.value=t}
  if ((s)&&(t=='i.e. London, UK')) {FFMP_FindLocation(c.value)}
}

function FFMP_onload() {
  if (arguments.callee.done) {return}
  arguments.callee.done = true
  if (self.parent.frames.length != 0) {self.parent.location=document.location}
  var mbh=document.getElementById('FFMP_mainbody').offsetHeight
  if (mbh>476) {
    mbh=mbh-476+340
    document.getElementById('FFMP_leftmsg').style.height=mbh+'px'
    document.getElementById('FFMP_rightmsg').style.height=mbh+'px'
  }
  FFMP_SetFocus()
  FFMP_leftmsg=new FFMP_ScrollWindow('FFMP_leftmsg',2,'v')
  FFMP_rightmsg=new FFMP_ScrollWindow('FFMP_rightmsg',0,'v')
  FFMP_picturerow=new FFMP_ScrollWindow('FFMP_picturerow',1,'h')
  FFMP_ShowGeoMap()
  try {return FFMPUSER_onload()}
  catch(err) {return}
}

function FFMP_onunload() {
  if (arguments.callee.done) {return}
  arguments.callee.done = true
  GUnload()
  try {return FFMPUSER_onunload()}
  catch(err) {return}
}

if (window.attachEvent) {
  window.attachEvent("onload", FFMP_onload)
  window.attachEvent("onunload", FFMP_onunload)
}
else if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", FFMP_onload, false)
  document.addEventListener("unload", FFMP_onunload, false)
}
window.onload = FFMP_onload
onunload = FFMP_onunload
