var IE,IE6,IE7,IE8;
$(document).ready( function() {

	/** browser detection **/
	IE = jQuery.browser.msie;
	IE6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6);
	IE7 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 7);
	IE8 = (jQuery.browser.msie && parseInt(jQuery.browser.version) == 8);

	/** toolbar : expand/collapse **/
	$('#toolbar a.toolbarctrl').click(function() {
		if( $(this).hasClass('expand') ) {
			$('#toolbar, #toolbarspace').animate( { 'margin-top': '0px' }, 400 );
			$(this).removeClass('expand').addClass('collapse');
		} else {
			$('#toolbar, #toolbarspace').animate( { 'margin-top': '-65px' }, 400 );
			$(this).removeClass('collapse').addClass('expand');
		}
		return false;
	});


	/** fix IE7 for missing selector **/
	if( IE ) {
		$("#subnav ul li:last-child").addClass("last-child");
		$(".infolist ul li:last-child").addClass("last-child");
	}

	if( IE8 ) {
		$("#calendartitle a.next").css('margin-top', '0px');
	}

	/** expand/collapse info list data **/
	$(".infolist ul li h3").click( function() {
		$(this).parent().toggleClass('open');
	});

	/** tabs **/
	$("ul.tabset li").click(function() {
		$(this).siblings().removeClass('active').end().parent().siblings('.tabcontent').removeClass('active');
		$(this).addClass('active').parent().siblings('.tabcontent').eq( $(this).index() ).addClass('active');
		return false;
	});


	/** form fields : default text **/
	$('input[title], textarea[title]').live('focus', function() {
		if( $(this).val() == $(this).attr('title') ) {
			$(this).val('');
			$(this).css('color', '#3c3c3c');
		}
	}).live('blur', function() {
		if( $(this).val() == '' ) {
			$(this).val($(this).attr('title'));
			$(this).css('color', '#aaa');
		}
	}).each( function() {
		if( $(this).val() == $(this).attr('title') ) {
			$(this).css('color', '#aaa');
		}
	});

	$("form").submit( function() {
		$(this).find('input[title], textarea[title]').each( function() {
			if( $(this).val() == $(this).attr('title') ) $(this).val('');
		});
		return true;
	});

	/** main nav : dropdowns **/
	$('ul#nav > li').hover(function() {
		$(this).find('a:first').addClass('hover');
		$(this).find('.dropdown').show();
	},function() {
		$(this).find('a:first').removeClass('hover');
		$(this).find('.dropdown').hide();
	});


	/** homepage : main banner **/
	$("#homebanner ul#carouselnav li").click(function() {
		clearTimeout(homeBannerTO);
		$("#homebanner #carousel ul li").fadeOut('slow').eq( $(this).index() ).fadeIn('slow');
		$(this).siblings('li').removeClass('active').end().addClass('active');
		homeBannerTO =  setTimeout('homeBannerNext()', 9000);
		return false;
	});

	var homeBannerTO =  setTimeout('homeBannerNext()', 9000);


	/** content page : carousels **/
	/** TODO **/


	/** Virtual Tour **/
	$("a.virtualtour").prettyPhoto({ theme: 'dark_rounded', slideshow: 5000, autoplay_slideshow: true });
	$("a#launchVirtualTour").click( function() {
		$(this).siblings('ul').find('a').eq(0).click();
		return false;
	});

	/** attach datepicker **/
	$('input.dateselect').datepicker(); 

	/** format phone numbers **/
	$("input[name*='phone']").live('blur', wchFixPhone);

	/**
	* Classes & Programs 
	**/
	$("div.classchecks input").click( function() {
		$(this).siblings('span.classicon').toggleClass('off', !this.checked);
		$("li.category" + $(this).val()).toggle(this.checked);
	});

	$('#paymentmethod').change( function() {
		$('#creditcard, #check').hide();
		$('#' + this.value).show();
	});

	$("#ship_same").click( function() {
		$("#billfields").toggle(!this.checked);
	});

	/**
	* Find a Physician 
	**/
	$("a.videolink").prettyPhoto({ theme: 'dark_rounded' });


	/**
	* Careers : Application
	**/
	$("ul.tabs.live li").click(function() {
		$(this).siblings().removeClass('active').end().parent().siblings('.tabcontent').removeClass('active');
		$(this).addClass('active').parent().siblings('.tabcontent').eq( $(this).index() ).addClass('active');
		return false;
	});


});


function homeBannerNext() {
	if( $("#homebanner ul#carouselnav li.active").next('li').length ) 
		$("#homebanner ul#carouselnav li.active").next('li').click();
	else 
		$("#homebanner ul#carouselnav li").eq(0).click();
}


function wchFixPhone() {

	var phone = this.value;
	var newphone;
	phone = phone.replace(/[^0-9]+/g, ''); // get rid of everything that is not a number

	switch(phone.length) { 
		case 7: 
			newphone = 'xxx-' + phone.substring(0,3) + '-' + phone.substring(3); break;
		case 10: 
			newphone = phone.substring(0,3) + '-' + phone.substring(3,6) + '-' + phone.substring(6); break;
		case 11:  // an extre digit, must be the country code, right?
			newphone = phone.substring(1,4) + '-' + phone.substring(4,7) + '-' + phone.substring(7); break;
		default:  // if not, blank it out and replace it with a format example.
			newphone = (phone.length > 1) ? 'xxx-xxx-xxxx' : '';break;
	} 

	this.value = newphone;
}

