var lastsearched = "";

function makeList() {
	var bizids = "";
	$("#bizadded tr").each(function(data) {
		if ( bizids.length > 0 ) bizids += ",";
		bizids += $(this).attr("bizid");
	});
	$.post(
		"/axj/",
		{
			action: "makeList",
			bizids: bizids
		},
		function(data) {
			if ( data.status == "NOTHUMAN" ) {
				doCheckHuman("makeList");
			} else if ( data.status == "OK" ) {
				setCookie("gh_softbaked",data.cookie,1000);
				location.href = data.page;
			}
		},
		"json"
	);
}

function addToList() {
	var bizids = "";
	$("#bizadded tr").each(function(data) {
		if ( bizids.length > 0 ) bizids += ",";
		bizids += $(this).attr("bizid");
	});
	$.post(
		"/axj/",
		{
			action: "makeList",
			bizids: bizids
		},
		function(data) {
			if ( data.status == "NOTHUMAN" ) {
				doCheckHuman("makeList");
			} else if ( data.status == "OK" ) {
				setCookie("gh_softbaked",data.cookie,1000);
				location.href = data.page;
			}
		},
		"json"
	);
}


function redrawAdded() {
	var num = $("#bizadded tr").length;
	if ( num > 0 ) {
		$("#nobizadded").hide();
		$("#listdone").show();
	} else {
		$("#nobizadded").show();
		$("#listdone").hide();
	}
}

function removebiz(id,name) {
	$("#added" + id).remove();
	redrawAdded();	
}

function makeBizRow(id,name) {
	var str = "";

	str += "<tr id=\"added" + id + "\" class=\"bizadded\" bizid=\"" + id + "\">";
	str += "<td nowrap>" + name + "</td>";
	str += "<td class=\"remove\"><a href=\"javascript:removebiz('" + id + "','" + name + "');\" nowrap><img src=\"/images/close.png\" /></a></td>";
	str += "</tr>";

	return str;
}

function choosebiz(id,name) {
	var html = makeBizRow(id,name);
	$("#bizadded").append(html);
	redrawAdded();
	hideOne('bizresults');
	hideOne('clearfader');
	$("#toadd").val("");
	$("#toadd").focus();
}

function makeRow(item, cnt) {
	var str = "";

	str += "<tr id=\"biz" + item.bizid + "\" class=\"biz\" onmouseover=\"javascript:doHover('" + item.bizid + "');\" onmouseout=\"javascript:undoHover('" + item.bizid + "');\">";
	str += "<td onclick=\"javascript:choosebiz('" + item.bizid + "','" + item.name + "');\" nowrap>" + item.name + "</td>";
	str += "<td align=\"right\" onclick=\"javascript:choosebiz('" + item.bizid + "','" + item.name + "');\" nowrap>Add</td>";
	str += "</tr>";

	return str;
}

function doHover(id) {
	$("#biz" + id).addClass("clickableHover");
}

function undoHover(id) {
	$("#biz" + id).removeClass("clickableHover");
}

function showClearFader() {
	$("#clearfader").css("height", $(document).height()).css("width", $(document).width())
	$("#clearfader").show();
}

function ghPromptFb(id, anchor, orient) {
  var anchor = anchor || "window";
  var orient = orient || "middle";
	$("#gpop").load("/axj/mineWithFacebook.html?id=" + id, function() {
		startPop(anchor,orient);
	});
}

function ghPromptFbShare(id, justSucceeded, anchor, orient) {
  var anchor = anchor || "window";
  var orient = orient || "middle";
  var justSucceeded = justSucceeded || "";
	$("#gpop").load("/axj/mineShareFacebook.html?id=" + id + "&just=" + justSucceeded, function() {
		startPop(anchor,orient);
	});
}

function ghPromptName(id, justSucceeded, anchor, orient) {
  var anchor = anchor || "window";
  var orient = orient || "middle";
  var justSucceeded = justSucceeded || "";
	$("#gpop").load("/axj/mineName.html?id=" + id + "&just=" + justSucceeded, function() {
		startPop(anchor,orient);
	});
}

function ghFbVerify(id) {
  FB.login(function(response) {
    if (response.authResponse) {
      // logged in
      $.post( "/axj/", {action: "saveFbMine", id: id, userId: response.authResponse.userID, userAt: response.authResponse.accessToken}, function() {
        location.href = location.href;
      }, "json");
    } else {
      location.href = location.href;
    }
  });
}

function ghFbShareMine(id, list, link, suppressName) {
  ghFbShare(
    null,
    function(response) { 
      if (response && response.post_id) {
        // shared
        $.post( "/axj/", {action: "saveFbShare", id: id, postId: response.post_id}, function() {
          location.href = location.href;
        }, "json");
      } else { 
        location.href = location.href;
      }
    },
    'My Customer Service Tricks | GetHuman.com',
    'I made a customer service cheat sheet on GetHuman.com',
    'Customer service phone numbers, chat info, tricks & more',
    'The fastest phone numbers, call-back services, live chat options, reviews and more for ' + list + ' (the companies I use) - to keep me from ever having to wait on hold.',
    link, 
    [{ 'name': 'Make Your Own List', 'link': 'http://gethuman.com/mine' }]
  );
}

function ghFbLogin() {
  FB.login(function(response) {
    if (response.authResponse) {
      location.href = location.href;
    }
  });
}


