jQuery(function($) { 
				
	if (!isDefined('showSlideIdx')) {
		showSlideIdx = 0;	
	}
	
	$("div.submenu").each(function(i){
		$(this).hide();		
	});
	
	// Binding click event on all the heading
	$("div.hierarchy_menu div.category_content a").each(function(i) {
		$(this).bind("click", {idx: i, tot_idx: $("div.hierarchy_menu div.category_content a").length, obj: $(this)}, Start);
		//if (i != 1) { // Exclude default 2nd slide
			$(this).bind("mouseover", function() {
				$("div.hierarchy_menu div.category_content a").removeClass('hover'); // clearing all the hover classes from the main headings							   
				$(this).addClass('hover');	
				if ($(this).parent().next().is("div.submenu:visible")) {					
					$(this).removeClass('active');
					$(this).removeClass('hover');
				}
			});
			$(this).bind("mouseout", function() {				
				if ($(this).parent().next().is("div.submenu")) { // If having the sub menu
					if ($(this).parent().next().is("div.submenu:visible")) {
						$(this).addClass('active');	
					} else {
						$(this).removeClass('hover');
					}
				} else {
					// No case now - every main menu will have submenu div with child UL or NO child elements in that
				}				
			});
		//}
	});
	
	// Trigger click to show default slide in OPEN mode
	$("div.hierarchy_menu div.category_content a").eq(showSlideIdx).trigger("click");
	
	// Binding mouseover and mouseout event on all the sub menus
	$("div.hierarchy_menu div.submenu a").each(function(i) {
		$(this).bind("mouseover", {idx: i, tot_idx: $("div.hierarchy_menu div.submenu a").length, obj: $(this)}, subMenuMouseOver);	
		$(this).bind("mouseout", {idx: i, tot_idx: $("div.hierarchy_menu div.submenu a").length, obj: $(this)}, subMenuMouseOut);	
	});
});	


function subMenuMouseOver(event) {
	menuInfo = new Object();
	menuInfo.data = event.data;
	curMenuObj = menuInfo.data.obj; 
	
	if (curMenuObj.parent().prev().children().is("a")) {
		// remove class removeBorderBottomClasses from all sub menus
		$("div.hierarchy_menu div.submenu a").removeClass('removeBottomBorder'); 
		// add removeBottomBorder class to one level up li's anchor tag
		curMenuObj.parent().prev().children().addClass('removeBottomBorder'); 
	} else {
		//alert(curMenuObj.parent().parent().parent().prev().children().text());
		if (curMenuObj.parent().parent().parent().prev().children().is("a")) {
			// remove class removeBorderBottomClasses from all main headings
			$("div.hierarchy_menu div.category_content a").removeClass('removeBottomBorder');
			// add removeBorderBottomClasses class to the main heading
			curMenuObj.parent().parent().parent().prev().children().addClass('removeBottomBorder'); 
		}	
	}
}


function subMenuMouseOut(event) {
	$("div.hierarchy_menu div.category_content a").removeClass('removeBottomBorder'); // remove class removeBorderBottomClasses from all main headings
	$("div.hierarchy_menu div.submenu a").removeClass('removeBottomBorder'); // remove class removeBorderBottomClasses from all sub menus
}


function isDefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


function my_function(objInfo) 
{
	idx = objInfo.data.idx;
	curMenuHeadingObj = objInfo.data.obj;
		
	/* START - Actions on previously selected heading */
	
	// Closing sub-menu of the previous heading
	$("div.submenu:visible").slideUp(200);
	
	// Start - Code to get info of previously selected element
	activeClassIdx = $("div.submenu:visible").prev().children().attr('id');	
	if (!activeClassIdx) {
		activeClassIdx = $($("div.hierarchy_menu div.category_content").find("a.active")).attr('id');
		if (!activeClassIdx) {			
			activeClassIdx = "slide_" + idx;
		}
	}	
	activeClassIdx = activeClassIdx.split('_');
	
	// End 
	
	// Adding / Removing class from the previous active element
	$("div.hierarchy_menu div.category_content").find("a.active").addClass('slide_' + activeClassIdx[1]);
	$("div.hierarchy_menu div.category_content").find("a.active").removeClass('slide_' + activeClassIdx[1] + '_active active');
	$("div.hierarchy_menu div.category_content").find("a.no_menu_active").addClass('no_menu');
	$("div.hierarchy_menu div.category_content").find("a.no_menu").removeClass('no_menu_active');
	
	/* END -  Actions on previously selected heading */
	
	/* START - Actions on new selected heading */
	
	if (activeClassIdx[1] != (idx+1)) {
		// Opening sub-menu of the new heading
		if ($("div.submenu").eq(idx).children().size() > 0) {
			$("div.submenu").eq(idx).slideDown(200);
		}
				
		// Adding / Removing class to the new element
		$("div.hierarchy_menu div.category_content a").eq(idx).removeClass('slide_'+(idx+1));
		
		if ($("div.hierarchy_menu div.category_content a").eq(idx).hasClass('no_menu')) {
			$("div.hierarchy_menu div.category_content a").eq(idx).removeClass('no_menu');
			$("div.hierarchy_menu div.category_content a").eq(idx).addClass('slide_'+(idx+1)+'_active no_menu_active');
		} else {
			$("div.hierarchy_menu div.category_content a").eq(idx).addClass('slide_'+(idx+1)+'_active active');
		}
	}
	
	/* END -  Actions on new selected heading */
}


var timerID = 0;
var tStart  = null;

function UpdateTimer(objInfo) {
	if(timerID) {
	  clearTimeout(timerID);
	  clockID  = 0;
	}

	if(!tStart)
	  tStart   = new Date();

	var tDate = new Date();
	var tDiff = tDate.getTime() - tStart.getTime();

	tDate.setTime(tDiff);
	
	$("#timer").val(parseInt($("#timer").val()) + 1);

	if ($("#timer").val() < 2) {
		timerID = setTimeout("UpdateTimer(objInfo)", 150);	
	} else {
		my_function(objInfo);
	}	 
}

function Start(event) {
	//alert('In Start ' + event.data.idx);
	objInfo = new Object();
	objInfo.data = event.data;
	
	tStart   = new Date();
	//document.getElementById('timer').value = "0";
	$("#timer").val("0");
	timerID  = setTimeout("UpdateTimer(objInfo)", 150);
}

function Stop() {
	if  (timerID) {
	  clearTimeout(timerID);
	  timerID  = 0;
	}
	tStart = null;
}

function Reset() {
   tStart = null;
   document.getElementById('timer').value = "0";
}
