/*-----------------------------------------------
	BEGIN - Featured Jobs Widget
-----------------------------------------------*/

// Load stylesheet
var cssLink = '/global/featuredJobs/widget-styles.css';
if (ccSite) { cssLink = 'http://www.affiniscape.com' + cssLink; }
var newLink = document.createElement('link');
newLink.type = 'text/css';
newLink.rel = 'stylesheet';
newLink.href = cssLink;
document.getElementsByTagName("head")[0].appendChild(newLink);

// Load jobs from Yahoo Pipes
ccUrl = ccUrl.replace(/^.+\:\/\//, '').replace(/\/.+/,'');
var baseUrl = 'http://pipes.yahoo.com/pipes/pipe.run?_id=d74eeb77877fdd49c4a73221a10c055f&_render=json&_callback=pipeFeatJobs&url=';
var pipeUrl = baseUrl + escape('http://'+ ccUrl +'/Common/featuredjobs.aspx?abbr='+ ccId +'&count=5&loc=y&desc=n&order=random&link=popup&allgroup=n');
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = pipeUrl;
document.getElementsByTagName("head")[0].appendChild(newScript);

// Callback Function to Pipe Jobs
function pipeFeatJobs(data){
	
	var wrapper = document.getElementById('cc-feat-jobs-widget');

	// Error Handling for Null Results
	if (data.count == 0){
		wrapper.innerHTML = '<h4>Sorry. There were no jobs found.</h4>';
		wrapper.className = 'error';
		
	// Process Piped Data
	} else {	
		var jobs = data.value.items[0].content;
		jobs = jobs.replace(/Common\/javascript\:open_FF_FeaturedJobs\(\'http\:\/\/www\.associationcareernetwork\.com\//g,'');
		jobs = jobs.replace(/\'\,\d+?\)/g,'');
		jobs = jobs.replace(/ rel\=\"nofollow\"/g,'');
		wrapper.innerHTML = jobs;
		
	}
	
	// Show Search Jobs and Post Jobs links.
	var utm = '&utm_source=Association-Website&utm_medium=Widget&utm_campaign=FeaturedJobsWidget&utm_content=';
	var btns = document.createElement('div');
		btns.setAttribute('class','jobs-buttons');
		btns.setAttribute('className','jobs-buttons');
	
	var searchBtn = document.createElement('a');
		searchBtn.setAttribute('class','search-jobs');
		searchBtn.setAttribute('className','search-jobs');
		searchBtn.setAttribute('target','_blank');
		searchBtn.setAttribute('href','http://'+ ccUrl +'/JobSeeker/Jobs.aspx?abbr=' + ccId + utm + 'SearchJobs');
		searchBtn.appendChild(document.createTextNode('Search Jobs'));
		
	var postBtn = document.createElement('a');
		postBtn.setAttribute('class','post-jobs');
		postBtn.setAttribute('className','post-jobs');
		postBtn.setAttribute('target','_blank');
		postBtn.setAttribute('href','http://'+ ccUrl +'/Employer/EmpHome.aspx?abbr=' + ccId + utm + 'PostJobs');
		postBtn.appendChild(document.createTextNode('Post Jobs'));
	
	btns.appendChild(searchBtn);
	btns.appendChild(postBtn);
	wrapper.appendChild(btns);
		
}
/*-----------------------------------------------
	END - Featured Jobs Widget
-----------------------------------------------*/
