$(function() {
	// lightbox code in one neat package
	$(".lightbox").click(function() {
		var pid = $(this).attr("id");
		if (!pid) {
			var srcsplit=$(this).attr("src").split("pid=");
			var pid = srcsplit[1].split("&");
			pid = pid[0];
		}
		showLb(pid);
	});
	linkflash();
	loadChart();
});

function closeLb() {
	$("#lbWindow").remove();
	$("#lbOverlay").remove();
	$(window).unbind('keydown');
}

function refreshLbListener()
{
	$(window).unbind('keydown');
	$(window).keydown(function(event) {
		if (event.keyCode == 27) closeLb();
		else if (event.keyCode == 39) {
			if ($(".nextpic").attr("id")) showLb($(".nextpic").attr("id"));
		}
		else if (event.keyCode == 37) {
			if ($(".prevpic").attr("id")) showLb($(".prevpic").attr("id"));
		}
	});
}

function showLb(pid) {
	var downloadable = $("#" + pid).hasClass("downloadable") ? 1 : 0;
	if (!$("#lbOverlay").length) {
		$("body").prepend('<div id="lbWindow"><div class="navi"><a href="javascript:closeLb();">close</a></div><div id="lbImage"></div><div id="lbDescription"></div></div>');
		$("body").prepend('<div id="lbOverlay"></div>');
	}
	else {
		$("#lbWindow").remove();
		$("body").prepend('<div id="lbWindow"><div class="navi"><a href="javascript:closeLb();">close</a></div><div id="lbImage"></div><div id="lbDescription"></div></div>');
	}
	
	var img = new Image();
	var imageUrl = "/image/show?pid=" + pid + "&width=600&height=500";
	$(img)
	    .attr('src', imageUrl)
	    .attr('id', 'bigpicture')
	    .load(function() {
		    if (downloadable) {
		    	$("#lbImage").append('<a href="/sendfile/?fid='+pid+'"></a>');
		    	$("#lbImage a").append($(this));
		    }
	    	else $("#lbImage").append($(this));
	    	$("#lbOverlay").css('width', $(document).width());
	    	$("#lbOverlay").css('height', $(document).height());
	    	$("#lbOverlay").show();
	    	$("#lbOverlay").click(function() { closeLb(); });
	    	$("#lbDescription").width($("#lbWindow").outerWidth(true)-20);
	    	$("#lbDescription").load("/ajax/lbdescription/", {pid: pid, downloadable: downloadable}, function() {
	    		refreshLbListener();
	    	});
	    	// calculate coordinates for centering lightbox
	    	var newx = $(document).scrollLeft() + $(window).width()/2 - $("#lbWindow").outerWidth(true)/2;
	    	var newy = $(document).scrollTop() + $(window).height()/2 - $("#lbWindow").outerHeight(true)/2;
	    	$("#lbWindow").css('left', newx);
	    	$("#lbWindow").css('top', newy);
	    	$("#lbWindow").fadeIn("normal");
	    });
}

function showLbText(pid) {
	$("#lbWindow").remove();
	$("#lbOverlay").remove();
	$("body").prepend('<div id="lbWindow"><div class="navi"><a href="javascript:closeLb();" class="closebutton"></a></div><div id="lbText"></div></div>');
	$("body").prepend('<div id="lbOverlay"></div>');
	$("#lbText").load("/ajax/getlbpage", {pageid: pid}, function() {
		$(".closebutton").html($(".closelabel").html());
		$("#lbOverlay").css('width', $(document).width());
	    $("#lbOverlay").css('height', $(document).height());
	    $("#lbOverlay").show();
	    $("#lbOverlay").click(function() { closeLb(); });
	    $("#lbText").width($("#lbWindow").outerWidth(true)-20);
	    // calculate coordinates for centering lightbox
	    var newx = $(document).scrollLeft() + $(window).width()/2 - $("#lbWindow").outerWidth(true)/2;
	    var newy = $(document).scrollTop() + 20;
	    $("#lbWindow").css('left', newx);
	    $("#lbWindow").css('top', newy);
	    $("#lbWindow").fadeIn("normal");
	    refreshLbListener();
	});
}

function checkForm(formId) {
	var oktosend = true;
	$("#"+formId+" .formfield").each(function() {
		if ($(this).hasClass('required') && !$(this).val()) {
			oktosend = false;
			$(this).addClass('warning');
		}
		else $(this).removeClass('warning');
	});
	if (oktosend) $("#"+formId).submit();
	else alert($("#"+formId+" #warningMsg").val());
}

function linkflash()
{
	$(".flashcontainer").each(function() {
		var id = $(this).attr("id");
		var parts = id.split("_");
		var flashvars = {};
		var params = {};
		var attributes = {};
		swfobject.embedSWF("/" + parts[0] + ".swf", id, "450", "300", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	});
}

function loadChart()
{
	$(".chartcontainer").each(function() {
		var chartid = $(this).attr("id").replace(/chart_/, "");
		var chartheader = $(this).children("span").attr("id");
		var chart = $(this);
		$.post("/ajax/chartdata", {chartid: chartid}, function(data) {
			chart.css({width: "100%;", height: data.data.length * 40 + "px"});
			var chartdata = data.data;
			var chartlabels = data.labels;
			var plot = $.jqplot(chart.attr("id"), [chartdata], {
				legend: {show: false}, 
				title: chartheader,
				seriesDefaults: {
					renderer: $.jqplot.BarRenderer, 
					rendererOptions: {barDirection: "horizontal", barPadding: 2, barMargin: 6},
					shadowAngle: 135,
					color: "#01743b"
				},
				axes: {
					xaxis: {
						min: 0, 
						max: 100,
						tickOptions: {fontSize: "1.2em"},
						tickInterval: 25
					},
					yaxis: {
						renderer: $.jqplot.CategoryAxisRenderer,
						ticks: chartlabels,
						tickOptions: {fontSize: "1.3em"}
					}
				}
			});
		}, "json");
	});
}
