var $j = jQuery.noConflict();

var change = {
	config : {
		sTag : "can-has-js",
		imgs : "/change/img/",
		fonts : {
			// List Change.org-approved sIFR typefaces here
			archerBoldLF : {
				src : "/change/swf/archer-bold-lf.swf"
			},
			privaFour : {
				src : "/change/swf/priva-four.swf"
			}
		}
	}

	,init : function() {
		var c = change;

		if (typeof theme.init == 'function') {
			theme.init(c.config);
		}

		$j(document).ready(function() {
			/*
				List function(s) to fire onload below
			*/
			c.tagIt();
			c.buildTabs("#cause-slides ul.nav");
			c.causesNav("#causes-index", "#mast #causes-nav li.more a i");
			c.checkToggle();
			c.comboBoxes();
			c.deleteLinks("a.delete", "li.type", "#notifications");
			c.progressBars("dd.status");
		});
	}

	/*
		"Tag" the HTML element if JS is available
	*/
	,tagIt : function() {
		var c = change;

		$j("html").addClass(c.config.sTag);
	}

	/*
		Create tabs
	*/
	,buildTabs : function(sSel) {
		$j(sSel).tabs();
	}

	/*
		Causes navigation
	*/
	,causesNav : function(sMenuSel, sNavSel) {
		jQuery.easing.def = "easeInOutQuad";

		// Define the variables
		var oMenu = $j(sMenuSel);
		var oNavTrigger = $j(sNavSel);
		var oNavLink = oNavTrigger.parent();

		// Show/hide the menu when clicking on the appropriate menu item
		oNavTrigger.click(function() {
			return false;
		});
		
		oNavTrigger.mouseover(function() {
			if (oMenu.css("display") != "block") {
				oNavLink.addClass("active");
				oMenu.slideDown("medium");
			}
		})

		// If the menu's visible, hide it when clicking anywhere else in the document...
		$j("body").click(function() {
			if (oMenu.css("display") == "block") {
				oNavLink.removeClass("active");
				oMenu.fadeOut("medium");
			}
		})

		// ...except if you're clicking on the menu itself.
		oMenu.click(function(event) {
			event.stopPropagation();
		});

		// add close menu link
		$j("#causes-index a#close-cause-nav").click(function() {
			if (oMenu.css("display") == "block") {
				oNavLink.removeClass("active");
				oMenu.slideUp("medium");
			}
		})
	}


	/*
		Checkbox toggles
	*/
	,checkToggle : function() {
		var oChecks = $j("input[type=checkbox][rel^=toggle]");

		$j(oChecks).each(function() {
			var sTarget = $j(this).attr("rel").split(" ")[1];
			if (sTarget.length) {
				var oTarget = $j("#" + sTarget);

				var fAction = function() {
					var isChecked = $j(this).is(":checked");
					var sTarget = $j(this).attr("rel").split(" ")[1];
					var oTarget = $j("#" + sTarget);

					if (isChecked) {
						oTarget.show();
					} else {
						oTarget.hide();
					}
				}

				$j(this).click(fAction);
				$j(this).focus(fAction);

				var currChecked = $j(this).is(":checked");
				if(!currChecked) { oTarget.hide(); }
			}
		});
	}
	
	/*
		Combo Boxes Toggle
	*/
	,comboBoxes : function() {
		var oBoxes = $j(".combo-box");

		$j(oBoxes).each(function() {
			var oDropdown = $j('#' + this.id + '-dd');

			if (oDropdown) {
				var pos = $j('#' + this.id).offset();
				var height = $j('#' + this.id).height();
				oDropdown.css("left", pos.left + "px");
				oDropdown.css("top", (pos.top+height+3) + "px");
				
				var fAction = function() {
					if (oDropdown.css("display") == 'block') {
						oDropdown.css("display", "none");		
					} else {
						oDropdown.css("display", "block");		
					}
				}

				$j(this).click(fAction);
			}
		});
	}

	/*
		Delete links
	*/
	,deleteLinks : function(sSel, sSelParent, sModule) {
		var cAnchors = $j(sSel);
		$j(cAnchors).click(function(e) {
			var oParent = $j(this).parents(sSelParent);
			var oContainer = $j(oParent).parent();

			$j(oParent).css("height", $j(oParent).height());

			// Collapse the list item, then remove it from the DOM
			$j(oParent).slideUp("normal", function() {
				$j(this).remove();

				// If the list is empty, replace it with an error message.
				if ($j(oContainer).children().length == 0) {
					$j(sModule).fadeOut("slow", function() {
						$j(this).remove();
					});
				}
			});

			/*
				NB: AJAX functionality isn't built yet, as these
				are just static files.
				(c.f., http://docs.jquery.com/Ajax)
			*/
			// $j(this).get($j(this).attr("href"));

			return false;
		});
	}

	/*
		Progress bars
	*/
	,progressBars : function(sSel) {
		$j(sSel).each(function() {
			var sWidth = $j(this).text();
			var iWidth = parseInt(sWidth);

			if (iWidth == 0) {
				$j(this).addClass("empty");
			} else if (iWidth == 100) {
				$j(this).css("width", $j(this).width())
			} else {
				$j(this).css("width", $j.trim(sWidth));
			}
		});
	}
};




change.init();
