// JavaScript Document

$(document).ready(function(){
	//doing our auto-init stuff
	$('#content').append('<div class="infowindow"><div class="top"></div><div class="corner"></div><div class="content"></div><div class="bottom"></div></div>');

	$(document).pngFix();


	$('#search-results').hide();
	
	$('#search-results strong').each(function(){
		$('#search-results').show();
	})

	$('.map').each(function(){
		$('.locationsList A').mouseover(function(){
			$('#'+$(this).attr('id').replace('location','point')).mouseover();
		});
	})

	$('.map .point').each(function(){

		var xy=$(this).children('SMALL').html().split(',');
		xy[1]=(xy[1]*1)-10;
		$(this).css('top',xy[1]+'px').css('left',xy[0]+'px').css('display','block');
		$('.map').click(function(){
			$('.infowindow').hide();
			$('.locationInformation').hide();
		});

		$(this).mouseover(function(e){
			$('.infowindow').hide();
			var content=$(this).children('ADDRESS').html();
			var o=$(this).offset();
			//$('.infowindow').html(content).css('top',e.pageY-36+'px').css('left',e.pageX-156+'px').fadeIn();
			$('.infowindow').css('top',o.top-26+'px').css('left',o.left-146+'px').show().children('.content').html(content);
			e.preventDefault();
			e.stopPropagation();
		});
		$(this).hover(
		function(){
			//$('.locationInformation').html($(this).children('SPAN').html());
		},
		function(){
			//$('.locationInformation').html('');
		}

		);
	});
	
	$('.map').each(function(){
		$('.locationInformation').hide();
	})

	$('#s').each(function(){
		$(this).autotip({'text':'Your Postcode'});
	})

	$('#locate-centre FORM .btn-go').click(function(){
		if($('#s').val()=='Your Postcode') $('#s').val('');
		$('#locate-centre FORM').submit();
	})

	$('.registerForm .btn-go').click(function(){
		$('.registerForm FORM').submit();
	})

	$('.search-block-top FORM A').click(function(){
		$('#search-results').html('<img src="images/ajax-loader.gif" alt=""/>');
		$.post('system.ajax.php',{postcode:$('.search-block-top FORM #postcode').val()},function(data){
			$('#search-results').html(data).show('slow');
		});
	});

	$('.search-block-top FORM #postcode').each(function(){
		if($(this).val()!=''){
			$('.search-block-top FORM A').click();
		}
		else{
			$(this).autotip({'text':'your postcode e.g. LE3 9QP'});
		}
	});
	
	$('A[href^=http://]').attr('target','_blank');

	$('#registration-form #userLookingFor').click(function(){
		if($(this).val()!="1"&&$(this).val()!=""){
			$('#requirments').show('fast');
		}
		else{
			$('#requirments').hide('fast');
		}
	});
	
	$('#registration-form #userLookingFor').each(function(){$(this).click()})


	$('#registration-form #userSource').click(function(){
		if($(this).val().indexOf('friend')!=-1||$(this).val().indexOf('Other Website')!=-1){
			if($(this).val().indexOf('friend')!=-1){
				$('#source label').html('If you were referred by a friend please state their name here');
			}
			if($(this).val().indexOf('Other Website')!=-1){
				$('#source label').html('If you heard about us via another other website please specify which here');
			}
			$('#source').show('fast');
		}
		else{
			$('#source').hide('fast');
		}
	});
	
	$('#registration-form #userSource').each(function(){$(this).click()})


	//for admin

	$('#tbladmin input.locationEnabled').change(function(){
		$.post('enable.ajax.php','locationID='+$(this).val()+'&status='+($(this).attr('checked')?'1':'0'));
	});
	
	$('#tbladmin input.vacancyEnabled').change(function(){
		$.post('enable.ajax.php','vacancyID='+$(this).val()+'&status='+($(this).attr('checked')?'1':'0'));
	});
	
	if($('.admin').size()>0){
		$('.admin INPUT.date').each(function(){$(this).datepicker({dateFormat: 'dd/mm/yy',yearRange:'-10:+10'})});
	}
	else{
		$('INPUT.date').each(function(){$(this).datepicker({dateFormat: 'dd/mm/yy'})});
	}
	

		//handle map functionality during node editing
	$('.admin .map').each(function(){
		place_point($('#locationXY').val().split(','));		
	}).click(function(e){
		base=$(this).offset();
		var xy=(e.pageX-base.left)+','+(e.pageY-base.top);
		$('#locationXY').val(xy);
		place_point(xy.split(','));
	});

	
	$('.admin INPUT[value=Remove]').click(function(){
		if(confirm('Are you sure?')){
			$(this.form).attr('action',$(this.form).attr('action').replace(/(add|edit)/,'del'));
			return true;
		}
		else{
			return false;
		}
	})
	
	$('.vacancies A').click(function(){$('DIV',$(this).parent()).toggle('slow')})
	$('#homepage-banner ul').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '60px'
	});


});

function place_point(xy){
	$('.map .point').remove();
	$('.map').append('<div class="point"></div>');
	$('.map .point').each(function(){
//		xy[0]-=12;
//		xy[1]-=12;
		$(this).css('top',xy[1]+'px').css('left',xy[0]+'px');
	});
};
