/*
	Site:		SugarJobs
	File:		/scrips/sugarjobs.js
	Author:		Andrew Disley, Simplified http://simplified.co.uk/
	Version:	2009-09-27
----------------------------------------------- */

// preload navigation images:

if (document.images)
{
	var img = new Image();

	// set image urls
	img.src = "images/employer_on.gif";
	img.src = "images/employer_off.gif";
	img.src = "images/jobseeker_on.gif";
	img.src = "images/jobseeker_off.gif";
	img.src = "images/about_on.gif";
	img.src = "images/about_off.gif";
	img.src = "images/home_on.gif";
	img.src = "images/home_off.gif";
	img.src = "images/employers_on.gif";
	img.src = "images/employers_off.gif";
	img.src = "images/jobseekers_on.gif";
	img.src = "images/jobseekers_off.gif";
	img.src = "images/jobsearch_on.gif";
	img.src = "images/jobsearch_off.gif";
	img.src = "images/news_on.gif";
	img.src = "images/news_off.gif";
	img.src = "images/contact_on.gif";
	img.src = "images/contact_off.gif";
	img.src = "images/startsearch_on.gif";
	img.src = "images/startsearch_off.gif";
	img.src = "images/viewall_on.gif";
	img.src = "images/viewall_off.gif";
}

function hiLite(imgID,changeID) {

	if (document.images) {
		document.images[imgID].src = eval(changeID + ".src");
		return true;
	}
}

/*	Global Configuration
----------------------------------------------- */
var $j = jQuery;
var isIE6 = false;
var isSafari = false;

/*	Global Literal Object: Site-wide functions
----------------------------------------------- */
var Global = {

	/* Utility: Browser Tests / Specific Fixes/Hooks */
	utilBrowserTests : function() {

		/* Detect if User Agent is IE6 using object detection, apply fix for background flicker bug */
		if (typeof document.body.style.maxHeight == 'undefined') { isIE6 = true; try { document.execCommand('BackgroundImageCache', false, true); } catch(e) { } }

		/* Detect if User Agent is Safari, add class '.safari' to body */
		if ($j.browser.safari) { isSafari = true; $j('body').addClass('safari'); }

	},

	init : function() {

		var cc = this;

		cc.utilBrowserTests();

	}

};

/*	Component: CHomeSectorSearch
----------------------------------------------- */
var CHomeSectorSearch = {

	init : function() {

		var cc = this;

		$j('.csectorsearch-nav').tabs('.csectorsearch-tabs > div', {current: 'active', onClick:function(e,i)
		{
			var l = $('#homelocation').children(':first').children('a:last');
			if(i > 0)
			{
				l.attr('href', 'alllocations');
			}
			else
			{
				l.attr('href', 'allsectors');
			}
		}});

	}

};

/*	Component: CHomeClients
----------------------------------------------- */
var CHomeClients = {

	init : function() {

		var cc = this;
		/* Insert scrollable navigation */
		$j('#homeclients .scrollable').before('<a class="scrollable-nav prev"></a>').after('<a class="scrollable-nav next"></a>');
		$j('#homeclients .scrollable').scrollable({
			keyboard:false,
			size: 4
		}).circular().autoscroll({
			interval: 10000, /* time to autoscroll miliseconds (curentl 10) */
			steps: 2 /* items to scroll on autoscroll (curentl 2) */
		});

	}

};

/*	DOM Ready events
----------------------------------------------- */
$j(function() {

	Global.init();

	if ($j('#homelocation').length) {
		CHomeSectorSearch.init();
	}
	if ($j('#homeclients').length) {
		CHomeClients.init();
	}

	// remember this only runs if there are any
	if ($j('textarea.wysiwyg').length) 
	{
		$j('head').append('<link/>');
		css = $j('head').children(':last');
		css.attr({
			rel:  'stylesheet',
			type: 'text/css',
			href: 'scripts/jquery.wysiwyg.css'
		});

		$j.getScript('scripts/jquery.wysiwyg.js', function()
		{
			//wait before initialising to prevent intermittent load error
			 setTimeout(function(){ $j("textarea.wysiwyg").wysiwyg({css:'scripts/jquery.wysiwyg.inner.css'}) },250);
		});

	}


	// save job buttons
	$j('a.save_job').one('click', function(ev)
	{
		// first click saves job. second click goes to 

		jobID = $j(ev.target).parent().attr('href').match(/jobID=([1-9][0-9]*)/)[1];

		$j.getJSON('ajax/?action=save_job&jobID='+jobID, function(d)
		{
			if(d["success"] == 1)
			{
				if(d["url"])
				{
					window.location = d["url"];
				}
				else
				{
					// change image to view saved link
					$j(ev.target).attr('src','images/viewsaved_off.gif').parent().attr('href','jobseeker_savedjobs');
				}
			}
		});

		return false;
	});


	// mailto link - only fire once per page load
	$j('.email-candidate').one('click', function()
	{
		$j.get('ajax/?action=contact&userID='+$(this).attr('id').replace(/email-candidate-/,''));
	});

});



function set_row_visited(ths)
{
	// set the row of the clicked result to visited - fixes an issue in ie678

	$(ths).closest('tr').children().removeClass('searchrow1').addClass('searchrowviewed');

}



function job_action(ths)
{
	var duplicateConfirm = "Create a duplicate of this job?";
	var removeConfirm = "Remove this job?";

	var ths = $(ths);
	var tr = ths.closest('tr');
	var jobID = tr.attr('id').match(/job_row_([1-9][0-9]*)/)[1];
	var txt = ths.text(), new_text = false, action = false;

	switch(ths.attr('class'))
	{
		case 'copy':
		{
			if(confirm(duplicateConfirm))
			{
				action = 'duplicate';
			}
			break;
		}
		
		case 'delete':
		{
			if(confirm(removeConfirm))
			{
				action = 'delete'
			}
			break;
		}
	
		case 'toggle':
		{
			action = (txt == 'Active' ? 'archive' : 'activate');
			new_txt = (txt == 'Active' ? 'Archived' : 'Active');
			break;
		}
	}

	if(action)
	{
		$.getJSON('ajax/?action='+action+'_job&job='+jobID, function(d)
		{
			if(d["success"] == 1)
			{
				if(action == 'duplicate' && d["job_id"])
				{
					// duplicate
					var tr2 = tr.clone().attr('id', 'job_row_'+d['job_id']).css('opacity', 0);
					tr2.children(':eq(1)').text('Copy: ' + tr2.children(':eq(1)').text()).next().next().text('0');
					tr2.insertAfter(tr.closest('table').find('tr:eq(0)')).animate({opacity:1}, 1000);
				}
				else if(action == 'delete')
				{
					tr.animate({opacity:0}, 1000, function(){ tr.remove() });
				}
				else if(new_txt)
				{
					ths.text(new_txt);

					if(action == 'archive')
					{
						ths.closest('tr').addClass('archived');
					}
					else if(action == 'activate')
					{
						ths.closest('tr').removeClass('archived');
					}
				}
			}
		});
	}

	return false;
}


function set_start_date_today()
{
	var dt = new Date();
	$('#jobStartDay').val( leadingZero(dt.getDate()));
	$('#jobStartMonth').val( leadingZero(dt.getMonth()+1));
	$('#jobStartYear').val(dt.getFullYear());
}

function leadingZero(nr)
{
	if (nr < 10) nr = "0" + nr;
	return nr;
}
