/* IE6 flicker fix
-------------------------------------------------- */
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

/* jQuery
-------------------------------------------------- */
var toggleH2boxes = {
	init : function(){
		$("h2.toggle a").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("h2").toggleClass("toggle-open");
		$(this).parents("h2").nextAll("div.toggle-wrap:first").slideToggle("fast");
		return false;
	}
};
function headingHover() {
	$("h2.toggle a").hover(
		function(){
			$(this).parents("h2").css("border-color","#00751e");
		},
		function(){
			$(this).parents("h2").css("border-color","#ccc");
		}
	);
}

var navChoice1 = {
	init : function(){
		$("p.nav-choice a.destination").click(this.clickIt);
	},
	clickIt : function(){
		$(this).addClass("active");
		$("p.nav-choice a.intranet").removeClass("active");
		$("ul.destination").show();
		$("ul.intranet").hide();
		
		return false;
	}
};
var navChoice2 = {
	init : function(){
		$("p.nav-choice a.intranet").click(this.clickIt);
	},
	clickIt : function(){
		$(this).addClass("active");
		$("p.nav-choice a.destination").removeClass("active");
		$("ul.intranet").show();
		$("ul.destination").hide();
		return false;
	}
};

/* data tables */

function dataHover() {
	$("table.data tbody:not(.form) tr").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
}
var toggleSubrow = {
	init : function(){
		$("table.data tbody a.toggle-subrow").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).parents("tr").next("tr.subrow").css("display") == "none") {
			$(this).parents("tr").addClass("toggler");
			$(this).parents("tr").next("tr.subrow").css("display","");
		} else {
			$(this).parents("tr").removeClass("toggler");
			$(this).parents("tr").removeClass("marked");
			$(this).parents("tr").next("tr.subrow").css("display","none");
		}
		return false;
	}
};
function fixIEoverflow() {
	if (!/*@cc_on!@*/0) return;

	var all = document.getElementsByTagName('*'), i = all.length;
	while (i--) {
		// adding a class match just to show the difference
		if (all[i].className.match(/scroll/) && all[i].scrollWidth > all[i].offsetWidth) {
			all[i].style['overflowY'] = 'hidden';
			all[i].style['paddingBottom'] = '17px';
		}
	}
}
var headingSort = {
	init : function(){
		$("table.data thead th.sort a").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).parent().is(".ascending")) {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().removeClass("ascending");
			$(this).parent().addClass("descending");
		}
		else if ($(this).parent().is(".descending")) {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().removeClass("descending");
			$(this).parent().addClass("ascending");
		}
		else {
			$(this).parents("thead").find("th").removeClass("ascending");
			$(this).parents("thead").find("th").removeClass("descending");
			$(this).parent().addClass("ascending");
		}
		return false;
	}
};

/* radio, checkbox */

var markRows = {
	init : function(){
		$("table.data tbody:not(.form) tr").dblclick(this.clickIt);
	},
	clickIt : function(){
		$(this).toggleClass("marked");
	}
};
var markAllCheck = {
	init : function(){
		$("div.sub-action a.mark-all").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checkbox").attr("checked","checked");
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checked").parents("tr").addClass("checked");
		return false;
	}
};
var markNoneCheck = {
	init : function(){
		$("div.sub-action a.mark-none").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input:checkbox").removeAttr("checked");
		$(this).parents("div.sub-action").nextAll("table.data:first").find("tbody tr td.check input").not(":checked").parents("tr").removeClass("checked");
		return false;
	}
};
var setCheckbox = {
	init : function(){
		$("table.data tr td.check input:checkbox").click(this.clickIt);
	},
	clickIt : function(){
		if($(this).is(":checked")) {
			$(this).parents("tr").addClass("checked");
		} else {
			$(this).parents("tr").removeClass("checked");
		}
	}
};
var setRadio = {
	init : function(){
		$("table.data tr td.check input:radio").click(this.clickIt);
	},
	clickIt : function(){
		$(this).parents("table").find("tr.checked").removeClass("checked");
		$(this).parents("tr").addClass("checked");
	}
};
function controlCheckbox() {
	$("table.data tbody tr td.check input:checkbox:checked").parents("tr").addClass("checked");
}
function controlRadio() {
	$("table.data tbody tr td.check input:radio:checked").parents("tr").addClass("checked");
}

/* document ready */

$(document).ready(function(){

	markRows.init();
	markAllCheck.init();
	markNoneCheck.init();
	controlRadio();
	controlCheckbox();
	setCheckbox.init();
	setRadio.init();
	headingSort.init();

	$("table.data tr.subrow").css("display","none");
	toggleSubrow.init();

	
	navChoice1.init();
	navChoice2.init();
	$("ul.intranet").css("display","none");
	

	headingHover();
	toggleH2boxes.init();
	$("div.toggle-wrap").css("display","none");

	fixIEoverflow();

	$("#start_date, #end_date").datepicker({ yearRange: '2009:2012' });

	/* IE */
	if($.browser.msie){
	}

	/* IE6 */
	if($.browser.msie && ($.browser.version < 7) ){
		dataHover();
	}

});
