$(document).ready(function() {

	// hide home page text
	$('.HomePageText').hide();

	var HomeTextTabShow = true;
	$('.HomeTextTab').click(function(){
		if(HomeTextTabShow){
			$('.HomePageText').slideDown();
			HomeTextTabShow = false;
			$('.HomeTextTab').text("hide");
		}else{
			$('.HomePageText').slideUp();
			HomeTextTabShow = true;
			$('.HomeTextTab').text("read more");
		}
	});

	// stretch background of small pages (we don't use body because we want to keep it free from bg colour so spaw in admin doesn't look like shit)
	function resizeViewport() {
		if ($('#background').height() < $(window).height()) {
			$('#background').height($(window).height())
		}
	}

	resizeViewport();

	// hook on window change
	$(window).resize(function() {
		resizeViewport();
	});

	// slimbox
	if ($('a[rev^="lightbox"]').length > 0) {
		$('a[rev^="lightbox"]').slimbox({}, null, function(el) {
			return (this == el) || ((this.rev.length > 8) && (this.rev == el.rev));
		});
	}

	// external links
	$('a.external').click(function() {
		window.open($(this).attr('href'), 'blank');

		return false;
	});

	// back links
	$('a.back').click(function() {
		history.go(-1);

		return false;
	});

	// style select

	$('form.overlaid label select').styledSelect({});

	$('form.overlaid label:has(select)').css( "position" , "relative" );
	$('form.overlaid label:has(select)').css( "z-index" , "250" );
	$('form.overlaid label:has(textarea)').css( "margin-top" , "3px" );


	// copy input title content into value only if value is empty (allows for fields to be pre-populated e.g. from php)
	$('input[title], textarea[title]').each(function() {
		var el = $(this);

		function sticky() {
			if (el.attr('value').length == 0) {
				el.attr('value', el.attr('title'));
			}
		}

		// reset on mouse out
		$(this).blur(function() {
			sticky();
		});

		// clear on mouse in
		$(this).focus(function() {
			if (el.attr('title') == el.attr('value')) {
				el.attr('value', '');
			}
		});

		// initialise
		sticky();
	});

	// clean junk on form submit
	$('form').submit(function() {
		$(this).find('input[title], textarea[title]').each(function() {
			if ($(this).attr('title') == $(this).attr('value')) $(this).attr('value', '');
		});
	});

	// google map
	if ($('#map').length > 0) {
		load();
	}

	//Tabs
	if ($('.tabs').length > 0) {
		$('.tabs').tabs();
	}

	if ($('.solicitor-tabs').length > 0) {
		$('.solicitor-tabs').tabs();
	}

		$('.solicitor-tabs a').click( function(event){

			var img = $(event.target).attr("href").replace("#","");
			img = "url("+url+"/images/"+img+".gif)";
			$('div.solicitor-box div.bottom').css("background-image",img);

		});

		$('.solicitor-tabs li').click( function(event){

			$(event.target).find("a").trigger('click');

		});

	if ($('.tip-of-the-day-tabs').length > 0) {
		$('.tip-of-the-day-tabs').tabs();
	}

		$('.tip-of-the-day-tabs a').click( function(event){

			var img = $(event.target).attr("href").replace("#","");
			img = "url("+url+"/images/midSect"+img+".jpg)";
			$('div.tip-of-the-day-box div.bottom').css("background-image",img);

		});

		$('.tip-of-the-day-tabs li').click( function(event){

			$(event.target).find("a").trigger('click');

		});


	if ($('.legal-exellence-tabs').length > 0) {
		$('.legal-exellence-tabs').tabs();
	}

		$('.legal-exellence-tabs a').click( function(event){

			var img = $(event.target).attr("href").replace("#","");
			img = "url("+url+"/images/"+img+".jpg)";
			$('div.legal-exellence-box div.bottom').css("background-image",img);

		});

		$('.legal-exellence-tabs li').click( function(event){

			$(event.target).find("a").trigger('click');

		});

	// dialog
	$('#dialog').dialog({
		title: $(this).find('#dialog .title:first'),
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
				if (delayDialog) $(delayDialog).dialog('open');
		}
	});

	// remove unwanted jQuery styles
	$('#content .ui-corner-all').removeClass('ui-corner-all');
	// remove old marker

	// overlaid form effect
	$('form.overlaid input, form.overlaid textarea').focus(function() {
		$(this).parent().addClass('active');
	});

	// cleanup
	$('form.overlaid input, form.overlaid textarea').blur(function() {
		$('form.overlaid label.active').removeClass('active');
	});

	// PNG fix
	jQuery.ifixpng(url + 'images/blank.gif');
	jQuery('img[src$=.png], .bgPng').ifixpng();


	//cycle quotes


	if($('.quotes') && $('.quotes li').length > 1){

		$('.quotes li').css("display","none");
		showQuote(0);
	}


	// tooltips

	$(".funding a").simpletip({
		fixed:true,
		position: [-50, 12],
		content: $(".funding .tooltip").html()
	});



	//flash


	$('#featureFlash').flash({
	    src: url+'media/element.swf',
	    width: 220,
	    height: 135
	});
});


//functions

	function showQuote(index){

		if( $('.quotes li:visible').length == 0 ){
			$('.quotes li:eq('+index+')').css("display","block");
			setTimeout("showQuote("+(index+1)+")",10000);
		}else{

			if( $('.quotes li').length <= index ){
				index = 0;
			}

			$('.quotes li:visible').fadeOut(500, function(){

						$('.quotes li:eq('+index+')').show("drop", { direction: "right" }, 3000);
						setTimeout("showQuote("+(index+1)+")",10000);

			});

		}

	}