/*
	Closures are used for each function so we lessen the memory footprint and increase speed by not polluting the global namespace.
	
	To create a new closure:
	(function() {
		// code here, variables won't pollute the global namespace
		var privateVariable = "private!";
		var privateFunction = function() { return "private"; };
		
		this.publicVariable = "public!";
		this.publicFunction = function() { return "public"; };
	});
	
	Adding () before the semi-colon will call it immediately. You can save it to a variable and call the function closure later.
	Adding $(); around the closure will call it on jQuery ready. Useful for making sure DOM is loaded before proceeding with script
*/

/*
	This is the main function
		- called on jQuery's ready event
*/
$((function() {
	
	var $pageurl = window.location;
	var $lang = $pageurl.toString().indexOf("fr") != -1 ? "fr" : "en";
	
	
	/*
		Bingo Centres section
	*/
	if (document.body.id.indexOf("centres") != -1) {
		
		//alert("test");
		
		(function() {
			var $expanders = $(".centre h3 span, .expander").css('cursor', 'pointer');
			var $expandables = $(".expandable").hide();
			
			$(".centre h3 span").hover(function() {
				$(this).next().addClass('hover');
			}, function() {
				$(this).next().removeClass('hover');
			});
			
			$expanders.click(function() {
				var $parent = $(this).parents(".centre");
				var $expandable = $parent.find(".expandable");

				if (!$expandable.is(":hidden")) {
					$parent.removeClass('selected');
					$expandable.stop().animate({height: 0, opacity:0}, 500, "swing", function() {
						$(this).css({display:"none", height:'auto', opacity:1});
					});
				}
				else {
					$parent.addClass('selected');
					var height = $expandable.height();
					$expandable.stop().css({display:"block", height:0, opacity:0}).animate({height: height, opacity:1}, 500, function() {
						$(this).css('height', 'auto');
					});
				}
				return false;
			});
		})();
		
		// Location Selector:
		(function() {
			var $select = $(".find select");
			var $headers = $(".find .headers").hide();
			var $table = $(".find .additional-centres").hide();
			/*var $table = $(".find table");
			var $headers = $(".find table thead");
			var $rows = $(".find table tr");*/
						
			$select.find("option").each(function(i) {
				$(this).data('id', i);
			});
			
			$select.change(function() {
				var $this = $(this);
				var $selected = $(this).find("option:selected");
				var id = $selected.data('id');
				if (id == 0) {
					$headers.css('display', 'none');
					$table.css('display', 'none');
				}
				else {
					$headers.css('display', 'block');
					$table.css('display', 'block');
					$table.find("div.centre:visible").css('display', 'none');
					$table.find("div.centre:eq(" + Number(id - 1) + ")").css('display', 'block');
					/*$table.find("tbody tr:visible").css('display', 'none');
					$table.find("tbody tr:eq(" + Number(id - 1) + ")").css('display', 'block');*/
				}
			});
		})();
		
		/*
		(function() {
			var $select = $(".find select");
			var $headers = $(".find .headers");
			var $table = $(".find table");
			var $tableData = $table.find("tbody");
						
			$select.find("option").each(function(i) {
				$(this).data('id', i);
			});
			
			$select.change(function() {
				var $this = $(this);
				var $selected = $(this).find("option:selected");
				var id = $selected.data('id');
				if (id == 0) {
					$table.css('display', 'none');
				}
				else {
					$table.css('display', 'block');
					$tableData.find("tr:visible").css('visibility', 'collapse');
					$tableData.find("tr:eq(" + Number(id - 1) + ")").css('visibility', 'visible');
				}
			});
		})();
		*/
		
		/*(function() {
			$.fn.textCounting.defaults.countDirection = "up";
			$(":input[type=textarea]").textCounting();
			
			return false;
		})();*/
	};
	
	if (document.body.id.indexOf("faq") != -1) {
	
 		//if($.browser.msie && $.browser.version=="6.0") {
			//alert("Im IE6");
		//} else {
			(function() {
				var $expanders = $("h3").css('cursor', 'pointer').append(' <span class="plus">+</span>');
				$expanders.next().hide();
				/*$expanders.each(function() {
					var $this = $(this);
					var $expandable = $this.next();
					
					$expandable.data('trueHeight', $expandable.height());
				});*/
				
				$expanders.click(function() {
					var $this = $(this);
					var $expandable = $this.next();
					
					if (!$expandable.is(':hidden')) {
						$this.removeClass('selected');
						$expandable.stop().animate({height: 0, opacity:0}, 500, "swing", function() {
							$(this).css({display:"none", height:"auto", opacity:1});
						});
						$expandable.data('visible', false);
					}
					else {
						$this.addClass('selected');
						var height = $expandable.height();
						$expandable.stop().css({display:"block", height:0, opacity:0}).animate({height: height, opacity:1}, 500, function() {
							$(this).css('height', 'auto');
						});
					}
					return false;
				});
			})();
		//}

		
		
	}
	
	
}));
