var limit = 0;
var currentJobNum = 0;
var currentJob;
var totalHotJobs = 0;
var direction = 1;
var ie8 = false;

var initHotJobs = function() {
	
	$('.hotJobsRight').click(function() { getHotJob(1); });
	$('.hotJobsLeft').click(function() { getHotJob(-1); });
	
	$('.hotJobsContainer').empty();
	
	ie8 = checkBelowIE8();
	
	$.ajax({
		type: "GET",
		//url: 'rss.xml',
		url: 'feedproxy.php?url=http://www.planbhealth.co.uk/p/rc_rss.php?rcf_id=179',
		dataType: "xml",
		success: function(d) {
					
			var i = 0;
			$(d).find('item').each(function() {
	 			
				if(limit == 0 || i < limit)
				{
					var jobItem = $('<div class="hotJobItem" id="hotJob'+i+'"><a href="'+$(this).find('link').text()+'"><h3>' + $(this).find('title').text() + '</h3><p>' + $(this).find('description').text() + '</p></a></div>').appendTo('.hotJobsContainer');
					if(i != 0)
					{
						jobItem.hide();
					}
					
					totalHotJobs++;
				}
				
				i++;
			});
		},
		error:function (xhr, ajaxOptions, thrownError){
			//alert('Could not load XML, thrown error: ' + thrownError );
			
		}
	});

}

var getHotJob = function(newDirection) {
	
	direction = newDirection;
	
	currentJob = $('#hotJob'+currentJobNum);
	
	if(ie8)
	{
		if(newDirection == 1)
		{
			$('#hotJob'+currentJobNum).hide();
			
			if(currentJobNum < totalHotJobs -1)
			{
				currentJobNum++;
			}
			else
			{
				currentJobNum = 0;
			}
			
			$('#hotJob'+currentJobNum).show();
		}
		else
		{
			$('#hotJob'+currentJobNum).hide();
			
			if(currentJobNum > 0)
			{
				currentJobNum--;
			}
			else
			{
				currentJobNum = totalHotJobs -1;
			}
			
			$('#hotJob'+currentJobNum).show();
		}
	}
	else
	{
	
		if(newDirection == 1)
		{			
			currentJob.animate({
				marginLeft: -244,
				opacity: 0
				}, 200, "swing",
			function() { showNextHotJob(); }
			);	
			
			if(currentJobNum < totalHotJobs -1)
			{
				currentJobNum++;
			}
			else
			{
				currentJobNum = 0;
			}
			
		}
		else
		{			
			currentJob.animate({
				marginLeft: 244,
				opacity: 0
				}, 200, "swing",
			function() { showNextHotJob(); }
			);		

			
			if(currentJobNum > 0)
			{
				currentJobNum--;
			}
			else
			{
				currentJobNum = totalHotJobs -1;
			}
			
		}
		
	}	
}

var showNextHotJob = function() {
	
	currentJob.hide();
	currentJob = $('#hotJob'+currentJobNum);
	
	if(direction == 1)
	{
		currentJob.show().animate(
			{marginLeft: 244},
			{duration: 0,
			easing: 'swing'});
	}
	else
	{
		currentJob.show().animate(
			{marginLeft: -244},
			{duration: 0,
			easing: 'swing'});
	}
	
	currentJob.animate(
			{marginLeft: 0,
			opacity: 1},
			{duration: 300,
			easing: 'swing'});
}

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

function checkBelowIE8() {
	var ver = getInternetExplorerVersion();
	if (ver > -1) {
		if (ver >= 8.0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

