if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

$(document).ready( function () {
	
 jQuery.prompt.setDefaults({
  buttons:{Ok:{ title: "OK", img: webDirectory + "img/ok.png", value: true }}
 });

 setNbrFavorites(0);

 $("div.menuentry")
  .mouseover(function() {$(this).find("a").addClass("menuhover")})
  .mouseout(function() {$(this).find("a").removeClass("menuhover")});
 
 if( VisitorMsg.length > 0 ) {
  $.prompt( VisitorMsg );
 }
 
});



function signIn( page )
{
 $.prompt(
  '<table border="0" width="380"><tr><td colspan="2">' +
  '<h2>Connect to your account</h2>' +
  '<span class="sign_form">Please enter your email address and password.</span>' +
  '</td></tr><tr><td width="140" valign="middle">' +
  'Email address</td><td width="240" valign="top"><input type="text" name="si_email" id="si_email" class="sign_form" />' +
  '</td></tr><tr><td valign="middle">' +
  'Password</td><td valign="top"><input type="password" name="si_password" id="si_password" class="sign_form" />' +
  '</td></tr></table>' +
  '<br /><span class="sign_form">Not signed up yet? <a href="' + webDirectory + 'SignUp">Click here</a><br>Forgot your password? <a href="" onclick="forgotPassword(); return false;">Click here</a></span>' +
  '<script type="text/javascript">$("input.sign_form").focus(function() { $(this).addClass("sign_form_focus"); }).blur(function() { $(this).removeClass("sign_form_focus") });</script>',
  {
   overlayspeed: 200,
   buttons:{
    Cancel:{ title: "Cancel", img: webDirectory + "img/cancel.png", value: false },
  	Ok:{ title: "OK", img: webDirectory + "img/sign_in.png", value: true }},
   focus: 1,
   callback: function (v,m,f) { if(v) {signInProceed( f.si_email, f.si_password, page );}}
  });
}



function signInProceed( email, password, page )
{
 $.post(
  webDirectory + "front/SignInProceed",
  {email: email,
   password: password
  },
  function(data) {
   if( data == "OK" ) {
    if( page ) {
     $("form#front").attr( "action", $("form#front").attr( "action" ) + '/' + page );
    }
    $("form#front").submit();
   } else {
    $.prompt(
	 "Your email and password seem incorrect.<br />Please try again.",
	 {
      overlayspeed: 200,
      buttons:{
       Cancel: { title: "Cancel", img: webDirectory + "img/cancel.png", value: 0 },
  	   Forgot: { title: "Forgot Password?", img: webDirectory + "img/forgotpassword.png", value: 1 },
  	   TryA: { title: "Try Again", img: webDirectory + "img/tryagain.png", value: 2 }},
      focus: 2,
      callback: function (v,m,f) {
	   if(v==1) {forgotPasswordShow();}
	   if(v==2) {signIn( page );} }
     });
   }
  }
 );
}


function forgotPassword()
{
 $.prompt.close();
 setTimeout("forgotPasswordShow()", 300);
}


function forgotPasswordShow()
{
 $.prompt(
  '<table border="0" width="380"><tr><td colspan="2">' +
  '<h2>Generate a new password</h2>' +
  '<span class="sign_form">Please enter your email address and click the Generate button, we will then send you an automatically generated password.</span>' +
  '</td></tr><tr><td width="140" valign="middle">' +
  'Email address</td><td width="240" valign="top"><input type="text" name="si_email" id="si_email" class="sign_form" />' +
  '</td></tr></table>',
  {
   overlayspeed: 200,
   buttons:{
    Cancel: { title: "Cancel", img: webDirectory + "img/cancel.png", value: false },
  	Ok: { title: "Generate", img: webDirectory + "img/generate.png", value: true }},
   focus: 1,
   callback: function (v,m,f) { if(v) {forgotPasswordProceed( f.si_email );}}
  });
}


function forgotPasswordProceed( email )
{
 $.post(
  webDirectory + "front/ForgotPasswordProceed",
  { email: email },
  function(data) {
   if( data == "OK" ) {
    $("form#front").submit();
   } else {
    $.prompt(
	 "Your email cannot be found in our database<br />Please try again.",
	 {
      overlayspeed: 200,
      buttons:{
       Cancel: { title: "Cancel", value: false },
  	   Ok: { title: "Try Again", value: true }},
      focus: 1,
      callback: function (v,m,f) { if(v) {forgotPasswordShow();} }
     });
   }
  }
 );
}


function AdDetails( transac, id, destination, adTitle )
{
 if( transac == 0 && id == 0 ) {
  var action = $("form#front").attr("action") + (destination ? '/' + destination : '');
 } else {
  var action = webDirectory + (transac == 1 ? 'ForSale' : 'ForRent') + '/' + adTitle + '/' + id + (destination ? '/' + destination : '');
 }
 
 $("form#front").attr("action", action );
 $("form#front").submit();
}


function connectFavorite( page )
{
 $.prompt(
  '<h2>Add to my Favorites</h2>' +
  'To add this property to your favorites, please connect or create your account:',
  {
   overlayspeed: 200,
   buttons:{
    Cancel: { title: "Cancel", img: webDirectory + "img/cancel.png", value: 0 },
  	SignUp: { title: "Register", img: webDirectory + "img/register.png", value: 1 },
  	SignIn: { title: "Log-in", img: webDirectory + "img/login.png", value: 2 }},
   focus: 2,
   callback: function (v,m,f) {
	if(v==1) {window.location = webDirectory + 'SignUp';}
	if(v==2) {signIn( page );}}
  });
}


function setFavorites( mode, id )
{
 $.get(
  webDirectory + "front/Favorites/" + mode + "/" + id,
  function(data) {
   if( data == "OK" ) {
    setNbrFavorites( mode == 'add' ? 1 : -1 );
    $("#addfav_" + id).slideToggle("slow", "swing");
    $("#remfav_" + id).slideToggle("slow", "swing");
   }
  }
 );
}


function setNbrFavorites( add )
{
 nbrFavorites += add;
 
 $("span#nbrFavorites").html( nbrFavorites ? "(" + nbrFavorites + ")" : '');
}






