/**** Add application wide javascripts below this point  ******/
var mapcount = 0;
$(window).load(function(){	
	init();	
	form_validate();
	$('.anti_bot').hide();
});

function init(){
	title_change();
	inline_page_load();
}
function form_validate(){
	$('#contact_form').submit(function(){
		$('#contact_form .errors').html('');
		$('#contact_form input, #contact_form textarea').removeClass('error');
		var return_me = true;
		$(this).find('input,textarea,select').each(function(){
			var ival = $(this).val();		
			if(ival.length == 0 && this.id != "anti_bot"){
				return_me = false;
				$(this).addClass('error');
			}else if(ival.length > 0 && this.id == "anti_bot"){
			  return_me = false;
			}			
		});
		if(!return_me) $('#contact_form .errors').html('<span>Please fill in all fields</span>');
		return return_me;
	});
}
function page_images(pgid,url){
	var pageid = '#'+pgid;
	if(!url) $(pageid).css('background', "url('/images/page-backgrounds/"+pgid+".jpg') no-repeat top left");
	else $(pageid).css('background', "url('"+url+"') no-repeat top left");
}
function title_change(){
	$('.navigation a, .subs a').click(function(){
		document.title = $(this).text() + ' - Abstract';		
	});
	$('.controls a').click(function(){
		document.title = 'Abstract';
	});
}

function inline_page_load(){
	$('.subs a[rel=sibling]').click(function(){
		var pg = $(this).attr('href') +"?inline=y";
		var nvpg = $(this).attr('href').replace('http://','').replace('www.','').replace('abstract','').replace('healthcare','').replace('.tbldigital', '').replace('.co.uk','').replace('.com', '');
		nvpg = nvpg.substring(1, nvpg.indexOf('/',1));
		divid = $(this).parent().parent().parent().parent().parent().parent().parent().attr('id');
		$('#'+divid+ ' .copy').css('background', "url('/images/cms/indicator.gif') no-repeat 300px 100px");
		$('#'+divid+ ' .copy *').fadeTo('fast', '0.2');
		$('#'+divid).load(pg, function(){
			$('#'+divid+ ' .copy').css('background-image', "none");
			page_height();
			init();
			//remove the trigger class, change the href & on click redirect;
			$('#page_navigation .active a').attr('href', nvpg).removeClass('trigger').click(function(){window.location=nvpg;});			
		});
		return false;
	});
}

function page_height(){
	var pid = $('body').attr('id').replace('body_', '');
	var max_h=0;
	$('#'+pid).each(function(){
		var ph = $(this).outerHeight();
		if(ph > max_h) max_h = ph;
	});
	$('#pages').css('height', max_h+'px');	
}


/** GOOGLE MAPS **/
function show_address(googlemap, geocoder, address) {
	
	if(geocoder){
		geocoder.getLatLng(address,
											function(point) {
												if (!point) alert(address + " not found");
												else {
													googlemap.setCenter(point, 15);
													var marker = new GMarker(point);
													googlemap.addOverlay(marker);
													googlemap.addControl(new GSmallZoomControl());
												}
											});
	}
	mapcount++;
}



