﻿$(document).ready(function()	{

	// hover background change for job search results
	$('ul.jobsSearchResults>li').hover(function(){$(this).addClass('focussed')},function(){$(this).removeClass('focussed')});
	// jobs tabs on 'gold' result
	$('div.job_tabs li>a').click(jobsGoldTabOn);
	// checkbox colouring on location map
	$('#map_show input.checkbox').click(jobLocationCheckboxColour);
	$('div.local_info>.jobsDetailsTabs>li>a').click(jobLocationDetailTabs);

	// jobsDetailsGold expander
	/* $('#jobDetailsGold #content .extra_content').before('<div id="more_details"><div id="more_details_sleeve"><p>More job details, contact information &amp; downloads</p></div></div>').siblings('#more_details').click(jobDetailsGoldExpand); */

	// jobs search results
	$('#jobssearchresults div.browse_section>div.facet_set>ul').each(function(){$(this).children('li:gt(8)').css('display','none')});
	$('#jobssearchresults div.browse_section>div.facet_set>a.viewmore').bind('click',{initialNumber:'9'},facetViewMore);
	$('#jobssearchresults div.browse_section>div.facet_set>a.viewfewer').bind('click',{initialNumber:'9'},facetViewLess);

	/*jobs saved*/
	$('#jobssaved div.browse_section>div.facet_set>ul').each(function(){$(this).children('li:gt(2)').css('display','none')});
	$('#jobssaved div.browse_section>div.facet_set>a.viewmore').bind('click',{initialNumber:'6'},facetViewMore);
	$('#jobssaved div.browse_section>div.facet_set>a.viewfewer').bind('click',{initialNumber:'6'},facetViewLess);
	/*jobs hub*/
	$('#jobshub div.browse_section>div.facet_set>ul').each(function(){$(this).children('li:gt(8)').css('display','none')});
	$('#jobshub div.browse_section>div.facet_set>a.viewmore').bind('click',{initialNumber:'9'},facetViewMore);
	$('#jobshub div.browse_section>div.facet_set>a.viewfewer').bind('click',{initialNumber:'9'},facetViewLess);
	// hover background change for job search results
	$('ul.jobsSearchResults>li').hover(function(){$(this).addClass('focussed')},function(){$(this).removeClass('focussed')});
	// jobs tabs on 'gold' result
	$('div.job_tabs li>a').click(jobsGoldTabOn);
	// checkbox colouring on location map
	$('#map_show input.checkbox').click(jobLocationCheckboxColour);
	$('div.local_info>.jobsDetailsTabs>li>a').click(jobLocationDetailTabs);

	// jobsDetailsGold expander
	//$('#jobDetailsGold #content .extra_content').before('<div id="more_details"><div id="more_details_sleeve"><p>More job details and contact information & downloads</p></div></div>').siblings('#more_details').click(jobDetailsGoldExpand);

	$('.saveSearchParagraph a').click(function()	{
		$(this).parent().siblings('.popupPanel').toggle();	
		return false;
	});
	
	// local profile expanding links
	$('#local_amenities div.assoc_info p.more>a').click(showMoreLocalAmenities)

	// Schoolmap links - must go to the right place / open the correct tab
	$('.schoolMap ul li:nth-child(1)').click(function()	{
		window.location = 'jobDetailsGold_LocalInfo.aspx?localinfo';
		return false;
	});
	$('.schoolMap ul li:nth-child(2)').click(function()	{
		window.location = 'jobDetailsGold_LocalInfo.aspx?map';
		return false;
	});
	$('.schoolMap ul li:nth-child(3)').click(function()	{
		window.location = 'jobDetailsGold_LocalInfo.aspx?journeyplanner';
		return false;
	});


	params = window.location.href.split('?');
	get = params[1];
	switch(get)	{
		case 'localinfo':
			$('#content .jobDetails_content .local_info li:nth-child(2)>a').click();
			break;
		case 'journeyplanner':
		case 'map':
			break;
	}

	// postcode search over flash map (jobSearchResults.aspx)
	if ($('.postcode_search>a').length > 0){
		$('.postcode_search>a').click(function()	{
			$('.jobSearchPostcodePopup').fadeIn();
			return false;
		});
	}

	// Ajax call on click
	$('body').append($('<div></div>').attr({
		id: 'saveJobMessage'
	}).css({
		display: 'none'
	}));

	//$('.saveJob').click(function()	{
	//	savedJobAjax(this, 'save')
	//	return false;
	//});
	$('.deleteJob').click(function()	{
		if(confirm('Are you sure?')) return true;//	savedJobAjax(this, 'delete');
		return false;
	});

	// to override the default style added by maps api
	$('.local_info #map_show').css('width','auto');

	$('.local_info .assoc_info tbody tr:even').addClass('even').css('backgroundColor','#f5f5f5');
	
	// Iteration 2 - added - make job search results header clickable, not just job title
	$('.jobsSearchSleeve > h2 > a').each(function()	{
		// Save the URL
		var url = $(this).attr('href');
		// Bind the click to a) the H2 and b) the H3
		$(this).parent().click(function()	{
			window.location = url;
		}).css('cursor', 'pointer').next().click(function()	{
			window.location = url;
		}).css('cursor', 'pointer');
	});

	// Iteration 2 - added - make the download docs title clickable
	$('.downloads > li > a').each(function()	{
		// Save the URL
		var url = $(this).attr('href');
		// Bind the click to a) the H2 and b) the H3
		$(this).parent().click(function()	{
			//window.location = url;
		}).css('cursor', 'pointer');
	});

});

function jobsGoldTabOn(){
	if($(this.parentNode).is('not:(.on)')) {
		//Problem showing and hiding tabs 
		//$(this).parents(".jobSearchSleave").find(".schoolDetails")	    	    
		var index = $(this.parentNode.parentNode).children().index(this.parentNode);
		$(this.parentNode).siblings().removeClass('on').end().addClass('on');
		$(this).parents('div.jobsSearchSleeve').find('div.schoolDetailsContainer').children('div').hide().eq(index).show();
	}
	return false;
}
function jobDetailsGoldExpand() {
	$('div.extra_content').slideDown('slow');
	$(this).hide('slow');
	//Here goes Omniture code
	var s=s_gi('tsltescouk');
	s.events= 'event6';
	s.tl(this,'o','Job Disclosure');
}
function jobLocationCheckboxColour(){
	if($(this).is(':checked')){$(this.parentNode).addClass('checked')}
	else{$(this.parentNode).removeClass('checked')}
}
function jobLocationDetailTabs(){
	if($(this.parentNode).is(':not(.on)')) {
		var index = $(this.parentNode.parentNode).children().index(this.parentNode);
		$(this.parentNode).siblings().removeClass('on').end().addClass('on');
		$('#journey_planner,#local_amenities').hide().eq(index).show();
		if($('#local_amenities').is(':visible')) {
			// XXX AJAX call to load in info from UpYourStreet to go here
		}
	}
	return false;
}

function savedJobAjax(el, type)	{
	if($(el).hasClass('deleteJob'))	type = 'delete';
	else	type = 'save';
	if(type == 'save')	{
		id = $(el).attr('href').replace('jobsSaved.aspx?ac=', '');
		url = 'savejob.ajax.aspx';
		onComplete = function()	{
			$('#saveJobMessage').fadeOut();
			$(el).text('job saved').removeClass().addClass('deleteJob');
		};
		timer = 2000;
	}
	else	{
		id = $(el).attr('href').replace('jobsSaved.aspx?delete&amp;ac=', '');
		url = 'deletejob.ajax.aspx';
		onComplete = function()	{
			$('#saveJobMessage').fadeOut();
			$(el).text('save job').removeClass().addClass('saveJob');
		};
		timer = 2000;
	}
	position = $(el).offset();
	$.post(
		url,
		{ jobID: id },
		function(reply)	{
			$('#saveJobMessage').html(reply.message).css({
				top: position.top,
				left: position.left - 40,
				display: ''
			});
			setTimeout(onComplete, timer);
		},
		"json"
	);
	return false;
}

function showMoreLocalAmenities() {
	$(this.parentNode).hide().parents('div.assoc_info').find('tr.hidden').show();
	return false;
}

function openPostcodePopup()	{
	$('#jobsHubPostcodeSearch').fadeIn();
}

