document.write('<script src="AC_OETags.js" language="javascript"></script>');

$(document).ready(function(){

  $("#email").each(function() {
    $(this).val("Your Email Address");
    $(this).addClass("untouched");
    $(this).focus(function() {
      if ($(this).hasClass("untouched")) {
        $(this).val("");
        $(this).removeClass("untouched");
      }
    });
  });
  $("#myusername").each(function() {
    $(this).attr('title', 'Username');
    $(this).prev().attr('title', 'Username');
    var username = getCookie('username');
    if (username) {
      $(this).val(username);
      $("#mypassword").focus();
    } else {
      $(this).focus();
    }
    //if (!$(this).val()) {
    //  $(this).val("Username");
    //  $(this).addClass("untouched");
    //}
  });
  $("#mypassword").each(function() {
    $(this).attr('title', 'Password');
    $(this).prev().attr('title', 'Username');
    //if (!$(this).val()) {
    //  $(this).val("Password");
    //  $(this).addClass("untouched");
    //} 
  });
  //var path = document.location.href.replace(/winequest/, "wineinsight").replace(/\/[^\/]*$/, "") + '/';
  var path = 'wineinsight/';
  $("#sendmail").submit(function() {
    var email = $('#email').val();
    if (email) {
      // Waiting indicator
      $('#loading').css({
        top: $(window).height()/2-10/2,
        left: $(window).width()/2-50/2
      });
      $('#loading').show();
      //Set heigth and width to mask to fill up the whole screen
      $('#mask').css({'width':$(window).width(),'height':$(document).height()});
      $('#mask').fadeTo("slow",0.4);  

      var url = path + 'usermailinstr.ajax.cgi';
      $.get(url, {'email': email}, function(data) {
        $('#mask').hide();
        $('#loading').hide();
        if (data) {
          alert(data.join('\n'));
        } else {
          alert('Email not found');
        }
      });
    }
    return false;
  });
  $("#login-form").submit(function() {
    var username = $("#myusername").val();
    var password = $("#mypassword").val();
    if (username) {
      if (password) {
        var dataout = {
          username: username,
          password: password,
          screenWidth: $(this).data('screenWidth'),
          screenHeight: $(this).data('screenHeight'),
          bodyWidth: $(this).data('bodyWidth'),
          bodyHeight: $(this).data('bodyHeight'),
          flashVersion: $(this).data('flashVersion')
        };
        var redirect = $.url.param('redirect');
        if (redirect) {
          dataout['redirect'] = redirect.value;
        }
        //if (username.match(/^(len@winequest.com|michael@winequest.com|jim@winequest.com|rod@winequest.com|dyquem1975@yahoo.com|mulanchan@klwines.com|tippernie@gmail.com|kirkwalker@klwines.com|mp23|steven.krueger@westin.com|mike.russo@ritzcarlton.com|michaela.nuebling@stregis.com|jason.ramshaw@ritzcarlton.com|adrian.lawlor@ritzcarlton.com|linda.torres@ritzcarlton.com|michaela.nuebling@stregis.com|CDuarte@rccl.com|justin.jelinek@ritzcarlton.com|nicole.ortega@ritzcarlton.com|sacramento@ruthschris.com|npfabio@hotmail.com)$/)) {
        //  path = 'wineinsight.5.0/';
        //}
        var url = path + 'login.ajax.cgi';
        var params = document.location.href.replace(/^.+\??/, "");
        if (params) url += '?' + params;
        $.get(url, dataout, function(data) {
          if (data.cookie) {
            for (var i in data.cookie) {
              document.cookie = data.cookie[i];
            }
          }
          if (data.bad_logon_message) {
            showSlickbox(data.bad_logon_message, data.help);
          } else if (data.url) {
            var continueurl = $.url.param("bad_logon_message");
            if (continueurl) {
            }
            document.location.href = path + data.url;
          } else {
            alert('bad data:'+data);
          }
        });
      } else {
        $("#mypassword").focus();
      }
    } else {
      $("#myusername").focus();
    }
    return false;
  });
  $("#top100-form").submit(function() {
    var name = $("#top100-form input[name='name']").val();
    var company = $("#top100-form input[name='company']").val();
    var email = $("#top100-form input[name='email']").val();
    var dataout = {
      event: 'Top100 Form',
      body: [name,company,email].join('\n')
    };
    if (name && company && email) {
      $.get('wineinsight/notify.ajax.cgi', dataout, function(data){
        document.location.href='top100.php';
      });
    } else {
      $("#popupbox-error").text('Please fill in all fields');
    }
    return false;
  });
  $('#share').hover(function() {
    $('#share_box').show();
  });
  $('#share_box').bind('mouseleave', function() {
    $('#share_box').hide();
  });

  var bad_logon_message_param = $.url.param("bad_logon_message") ? $.url.param("bad_logon_message").value : undefined; // From timeout
  showSlickbox(bad_logon_message_param);
  //select all the a tag with name equal to modal
  $('#top-wines').click(function(e) {
    //Cancel the link behavior
    e.preventDefault();
    
    //Get the A tag
    var id = $(this).attr('href');
  
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
  
    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});
    
    //transition effect   
    $('#mask').fadeIn(1000);  
    $('#mask').fadeTo("slow",0.8);  
  
    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
              
    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);
  
    //transition effect
    $(id).fadeIn(2000); 
  });
  
  //if close button is clicked
  $('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    
    $('#mask').hide();
    $('.window').hide();
  });   
  
  //if mask is clicked
  $('#mask').click(function () {
    $(this).hide();
    $('.window').hide();
  });
  setPlatformValues();
});

function showSlickbox(bad_logon_message, help) {
  if (bad_logon_message) {
    $('#bad_logon_message').text(bad_logon_message);
    if (!help) $('#login_help').hide(); else $('#login_help').show();
    $('#slickbox').show(400);
  }
}

function setPlatformValues() {
	var versionStr = GetSwfVer();
 var form = $('#login-form');
 form.data('screenWidth', screen.width);
 form.data('screenHeight', screen.height);
 form.data('bodyWidth', document.body.clientWidth);
 form.data('bodyHeight', document.body.clientHeight);
 form.data('flashVersion', versionStr);
}

function getCookie(c_name)
{
  if (document.cookie.length>0) {
    var c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      var c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

