$(document).ready(function(){
	
	// Tabs
	$("ul.tabs li").click(function() {
		return showTab($(this).find("a").attr("href"));
	});
	
	if (typeof defaultOpenTab == 'undefined'){
		$(".tab_content").hide();
		$("ul.tabs li:first-child").addClass("active").show();
		$(".block").find(".tab_content:first").show();
	} else {
		showTab(defaultOpenTab);
	}

	// Sidebar Tabs
	$("ul.composite li").click(function() {
		return showSidebarTab($(this).find("a").attr("href"));
	});
	
	if (typeof defaultOpenTab == 'undefined'){
		$(".side_content").hide();
		$("ul.composite li:first-child").addClass("active").show();
		$(".block").find(".side_content:first").show();
	} else {
		showSidebarTab(defaultOpenTab);
	}

	// Block search
	$('.block .block_head form .text').bind('click', function() { $(this).attr('value', ''); });
	
	// Messages
	$('.message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
	$('.message .close').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
		
	$('.message .close').click(function() {
		$(this).parent().fadeOut('slow', function() { $(this).remove(); });
	});
	
	// IE6 PNG fix
	//$(document).pngFix();
	
	if (typeof jQuery.ui != 'undefined'){
		
		$("#dialog-success").dialog({
	        modal: true,
	        bgiframe: true,
	        autoOpen: false });
		
		$("#dialog-warning").dialog({
	        modal: true,
	        bgiframe: true,
	        autoOpen: false });
		
		$("#dialog-confirm").dialog({
	        modal: true,
	        bgiframe: true,
	        autoOpen: false });
	}
	
	// associate any confirm links with a confirm dialog
	$('.confirm-link').click(function(){
		var callback = $(this).attr('callback');
		if (callback == undefined || callback == ""){
			callback = "go";
		}
		showConfirmDialog($(this).attr('confirm-msg'),eval(callback),$(this).attr('link'));
		return false;
	});
});

function go(link){
	window.location = link;
}
function showSuccessDialog(msg){
	$("#dialog-success p").html(msg);
	$('#dialog-success').dialog('option', 'buttons', { 
	    "Ok": function() { $(this).dialog("close"); }
	});
	$('#dialog-success').dialog('open');
}

function showWarningDialog(msg){
	$("#dialog-warning p").html(msg);
	$('#dialog-warning').dialog('option', 'buttons', { 
	    "Ok": function() { $(this).dialog("close"); }
	});
	$('#dialog-warning').dialog('open');
}

function showConfirmDialog(msg,callback,data){
	$("#dialog-confirm p").html(msg);
	$('#dialog-confirm').dialog('option', 'buttons', { 
	    "No": function() { $(this).dialog("close"); },
	    "Yes":  function(){
	    	setTimeout(function() {
	    		$('#dialog-confirm').dialog('close');
			}, 1);
	    	callback.call(data,data);
	    } 
	});
	$('#dialog-confirm').dialog('open');
}

function showSuccessMsg(msg){
	alert(msg);
}

function showErrorMsg(msg){
	alert("Error: " + msg);
}

function showLoadingAnim(){
	$(".follow").show();
	$("body").mousemove(function(e){
	      $('.follow').css('top', (e.pageY+10)).css('left', (e.pageX+10));
	});
}

function hideLoadingAnim(){
	$(".follow").hide();
	$("body").unbind('mousemove');
}

function showSidebarTab(linkId){
	var link = $('a[href='+linkId+']');
	var tab = $(link).parent();
	$(tab).parent().find('li').removeClass("active");
	$(tab).addClass("active");
	$(tab).parents('.block').find(".side_content").hide();

	var activeTab = $(tab).find("a").attr("href");
	$(activeTab).show();
	return false;
}

function showTab(linkId){
	var link = $('a[href='+linkId+']');
	var tab = $(link).parent();
	$(tab).parent().find('li').removeClass("active");
	$(tab).addClass("active");
	$(tab).parents('.block').find(".tab_content").hide();

	var activeTab = $(tab).find("a").attr("href");
	$(activeTab).show();
	return false;
}

//Simple JavaScript Templating
//John Resig - http://ejohn.org/ - MIT Licensed
(function(){
	var cache = {};
	
	this.tmpl = function tmpl(str, data){
	 // Figure out if we're getting a template, or if we need to
	 // load the template - and be sure to cache the result.
	 var fn = !/\W/.test(str) ?
	   cache[str] = cache[str] ||
	     tmpl(document.getElementById(str).innerHTML) :
	   
	   // Generate a reusable function that will serve as a template
	   // generator (and which will be cached).
	   new Function("obj",
	     "var p=[],print=function(){p.push.apply(p,arguments);};" +
	     
	     // Introduce the data as local variables using with(){}
	     "with(obj){p.push('" +
	     
	     // Convert the template into pure JavaScript
	     str
	       .replace(/[\r\t\n]/g, " ")
	       .split("<%").join("\t")
	       .replace(/((^|%>)[^\t]*)'/g, "$1\r")
	       .replace(/\t=(.*?)%>/g, "',$1,'")
	       .split("\t").join("');")
	       .split("%>").join("p.push('")
	       .split("\r").join("\\'")
	   + "');}return p.join('');");
	 
	 // Provide some basic currying to the user
	 return data ? fn( data ) : fn;
	};
})();

function truncateVal(val){
	var dec = 2;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


