var SURVEY_SHOW_COOKIE = "sgm_survey_show";

$(document).ready(function () {
	if ($.cookie(SURVEY_SHOW_COOKIE) == null) {
		$('#survey-tickler').show("slow");
	}
});

$(function() {
	var contactBoxy = null;
	$('.take_survey').click(function(){
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", "/boxy.css");
		document.getElementsByTagName("head")[0].appendChild(fileref);
			var tsTimeStamp = new Date().getTime();
			$.get("/survey-form.php", {action: "get", time:tsTimeStamp}, function(data){
				contactBoxy = new Boxy("<div style=\"width: 650px; height: 600px; overflow: auto;\">"+data+"</div>", {
					title: "Customer Survey",
					draggable: false,
					modal: true,
					fixed: false,
					behaviours: function(c) {
						c.find('#survey-form').submit(function() {
							var formData = c.find("#survey-form").serialize();
							Boxy.get(this).setContent("<div style=\"width: 300px; height: 150px\">Sending...</div>").center();
							// submit form by ajax using post and send 3 values: subject, your_email, comment
							$.post("/Scripts/process-survey-form.php", formData,
							function(data){
								/*set boxy content to data from ajax call back*/
								contactBoxy.setContent("<div style=\"width: 300px; height: 150px\">"+data+"</div>").center();
								/* if error, php script will return the following error code in its textual message */
								/* Not a great way to do it, but it will work for this one task. */
								if (data.indexOf("0x00000001") == -1) {
									hide_survey_tickler();
								}
							});
							return false;
						});
						c.find('.survey-content table.multiple-choice tbody tr').mouseover(function(){
								$(this).addClass('survey-table-row-highlight');
							}).mouseout(function(){
								$(this).removeClass('survey-table-row-highlight');
							});
					}
				}).center();
			});
			return false;
		});
});

function hide_survey_tickler() {
	$('#survey-tickler').hide("slow");
	$.cookie(SURVEY_SHOW_COOKIE, "false", { expires: 365 });
}

$(function() {
	$('.no_thanks_survey').click(function() {
		hide_survey_tickler();
		return false;
	});
});

