jQuery.noConflict(); // Tell $$$ you are going with noConflict mode.

(function($){

var debug = 0 ;

$(document).ready(function() {


	// FileTree
		// Hide all subfolders at startup
		$(".php-file-tree").find("UL").hide();
		
		// Expand/collapse on click
		$(".pft-directory A").click( function() {
			$(this).parent().find("UL:first").slideToggle("medium");
			if( $(this).parent().attr('className') == "pft-directory" ) return false;
		});		
		
	// Listen to label clicks
	$(".task-editable").click( function() {
		var taskflag = $(this).children(".task-check").attr("checked");
		// If the task is being required
		if(taskflag){
			$(this).children(".hidden").html("1");	
			$(this).children(".text").html("Included");
			$(this).addClass("checked");
			$(this).parent().parent().removeClass("unchecked");
		}else{
			$(this).children(".hidden").html("0");	
			$(this).children(".text").html("Optional");	
			$(this).removeClass("checked");
			$(this).parent().parent().addClass("unchecked");
		}
		recalc();
	});	
		
	// Listen to checkbox clicks - fallback for no styles
	$(".task-check").click( function() {
		var taskflag = $(this).attr("checked");
		// If the task is being required
		if(taskflag){
			$(this).parent().children(".hidden").html("1");	
		}else{
			$(this).parent().children(".hidden").html("0");	
		}
		recalc();
	});
			
	// bind the recalc function to the quantity fields
	$("input[name^=qty_]").bind("keyup", recalc);
	// run the calculation function now
	recalc();
	tabsections();


});

function tabsections() {
		
	$( "#sections" ).tabs();
	$( "#sections" ).bind('tabsselect', function(event, ui) {
	    window.location.href=ui.tab;
	});
    var param = $(document).getUrlParam('selectedTab');
    $( "#sections" ).tabs('select', param);
}

function recalc(){
	
	$("[id^=item_]").calc("req * qty * price",{
			req: $("[id^=req_]"),
			qty: $("[id^=qty_]"),
			price: $("[id^=price_]")
		},function (s){
			return "$" + s.toFixed(0);
		},function ($this){
			var taxstring = $("#taxstring").html();
			if(debug==1){console.log("taxstring: "+taxstring);}
			var tax = parseFloat(taxstring);
			var subtotal = $(this).sum() * 1.0;
			var gst = $(this).sum() * tax;
			var total = $(this).sum() + gst;
			if(debug==1){console.log("tax: "+tax);}
			// Totals Calculation
			$("#subtotal").text("$" + subtotal.toFixed(0));		
			$("#gst").text("$" + gst.toFixed(0));
			$("#total").html("<strong>$" + total.toFixed(0) + "</strong>");
			$("#due_1").html("$" + total.toFixed(0));
		}
	);
	
	// Tally the Project Blocks
	var sum = $(".blocks").sum();
	$("#tasksum").text(sum.toString());

	// Tally the Payments
	var due = $(".due").sum();
	var rcv = $(".rcv").sum() * -1; 
	$("#total_rcv").html("$" + rcv.toString());
	
	// Work out the outstanding balance 
	var owing = $(".payments_total").sum();
	$("#payments_outstanding").html("$" + owing.toString());
	
}

function init_php_file_tree() {
	if (!document.getElementsByTagName) return;
	
	var aMenus = document.getElementsByTagName("LI");
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		if (mclass.indexOf("pft-directory") > -1) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					
					submenu[j].onclick = function() {
						var node = this.nextSibling;
											
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "open" : "closed";
									return false;
								}
								node = node.nextSibling;
							} else {
								return false;
							}
						}
						return false;
					}
					
					submenu[j].className = (mclass.indexOf("open") > -1) ? "open" : "closed";
				}
				
				if (submenu[j].tagName == "UL")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
			}
		}
	}
	return false;
}

})(jQuery);
