
/* ************************************************************************** */
$(document).ready(function()
{
	// Check for querystring params and initiate search if they're present
	var keyword = qetQuerystringParam("txt_keyword");
	var category = qetQuerystringParam("slt_category_job");
	// The homepage form field for category has a different name
	if(!category){ category = qetQuerystringParam("stl_category"); }
	var posted_within = qetQuerystringParam("slt_category_time");
	if(!posted_within){ posted_within = "30"; $("#slt_category_time").val(posted_within); }
	var location = qetQuerystringParam("txt_location");
	
	if(keyword || category || location)
	{
		$("#txt_keyword").val(keyword);
		$("#slt_category_job").val(category);
		$("#slt_category_time").val(posted_within);
		$("#txt_location").val(location);
		trackEvent("/event/job-search-submit-from-homepage");
		search();
	}
	
	// Job search
	$("#divSearch form").submit(function(event)
	{
		event.preventDefault();
		trackEvent("/event/job-search-submit");
		
		var validates = true;
		$("#form-error").hide();
		$("#txt_keyword, #txt_location").css("border-color", "#333");
		if($("#txt_keyword").val() == "" || $("#txt_keyword").val() == "Keyword")
		{
			$("#txt_keyword").css("border-color", "#900");
			$("#form-error").show();
			validates = false;
			
		}
		if($("#txt_location").val() == "")
		{
			$("#txt_location").css("border-color", "#900");
			$("#form-error").show();
			validates = false;
		}
		if(validates){ search(); }
	});
	
	// Validate homepage form
	//$("#divSearchHome #frmSrch").validate();
	$("#frmSrch").submit(function(event)
	{
		$("#form-error").hide();
		$("#txt_keyword, #txt_location").css("border-color", "#333");
		if($("#txt_keyword").val() == "" || $("#txt_keyword").val() == "Keyword")
		{
			$("#txt_keyword").css("border-color", "#900");
			$("#form-error").show();
			event.preventDefault();
		}
		if($("#txt_location").val() == "")
		{
			$("#txt_location").css("border-color", "#900");
			$("#form-error").show();
			event.preventDefault();
		}
	});
	
	// Clear out the text "Keyword" from the form field when it's clicked
	$("#txt_keyword").click(function(event)
	{
		if($(this).val() == "Keyword"){ $(this).val(""); }
	});
	
	// Show 30 Day panel
	$("#a30").click(function(event)
	{
		event.preventDefault();
		trackEvent("/event/30-day-guarantee");
		thirtyDay();
	});
	
	// External links
	$("a[rel=external]").click(function(event)
	{
		event.preventDefault();
		trackEvent("/external/" + $(this).attr("href"));
		window.open($(this).attr("href"));
	});
	
	// Check for T's & C's
	// If the checkbox loads as checked, make sure it's initalized in the proper state (off)
	$("#chkTerms").attr("checked", false);
	$("#chkTerms").click(function(event)
	{
		trackEvent("/event/giveaway/check-terms");
		$("#divCheck, #divNoCheck").toggle();
	});
	
	// Click on the sweeps submit button to launch email client
	$("#divCheck #btnSubmit").click(function(event)
	{
		trackEvent("/event/giveaway/submit");
		trackFloodlight("stayo269");
	});
	
	// Focus keyword field 
	$("#divSearch #txt_keyword").focus();
	
	// Track AddThis events
	$("a.addthis_button#aShare").mouseover(function(event){ trackEvent("/event/share/addthis_button"); });
	$("a.addthis_button_delicious").click(function(event){ trackEvent("/event/share/addthis_button_delicious"); });
	$("a.addthis_button_twitter").click(function(event){ trackEvent("/event/share/addthis_button_twitter"); });
	$("a.addthis_button_facebook").click(function(event){ trackEvent("/event/share/addthis_button_facebook"); });
	$("a.addthis_button_digg").click(function(event){ trackEvent("/event/share/addaddthis_button_digg"); });
	$("a.addthis_button_email").click(function(event){ trackEvent("/event/share/addthis_button_email"); });
});
/* ************************************************************************** */


/* ************************************************************************** */
function search()
{	
	showSpinner();
	
	var f = {};
	f.url = "http://api.careerbuilder.com/v1/jobsearch";
	f.DeveloperKey = "WDhf6JN6NT5X7W3NHJPX";
	f.SiteID = "";
	f.CoBrand = "";
	f.PerPage = "12";
	f.Keywords = $("#txt_keyword").val().replace(" ", ",");
	f.Location = $("#txt_location").val().replace(" ", ",");	
	f.PostedWithin = $("#slt_category_time").val();
	f.Category = $("#slt_category_job").val();
	f.LR = "TPRO";
	f.CoBrand = "TPRO";
	f.SiteID = "TPRO01";
	
	$.ajax(
	{
		url: "/services/job-search.php",
		type: "GET",
		data: f,
		dataType: "xml",
		success: function(response, status)
		{
			// Empty previous results
			$("#divResults table tbody tr").remove();
			
			// Get the new results from the xml response
			var results = $(response).find("Results > JobSearchResult");
			
			// Loop through the results and create a string of html content
			var html = "";
			$.each(results, function(i, result)
			{
				var company = $(result).find("Company").text();
				if(!company){ company = "n/a"; }
				
				var distance = $(result).find("Distance").text();
				var distance_number = "";
				var distance_units = "";
				if(!distance){ distance_number = "n/a"; distance_units = ""; }
				else
				{
					distance_number = distance.split(" ")[0];
					distance_units = distance.split(" ")[1];
				}
				 
				html += "<tr>";
				html += "<td class=\"tdLeft\">";
				html += "<h3><a href=\"" + $(result).find("JobDetailsURL").text() + "&LR=TPRO" + "\"><span class=\"sortable-content\">" + $(result).find("JobTitle").text() + "</span></a></h3>";
				html += "<p>" + $(result).find("DescriptionTeaser").text() + "</p>";
				html += "</td>";
				html += "<td><span class=\"sortable-content\">" + company + "</td>";
				html += "<td>" + $(result).find("Location").text() + "<br />(<span class=\"sortable-content\">" + distance_number + "</span> " + distance_units + ")</td>";
				html += "<td><span class=\"sortable-content\">" + $(result).find("PostedDate").text() + "</span></td>";
				html += "</tr>";
				
			});
			
			if(html.length > 0)
			{
				$(html).appendTo("#divResults table tbody"); 
				
				// Stripe the table
				$("#divResults table tbody tr:odd").addClass("trOdd");
				
				// Make it sortable
				/*
				$("#divResults table").tablesorter(
				{ 
					// define a custom text extraction function 
					textExtraction: function(node)
					{ 
						// extract data from markup and return it  
						var sortable = $("span.sortable-content", $(node)).html();
						return sortable;
					}
				});
				*/
				
				trackEvent("/event/job-search-results");
			}
			else
			{
				html = "";
				html += "<tr>";
				html += "<td class=\"tdLeft\" colspan=\"4\">";
				html += "<p><strong>Sorry, no results were found based on your search request.</strong></p><p>Try using the form at the top of the page to start a new search.</p>";
				html += "</td>";
				html += "</tr>";
				$(html).appendTo("#divResults table tbody");
				trackEvent("/event/job-search-results-empty");
			}
			
			$("#divResults").show();
			$("#spinner").hide();
		},
		error: function(xhr, status, exception)
		{
			// Empty previous results
			$("#divResults table tbody tr").remove();
			
			html = "";
			html += "<tr>";
			html += "<td class=\"tdLeft\" colspan=\"4\">";
			html += "<p><strong>Sorry, no results were found based on your search request.</strong></p><p>Try using the form at the top of the page to start a new search.</p>";
			html += "</td>";
			html += "</tr>";
			$(html).appendTo("#divResults table tbody");
			trackEvent("/event/job-search-results-error");
			$("#spinner").hide(); 
		
		},
		complete: function(xhr, status){}
	});
}
/* ************************************************************************** */


/* ************************************************************************** */
function showSpinner()
{
	// Create the markup if it doesn't exist
	if($("#spinner").length <= 0)
	{
		$("<div id=\"spinner\"></div>").prependTo("#divSearch");
	}
	$("#spinner").show();
}
/* ************************************************************************** */


/* ************************************************************************** */
// Convenience function for Google Analytics page/event tracking
function trackEvent(url)
{
	if(pageTracker){ pageTracker._trackPageview(url); }
}
/* ************************************************************************** */


/* ************************************************************************** */
/* *********Shadow Box Popup******** */
function thirtyDay()
{

	Shadowbox.open(
	{
       content:    '<div id="div30"><p>If during the first 30 days following your purchase you believe our boots with the PowerFit™ comfort system are not more comfortable than other brands you have worn, Timberland will refund the purchase price of the boots to you, no questions asked, provided you still have your dated receipt of purchase. Please contact Timberland customer service at 1-800-445-5545 for more details.</p></div>',
        player:     "html",
        height:     300,
        width:      500
    });
}
/* ************************************************************************** */


/* ************************************************************************** */
function qetQuerystringParam(param)
{
	var val = unescape($.getQueryString({ ID: param }))
	if(!val || val == "undefined"){ val = ""; }
	val = val.replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/\+/g," ");
	return val;
}
/* ************************************************************************** */


/* ************************************************************************** */
// This function replicates the functionality of a Floodlight tag as an event-based
// tracking mechanism.
/* ************************************************************************** */
function trackFloodlight(cat)
{
	var src = "1562371";
	var type = "timbe207";
	
	var axel = Math.random() + "";
	var a = axel * 10000000000000;
	
	// Create a new iframe
	var iframe = document.createElement("iframe");
	iframe.src = "http://fls.doubleclick.net/activityi;src=" + src + ";type=" + type + ";cat=" + cat + ";ord=" + a + "?";
	
	// Make it as small as possible
	iframe.width = "1";
	iframe.height = "1";
	iframe.frameBorder = "0";
	
	// Append it
	document.body.appendChild(iframe);
}
/* ************************************************************************** */