
/* ************************************************************************** */
$(document).ready(function()
{
	
	// 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(); }
	});
	
	// Clear out the text "Keyword" from the form field when it's clicked
	$("#txt_keyword").click(function(event)
	{
		if($(this).val() == "Keyword"){ $(this).val(""); }
	});
	
	// homepage tile
	$("#li2 a").click(function(event)
	{
		event.preventDefault();
		trackFloodlight("soyfh701");
		goYouTube()
	});
	
	// 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"));
	});
	
	// Homepage Boot Slideshow functionality
	var counter = 1;
	// set the buy now button to the first boot
	shopButton(counter);
	// clicking on the thumbnails
	$("#thumbnails li a").click(function(){
	var bootString = $(this).attr("id");
	var bootNum = bootString.substring(0,5);
	$("#photo").attr("src", "/images/helix/home/photos/"+bootNum+".png");
	$("#bLearn").attr("href", "boots.php?bootID=d"+bootNum);
	turnOff();
		$(this).attr("id", function() {
			return  this.id + 'On';
		});
	$(this).blur();
	counter = parseInt(bootNum.substring(4,5));
	shopButton(counter);
	return false;
	});
	
	// clicking the next arrow
	$("#bNext").click(function(){
	turnOff();
	counter=(counter==6) ? 1 : counter+1;
	moveLR(counter);
	});
	// clicking the previous arrow
	$("#bPrev").click(function(){
	turnOff();
	counter=(counter==1) ? 6 : counter-1;
	moveLR(counter);
	});
	
	// Anti-Fatigue Page Rollovers
	$("#divAntiFat ul li").hover(
		function() {
			id = $(this).attr('id');
			$(this).parent().removeClass().addClass(id);
		},
		function() {
			$(this).parent().removeClass().addClass("base");
		} 
	);
	
	// Track AddThis events
	$("a.addthis_button#aShare").mouseover(function(event){ trackEvent("/outging/share-this"); });
	$("a.addthis_button_delicious").click(function(event){ trackEvent("/outging/post-delicious"); });
	$("a.addthis_button_twitter").click(function(event){ trackEvent("/outging/post-twitter"); });
	$("a.addthis_button_facebook").click(function(event){ trackEvent("/outgoing/post-wall"); });
	$("a.addthis_button_digg").click(function(event){ trackEvent("/outgoing/post-digg"); });
	$("a.addthis_button_email").click(function(event){ trackEvent("/outgoing/email-share"); });
	$("#aFootPro").click(function(event){ 
		trackEvent("/external/http://www.timberland.com/corp/index.jsp?page=storeLocator&clickid=topnav_store_txt"); 
	});
	$("#divBoots #thumbnails li a").click(function(event){ trackEvent("/event/product/product_thumbnail"); });
	// Floodlight Track Click events
	$("#bLearn").click(function(event){ trackFloodlight("soyfh880"); });
	$("#bShop").click(function(event){ trackFloodlight("soyfh343"); }); 
	$(".aBuy").click(function(event){ trackFloodlight("soyfh874"); });
	$(".aSolves").click(function(event){ trackFloodlight("soyft458"); });
	$(".aSearchs").click(function(event){ trackFloodlight("soyfn668"); });
	$(".aActions").click(function(event){ trackFloodlight("soyfs113"); });
	$("#aJoinD").click(function(event){ trackFloodlight("soyfs687"); });
	$(".aComment").click(function(event){ trackFloodlight("soyfs530"); });
	$(".aForum").click(function(event){ trackFloodlight("soyfs530"); });
	
});
/* ************************************************************************** */


// functions for the boot slideshow
var turnOff = function() {
	$("#thumbnails li a").each(function(i) {
		$(this).attr("id", function() {
		var theID = $(this).attr("id");
		return theID.replace("On", "");
		});
	});
}

var showDiv = function(theCount) {
	// turn on the main div
	$("#divBoots div").each(function() {
		$(this).hide();
		var theBootDiv = $(this).attr("id");
		if((theCount)==theBootDiv) {
			$(this).show();
			
		}
	});
	// turn on the link
	$("#thumbnails li a").each(function() {
		var theBootDiv = "d"+$(this).attr("id");
		if((theCount)==theBootDiv) {
			$(this).attr("id", function() {
			return  this.id + 'On';
			});
		}
	});
}

var shopButton = function(theCount) {
	$("#bShop").attr("href", bootLinks[theCount]);
}

var moveLR = function(theCount) {
	$("#photo").attr("src", "/images/helix/home/photos/boot"+theCount+".png");
	$("#bLearn").attr("href", "boots.php?bootID=dboot"+theCount);
	$("#bShop").attr("href", bootLinks[theCount]);
	$("#thumbnails li a").each(function(i) {
		if((theCount-1)==i) {
			$(this).attr("id", function() {
			return  this.id + 'On';
			});
		}
	});
}

var bootLinks = [
	'http://www.site0.com',
	'http://shop.timberland.com/product/index.jsp?productId=10880490&camp=MS:PRO:Link:NA:TBL',
	'http://shop.timberland.com/product/index.jsp?productId=11252166&camp=MS:PRO:Link:NA:TBL',
	'http://shop.timberland.com/product/index.jsp?productId=10880493&camp=MS:PRO:Link:NA:TBL',
	'http://shop.timberland.com/product/index.jsp?productId=10880489&camp=MS:PRO:Link:NA:TBL',
	'http://shop.timberland.com/product/index.jsp?productId=11252160&camp=MS:PRO:Link:NA:TBL',
	'http://shop.timberland.com/product/index.jsp?productId=10880494&camp=MS:PRO:Link:NA:TBL'
];


/* ************************************************************************** */
// 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<sup class="superTM">TM</sup> 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
    });
}
/* ************************************************************************** */


/* ************************************************************************** */
// This function replicates the functionality of a Floodlight tag as an event-based
// tracking mechanism.
/* ************************************************************************** */
function trackFloodlight(cat)
{
	var src = "1562371";
	var type = "tprof105";
	
	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);
}
/* ************************************************************************** */

/* ************************************************************************** */
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;
}
/* ************************************************************************** */


/* ************************************************************************** */
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();
}
/* ************************************************************************** */
