//var jobsRSS = "storage/xml/rss.xml";
var jobsRSS = "feedproxy.php?url=http://www.i-hs.co.uk/p/rc_rss.php?rcf_id=";
var jobseekersArray;
var currentItem;
var currentQuote;
var currentNum = 0;
var totalNumItems = 0;
var totalNumQuotes = 0;
var currentQuoteNum = 0;
var isBelowIE8;

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;
	}
}

var showCategory = function(num)
{
	var n = num + 1;	
	
	if(isBelowIE8)
	{
		$('.jobseekersJobsContainer').hide();
		$('.jobseekersJobsContainer:nth-child('+n+')').show();
	}
	else
	{
		var newMargin = num * -606;
		
		$('#jobseekersMover').clearQueue();
		$('#jobseekersMover').animate({
			marginLeft: newMargin
			}, 300, "easeOutExpo",
			function() {  }
		);
	}
}

var initJobseekers = function() {
	
	$('#jobseekersMover').empty();
	
	isBelowIE8 = checkBelowIE8();
	
	$.ajax({
		type: "GET",
		url: encodeURI(jobsRSS),
		dataType: "xml",
		success: function(xml) {
			
			jobsXML = xml;
			
			jobseekersArray = new Array("all", "sales", "market access", "marketing", "training", "lab staff", "medical", "management", "hq / executive", "scientific");
			
			initCategories();
			
			if(isBelowIE8)
			{
				Cufon.replace('.rssItem .applyButton, .rssItem .rssCategory', { fontFamily: 'myriad_pro' });
			}
			else
			{
				Cufon.replace('.rssItem h2, .rssItem .applyButton, .rssItem .rssCategory', { fontFamily: 'myriad_pro' });
			}
	
		}
	});
}

var initCategories = function(rssPath)
{
	var limit = 10;
	
	var i;
	
	for(i = 0; i < jobseekersArray.length; i++)
	{
		var container = $('<div class="jobseekersJobsContainer"></div>').appendTo('#jobseekersMover');
		
		if($('#jobseekersMover .jobseekersJobsContainer').length == jobseekersArray.length)
		{
			$('#jobseekersMover').append('<div style="clear: both;"></div>');
			
			if(isBelowIE8)
			{
				$('.jobseekersJobsContainer').hide();
				$('.jobseekersJobsContainer:nth-child(1)').show();
			}
		}
		
		var count = 0;
	
		$(jobsXML).find('item').each(function(){
			if(count < limit)
			{
				var category = $(this).find("category").text();
				var categoryClass = category.toLowerCase();
				
				if(jobseekersArray[i] == "all" || jobseekersArray[i] == categoryClass)
				{
					count++;
					
					var title = $(this).find("title").text();
					var location = $(this).find("location").text();
					var urlPath = $(this).find("link").text();
					var salary = $(this).find("salary").text();
				
					var categoryColour = "";
					
					switch(categoryClass)
					{
						case "sales": case "management":
							categoryColour = "olive";
							break;
						case "market access": case "training": case "scientific":
							categoryColour = "pink";
							break;
						case "marketing": case "healthcare": case "lab staff":
							categoryColour = "green";
							break;
						case "medical": case "hq / executive":
							categoryColour = "orange";
							break;
						default:
							categoryColour = "orange";
							break;
					}
					
					switch(categoryClass)
					{
						case "market access":
							categoryClass = "market";
							break;
						case "lab staff":
							categoryClass = "lab";
							break;
						case "hq / executive":
							categoryClass = "hq";
							break;
						case "sales": case "management": case "training": case "lab": case "medical": case "healthcare": case "marketing": case "scientific": case "market": case "hq":
							break;
						default:
							categoryClass = "ihs";
							break;
					}
					
					
					var itemStyle = "";
					
					if((count) % 2 == 0)
					{
						itemStyle = 'style="margin-right: 0px;"';
					}
		
					
					var jobEntry = $('<div class="rssItem" id="rssItem'+i+'" '+itemStyle+'><div class="iconWrapperRight '+categoryColour+'"><div class="icon '+categoryClass+'"><div class="iconImageDiv"></div></div></div><h2>'+title+'</h2><div style="clear:both;"></div><div class="fieldWrapper"><p class="rssFieldTitle">Sector</p><p class="rssField" id="location">'+category+'</p><div style="clear:both;"></div></div><div class="fieldWrapper"><p class="rssFieldTitle">Location</p><p class="rssField" id="location">'+location+'</p><div style="clear:both;"></div></div><div class="fieldWrapper"><p class="rssFieldTitle">Salary</p><p class="rssField">'+salary+'</p><div style="clear:both;"></div></div><div style="clear:both;"></div><div class="applyButton '+categoryColour+'"><a href="'+urlPath+'">Apply</a></div><div style="clear:both;"></div><div class="colourStrip '+categoryColour+'"></div></div>').appendTo(container);
					
					
					if((count) % 2 == 0)
					{
						container.append('<div style="clear: both;"></div>');
					}
				}
			}
			
		});
		
		if(count == 0)
		{
			container.append('<p>No jobs found for selected sector, please check back later.</p>');
		}
		
	}
			
}

var initQuoteOverlay = function () {
	
	var quoteOverlayHTML = $('<div class="enlargeOverlayBG"></div><div class="enlargeOverlay"><div class="quoteWrapper"><div class="quoteCloseButton" id="quoteCloseBtn"></div><div style="clear: both;"></div><div class="quoteArrowRight" id="quoteArrowRight"></div><div class="quoteArrowLeft" id="quoteArrowLeft"></div><div class="quoteContainer"></div><div style="clear: both;"></div></div></div>');
	quoteOverlayHTML.appendTo('body');
	
	
	$('.quoteBox').empty();
	$('.quoteBox').click(function() { openOverlay(); });
	$('#quoteCloseBtn').click(function() { closeOverlay(); });
	
	$.ajax({
		type: "GET",
		url: encodeURI("storage/xml/testimonials.xml"),
		dataType: "xml",
		success: function(xml) {
				
			var i = 0;
			
			currentQuoteNum = Math.floor(Math.random() * $(xml).find('item').length);
			
			$(xml).find('item').each(function(){
											  
				var newQuote = $(this).attr("quote");
				
				var quoteEntry = $('<div class="quoteEntry" id="quoteEntry'+i+'">'+newQuote+'</div>').appendTo('.quoteContainer');
				
				if(i == currentQuoteNum)
				{
					$('.quoteBox').append(newQuote);
					var firstP = $('.quoteBox p:nth-child(1)').html();
					$('.quoteBox').empty();
					$('.quoteBox').append('<p>'+firstP+'&#8230;</p>');
				}
				
				if(i == 0)
				{
					currentQuote = quoteEntry;
				}
				else
				{
					quoteEntry.hide();
				}
				
				i++
				
			});
			
			totalNumQuotes = i;
			
			$('#quoteArrowLeft').click( function() {
				getQuote(-1);
			});
			
			$('#quoteArrowRight').click( function() {
				getQuote(1);
			});
			
			currentQuoteNum--;
			getQuote(1);
			
		}
	});
}

var initQuotePage = function () {
	
	var quoteOverlayHTML = $('<div class="enlargeOverlayBG"></div><div class="enlargeOverlay"><div class="quoteWrapper"><div class="quoteCloseButton" id="quoteCloseBtn"></div><div style="clear: both;"></div><div class="quoteArrowRight" id="quoteArrowRight"></div><div class="quoteArrowLeft" id="quoteArrowLeft"></div><div class="quoteContainer"></div><div style="clear: both;"></div></div></div>');
	quoteOverlayHTML.appendTo('body');
	
	
	$('#quoteContainer').empty();
	$('#quoteCloseBtn').click(function() { closeOverlay(); });
	
	$.ajax({
		type: "GET",
		url: encodeURI("storage/xml/testimonials.xml"),
		dataType: "xml",
		success: function(xml) {
				
			var i = 0;
			
			$(xml).find('item').each(function(){
											  
				var newQuote = $(this).attr("quote");
				
				var rightClass = '';
				
				if((i+1) % 3 == 0)
				{
					rightClass = ' right';
				}
				
				var quoteEntry = $('<div class="quoteEntry" id="quoteEntry'+i+'">'+newQuote+'</div>').appendTo('.quoteContainer');
				
				var newQuoteBox = $('<div class="quoteBox'+rightClass+'"></div>');
				
				newQuoteBox.append(newQuote);
				var firstP = $('p:nth-child(1)', newQuoteBox).html();
				var authorP = $('p.author', newQuoteBox).html();
				newQuoteBox.empty();
				newQuoteBox.append('<p class="quotePart">'+firstP+'&#8230;</p>');
				newQuoteBox.append('<p class="author">'+authorP+'</p>');
				
				var localI = i;
				
				newQuoteBox.click(function() { openOverlay(); getQuoteNum(localI); });
				
				$('#quotePageContainer').append(newQuoteBox);
				if((i+1) % 3 == 0)
				{
					$('#quotePageContainer').append('<div style="clear: both;"></div>');
				}

				
				if(i == 0)
				{
					currentQuote = quoteEntry;
				}
				else
				{
					quoteEntry.hide();
				}
				
				i++
				
			});
			
			totalNumQuotes = i;
			
			$('#quoteArrowLeft').click( function() {
				getQuote(-1);
			});
			
			$('#quoteArrowRight').click( function() {
				getQuote(1);
			});
			
			currentQuoteNum--;
			getQuote(1);
			
		}
	});
}

var getQuote = function(direction)
{
	
	var lastNum = currentQuoteNum;
	currentQuoteNum += direction;
	
	if(currentQuoteNum < 0)
	{
		currentQuoteNum = totalNumQuotes - 1;
	}
	else if(currentQuoteNum > totalNumQuotes - 1)
	{
		currentQuoteNum = 0;
	}
	
	currentQuote.stop().animate({
			opacity: 0
			}, 200, "swing",
		function() { showNextQuote(direction); }
		);		
	
}

var getQuoteNum = function(num)
{
	
	var lastNum = currentQuoteNum;
	currentQuoteNum = num;
	
	if(currentQuoteNum < 0)
	{
		currentQuoteNum = totalNumQuotes - 1;
	}
	else if(currentQuoteNum > totalNumQuotes - 1)
	{
		currentQuoteNum = 0;
	}
	
	currentQuote.stop().animate({
			opacity: 0
			}, 0, "swing",
		function() { showNextQuote(1); }
		);		
	
}

var showNextQuote = function(direction)
{
	currentQuote.hide();
	
	currentQuote = $('#quoteEntry'+currentQuoteNum);
	
	$(currentQuote).css('opacity', 1);
	currentQuote.show();
	$('p', currentQuote).css('opacity', 0);
	
	var fadeSpeed = 200;
	var runningDelay = 0;
	
	$('p', currentQuote).each(function() {
		$(this).delay(runningDelay).animate({
			opacity: 1
			}, fadeSpeed, "swing");
		
		runningDelay += fadeSpeed;
		
	});
	
	
}


var openOverlay = function()
{
	documentWidth = $(document).width();
	documentHeight = $(document).height();
	
	$('.enlargeOverlay, .enlargeOverlayBG').width(documentWidth).height(documentHeight);
	
	$('.enlargeOverlay').stop().fadeIn(100);
	$('.enlargeOverlayBG').stop().fadeTo(100, 0.5);
	$("html, body").animate({ scrollTop: 0 }, "slow", "swing");
}

var closeOverlay = function()
{
	
	$('.enlargeOverlay').stop().fadeOut(250);
	$('.enlargeOverlayBG').stop().fadeOut(250);
	
}



