$(document).ready(function() {

	// add the jsok class to the body if javascript is on
	// used for hiding content to js-enabled browsers etc
	$('body').addClass("jsok");

	$('a.print').click(function() {
		window.print();
		return false;
	})

	// sifr replacements

	sIFR.replace(plume, {selector: '.homepage_box h2',
								css: 		 '.sIFR-root { background-color: #FFF; color: #626262; }'
								}
					);

	sIFR.replace(plume, {selector: '.highlight_box h1',
								css: 		 '.sIFR-root { background-color: #EBEBEB; color: #626262; }'
							}
				);

	sIFR.replace(plume, {selector: 'h1.replace',
							css: 		 '.sIFR-root { background-color: #FFF; color: #626262; }'
						}
			);

	sIFR.replace(plume, {selector: '#content_wrapper h1',
						css: 		 '.sIFR-root { background-color: #FFF; color: #626262; }'
					}
		);

	// some IEs don't have the :focus pseudo-selector. Mimic it with .focus class
	$('input[type=text], input[type=password], textarea').focus(function() {
		$(this).addClass('focus');
		$(this).parent('.input').addClass('active');
	});

	$('input[type=text], input[type=password], textarea').blur(function() {
		$(this).removeClass('focus');
		$(this).parent('.input').removeClass('active');
	});
	//
	//$('input[type=submit]').hover(function() {
	//	$(this).addClass('hover');
	//}, function() {
	//	$(this).removeClass('hover');
	//});

	// front-page questions thing
	$('#questions').removeClass('grid_8').addClass('grid_4').after('<div id="answers" class="grid_4 homepage_box"><h2>Answers</h2><div class="ans"></div></div>');

	$('#questions').after('<div class="box_arrow"></div>');

	$('#questions dt a').click(function() {
		$('dt.active').removeClass('active');
		$(this).parent().addClass('active');

		var ques = $(this).text();
		var ans = $(this).parent().next().html();
		$('#answers').html('<img class="home_face" src="/img/faces_red.png" width="53" height="53" alt="" /><h2>' + ques + '</h2><div class="ans">' + ans + '</div>');

		sIFR.replace(plume, {selector: '#answers h2',
									css: '.sIFR-root { background-color: #FFF; color: #626262;}'
									}
						);

		var qtop = $(this).position('top');
		var arrow_top = 235 + qtop.top;
		$(".box_arrow").animate({top:  arrow_top+'px'},
										{queue:false, duration: 350}
										);

		return false;
	});


	// start with q1 clicked
	$('#questions dt#q1 a').click();

	// make clicking on the header page navs submit the form first
	$('div.incident_form_header p.form_crumb a').click(function() {
		$('form#IncidentForm').trigger('submit');
		return true;
	});

	$('input.toggle').click(function() {
		if(this.value == 1) {
			var target = $(this).attr('rel');
			$('#' + target).show();
			$('#' + target).removeClass('hidden');//.effect("highlight", {}, 900);;
		} else {
			var target = $(this).attr('rel');
			$('#' + target).hide();
			$('#' + target).addClass('hidden');
		}
	});

	$('input.switch').click(function() {
		var choices = eval($(this).attr('rel'));
		if(this.value == 1) {
			$('#' + choices[0]).hide();
			$('#' + choices[0]).addClass('hidden');
			$('#' + choices[1]).show();
			$('#' + choices[1]).removeClass('hidden').effect("highlight", {}, 900);
		} else {
			$('#' + choices[0]).show();
			$('#' + choices[0]).removeClass('hidden').effect("highlight", {}, 900);
			$('#' + choices[1]).hide();
			$('#' + choices[1]).addClass('hidden');
		}
	});

	$('.enable').change(function() {
		var value = this.value;
		var data = eval($(this).attr('rel'));
		var enable_array = data[0].split(',');
		var target = data[1];

		if(in_array(value, enable_array)) {
			$('#' + target).removeClass('disabled');
			$('#' + target).children('input').removeAttr('disabled');
			$('#' + target).removeClass('hidden');
		} else {
			$('#' + target).addClass('disabled');
			$('#' + target).addClass('hidden');
			$('#' + target).children('input').attr('disabled', 'disabled');
		}
	});

	$('.multiple.advanced .checkbox input').click(function() {
		var val = this.value;
		var target = '#o' + val;

		$('.advanced_options').show();
		$('.advanced_options').removeClass('hidden').effect("highlight", {}, 900);
		$(target).toggleClass('hidden');
	})

	$('a.inline_edit').click(function() {
		me = $(this);
		href = me.attr('href');
		field = me.siblings('.editable');
		value = field.text();
		field.after('<input id="inline_edit" type="text" value="' + value + '"/>').show().focus();
		field.hide();
		me.hide();

		$('#inline_edit').change(function() {
			field.text($(this).val());
			$(this).hide();
			field.show();

			$.ajax({
				type:		"POST",
				url:		href,
				data:		field.attr('id') + '=' + $(this).val(),
				success:	function() {
					me.show();
				}
			});
		});

		return false;
	});

});

function readLinked(id) {
	$('#' + id).val(
		$('#' + id + 'Month').val() + '/' +
        $('#' + id + 'Day').val() + '/' +
        $('#' + id + 'Year').val()
    );
    return {};
}

function updateLinked(date, id) {
	$('#' + id + 'Month').val(date.substring(0, 2));
    $('#' + id + 'Day').val(date.substring(3, 5));
    $('#' + id + 'Year').val(date.substring(6, 10));
}

function ajaxLinks(l, activeClass) {
	activeClass = typeof(activeClass) != 'undefined' ? activeClass : 'active';

	$(l).click(function() {
		var link = this.href.split('#');
		var href = link[0];

		var target = link[1];
		target = typeof(target) != 'undefined' ? target : 'content_container';

		$('.' + activeClass).removeClass(activeClass);

		$('#'+target).fadeOut('fast');
		$('#'+target).load(href, null, function() {
			$(this).fadeIn('fast');
		});

		$(this).addClass(activeClass);
		return false;
	});
}

function in_array(needle, haystack) {
	var found = false;
	var el;

	for (el in haystack) {
		if (haystack[el] == needle) {
			found = true;
			break;
		}
	}

	return found;
}

