/** Menu Item animation javascript, (c) Fabian Valka and Clemens Scott 2009 */

$(document).ready(
	function(){
		var levels=1;
		var statusArray = new Array();

		// Go through each level
		for(var loopCounter = 0; loopCounter<levels; loopCounter++) {

			// default hide
			$(".toggle_box" + loopCounter).each(function() {
								   $(this).hide();
								   });

			 // Stores the current open/closed status
			 statusArray[loopCounter] = new Array();


			 // go through each link and attach a event listener
			 $(".toggle_href" + loopCounter).each(function(i) {
					$(this).bind('click', {index:i,curLevel:loopCounter}, function(e){

						 // Iterate through all toggle boxes on this level in the form
						 $(".toggle_box" + e.data.curLevel).each(function(divIndex) {
								// if the found box is the one that has been clicked, toggle it
								if(e.data.index==divIndex) {
									if(!statusArray[e.data.curLevel][divIndex]) {
										$(this).animate({ height: 'show', opacity: 'show' }, 'slow');
										//$(this).show();

										//alert('Showing: ' + e.data.curLevel + '/' + divIndex);

										statusArray[e.data.curLevel][divIndex] = true;
									} else {
										$(this).animate({ height: 'hide', opacity: 'hide' }, 'slow');

										// Hide the sub boxes of this box
										hideSubBoxes(e.data.curLevel,levels);

										statusArray[e.data.curLevel][divIndex] = false;
									}

								// close all the others, nice and slow
								} else {
									if(statusArray[e.data.curLevel][divIndex]) {
										$(this).animate({ height: 'hide', opacity: 'hide' }, 'slow');

										hideSubBoxes(e.data.curLevel,levels);

										statusArray[e.data.curLevel][divIndex] = false;
									}
								}
							});
					  });

				});
		}

                // Hides all the boxes below the given level, until the level levels has been reached
		function hideSubBoxes(curLevel, levels) {
			for(var i = curLevel+1; i <=levels; i++) {

					// iterate through the list of subboxes
					$(".toggle_box" + i).each(function(index) {
						   if(statusArray[i][index]) {
							   $(this).animate({ height: 'hide', opacity: 'hide' }, 'slow');
						   }


						   statusArray[i][index] = false;
					});
			}


		}
	}
 );


