// load stylesheet for javascript enabled browsers
$(function(){
	var addStyleSheet = "<link rel='stylesheet' type='text/css' href='static/css/js-enabled-styles.css' />";
	$('head')
		.append(addStyleSheet);
});

// select combo dropdowns
$(function() {
	// hide submit button
	$('#submit-school').hide()
	$('#school').change(function() {
		//alert('oi')
		var selected = $("#school option[@selected]");
        // only change when selected option is not the first
		if (selected.val() > 0) {
			// if already exist, remove it
			$('#department').remove()
			// create new select
			$('<select></select>')
			.attr('id', 'department')
			.attr('name', 'department')
			.addClass('hide')
			// insert after #school
				.insertAfter(this);
			// create new submit
			// if already exist, remove it
			$('#submit-schooldept').remove()
			$('<input type="submit" />')
				.attr('id', 'submit-schooldept')
				.attr('name', 'submit-schooldept')
				.attr('value', 'Make a gift online')
				.addClass('submit')
				.addClass('hide')
				//.insertAfter('.button-container fieldset');
				.appendTo('.button-container fieldset');
			// get option values via ajax
			if (selected.val() == 1) {
				var url = "arts.html";
				//$('#department').load(url);
			};
			if (selected.val() == 2) {
				var url = "business.html";
				//$('#department').load(url);
			};
			if (selected.val() == 3) {
				var url = "computing.html";
				//$('#department').load(url);
			};
			if (selected.val() == 4) {
				var url = "dentistry.html";
				//$('#department').load(url);
			};
			$('#department').load(url);
		};
	});
	// form submit
	$('form#select-school').submit(function() {
		$(this).attr('action', 'make-a-gift.php')
	});
});

// loading
$(function(){
	$('#loading').ajaxStart(function() {
		$(this).show();
	}) .ajaxStop(function() {
		$(this).hide()
		$('#department').removeClass('hide')
		$('#submit-schooldept').removeClass('hide');
	});
});

$(function(){
	// when field gets clicked
	$('.primary-content :input.example').focus(function() {
		// if not empty and if class example get value
		if ($ (this) .is('.example') && !(this.value == ''))  {
			// store value
			exampleValue = this.value;
			// empty value
			$(this).attr({'value' : ''})
			$(this).removeClass('example');
		};
	}).blur(function() {
		//alert('oi');
		// if empty replace example value
		if (this.value == '') {
		//if ($ (this) .is('.example') && (this.value == ''))  {
			$(this).addClass('example')
			$(this).attr({'value' : exampleValue})
		};
	});
});

// !!! this must be the last function in this file !!!
$(function(){
	// check for class accordion
	if ( $("div.accordion").length > 0 ) {
		//alert("The DIV exists");
	jQuery('.accordion').accordion({
		header: 'h3.accordion-heading',
		active: '.open',
		selectedClass: 'open',
		alwaysOpen: false,
		animated: 'slide',
		autoheight: false
	});
	};
});
