$(document).ready(function(){



//GALLERY PAGE
	$('body.body_photos').addClass("gallery");
	var thumbs = $('body.gallery div.content div.text-bar div.text a img').clone();
	$('body.gallery div.sidebar, body.gallery div.content div.text-bar div.text').empty();
	$('body.gallery div.sidebar').prepend('<div id="scroll_wrapper"></div>');
	//$('body.gallery div#middle_right').prepend(thumbs);
	jQuery.each(thumbs, function() {
		var href = $(this).attr('src');
		$(this).parent().attr('rel',href);
		var parts1 = href.split('wp-content');
		var parts2 = parts1[1].split('-');
		var thumb_url = parts1[0] + "wp-content" + parts2[0];
		thumb_url = thumb_url.replace(".jpg","");
		thumb_url += '-112x112.jpg';
		$(this).attr('src',thumb_url).attr('width','').attr('height','').attr('rel',href);	
		$('body.gallery div.sidebar div#scroll_wrapper').prepend($(this));
	});//close each

	var thumbs2 = $('body.gallery div.sidebar div#scroll_wrapper img');
	jQuery.each(thumbs2, function() {
		$(this).wrap('<a href="' + $(this).attr('rel') + '"></a>');
	});

	$('body.gallery div.sidebar div#scroll_wrapper a').click(function() {
		var target = $(this).attr("href");
		var caption = $(this).children().attr("alt");
		$('div.content div.text-bar div.text').empty().prepend('<div class="photo"><img src="' + target + '" /><p>' + caption + '</p></div>');
		var imageWidth = $('div.text-bar div.text img').width();
		$('div.text-bar div.text').css('width',imageWidth + 12);
		return false;
	});

	$('body.gallery div.sidebar div#scroll_wrapper a').reverseOrder();

	$('body.gallery div.content div.text-bar div.text').empty().prepend('<div class="photo"><img src="' + $('div.sidebar div#scroll_wrapper a img:first').attr('rel') + '" /><p>' + $('div.sidebar div#scroll_wrapper a img:first').attr('alt') + '</p></div>');


	$('body.gallery div.sidebar div#scroll_wrapper').jScrollPane();
	$('body.gallery div.sidebar div#scroll_wrapper a:even').css('margin','3px 6px');





	

//LAYOUT
$('div.sidebar-bg div:first').addClass("first");
$('div.sidebar-bg div:last').prev().addClass("last");

$('div.text div.wp-caption').css('width','auto').addClass('photo');
$('body:not(.body_photos) div.photo p').each(function() {
	var caption = $(this).html();
	$(this).html(caption + ' <a href="photos.php">See more photos</a>.');
})

$('div.courses div:first').addClass('first');
$('div.courses div:last').prev().addClass('last');

$('body.body_case_studies .text ul.links li:last').addClass('last');

$('.studies .right ul.links li').filter(function (index) {
                  return index > 2;
                }).hide();

$('div.news-bg, div.post-bg').biggerlink();

$('body.body_about_us .text p:first, body.body_about_osteopathy .text p:first').wrap('<div class="first-p"></div>');



//HOVERS
$('div.news div').hover(function() {
	$(this).addClass("dark");
}, function() {
	$(this).removeClass("dark");
});

$('div.courses div div').hover(function() {
	$(this).addClass("dark");
}, function() {
	$(this).removeClass("dark");
});

//SORT FORMS

$('#location_select').selectedIndex = -1;
$('#course_select').selectedIndex = -1;
$('#sequence_select').selectedIndex = -1;



var locationSort = "";
var sequenceSort = "";
var courseSort = "";

$('#location_select').change(function() {

	$.get('ajax.courses.php', {location: $('#location_select').attr('value'), course: $('#course_select').attr('value'), sequence: $('#sequence_select').attr('value')} ,
	function(data) {
		$('div.courses div.post').remove();
		$('div.courses h2').after(data);
		$('div.courses div:last').prev().addClass('last');
		$('div.post-bg').biggerlink();
	}
);

})

$('#course_select').change(function() {

	$.get('ajax.courses.php', {location: $('#location_select').attr('value'), course: $('#course_select').attr('value'), sequence: $('#sequence_select').attr('value')} ,
	function(data) {
		$('div.courses div.post').remove();
		$('div.courses h2').after(data);
		$('div.courses div:last').prev().addClass('last');
		$('div.post-bg').biggerlink();
	}
);

})

$('#sequence_select').change(function() {

	$.get('ajax.courses.php', {location: $('#location_select').attr('value'), course: $('#course_select').attr('value'), sequence: $('#sequence_select').attr('value')},
	function(data) {
		$('div.courses div.post').remove();
		$('div.courses h2').after(data);
		$('div.courses div').prev().removeClass('last');
		$('div.courses div:last').prev().addClass('last');
		$('div.post-bg').biggerlink();
	} );

})

//blur links
$('a').click(function() {
	this.blur();
});

//WHERE R U
$('body.body_courses li.cour, body.body_course li.cour, body.body_events li.event, body.body_case li.case, body.body_contact li.contact, body.body_about_us li.about, body.body_about_osteopathy li.ost, body.body_case_studies li.case, body.body_course_information li.cour').addClass('active');



	$('div.wp-caption img').each(function() {
		var imgWidth = $(this).width();
		$(this).parent().css('width',imgWidth + "px");
	});



//POST PAGE
$('a.show_more').click(function() {

	var post = $(this).parent();
	var post_children = post.children();
	post_children.filter('.second_part').show().css('display','inline');
	post_children.filter('.ellipses').hide();
	post_children.filter('a.show_more').hide();
	post_children.filter('a.show_less').show();
	return false;

})

$('a.show_less').click(function() {

	var post = $(this).parent();
	var post_children = post.children();
	post_children.filter('.second_part').hide();
	post_children.filter('.ellipses').show();
	post_children.filter('a.show_more').show();
	post_children.filter('a.show_less').hide();
	return false;

})


//MAPS
load();
var biggest = 0;
$('.practitioner_text_list div div').each(function() {
	if ($(this).height() > biggest) {
		biggest = $(this).height();
	}
})

$('.practitioner_text_list div div').each(function() {
	$(this).css('height',biggest);
})

var biggest2 = 0;
$('.practitioner_text_list2 div div div').each(function() {
	if ($(this).height() > biggest2) {
		biggest2 = $(this).height();
	}
})

$('.practitioner_text_list2 div div div').each(function() {
	$(this).css('height',biggest2);
})


var content2 = $('.content2').html();
if (trim(content2) == "" || trim(content2) == "<br>" || trim(content2) == "<br />") {
	$('.content2').remove();
}

$('div.na div, div.europe div').hide();


$('#toggle_america').click(function() {
	$('div.na div').toggle();
	$('div.europe div').hide();
	$('div.na div div').hide();	
	return false;
})

$('#toggle_europe').click(function() {
	$('div.europe div').toggle();
	$('div.na div').hide();
	$('div.europe div div').hide();	
	return false;
})

$('div.na div h3 span a.toggle, div.europe div h3 span a.toggle').click(function() {
	$('div.na div div, div.europe div div').hide();
	$(this).parent().parent().next().toggle().children().toggle();
	return false;
})



$('#address').focus(function() {
	$(this).val("");
})



});//close onReady



function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
