var $j = jQuery.noConflict();

//nav init
$j(document).ready(function() {

  $j('.subnav li').hover(
    function() {
      $j(this).addClass('on')
    },
    function() {
      $j(this).removeClass('on')
    }
  )

  $j('.subnav li').click(function() {
    if ($j(this).children('a').length > 0)
      location.href = $j(this).children('a').attr('href')
  })

  function clearAllMenus() {
    $j('#menu li').each(function() {
      var menu = this.id.split('-').slice(1).join('-')
      $j('#' + menu).hide()
    })
  }
  function removeOn() {
    $j('#menu li').each(function() {
      var menu = this.id.split('-').slice(1).join('-')
      $j('#nav-' + menu).removeClass('on')
      $j('#nav-' + menu + " a").removeClass('on')
    })
  }

  var lastY = 0;
  $j('#menu li').each(function() {
    var menu = this.id.split('-').slice(1).join('-')
    $j(this).hover(
      function(e) {
        clearAllMenus()
        removeOn()
        $j('#' + menu).show()
        $j('#nav-' + menu).addClass('on')
        $j('#nav-' + menu + " a").addClass('on')
        lastY = e.clientY
      },
      function(e) {
        if ($j('#nav-' + menu).hasClass('current'))
        	$j('#nav-' + menu).removeClass('on')
      }
    )
  })

})


$j(document).ready(function() {
	// init modal
	$j('#dialog').jqm();
	$j('#dialog2').jqm();
}); 

$j(document).ready(function() {
	// init modal
	$j('#noProductsAvailable').jqm();
	$j('#noProductsAvailable #errorTitle').html('No additional products available');
	$j('#noProductsAvailable #errorBody').html('There are no additional products available for purchase. Please click the "Continue" button to proceed.');
	$j('#noProductsAvailable').jqmShow();
}); 

// show national search box
$j(document).ready(function() {
	$j("input[@name='localRadio']").click(
		function() {		
			$j("input[@name='nationalRadio']:nth(1)").attr("checked","checked");
			$j("input[@name='localRadio']:nth(1)").attr("checked","checked");
			$j("#jobsSearchBoxFooterLocal").css("display","none");
			$j("#jobsSearchBoxFooterNational").css("display","block");
			$j("#resumeImage2").css("visibility","hidden");	
			$j("#jobsSearchBox").css("display","none");
			$j("#jobsSearchBoxNational").css("display","block");

	});
}); 

// show local search box
$j(document).ready(function() {
	$j("input[@name='nationalRadio']").click(
		function() {
			$j("input[@name='nationalRadio']:nth(0)").attr("checked","checked");
			$j("input[@name='localRadio']:nth(0)").attr("checked","checked");	
			$j("#jobsSearchBoxFooterNational").css("display","none");			
			$j("#jobsSearchBoxFooterLocal").css("display","block");
			$j("#resumeImage2").css("visibility","visible");				
			$j("#jobsSearchBox").css("display","block");
			$j("#jobsSearchBoxNational").css("display","none");			
		});
}); 


//nulling out form fields if user types nothing in so search runs default on national jobs. works for all search boxes on site.
//note: local search already has a function in jobs_search_box.js that does the same thing.
$j(document).ready(function() {
	$j("form[@name^='JobSearchDisplayFormNational']").submit(
		function() {
			if ($j("form[@name^='JobSearchDisplayFormNational'] input[@name='q']").val() == "e.g. Marketing") {
				$j("form[@name^='JobSearchDisplayFormNational'] input[@name='q']").val("");
			}
			if ($j("form[@name^='JobSearchDisplayFormNational'] input[@name='l']").val() == "e.g. New York, NY") {
				$j("form[@name^='JobSearchDisplayFormNational'] input[@name='l']").val("");
			}
			return true;	
		});
}); 
 


function convertToBR(content) {
    return content.replace(/2t2t/g,"<BR/>");
}

function convertToNewline(content){
    return content.replace(/2t2t/g,"\n");
}

function removePRE(content) {
    return content.replace(/\<pre.*\>|\<\/pre\>/g,"");
}

function formatImage(id, width) {
	if (document.getElementById(id)!= null) {
		imageHeight = document.getElementById(id).clientHeight;
		imageWidth = document.getElementById(id).clientWidth;
		if (imageWidth >= width) {
			document.getElementById(id).style.width = width+'px';
			newHeight = (width/imageWidth)*(imageHeight);
			document.getElementById(id).style.height = newHeight+'px';
		} 
	}
}

// to use this function, an variable named offset must exist on the page.
var offset = 0
var startIndex;
var endIndex;
function fetchResults (results, direction, index, currentID) {
	var totalResults = results.length;
	var newResults = new Array();
	var previous = "true";
	var next = "true";
	
	// get start and end index based on how my results are available
	if (direction == "previous") {
		var resultsLeft = index + offset - 2;
		if (index == 0){
			resultsLeft = index + offset;
		}
		if (index == 1){
			resultsLeft = index + offset -1;
		}
		if (resultsLeft > 5) {
			startIndex = index + offset - 7;
			endIndex = index + offset - 3;
			if (index == 0){
				startIndex = startIndex + 2;
				endIndex = endIndex + 2;
			}
			if (index == 1){
				startIndex = startIndex + 1;
				endIndex = endIndex + 1;			
			}
			previous = "true";
			next = "true";
			offset = offset - 5;
		} else if (resultsLeft <= 5 && resultsLeft > 0) {
			startIndex = 0;
			endIndex = 4;			
			previous = "false";
			next = "true";
			offset = offset - resultsLeft;		
		} else {
			previous = "false";
			next = "true";
		}
	} else if (direction == "next") {
		if (index <= 2) {
			var resultsLeft = totalResults - offset - 5;
		} else {
			var resultsLeft = totalResults - index - offset - 3;
		}
		if (resultsLeft < 0) {
			resultsLeft = 0;
		}		
		if (resultsLeft > 5) {
			startIndex = index + offset + 3;
			endIndex = index + offset + 7;
			if (index == 0){
				startIndex = startIndex + 2;
				endIndex = endIndex + 2;
			}
			if (index == 1){
				startIndex = startIndex + 1;
				endIndex = endIndex + 1;			
			}
			next = "true";
			previous = "true";
			offset = offset + 5;
		} else if (resultsLeft <= 5 && resultsLeft > 0) {
			startIndex = totalResults - resultsLeft;
			endIndex = totalResults -1;
			next = "false";
			previous = "true";
			offset= offset + 5;
		} else {
			next = "false";
			previous = "true";
		}
	}
	// create new array based on new boundaries
	var i=0;
	for (i=startIndex;i<=endIndex;i++)
	{
		newResults.push(results[i]);
	}
	displayResults(newResults,previous,next,currentID);
}

function createSearchResultHTML(jobSearchResult,currentID) {
	var html;
	if(jobSearchResult.id == currentID) {
		html = "<span class='search-results-current'>"+jobSearchResult.title+"</span>";
		html +="<br><span class='search-result-location-current'>"+jobSearchResult.location+"</span>";
	} else {
		html = "<a href='JS_JobSearchDetail?jobid="+jobSearchResult.id+"&jobSummaryIndex="+jobSearchResult.index+"&agentID="+getQueryParam('angentID')+"&QUICK_SEARCH="+getQueryParam('QUICK_SEARCH')+"'>"+jobSearchResult.title+"</a>";
		html +="<br><span class='search-result-location'>"+jobSearchResult.location+"</span>";
	}
	return html;
}

function displayResults(results,previous,next,currentID) {
	if (null != results[0]) {
	    	document.getElementById("search-result-rt-1").innerHTML=createSearchResultHTML(results[0],currentID);
	    	document.getElementById("search-result-rt-1").style.display="block";
    } else {
    	document.getElementById("search-result-rt-1").style.display="none";
    }
	if (null != results[1]) {
		    document.getElementById("search-result-rt-2").innerHTML=createSearchResultHTML(results[1],currentID);
	    	document.getElementById("search-result-rt-2").style.display="block";
	} else {
    	document.getElementById("search-result-rt-2").style.display="none";		
	}
	if (null != results[2]) {
	    document.getElementById("search-result-rt-3").innerHTML=createSearchResultHTML(results[2],currentID);
    	document.getElementById("search-result-rt-3").style.display="block";
	} else {
	    document.getElementById("search-result-rt-3").style.display="none";
	}
	if (null != results[3]) {
	    document.getElementById("search-result-rt-4").innerHTML=createSearchResultHTML(results[3],currentID);
    	document.getElementById("search-result-rt-4").style.display="block";
	} else {
	    document.getElementById("search-result-rt-4").style.display="none";
	}
	if (null != results[4]) {
	    document.getElementById("search-result-rt-5").innerHTML=createSearchResultHTML(results[4],currentID);
    	document.getElementById("search-result-rt-5").style.display="block";	
	} else {
    	document.getElementById("search-result-rt-5").style.display="none";
	}
	// next/previous links
	
	if (previous == "true") {
    	document.getElementById("search-results-previous-rt").style.display="block";	
   	} else {
   		document.getElementById("search-results-previous-rt").style.display="none";
   	} 
   	if (next == "true")
    {
   		document.getElementById("search-results-next-rt").style.display="block";
   	} else {
   		document.getElementById("search-results-next-rt").style.display="none";
   	}
   	
}

function getQueryParam(paramName) {
	paramName = paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+paramName+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) {
		return "";
	} else {
		return results[1];
	}
}

function encodeURL(url) {
	if (url.match(/\$/)) {
		var url_parts = url.split("?");
		var parsed_url = url_parts[0]+"?"+escape(url_parts[1]);
	} else {
		var parsed_url = url;
	}
	return url;
}


function toggleSearch(radioButton) {
	var selectedName = radioButton.name;
	if (selectedName.indexOf("scopeLocal") > -1) {
		var index = selectedName.replace("scopeLocal","");
	} else if (selectedName.indexOf("scopeNational") > -1) {
		var index = selectedName.replace("scopeNational","");
	}
	if (index >= 0) { 
		if (selectedName.match("scopeLocal"+index)) {
			document.getElementById("scopeNational"+index).checked = false;
			document.getElementById("jobs_search_form_local"+index).style.display="block";
			document.getElementById("jobs_search_form_national"+index).style.display="none";
		} else {
			document.getElementById("scopeLocal"+index).checked = false;
			document.getElementById("jobs_search_form_local"+index).style.display="none";
			document.getElementById("jobs_search_form_national"+index).style.display="block";
		}	
	}
}

function submitenter(myfield,e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}
	
	if (keycode == 13) {
	   myfield.form.submit();
	   return false;
	} else {
	   return true;
    }
}



function enableFields() {
	$j(":disabled").removeAttr("disabled");
}
