$(function(){

	$('body .header .nav ul.primary > li').hover(function(){
		var li = $(this);
		li.parent().find('.selected').removeClass('selected').css({
			backgroundColor: 'transparent'
		});

		li.addClass('selected').css({
			backgroundColor: '#282828'
		});
	});

	$('a.external').click(function($e){
		$e.preventDefault();
		window.open(this.href);
	});

	switch ($('body').attr('id')) {

		case 'home':

			var slideshow = $('div.slideshow');

			slideshow.fadeIn(300);

			slideshow.find('h2,h3,h4,h5,h6').each(function(i, element){

				var title     = $(element);
				var text_parts  = title.text().split(' ');
				var first_parts = [];
				var half_words  = text_parts.length / 2;

				for(var c = 1; c <= half_words; c++) {
					first_parts.push(text_parts.shift());
				}

				title.html(
					'<span class="lead">'     +
						first_parts.join(' ') +
					'</span>'                 +
					text_parts.join(' ')
				);

			});

			slideshow.find('li').each(function(i, element){

				var timeout = 7000 + (i * 7000);
				var slide   = $(element);
				var next    = slide.next();
				var heading = slide.find('h2,h3,h4,h5,h6');

				setTimeout(function(){
					heading.animate({left: '100px'}, 5000, null, function(){
						if (next.length && $.browser.msie) {
							heading.animate({opacity: 0}, 2000);
						}
					});
				}, timeout - 7000);

				if (next.length) {
					setTimeout(function(){
						slide.fadeOut(1000);
					}, timeout - 500);
				}
			});

			break;

		case 'office_tour':

			$('div.slideshow, #training_center .slideshow').inkshow({
				'startFadedOut' : false

			}, {
				// '↕ Down'  : 'scrollSlidesDown',
				// '↕ Up'    : 'scrollSlidesUp'
			});

			break;

		case 'specialists':

			$('ul.specialists').css('overflow', 'hidden').jScrollPane();

			break;

		case 'staff':

			$('ul.employees').css('overflow', 'hidden').jScrollPane();

			break;

		case 'schedule_appointment':

			var existing        = $('input.toggle_existing');
			var toggle_existing = function() {
				if (existing.attr('checked')) {
					var show = $('form .existing_patient_options');
					var hide = $('form .new_patient_options');
				} else {
					var show = $('form .new_patient_options');
					var hide = $('form .existing_patient_options');
				}

				hide.fadeOut(500, function(){

					hide.css({
						display:    'none'
					});


					show.fadeIn(1000);
				});
			};

			existing.click(toggle_existing);
			toggle_existing();

			break;

		case 'contact':

			var directions = $('.directions');

			directions.css({
				width: '550px',
				height: '300px',
				margin: 'auto',
				marginBottom: '3em',
				background: 'url(/user/styles/default/images/ajax-loader.gif) no-repeat center center'
			}).empty();

			var geocoder  = new google.maps.Geocoder();

			geocoder.geocode({
				address: $('address .address').text() + ' '  +
						 $('address .city').text()    + ', ' +
						 $('address .state').text()
			}, function(results, status){
				var map = new google.maps.Map(directions[0], {
					zoom: 13,
					center: results[0].geometry.location,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				});

				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location
				});

			});

			break;

		case 'testimonials':
			break;

		case 'blog':
			flowplayer(
				'player',
				'/user/lib/flowplayer/flowplayer-3.2.7.swf'
			);			
			break;
	}
});

