
// run scripts on load
$(function()
{
	// initialise the job browser
	if($('#browse-jobs').length > 0 ){
		EA.listBrowser.init('#browse-jobs');
	}
	
	// initialise the job results overlay and the shortlist box
	if($('table.js-overlay').length > 0 ){
		EA.jobsList.init('table.js-overlay');
	}
	if($('#shortlist-box').length > 0 ){
		EA.jobShortList.init();
	}
	
	// featured jobs rollover
	if($('#featured-jobs').length > 0 ){
		$('.featured-job-item')
			.css({backgroundPosition: '0 0'})
			.mouseover(function(){
				$(this).css({backgroundPosition: '-156px 0'});
			})
			.mouseout(function(){
				$(this).css({backgroundPosition: '0 0'});
			});
	}
	
	// initialise search filter options
	if($('#filter-results').length > 0 ){
		EA.filterOptions.init();
	}
	
	// advanced search link actions
	$('#advanced-search .js-form-reset').click(function(){
		$('form#advanced-search')[0].reset();
		return false;
	});
	$('a.js-select-all').click(function(){
		$(this).parent().parent().find('li input[type=checkbox]').attr('checked', true);
		return false;
	});
	$('a.js-clear-all').click(function(){
		$(this).parent().parent().find('li input[type=checkbox]').attr('checked', false);
		return false;
	});
	
	// job description show
	$('a.js-toggle-desc').click(function(){
		
		// get the related content box
		var container = $(this).parents('.collapsable-box');
		var description = container.find('table.job-description');
		
		// show-hide the content depending on status
		if(description.is(':visible')){
			container.removeClass('open');
			description.hide();
		}
		else{
			container.addClass('open');
			description.show();
		}
		
		// cancel the link action
		return false;
	});
	// faq show
	$('a.js-toggle-faq').click(function(){
		
		// get the related content box
		var container = $(this).parents('.collapsable-box');
		var answer = container.find('.padded-box');
		
		// show-hide the content depending on status
		if(answer.is(':visible')){
			container.removeClass('open');
			answer.hide();
		}
		else{
			container.addClass('open');
			answer.show();
		}
		
		// cancel the link action
		return false;
	});
	
	// init account manager
	EA.account.init();
	
	
	
});



/* browser for jobs lists
-------------------------------------------------------------------------*/
EA.account = {
	
	// initialise account menu
	init: function(){
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
	},
	
	bindEventHandlers: function(obj){
		
		// set the parent element
		if(typeof obj == 'undefined') obj = $('html');
		
		// bind events
		obj.find('a.js-action[href=#remove-recommended]').bind('click', function(event){
			EA.account.removeRecommendJob(event);
			return false;
		});
		obj.find('a.js-action[href=#add-shortlist]').bind('click', function(event){
			EA.account.addShortlistJob(event);
			return false;
		});
		obj.find('a.js-action[href=#remove-shortlist]').bind('click', function(event){
			EA.account.removeShortlistJob(event);
			return false;
		});
		obj.find('a.js-action[href=#remove-history]').bind('click', function(event){
			EA.account.removeApplication(event);
			return false;
		});
		obj.find('a.js-action[href=#remove-search]').bind('click', function(event){
			EA.account.removeSearch(event);
			return false;
		});
		
		// return the object
		return obj;
		
	},
	
	removeRecommendJob: function(event){
		
		// remove from the table
		var jobId = $(event.target).attr('rel');
		this.removeJobRow('#recommended-jobs', jobId);
		
		// call the ajax remove code here to remove from the recommended list
		
		
	},
	
	addShortlistJob: function(event){
		
		// get the table rows from the recommended table
		var jobId = $(event.target).attr('rel');
		var row1 = $('#recommended-jobs #jr-' + jobId).html();
		
		// edit the html
		EA.jobsList.destroyOverlay();
		$('#recommended-jobs #jr-' + jobId + ' a[href=#add-shortlist]').parent().html('<span class="job-added">Added to shortlist</span>');
		
		// call the ajax remove code here to add to the short list
		
		
	},
	
	removeShortlistJob: function(event){
		
		// remove from the table
		var jobId = $(event.target).attr('rel');
		this.removeJobRow('#job-shortlist', jobId);
		
		// call the ajax remove code here to remove from the short list
		
		
	},
	
	removeApplication: function(event){
		
		// remove from the table
		var jobId = $(event.target).attr('rel');
		this.removeJobRow('#application-history', jobId);
		
		// call the ajax remove code here to remove from the applications list
		
		
	},
	
	removeSearch: function(event){
		
		// remove from the table
		var searchId = $(event.target).attr('rel');
		$('#search-list #sc-' + searchId).fadeOut('normal', function(){
			$('#search-list #sc-' + searchId).remove();
		});
		
		// call the ajax remove code here to remove from the recommended list
		
		
	},
	
	removeJobRow: function(table, jobId){
		
		// remove the row from the table
		EA.jobsList.destroyOverlay();
		$(table + ' #jr-' + jobId).addClass('remove').fadeOut('normal', function(){
			$(table + ' #jr-' + jobId).next().remove();
			$(table + ' #jr-' + jobId).remove();
		});
	}
	
};


/* browser for jobs lists
-------------------------------------------------------------------------*/
EA.listBrowser = {
	
	// declare vars
	overlayHTML: '',
	flagOverLink: false,
	flagOverList: false,
	
	// initialise menu
	init: function(elem){
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers(elem);
	},
	
	bindEventHandlers: function(elem){
		
		// bind events
		$(elem + ' ul.job-categories:lt(2) li:has(ul) a').bind('mouseover', function(event){ EA.listBrowser.showOverlay(event, 'right'); });
		$(elem + ' ul.job-categories:eq(2) li:has(ul) a').bind('mouseover', function(event){ EA.listBrowser.showOverlay(event, 'left'); });
	},
	
	showOverlay: function(event, pos){
		
		// build the HTML - make sure there isn't one already
		this.destroyOverlay();
		this.overlayHTML = this.buildOverlay(event.target, pos);
		$('div.container:first').prepend(this.overlayHTML);
		
		// bind events
		$('#jobs-hover-link').bind('mouseenter', function(event){
			EA.listBrowser.flagOverLink = true;
		});
		$('#jobs-hover-link').bind('mouseleave', function(event){
			EA.listBrowser.flagOverLink = false;
			setTimeout(function(){
				EA.listBrowser.hideOverlay();
      		}, 100);
		});
		$('#jobs-hover-list').bind('mouseenter', function(event){
			EA.listBrowser.flagOverLink = true;
		});
		$('#jobs-hover-list').bind('mouseleave', function(event){
			EA.listBrowser.flagOverLink = false;
			setTimeout(function(){
				EA.listBrowser.hideOverlay();
      		}, 100);
		});
		
		// calculate positioning
		var offsetLink = $(event.target).offset();
		var offsetContainer = $('div.container:first').offset();
		var offsetX = offsetLink.left - offsetContainer.left - 10;
		var offsetY = offsetLink.top - 10;
		var linkPosX = parseInt($('#jobs-hover-link').css('left').replace('px', '')) + offsetX;
		var linkPosY = parseInt($('#jobs-hover-link').css('top').replace('px', '')) + offsetY;
		var listPosX = parseInt($('#jobs-hover-list').css('left').replace('px', '')) + offsetX;
		var listPosY = parseInt($('#jobs-hover-list').css('top').replace('px', '')) + offsetY;
		if(pos == 'left'){
			linkPosX = linkPosX - 5;
			listPosX = listPosX - 425;
		}
		
		// set positioning and styles
		$('#jobs-hover-link').css({
			left: linkPosX + 'px',
			top: linkPosY + 'px'
		}).supersleight();
		$('#jobs-hover-list').css({
			left: listPosX + 'px',
			top: listPosY + 'px'
		}).supersleight();
	},
	
	hideOverlay: function(){
		
		// only hide if not over either overlay
		if(!this.flagOverLink && !this.flagOverList){
			// destroy it
			this.destroyOverlay();
		}
	},
	
	buildOverlay: function(elem, pos){
		
		// extract html
		var title = $(elem).text();
		var href = $(elem).attr('href');
		var list = $(elem).next().html();
		
		// create the HTML
		html = $('<div id="jobs-hover-link" class="' + pos + '"><a href="' + href + '">' + title + '</a></div>' +
					'<div id="jobs-hover-list" class="' + pos + '">' +
						'<ul>' +
							list +
						'</ul>' +
						'<div></div>' +
					'</div>');
		return html;
	},
	
	destroyOverlay: function(){
		
		// remove the overlay from the node tree
		$('#jobs-hover-link').remove();
		$('#jobs-hover-list').remove();
	}
};


/* rollover layer for the jobs table
-------------------------------------------------------------------------*/
EA.jobsList = {
	
	// declare vars
	overlayHTML: '',
	flagOverRow: false,
	flagOverDesc: false,
	
	// initialise menu
	init: function(elem){
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers($(elem));
	},
	
	bindEventHandlers: function(obj){
		
		// set the parent element
		if(typeof obj == 'undefined') obj = $('html');
		
		// bind events
		obj.find('a.job-title').bind('mouseover', function(event){ EA.jobsList.showOverlay(event); });
		
		// return the object
		return obj;
	},
	
	showOverlay: function(event){
		
		// build the HTML - make sure there isn't one already
		this.destroyOverlay();
		this.overlayHTML = this.buildOverlay(event.target);
		$('div.container:first').prepend(this.overlayHTML);
		
		// bind events
		$('#job-overlay-details').bind('mouseenter', function(event){
			EA.jobsList.flagOverRow = true;
		});
		$('#job-overlay-details').bind('mouseleave', function(event){
			EA.jobsList.flagOverRow = false;
			setTimeout(function(){
				EA.jobsList.hideOverlay();
      		}, 100);
		});
		$('#job-overlay-desc').bind('mouseenter', function(event){
			EA.jobsList.flagOverDesc = true;
		});
		$('#job-overlay-desc').bind('mouseleave', function(event){
			EA.jobsList.flagOverDesc = false;
			setTimeout(function(){
				EA.jobsList.hideOverlay();
      		}, 100);
		});

		// calculate positioning for link pos
		var offsetLink = $(event.target).offset();
		var offsetContainer = $('div.container:first').offset();
		var linkOffsetX = offsetLink.left - offsetContainer.left - 17;
		var linkOffsetY = offsetLink.top - 16;
		var linkPosX = parseInt($('#job-overlay-details').css('left').replace('px', '')) + linkOffsetX;
		var linkPosY = parseInt($('#job-overlay-details').css('top').replace('px', '')) + linkOffsetY;
		
		// set positioning
		$('#job-overlay-details').css({
			left: linkPosX + 'px',
			top: linkPosY + 'px'
		}).supersleight();
		
		// and for description pos
		var offsetDescFooter = $('#job-overlay-details .details-footer').offset();
		var descOffsetX = offsetLink.left - offsetContainer.left + 138;
		var descOffsetY = offsetDescFooter.top + 10;
		var descPosX = parseInt($('#job-overlay-desc').css('left').replace('px', '')) + descOffsetX;
		var descPosY = parseInt($('#job-overlay-desc').css('top').replace('px', '')) + descOffsetY;
		
		// set positioning
		$('#job-overlay-desc').css({
			left: descPosX + 'px',
			top: descPosY + 'px'
		}).supersleight();
	},
	
	hideOverlay: function(){
		
		// only hide if not over either overlay
		if(!this.flagOverRow && !this.flagOverDesc){
			// destroy it
			this.destroyOverlay();
		}
	},
	
	buildOverlay: function(elem){
		
		// extract html
		var jobTable = $(elem).parents('table.jobs-list');
		var jobCols = jobTable.find('colgroup').html();
		var jobRow = $(elem).parents('tr.job-role');
		var jobId = jobRow.attr('id').substring(3, jobRow.attr('id').length);
		var jobDetails = jobRow.html();
		var jobLogo = jobRow.next().find('td:eq(0)').html();
		var jobDescription = jobRow.next().find('td:eq(1)').html();
		
		// if there is as image, style the p tag to accomodate it
		var styling = (jobLogo != '') ? ' style="padding-left:130px;"' : '';
		
		// create the HTML
		html = $('<div id="job-overlay-details">' +
    				'<div class="details-body">' +
						'<table class="jobs-list">' +
							'<colgroup>' + jobCols + '</colgroup>' +
							'<tbody>' +
								'<tr id="jo-' + jobId + '" class="job-role">' +	
									jobDetails +
								'</tr>' +
							'</tbody>' +
						'</table>' +
    				'</div>' +
    				'<div class="details-footer"></div>' +
    			'</div>' +
    			'<div id="job-overlay-desc">' +
    				'<div class="desc-body">' +
						jobLogo +
    					'<div' + styling + '>' + jobDescription + '</div>' +
    				'</div>' +
   					'<div class="desc-footer"></div>' +
    			'</div>');
		
		// bind click events to any buttons
		html.find('a.shortlist-add').bind('click', function(event){ EA.jobShortList.addJob(event); return false; });
		html = EA.account.bindEventHandlers(html);
		
		// return the html nodes
		return html;
	},
	
	destroyOverlay: function(){
		
		// remove the overlay from the node tree
		$('#job-overlay-details').remove();
		$('#job-overlay-desc').remove();
	}
};


/* jobs shortlist - adds reoves from list
-------------------------------------------------------------------------*/
EA.jobShortList = {
	
	// initialise menu
	init: function(){
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
	},
	
	bindEventHandlers: function(){
		
		// bind events
		$('a.shortlist-add').bind('click', function(event){ EA.jobShortList.addJob(event); return false; });
		$('a.shortlist-remove').bind('click', function(event){ EA.jobShortList.removeJob(event); return false; });
	},
	
	addJob: function(event){
		
		var flagInList = false;
		var numJobs = 0;
		
		// grab the jobId
		var jobId = $(event.target).parents('tr.job-role').attr('id');
		jobId = jobId.substring(3, jobId.length);
		
		// add to the list if it's on the page
		if($('#shortlist-box').length > 0 ){
			
			// check that the jobId isn't already in the list
			$('#shortlist-jobs li').each(function(){
				if($(this).attr('id').substring(3, $(this).attr('id').length) == jobId){
					flagInList = true;
				}
				numJobs++;
			});
			
			// if the job id wasn't found in the list
			if(!flagInList){
				
				// create the html and add to the list
				var html = this.buildJobListItem(event.target);
				$('#shortlist-jobs').prepend(html).find('li:eq(0)').hide().show('blind');
				
				// update the total
				var jobsText = (numJobs == 0) ? '(' + (numJobs + 1) + ' job)' : '(' + (numJobs + 1) + ' jobs)';
				$('#shortlist-total').text(jobsText);
				
				// update the link
				$('#jr-' + jobId + ' span.job-action').text('Added to shortlist');
				$('#jo-' + jobId + ' span.job-action').text('Added to shortlist');
			}
		}
		
		// CALL AJAX CODE HERE TO ADD TO THE SHORTLIST
		// jobId
		
		
		
	},
	
	removeJob: function(event){
		
		// extract data from html
		var numJobs = $('#shortlist-jobs li').length;
		var jobListItem = $(event.target).parents('li.shortlist-job');
		var jobId = jobListItem.attr('id').substring(3, jobListItem.attr('id').length);
		jobListItem.hide('blind', function(){ jobListItem.remove() });
		
		// update the total
		var jobsText = (numJobs == 2) ? '(' + (numJobs-1) + ' job)' : '(' + (numJobs-1) + ' jobs)';
		$('#shortlist-total').text(jobsText);
		
		
		// update the link
		var html = $('<a href="#" class="red-plus shortlist-add">Add to shortlist</a>');
		html.bind('click', function(event){ EA.jobShortList.addJob(event); return false; });
		$('#jr-' + jobId + ' span.job-action').empty().append(html);
		$('#jo-' + jobId + ' span.job-action').empty().append(html);
		
		// CALL AJAX CODE HERE TO REMOVE TO THE SHORTLIST
		// jobId
		
		
		
	},
	
	buildJobListItem: function(elem){
		
		// extract data from html
		var jobRow = $(elem).parents('tr.job-role');
		var jobId = jobRow.attr('id').substring(3, jobRow.attr('id').length);
		var jobTitle = jobRow.find('.job-title').html();
		var jobLocation = jobRow.find('.job-location').html();
		var jobCompany = jobRow.find('.job-company').html();
		var jobSalary = jobRow.find('.job-salary').html();
		
		// build html list item
		var html = $('<li id="sl-' + jobId + '" class="shortlist-job"><div>' +
								'<a class="job-title" href="#">' + jobTitle + '</a><br />' +
								jobCompany + '<br />' +
								'<strong>' + jobLocation + '</strong> - ' + jobSalary + '<br />' +
								'<a href="#" class="red shortlist-remove">(remove)</a></div>' +
							'</li>');
		
		// add event handlers
		html.find('a.shortlist-remove').bind('click', function(event){ EA.jobShortList.removeJob(event); return false; });
		
		// return the html nodes
		return html;
	}
};




/* filter options expandable lists
-------------------------------------------------------------------------*/
EA.filterOptions = {
	
	// declare vars
	slideSpeed: 0,			// animation slide speed
	
	// initialise menu
	init: function(){
		
		// add display attributes to links
		$('div.filter-options p a').attr('display', '');
		
		// shrink the lists to the first 3 options
		$('div.filter-options').each(function(){ $(this).find('ul:eq(1)').hide(); });
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
	},
	
	bindEventHandlers: function(){
		
		// bind events
		$('div.filter-options p a').bind('click', function(event){ EA.filterOptions.toggleOptions(event); return false; });
	},
	
	toggleOptions: function(event){
		
		// run the right code
		if($(event.target).attr('display') != 'on'){
			this.showOptions(event.target);
		}
		else{
			this.hideOptions(event.target);
		}
	},
	
	showOptions: function(link){
		$(link).parents('div.filter-options').find('ul:eq(1)').show('blind');
		$(link).removeClass('blue-open').addClass('blue-close')
			.attr('copy', $(link).text()).text('Close');
		$(link).attr('display', 'on');
	},
	
	hideOptions: function(link){
		$(link).parents('div.filter-options').find('ul:eq(1)').hide('blind');
		$(link).removeClass('blue-close').addClass('blue-open')
			.text($(link).attr('copy'));
		$(link).attr('display', '');
	}
	
}

/* popups
-------------------------------------------------------------------------*/

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=310,height=380,left = 640,top = 375');");
}
function popUp2(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=360,height=380,left = 640,top = 375');");
}

/* account overview tabs
-------------------------------------------------------------------------*/
$(function() {
		$("#account-tabs").tabs();
	});