/*	Expanding Menus for Indexhibit
 *		uses jquery
 *
 *	Created by Ross Cairns  Mar 2008
 *  Modified by Ivan Razine Aug 2009
*/

/* ++++++++++++++++++ WORKING VERSION 1*/
/*
$(function() { 
	var menuNum = 20;
	for(var i=0;i<menuNum;i++) { expandingMenu(i); }
	$(".section-title").click( function(){
		if($(".section-title").next("div").is(":hidden")) {
			$(".section-title").next("div:visible").slideUp(300);
			$(this).next("div").slideDown(300);
		}
	});
});


function expandingMenu(num) {

	var item_title = $(".section-title").eq(num);	
	var last_title = item_title;
	
	var items = $(".section-title").eq(num).next("div");
	var active_item = $(".section-title").eq(num).next("div").children().children();
	//var active_item = $(".section-title").eq(num).next("div").children();
	if (active_item.is(".active") == false) {
		items.hide();
	}
}
*/
/* ++++++++++++++++++ END OF WORKING VERSION 1 */



/* ++++++++++++++++++ WORKING VERSION 2 */
/*
$(document).ready(function(){
// First we hide all exhibits
$("#menu ul li.section-title").nextAll().hide();
// then the active exhibit is shown
$("#menu ul").each(function(){
	$(this).find("li.active").prevAll().nextAll().show();
});
// This is the toggle function. First it hides all sections
$("#menu ul li.section-title").click(function(){
	$("#menu ul li.section-title").nextAll().hide(300).animate({easing:"linear"});
	$(this).nextAll().show(300);
});

});
*/
/* ++++++++++++++++++ END OF WORKING VERSION 2 */


/* ++++++++++++++++++ MODIFIED ORIGINAL WORKING VERSION 1 */
/*
function expandingMenu(num) {

	var speed = 200;

	//var item_title = $(".section-title").eq(num).next("div");
	//var items = $(".section-title").eq(num).next("div").children().children();
	//var active_item = $(".section-title").eq(num).next("div").children().children();
	
	var item_title = $("#menu ul").eq(num).children(":first");
	
	var items = $("#menu ul").eq(num).children().filter(function (index) { 
		return index > 0; 		
	});

	//items.find('.active').css('background','red');

	//if (items.find('.active').is(".active") == false){
		//items.fadeOut();
		//items.slideUp(speed);		
	//}

	if (items.is(".active") == false) {	//*hide items if not active*
		//items.hide();	
		items.slideUp(speed);
	}
	
	item_title.css({cursor:"pointer"}).toggle(	//*add click functions + pointer to title*
		function () {
			//items.show(speed); //slide + fade
			items.slideDown(speed); //just slide
		}, function () {
			//items.hide(speed); //slide + fade
			items.slideUp(speed); //just slide
		}
	)
}
*/
/* ++++++++++++++++++ END OF MODIFIED ORIGINAL WORKING VERSION 1 */


/* ++++++++++++++++++ ORIGINAL WORKING VERSION 1 */
/*
function expandingMenu(num) {
	var speed = 200;

	var item_title = $("#menu ul").eq(num).children(":first");
	
	var items = $("#menu ul").eq(num).children().filter(function (index) { 
		return index > 0; 		
	});

	
	if (items.is(".active") == false) {	//*hide items if not active*
		items.hide();
	}


	item_title.css({cursor:"pointer"}).toggle(	//*add click functions + pointer to title*
		function () {
			items.show(speed);
			//items.slideToggle('normal');
			
		}, function () {
			items.hide(speed);
		}
	)
}
*/
/* ++++++++++++++++++ END OF ORIGINAL WORKING VERSION 1 */

/* ++++++++++++++++++ Version from here: http://www.michaelhaene.com */

function expandingMenu(num) {
	var speed = 200;
	
	//var item_title = menues.children(":first");
	var item_title = $("#menu ul").eq(num).children(".section-title");
	var menu = item_title.parent("ul");
// add an ID to the item_title object for each li.section-title
	item_title.id="menu_item:" + num;
	
	menu.id="menu:" + num;
	
	var items = menu.children().filter(function (index) { return index > 0; });
	
	/* hide items if not active */
	if ( ( getCookie(menu.id) == '0' || getCookie(menu.id) == 'closed' ) ) {
		items.hide(); //just slide
		//menu.children().filter(function (index) { return index > 0; }).hide();
	}

	/* add click functions + pointer to title */
	item_title.css({cursor:"pointer"}).click(
		function () {
			//items.show(speed);
			if ( (getCookie(menu.id) == '0' || getCookie(menu.id) == 'closed' ) ) {
				var state = 'open';
				//items.show(speed); // fade and slide
				//items.slideDown(speed); //just slide
				
				items.slideDown(speed,function(){ //slide down and callback
									$('#panel').jScrollPane();
				});
							
							
			} else {
				var state = 'closed';
				//items.hide(speed); // fade and slide
				//items.slideUp(speed); //just slide
				
				items.slideUp(speed,function(){ //slide down and callback
									$('#panel').jScrollPane();
				});	
			}
			setCookie(menu.id, state, 1);
		}
	)
}



function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
	((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + '; path=/';
}

function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")

		if (c_start != -1)
		{ 
			c_start = c_start + c_name.length + 1 
			c_end = document.cookie.indexOf(";", c_start)
			
			if (c_end == -1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		} 
	}
	return 0;
}

