// For toggling input boxes
jQuery.fn.toggleVal=function(focusClass){this.each(function(){$(this).focus(function()
{if($(this).val()==this.defaultValue){$(this).val("")}if(focusClass)
{$(this).addClass(focusClass)}}).blur(function(){if($(this).val()=="")
{$(this).val(this.defaultValue)}if(focusClass){$(this).removeClass(focusClass)}})})}

$(document).ready(function(){

	// Toggle the value of an input or textarea on focus
	$(".toggle").toggleVal("active");

	// Initialise the slider by hiding all the child divs
	$('div.jslider> div.slide_content').hide();
		
	//Toggle the divs
	$('div.jslider>h3.slide_header').toggle (function() {
		$(this).prev('.slide_up').css('display', 'block'); 
		$(this).prev().slideDown('fast');
	}, function() {
		$(this).prev().slideUp('fast');
	});

	// Remove last navigation separator
	$("#nav li:last-child img").remove();
	
	$("#nav .sub-menu li img").remove();
	
	$("#content_right").css('height', $("content_left").height() + 'px');
		
	// Checking for any input changing of the initial value
	$('.custom_input').click( function() {
		if (this.value == this.defaultValue)
			this.value = '';
	});
	
	$('.custom_input').blur( function() {
		if (this.value == '') 
			this.value = this.defaultValue;
	});
		
	// Lightbox
	$(".fancybox").fancybox(
		{'titleShow': true,
		'titlePosition':'inside',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'}
	);	
	
	// Homepage slideshow
	var hoptions = {
		'slideType':'fade', // slidehorizontal/slidevertical/fade
		'fadeInOut':true,
		'transitionTime':2000,
		'slideDirection':'default'
	}; 
	$('#slideshow_container .item').dumbCrossFade(hoptions);
	
	// Doctors Slideshow
	var doptions = {
		'slideType':'slidehorizontal',
		'fadeInOut':true,
		'transitionTime':500,
		'showTime':5000,
		'slideDirection':'default',
		slideChange : function (currentSlideIndex) {
			$('#doc_navigation li').removeClass('selected');
			index='#'+currentSlideIndex;
			$(index).addClass('selected');
		} 		
	}; 
	$('.crossfade .item').dumbCrossFade(doptions);
	
	$('.doc_nav').click(function() {
		index = $(this).attr('id');
		$('.crossfade .item').dumbCrossFade('jump',index);
		$('.doc_nav').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});	
	
	// For dropdown menu to work in older IE versions
	$("ul.dropdown li").dropdown();
	
	$('.to-doctor input').click(function() {
		var $checkbox = $(this).find(':checkbox');
		
		var doctors = ['Dr Francis Tomlinson', 'Dr Terry Coyne', 'Dr Richard Kahler', 'Dr David Walker', 'Dr Michael Bryant'];
		var hospitals = ['The Wesley Hospital', "St Andrew's Hospital", 'Fortus Clinic', 
						 'United Medical Centre', "St Vincent's Medical Centre", 'Sunshine Coast Private Hospital Medical Centre',
						 'Mater Private Rockhampton', 'Tweed Day Surgery', "St Vincent's Hospital Lismore"];
		
		$('.to-doctor input').attr("disabled", true);
		$('.to-doctor input:eq(' + $('.to-doctor input').index(this) + ')').attr("disabled", false);
		$('.to-clinic input').attr("disabled", true);
		
		if($(this).val() == doctors[0]) $(this).enablechkbx(0, 1);
		else if($(this).val() == doctors[1]) $(this).enablechkbx(0, 1, 5);
		else if($(this).val() == doctors[2]) $(this).enablechkbx(0, 2, 6);
		else if($(this).val() == doctors[3]) $(this).enablechkbx(0, 1, 4, 7, 8);
		else if($(this).val() == doctors[4]) $(this).enablechkbx(0, 2, 3, 4);
		else $(this).enablechkbx(0, 1, 2, 3, 4, 5, 6, 7, 8);

		var doctorskeep;
		$("input[@name='to-doctor[]']:checked").each(function() {
			doctorskeep = ($(this).val() + " ");
		});

		$('input[name=to-doctor-keep]').val(doctorskeep);

	});

	$('.to-clinic input').click(function() {

		var clinicskeep = "";


		$("input[@name='to-clinic[]']:checked").each(function() {
			clinicskeep += ($(this).val() + " - ");
		});

		$('input[name=to-clinic-keep]').val(clinicskeep);

	});
	
});


$.fn.dropdown = function() {
	$(this).hover(function(){
		$(this).addClass("hover");
		$('.sub-menu',this).addClass("open");
		$('ul:first',this).css('visibility', 'visible');
	},function(){
		$(this).removeClass("hover");
		$('.open',this).removeClass("open");
		$('ul:first',this).css('visibility', 'hidden');
	});
}

jQuery.fn.enablechkbx = function() {
	if($(this).is(':checked'))
		for(i = 0; i < arguments.length; i++)
			$('.to-clinic input:eq(' + arguments[i] + ')').attr("disabled", false);
	else {
		$('.to-doctor input').attr("disabled", false);
		$('.to-clinic input').attr("disabled", false);
	}
};
