$(function(){
	$("div.rounded").corner();
	
	var messages = $("div#messages");
	var oldBgColor = messages.css("backgroundColor");
	messages.animate( { backgroundColor: "#FF0000" }, 1500 ).animate( { backgroundColor: oldBgColor }, 1500 );
	$("div#messages a.close").click(function() {
		$("div#messages").fadeOut("slow", function(){$(this).remove();});
	});
	
	// tab panels
	$("div.tabpanel div.sidebar li a, div.tabpanel div.topbar li a").click(function(){
		if ($(this).is(".external")) return;
		$(this).blur();
		if($(this).parent(".active")[0]) return false;
		$(this).parents("li").siblings("li").removeClass("active");
		var container = $(this).parents("div.sidebar, div.topbar").siblings("div.tabcontainer");
		$("div.tabcontent", container).slideUp();
		$("div.panel-"+this.hash.substring(1), container).slideDown();
		$(this).parent("li").addClass("active");
		window.location.replace(this.hash);
		return false;
	});
	
	// show panel indicated by hash
	if (window.location.hash) {
		$("div.tabcontent.active").hide();
		$("div.panel-"+window.location.hash.substring(1)).show();
		$("div.sidebar li.active, div.topbar li.active").removeClass("active");
		$("div.sidebar li a[href="+window.location.hash+"], div.topbar li a[href="+window.location.hash+"]").parent("li").addClass("active");
	}
	
	//set up dialogs
	var target = $('div.dialog div#dialogcontent')[0];
	$("div.dialog").jqm( {
		ajax: '@href',
		params: 'ajax&src='+escape(document.location),
		trigger: 'a.dialog',
		target: target,
		closeClass: 'dialogclose',
	    onShow: function(h) {
			loadingContent = $("#dialogcontent").html();
			h.o.fadeIn(400);
			h.w.slideDown(200);
			$("a.newwindow", h.w)[0].href= h.t.href;
		},
		onLoad: function(h) {
			$(this).slideDown(500);
			if ($("form", this).length == 0) {
				$("div.dialog div.titlebar").show();
			}
		},
		onHide: function(h) {
			h.o.fadeOut(500);
			h.w.slideUp(250,function(){ 
				$("#dialogcontent").html(loadingContent);
			});
		}
	});
	
	// reveal more links
	$("div.infoblock a.more").click(function(){
		$(this).next("span.more").slideDown(800);
		$(this).fadeOut();
		return false;
	});
	
	// make callouts clickable
	$("div.callout.seekers, div.callout#employers").click(function(){
		location.href = $("a:first",this)[0].href;
	});
	
	// lightbox galleries
	$('div.gallery a, div.ngg-gallery-thumbnail a').lightBox({
		imageLoading:			'/assets/images/busy.gif',	   
		imageBtnPrev:			'/assets/images/lightbox-btn-prev.gif',		   
		imageBtnNext:			'/assets/images/lightbox-btn-next.gif',		   
		imageBtnClose:			'/assets/images/lightbox-btn-close.gif',	   
		imageBlank:				'/assets/images/lightbox-blank.gif'
	});
	
	//ratings
	function showRatingActivation(){
		$("h4",this).hide();
		$("div.rating-activate",this).show();
	}
	function showRatingSummary(){
		$("h4",this).show();
		$("div.rating-activate",this).hide();
	}
	function hoverStars(){
		$(this).addClass("star-on");
		$(this).prevAll("a.star").addClass("star-on");
		$(this).nextAll("a.star").removeClass("star-on");
	}
	function resetStars(){
		numStars = $(this).parents("form").find("input#rating").val();
		if (numStars == 0) {
			$(this).parents("form").find("a.star").removeClass("star-on");
			return;
		}
		setStar = $(this).parents("form").find("a.star").eq(numStars);
		setStar.addClass("star-on");
		setStar.prevAll("a.star").addClass("star-on");
		setStar.nextAll("a.star").removeClass("star-on");
	}
	$(function(){
		$("div.rating-summary").hover(showRatingActivation, showRatingSummary);
		$("div.rating-summary",this).click(function(){
			$(this).slideUp(300, function(){$("div.rating-activate",this).remove()});
			$(this).parents("div.rating").find("div.rating-input form").slideDown("normal",function(){
				ratingForm = $(this);
				scrollTargetY = ratingForm.offset().top + ratingForm.height() + 15;
				if ($(document).scrollTop() + $(window).height() < scrollTargetY) {
					$.scrollTo(scrollTargetY - $(window).height(), 200);
				}
			});
		});

		$("div.rating-input button.dialogclose").click(function(){
			$(this).parents("div.rating-input").find("form").slideUp();
			$(this).parents("div.rating").find("div.rating-summary h4").show().parents("div.rating-summary").slideDown().append('<div class="rating-activate"><em>Rate or add notes to this candidate</em></div>');
		});
		$("a.star").click(function(){
			$(this).parents("form").find("input#rating").val(this.href.substring(this.href.lastIndexOf("=")+1));
			this.blur();
			return false;
		});
		$("a.star").hover(hoverStars, resetStars);

		$("div.rating-input form").submit(function(){
			$.get(this.action + "?" + $(this).serialize());
			return false;
		});
	});
});
