jQuery(document).ready(function() {

  // AJAX REVIEW VOTES ---------------------------------------------------------
  $("a.feedback_checkbox:not(.disabled)").click(function(event) {

    // remove default behavior
    event.preventDefault();

    // initialize arguments to pass to server
    var hash_key = $(this).parents(".feedback_option").parents(".feedback_box").attr("id");
    var action;

    // determine which action to take based on checkbox and checkbox state
    if (!$(this).hasClass("selected")) {

      // check which voting option
      if ($(this).hasClass("insightful")) {
        action = "Vote Insightful";
      } else if ($(this).hasClass("witty")) {
        action = "Vote Witty";
      } else if ($(this).hasClass("spot_on")) {
        action = "Vote Spot On";
      }        
      // change current checkbox to checked graphic
      $(this).addClass("selected");

    } else {  
    
      // check which voting option
      if ($(this).hasClass("insightful")) {
        action = "Unvote Insightful";
      } else if ($(this).hasClass("witty")) {
        action = "Unvote Witty";
      } else if ($(this).hasClass("spot_on")) {
        action = "Unvote Spot On";
      }        
      // change current checkbox to unchecked graphic
      $(this).removeClass("selected");
    }
    
    // ajax call to server to update review votes
    $.post("/ajax_vote_review", { id: hash_key, action: action }, function(xml) {
      // convert xml to json (for IE)
      var json = $.xml2json(xml);
      var hash_key = json.hash_key;
      var num_insightful_votes = json.num_insightful_votes;
      var num_witty_votes = json.num_witty_votes;
      var num_spot_on_votes = json.num_spot_on_votes;

      $("#"+hash_key).children(".feedback_option").children(".feedback_label").children(".insightful").text("("+num_insightful_votes+")");
      $("#"+hash_key).children(".feedback_option").children(".feedback_label").children(".witty").text("("+num_witty_votes+")");
      $("#"+hash_key).children(".feedback_option").children(".feedback_label").children(".spot_on").text("("+num_spot_on_votes+")");
    })
  })

  // AJAX COMPLIMENT WINDOW ----------------------------------------------------
  
  // create compliment popup window
	$("#compliment_window").draggable();
	
  // open compliment window
	$(".compliment_link").click(function(event) {

    // remove default behavior
    event.preventDefault();

    // only show compliment window if not disabled
    if (!$(this).hasClass("disabled")) {
      // position window relative to mouse click
      $("#compliment_window").css("top", event.pageY-350).css("left", event.pageX-280);
  
      // prepopulate information
      $("#compliment_window").removeClass("cat").removeClass("monkey").removeClass("dog").addClass("penguin");
      $("input[name='compliment_type']:first").attr("checked","checked");
      $("#compliment_comments").children("textarea").attr("value", "Thanks for your review!");
      $("#compliment_error").text("");
  	  $("#send_compliment").removeAttr("disabled");
  
      var url = $(this).attr("href");
      var query_string = url.split("?")[1];
      var name = query_string.split("&")[0].split("=")[1];
      var hash_key = query_string.split("&")[1].split("=")[1];   
      $("#compliment_recipient_name").text(name);
      $("#compliment_review_id").val(hash_key);
  
      // show window
  		$("#compliment_window").fadeIn("fast");
      event.preventDefault();
    }
	});

  // change image when clicking on radio button
	$("input[name='compliment_type']").click(function(event) {
    var graphic_name = $("input[name='compliment_type']:checked").attr("class");
    $("#compliment_window").removeClass("dog").removeClass("cat").removeClass("monkey").removeClass("dog").addClass(graphic_name);
  });	
	
  // change image when clicking on compliment name
	$(".compliment_name").click(function(event) {
    $(this).siblings("input").attr("checked","checked");
    var graphic_name = $(this).siblings("input").attr("class");
    $("#compliment_window").removeClass("dog").removeClass("cat").removeClass("monkey").removeClass("dog").addClass(graphic_name);
    event.preventDefault();
  });	
	
  // submit AJAX form when user clicks send
	$("#send_compliment").click(function() {
    // disable button immediately
	  $(this).attr("disabled","disabled");
	
    // initialize arguments to pass to server
    var hash_key = $("#compliment_review_id").val();
    var action = "Compliment";
    var compliment_type = $("input[name='compliment_type']:checked").val();
    var comments = $("#compliment_comments").children("textarea").attr("value");

    // ajax call to server to update review ratings
    $.post("/ajax_compliment_review", { id: hash_key, action: action, compliment_type: compliment_type, comments: comments}, function(xml) {
      // convert xml to json (for IE)
      var json = $.xml2json(xml);
      var error = json.error_message;
      var hash_key = json.hash_key;

      // show error message
      if(error.length > 0) {
        $("#compliment_error").css("color","#ff0000").text(error).fadeIn("fast");
        $("#send_compliment").removeAttr("disabled");
      // show success message, fade window, disable link
      } else {
        $("#compliment_error").css("color","green").css("font-weight","bold").text("Your compliment has been sent!").fadeIn("fast");
      	$("#compliment_window").animate({opacity: 1.0}, 1500).fadeOut("slow");
        $("#compliment_"+hash_key).addClass("disabled");
      }
    })
  });
	
	// hide window when user clicks cancel
	$("#cancel_compliment").click(function(event) {
		$("#compliment_window").fadeOut("slow");
    event.preventDefault();
	});
	
	
  // AJAX SHARE REVIEW WINDOW --------------------------------------------------

  // create share popup window
	$("#share_window").draggable();
	
  // open share popup window
	$(".share_review_button,.share_review_link,.share_site_profile_button,.email_share_review_link").click(function(event) {

    // position window relative to mouse click, depending on clicked element
    if ($(this).hasClass(".share_review_button")) {
      $("#share_window").css("top", event.pageY-100).css("left", event.pageX);
    } else if ($(this).hasClass(".share_review_link")) {
      $("#share_window").css("top", event.pageY-340).css("left", event.pageX);
    } else if ($(this).hasClass(".share_site_profile_button")) {
      $("#share_window").css("top", event.pageY-200).css("left", event.pageX);
    } else if ($(this).hasClass(".email_share_review_link")) {
      $("#share_window").css("top", event.pageY-200).css("left", event.pageX);      
    }

    // prepopulate information
    $("#share_window_emails").children("textarea").attr("value","");
    $("#share_window_comments").children("textarea").attr("value","");
    $("#share_window_error").text("");
	  $("#send_share_button").removeAttr("disabled");

    // if sharing review
    if ($(this).hasClass(".share_review_button") || $(this).hasClass(".share_review_link") || $(this).hasClass(".email_share_review_link")) {
  
      var url = $(this).attr("href");
      var query_string = url.split("?")[1];
      var url_address = query_string.split("&")[0].split("=")[1];
      var hash_key = query_string.split("&")[1].split("=")[1];   
      if ($(this).hasClass(".email_share_review_link")) {
        $("#share_window_title").text("Share your review of "+url_address);
      } else {
        $("#share_window_title").text("Share this review of "+url_address);      
      }
      $("#share_id").val(hash_key);
      $("#share_action").val("Share Review");

      // hide facebook / twitter options for sharing individual reviews
      $("#share_window_options").hide();

    // if sharing site profile page
    } else if ($(this).hasClass(".share_site_profile_button")) {
      var url = $(this).attr("href");
      var query_string = url.split("?")[1];
      var url_address = query_string.split("&")[0].split("=")[1];
      var url_id = query_string.split("&")[1].split("=")[1];   
      var facebook_share_link = query_string.split("&")[2].split("=")[1];
      var twitter_share_link = query_string.split("&")[3].split("=")[1];
      $("#share_window_title").text("Share reviews of "+url_address);    
      $("#share_id").val(url_id);
      $("#share_action").val("Share Site Profile");
      $("#facebook_share_link").click(function(event) {
        window.open("http://www.facebook.com/sharer.php?u="+facebook_share_link,"share_window","toolbar=no,menubar=no,resizable=yes,scrollbars=no,height=400,width=600");
        event.preventDefault();
      });
      $("#twitter_share_link").click(function(event) {
        window.open("http://twitter.com/home?status=Check+out+these+reviews+of+"+url_address+"+on+SiteJabber:+"+twitter_share_link,"share_window","toolbar=no,menubar=no,resizable=yes,scrollbars=no,height=600,width=800");
        event.preventDefault();
      });

      // show facebook / twitter options for sharing site profile page
      $("#share_window_options").show();
    }

    // show window
		$("#share_window").fadeIn("fast");
    event.preventDefault();
	});

  // submit AJAX form when user clicks send
	$("#send_share_button").click(function() {

    var action = $("#share_action").val();

    // if sharing review
    if (action == "Share Review") {

      // disable button immediately
  	  $(this).attr("disabled","disabled");
  	
      // initialize arguments to pass to server
      var hash_key = $("#share_id").val();
      var sender_name = $("#share_window_sender_name").children("input").attr("value");
      var emails = $("#share_window_emails").children("textarea").attr("value");
      var comments = $("#share_window_comments").children("textarea").attr("value");
  
      // ajax call to server to send out share email
      $.post("/ajax_share_review", { id: hash_key, action: action, sender_name: sender_name, emails: emails, personal_message: comments}, function(xml) {
  
        // convert xml to json (for IE)
        var json = $.xml2json(xml);
        var error = json.error_message;
  
        // show error message
        if (error.length > 0) {
          $("#share_window_error").css("color","#ff0000").text(error).fadeIn("fast");
          $("#send_share_button").removeAttr("disabled");
        // show success message, fade window, disable link
        } else {
          $("#share_window_error").css("color","green").css("font-weight","bold").text("Thanks for sharing this review!").fadeIn("fast");
        	$("#share_window").animate({opacity: 1.0}, 1500).fadeOut("slow");
        }
      })

    // if sharing site profile
    } else if (action == "Share Site Profile") {

      // disable button immediately
  	  $(this).attr("disabled","disabled");
  	
      // initialize arguments to pass to server
      var url_id = $("#share_id").val();
      var sender_name = $("#share_window_sender_name").children("input").attr("value");
      var emails = $("#share_window_emails").children("textarea").attr("value");
      var comments = $("#share_window_comments").children("textarea").attr("value");
  
      // ajax call to server to send out share email
      $.post("/ajax_share_site_profile", { id: url_id, action: action, sender_name: sender_name, emails: emails, personal_message: comments}, function(xml) {
  
        // convert xml to json (for IE)
        var json = $.xml2json(xml);
        var error = json.error_message;
  
        // show error message
        if (error.length > 0) {
          $("#share_window_error").css("color","#ff0000").text(error).fadeIn("fast");
          $("#send_share_button").removeAttr("disabled");
        // show success message, fade window, disable link
        } else {
          $("#share_window_error").css("color","green").css("font-weight","bold").text("Thanks for sharing this website!").fadeIn("fast");
        	$("#share_window").animate({opacity: 1.0}, 1500).fadeOut("slow");
        }
      })
    }
  });
	
	// hide window when user clicks cancel
	$("#cancel_share_link").click(function(event) {
		$("#share_window").fadeOut("slow");
    event.preventDefault();
	});
  
  
  // AJAX URL LINK WINDOW ------------------------------------------------------

  // open share popup window
	$(".url_share_review_link").click(function(event) {

    // position window relative to mouse click, depending on clicked element
    $("#url_link_window").css("top", event.pageY-50).css("left", event.pageX-100);

    // select all on focus
    $("#url_link_textfield").focus(function() {
      this.select();
    });

    // show window
		$("#url_link_window").fadeIn("fast");
    event.preventDefault();
	});

	// hide window when user clicks cancel
	$("#url_link_window_close").click(function(event) {
		$("#url_link_window").hide();
    event.preventDefault();
	});	
});  	