jQuery(document).ready(function($) {
  $("#send-to-friend a").click(function() {
		var position = $("#send-to-friend a").position();
		$('#wg_tellafriend_box').css('top', (position.top - 390) + 'px').css('left', position.left + 'px');

    $("#wg_tellafriend_form textarea[name=message]").val("Get Top Deals at Wego: " + document.location);

    //Show Form
    $("#wg_tellafriend_box").fadeIn("slow", function() {
      $("#wg_tellafriend_referrer_name").focus();
    });
    // Iframe shim - load bgiframe plugin if it can't be found.
    if (!$.fn.bgiframe) {
      $.getScript('http://media.wego.com/js/jquery/plugins/jquery.bgiframe.min.js', function() {
				if(!$.browser.msie) { $("#wg_tellafriend_box").animate({ opacity: 1.0 }, 1000); }// Put in a delay for Safari 2 and older. 
        $("#wg_tellafriend_box").bgiframe();
      });
    } else {
      $("#wg_tellafriend_box").bgiframe();
    }
		
		return false;
  });

  $("#wg_tellafriend_header .close").click(function() {
    $("#wg_tellafriend_box").fadeOut("slow", function() {

      //Reset Input Area
      $("#wg_tellafriend_box").css("background-color", "#e8e8e8");
      $("#wg_tellafriend_content").remove();

      $("#wg_tellafriend_form").show();
      //Reset Input Area
      $("#wg_tellafriend_form label span[class!=note]").remove();
      $("#wg_tellafriend_form input[name!=commit]").removeClass("error").val("");
      $("#wg_tellafriend_form textarea")           .removeClass("error").val("");
			
    });

		return false;
  });

  $("#wg_tellafriend_form").submit(function () {
    $("#wg_tellafriend_box").append("<div id=\"wg_tellafriend_load\"><img src=\"http://www.wego.com/tellafriend/images/loading.gif\"/></div>").show();
    $("#wg_tellafriend_form").hide();

    var query_str = $("#wg_tellafriend_form").serialize();
    $.post( "/tellafriend/starttelling.php",
            query_str,
            function(data) {

              //Reset Input Area
              $("#wg_tellafriend_form label span[class!=note]").remove();
              $("#wg_tellafriend_form input[name!=commit]").removeClass("error");
              $("#wg_tellafriend_form textarea")           .removeClass("error");

              $("#wg_tellafriend_load").remove();
              if(data.match("Invalid Name")) {
                $("#wg_tellafriend_form input[name=referrer_name]") .addClass("error").before("<span class='name_error'>Invalid Name</span>");
                $("#wg_tellafriend_form").show();
              } else
              if(data.match("Invalid Email")) {
                $("#wg_tellafriend_form input[name=referrer_email]").addClass("error").before("<span class='email_error'>Invalid Email</span>");
                $("#wg_tellafriend_form").show();
              } else
              if(data.match("Please enter your friends' emails")) {
                $("#wg_tellafriend_form textarea[name=emails]")     .addClass("error").before("<span class='emails_error'>Please enter your friends' emails</span>");
                $("#wg_tellafriend_form").show();
              } else
              if(data.match("Limit of 10 emails exceeded")) {
                $("#wg_tellafriend_form textarea[name=emails]")     .addClass("error").before("<span class='emails_limit_error'>Limit of 10 emails exceeded</span>");
                $("#wg_tellafriend_form").show();
              } else
              if(data.match("Please enter a message for your friends")) {
                $("#wg_tellafriend_form textarea[name=message]")    .addClass("error").before("<span class='message_error'>Please enter a message</span>");
                $("#wg_tellafriend_form").show();
              } else {
                $("#wg_tellafriend_box").append("<div id='wg_tellafriend_content'>" + data + "</div>").css("background-color", "#deeeff").show();
              }
            });
    return false;
  });

});