function submit_form(form_id)
{
    if( form_id == undefined || form_id == null ) return false;
    $('form#'+form_id).submit();
    return true;
}
function load_link(href)
{
    window.location  = href;
    return true;
}

function submitCV(response_uri)
{
    window.open(response_uri,'cva','width=500,height=370');
}


jQuery().ready(function(){

	//show the print button....
	$('div#content ul.jobOptions li a.print').css('display','block');
	$('div#content ul.jobOptions li a.print').click( function()
	{
		window.print();
		return false;
	});

	//handle the 'clicks' on the jobsearch/olr tabs
	$('div#leftColumn ul.tabs li a').click(function(){

		// Active - don't do anything....
		if( $(this).attr('class').indexOf('selected')>= 0 )
		{
			return false
		}
		$('div#leftColumn form.active').animate({opacity: "0"}, 1,function()
		{
			$(this).css('display','none');

			$('div#leftColumn form.noDisplay').css('opacity','0');
			$('div#leftColumn form.noDisplay').css('display','block');
			$('div#leftColumn form.noDisplay').animate({opacity: "100"},1,function()
			{
				$(this).removeClass('noDisplay');
				$(this).addClass('active');
			});
			$(this).addClass('noDisplay');
			$(this).removeClass('active');
		});


		$('div#leftColumn ul.tabs li a.selected').addClass('pending');
		$('div#leftColumn ul.tabs li a').addClass('selected');
		$('div#leftColumn ul.tabs li a.selected.pending').removeClass('selected');
		$('div#leftColumn ul.tabs li a.pending').removeClass('pending');

		return false;
	})

    if(typeof datepicker == "function"){
        //add the date picker start date on staff match
        $("#staff_start_date").datepicker({
            duration: "slow",
            dateFormat: "dd/mm/yy"
        });
    }

    //Get the current language of the site from the language menu
    var sSiteLang = $('ul#language_nav li.sel a').attr('href').toLowerCase().replace('/','');

    // don't display the check boxes on the olr
    // use images instead.
    $('table input[@type=checkbox]').each(function(){
         $(this).css('position', 'absolute');
         $(this).css('marginTop', '-999em');
         $(this).css('marginLeft', '-999em');

     $(this).parent('td').append('<img src="/img/lang/'+sSiteLang+'/btn/checkbox-off.gif" class="checkReplace">');
    });

    //check the hidden checkbox on td click
    //change the image to match
    $('#Form_OlrShortlist td.olr_ats').click(function(){
        if( $(this).attr('class') == 'olr_ats' )
        {

            switch( $(this).children('input').attr('checked') )
            {
                case true:
                $(this).find("input").attr('checked','');
                $(this).find("img").attr('src', $(this).find("img").attr('src').replace('-on.gif','-off.gif') )
                break;

                case false:
                $(this).find("input").attr('checked','checked');
                $(this).find("img").attr('src', $(this).find("img").attr('src').replace('-off.gif','-on.gif') )
                break;
            }
            // add a class to the short listed CV
            if (!$(this).children('input').attr('checked')){
            	$(this).parent().removeClass('addedCV');
            }else {
            	$(this).parent().addClass('addedCV');
            }
        }
        else
        {
        return true;
        }
    });


	//preload all mouseover form buttons
	$('form input[@type=image]').each(function(){
		if( $(this).attr('src').indexOf('spacer.gif') < 0 )
		{
			if( $('div#preLoad').attr('id') != 'preLoad' )
			{
				$('#wrapper').after('<div style="display:none;" id="preLoad"></div>');
			}
			$('#preLoad').prepend('<img width="1" height="1" src="'+$(this).attr('src').replace('-off.gif','-on.gif')+'"/>');
		}
	})


    //swap the form submission button[s] to over on mouseover
    $('form input[@type=image], img.onOff').mouseover(function(){
        if( $(this).attr('src').indexOf('-on.gif') <= 0 )
        {
            var NewSubmit = $(this).attr('src').replace('-off.gif','-on.gif');
            $(this).attr('src',NewSubmit);
        }
    });

    $('form input[@type=image], img.onOff').mouseout(function(){
        if( $(this).attr('src').indexOf('-off.gif') <= 0 )
        {
            var NewSubmit = $(this).attr('src').replace('-on.gif','-off.gif');
            $(this).attr('src',NewSubmit);
        }
    });


    //swap the form submission button[s] to active on form submission
    $('form input[@type=image]').click(function(){

        if( $(this).attr('src').indexOf('img/spacer')> 0 )
        {
            return true;
        }

        if( $(this).attr('src').indexOf('-active.gif')> 0 ) return false;

        var NewSubmit = $(this).attr('src').replace('-on.gif','-active.gif');
        var NewSubmit = $(this).attr('src').replace('-off.gif','-active.gif');
        $(this).attr('src',NewSubmit.replace('-on.gif','-active.gif'));


        if( $(this).attr('class') == 'click2animate' && $(this).parent('a').attr('id') != 'apply_top' && $(this).parent('a').attr('id') != 'apply_bot' )
        {
            setTimeout('load_link("'+$(this).parent('a').attr('href')+'")',1);
            return false;
        }
        else
        {
            setTimeout('submit_form("'+$(this).parents().filter('form').attr('id')+'")',1);
            return false;
        }

    });



    //Apply button actions....
    $('#JobSearch_ApplyTop,#JobSearch_ApplyBottom,.scv, .bar_submit_cv').click(function(){
        submitCV($(this).attr('href'));
        return false;
    });


	// Search Result page - style the whole row when mouseover.
	serpHover();

    //Home Page
    toggleFeaturedJobs();

    //Contact Us Office selected
    contactUsSelectedCountry();

    //SCVS page
    $("#olrcandidatesBar_scvs_Container a, div.scvsContainer ul.scvs_link_list li a").click(function(){
		submitCV($(this).attr('href'));
		return false;
	});

	//toggle expanding text
	showHideText();

	//insert the label name into the input field. i.e. OLR email
	autoFill('#Form_OlrLogin_Email_Label', '#Form_OlrLogin_Email');
	//insert the error message into the input field. i.e. OLR Keyword
	autoFill('div#Form_Olr_ErrorMessages ul li', '#Form_Olr_Keywords');
});

function restorImg()
{

    $('img').each(function(){
        if( $(this).attr('restoreimage') == 'yes' )
        {
            $(this).attr('src',$(this).attr('src').replace('-active.gif','-on.gif'));
            $(this).attr('restoreimage','');
        }
    })
}

function GoToMap(sLocation)
{
	//get the current url
	if( window.location.href.indexOf('&') < 0 )
	{
		var sUrl = window.location.href+sLocation+'/';
	}
	else
	{
		var sUrl = window.location.href+'&option='+sLocation;
	}
	window.location = sUrl;
}
/*
EXAMPLE GOOGLE MAP ZOOM
function ZoomMap(sLocation)
{
        for (key in aOfficeLongLats)
        {
            if( key == sLocation )
            {
                var sOffice = key;
                aLatLong = aOfficeLongLats[key].split(',')
                var long = parseFloat(aLatLong[0]);
                var lat = parseFloat(aLatLong[1]);
            }
        }

        map.panTo(new GLatLng(long,lat));
        if( map.getZoom() == iZoom )
        {
            window.setTimeout(function() {
                  map.setCenter(new GLatLng(long,lat), 15);
            }, 1000);
        }

        var aClassNames = $('h1.location').attr('class').split(' ');
        for (i=0; i<aClassNames.length; i++)
        {
            if( aClassNames[i] != sOffice && aClassNames[i] != 'location' )
            {
                $('h1.location').removeClass(''+aClassNames[i]+'');
            }
        }
        $('h1.location').addClass(''+sOffice+'');
}
*/

// Sector tabs
function showSectorList(){
	$('div#filterBySector').css('borderColor','#CCCCCC');
	$('div#filterBySector ul#FeaturedJobsListNav').removeClass('hideall');
}
function hideSectorList(){
	$('div#filterBySector').css('borderColor','#FFFFFF');
	$('ul#FeaturedJobsListNav').addClass('hideall');
}
function toggleFeaturedJobs(){
	// Featured Job - filter by sector
	$('div#filterBySector').css('visibility','visible');
	$('div#filterBySector').click(function(){
		if ($('ul#FeaturedJobsListNav').attr('class').indexOf('hideall') >= 0) {
			showSectorList();
		}else {
			hideSectorList();
		}
	});

	//toggle featuredJobList
	$('div.colRight ul#FeaturedJobsListNav li a').click(function () {
		$('div.colRight ul.featuredJobsList:visible').hide();
		$('div.colRight ul li.on').removeClass('on');
		if ($(this).attr('id')){
			var activeTab = $(this).attr('id');
			$(this).parent().addClass('on');
			var active_li  = activeTab.replace('FeaturedJobs_','FeaturedJobsList_');
			var active_title  = activeTab + '_title';
			$('ul#' + active_li).show();
			hideSectorList();
			$('div.colRight div.FeaturedJobTitle span.cnr:visible').addClass('hideall');
			$('span.' + active_title).removeClass('hideall');
		}
		return false;
	});
}

function serpHover() {
	$(".tabularResults tbody tr").hover(function(){
		$(this).addClass('activeRowClick');
	}, function(){
		$(this).removeClass('activeRowClick');
	});

	//job search
	$("#JobSearchResults tbody tr").click(function(){
		var sTheLink = '/' + $(this).find(".JobSearch_job_title a").attr('href');
		window.location = sTheLink;
	});

	//staff search open CV window
    $('#Olr_SearchResults td.olr_jt').click(function(){
        window.open( '/' + $(this).children('a').attr('href'),'cand_cv', 'scrollbars=yes,width=700, height=600,resizable=yes');
        return false;
    });
}

function contactUsSelectedCountry() {
    //office selector: add class to the country of its city.
    $('div.colRight ul.pageMenu li ul.office li').each(function(){
        $("div.colRight ul.pageMenu li ul.office li.sel").parent().parent().addClass('selCountry');
    });
}


function showHideText() {
	$('ul.expandContent li a').css('display','inline');
	$('ul.expandContent li').css('cursor','pointer');
	// don't show the hide link
	$('ul.expandContent li a').find('span:eq(1)').css('display','none');

	$('ul.expandContent li').toggle(function(){
		var sMoreLink = $(this).children('a').find('span:eq(0)');
		var sHideLink = $(this).children('a').find('span:eq(1)');

		$(this).children('div').slideDown('fast');
		sMoreLink.hide();
		sHideLink.show();
		$(this).addClass('open');
		return false;
	}, function(){
		var sMoreLink = $(this).children('a').find('span:eq(0)');
		var sHideLink = $(this).children('a').find('span:eq(1)');

		$(this).children('div').slideUp('fast');
		sMoreLink.show();
		sHideLink.hide();
		$(this).removeClass();
		return false;
	})
}

// auto fill the form field
function autoFill(labelId, inputId){
	var v = $(labelId).text();
	$(inputId).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).val(v);
		}
	});

}
