var nextLink;
var overlay;
var email;
var blockClose = false;
$(function() {
  // Setup a basic iframe for use inside overlays.
  var theframe = $('<iframe frameborder="0" scrolling="no" id="overlayFrame"></iframe>');
  var blockClose = false;
  
  // Overlay
  overlay = $('a[rel]').overlay({


    closeOnClick: false,
    fixed: false,
    api:true,

    mask: {
      color: '#3A1F01',  //'#b98434',
      loadSpeed: 200,
      opacity: 0.6
    },

    onBeforeClose: function() {
      if( blockClose ) {
      alert("signin to continue");
      return false;
      }
      return true;
    },


    onBeforeLoad: function() {
      var rel = this.getTrigger().attr("rel");
      nextLink = window.location.href;
      //First get the wrap
      var wrap;
      //Get the URL from the trigger
      var link = this.getTrigger().attr("href");
      
      //Add the link and style attributes to the basic iframe
      blockClose = false;

      switch( rel ) {
          case "#search_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#search_overlay_wrap");
            wrap.html(theframe);
          break;
          
          
          case "#editor_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:736px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#editor_overlay_wrap");
            wrap.html(theframe);
          break;
          
          case "#privacy_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:736px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#privacy_overlay_wrap");
            wrap.html(theframe);
          break;

          case "#terms_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:736px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#terms_overlay_wrap");
            wrap.html(theframe);
          break;


      
          case "#signin_overlay":
          
            var isBlockClose = $.url(link).param('blockClose');
            if( isBlockClose =="yes") {
              blockClose = true;
            }
          
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#signin_overlay_wrap");
            wrap.html(theframe);
          break;
          
          case "#join_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#join_overlay_wrap");
            wrap.html(theframe);
          break;
          
          
 
          
          case "#change_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#change_overlay_wrap");
            wrap.html(theframe);
          break;
          



          case "#forgot_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:600px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#forgot_overlay_wrap");
            wrap.html(theframe);
          break;
            
          case "#search_overlay1":
            wrap = this.getOverlay().find("#search_overlay_wrap");
            wrap = wrap.load(link);
            break;


          case "#contact_overlay":
            $(theframe).attr({
              src: link,
              scrolling: 'auto',
              style: 'height:732px; width:600px; border: 0; overflow-x:auto;'
            });
            wrap = this.getOverlay().find("#contact_overlay_wrap");
            wrap.html(theframe);
          break;



        default:
          alert(rel);
      }
      //Write the iframe into the wrap
    }
  });
  
  $('a[rev]').click( function() {
      var rev = $(this).attr("rev");
      var nextLink = window.location.href;
      var link = $(this).attr("href");
      
      switch( rev ) {
        case "#signout_overlay":
            $.post(link, function(data) {
              window.location.replace(  window.location.href);
            });
            return false;
          break;
                  
          
        default:
          alert(rev);
          break;
        }
});
  
  
});


$(function() {
  $("#SEA_NAME").val("");
  
  $(".email_go2").click( function() {
    emailGo();
  });
    $(".email_go1").change( function() {
    emailGo();
  });
  
  

  
  $(".password_go2").click( function() {
    passwordGo();
  });
    $(".password_go1").change( function() {
    passwordGo();
  });
  
 
  
  
  $('#page_not_my_email').click( function(){
       $("#page_signin_password").hide();
       $("#page_signout").hide();
       $("#page_signin_email").show();
  });
  
  $('#signout_go').click( function(){
   $.post("signout.php",{},
   function(data) {
     window.location.replace(  window.location.href );
   });
  });
  
  
  
    $("#SEA_NAME").change( function() {
    var name = $(this).val();
    $.post("search_save.php", { SEA_NAME: name },
   function(data) {
     if( data ) {
       $("#SEA_NAME").val(data);
       $("#save_confirmation").html("Search Saved");
     } else {
       $("#SEA_NAME").val("");
     }
   });
  });
  
  $('.force_signin').click( function() {
 
              window.location.replace(link);

  })
  
  
  
});

function goToReload() {
  parent.window.location.replace(  nextLink );
}



  function emailGo() {
         var email = $("#SIGNIN_EMAIL").val();
     if( email == "" ) {
       return "";
     }
    $.post("signin_login.php", { SIGNIN_EMAIL: email },
   function(data) {
     if( data ) {
       $("#page_signin_password").show();
       $("#page_signin_email").hide();
       $("#page_signout").hide();
       $("#SIGNIN_PASSWORD").focus();
     } else {
       $("#signin_join").click();
     }
   });
  }
  
   function passwordGo() {
     var password = $("#SIGNIN_PASSWORD").val();
     if( password == "" ) {
       return;
     }
     var email = $("#SIGNIN_EMAIL").val();
    $.post("signin_password.php", { SIGNIN_EMAIL: email, SIGNIN_PASSWORD: password },
   function(data) {
     if( data ) {
       window.location.replace(  window.location.href );
       MEMBER_EMAIL = email;
       $("#page_signout_email").html(email);
     } else {
       alert("Password doesn't match email address");
     }
   });
  }
  
