// jquery Configuration

$(document).ready(function(){   
	
	// main menu
	
	// add unique class to main menu lists
	$("#menu ul > li.page_item > a").each( function(i) {
		$(this).addClass('menu' + i);
	});
	
	// put class on 1st level <li>, when you are on third level 
	if ( $("#menu ul > li > ul > li > ul > li.current_page_item") ) {
		$("#menu ul > li > ul > li > ul > li.current_page_item").parent().parent().parent().parent().addClass("current_page_parent");		
	}
	
	// put class on 1st level <li>, when you are on forth level 
	if ( $("#menu ul > li > ul > li > ul >li.current_page_parent") ) {
		$("#menu ul > li > ul > li > ul > li.current_page_parent").parent().parent().parent().parent().addClass("current_page_parent");
	}
	
	
	// submene
	
	// add class on first <li> of submenu
	//$("#submenu li:first").addClass("firstLi");
	
	// submenu & submenu2 hover animation
	$("#submenu li a, #submenu2 li a").mouseover( function() {
		$(this).animate({
			marginLeft: "5px",
			color: "#1b86c8"
		}, 150);
		/*$(this).parent().animate({
			backgroundColor: "#e5f0f7"
		}, 200);*/
	});
		
	$("#submenu li a, #submenu2 li a").mouseout( function() {
		$(this).animate({
			marginLeft: "0px",
			color: "#444444"
		}, 150);
		/*$(this).parent().animate({
			backgroundColor: "#fffff"
		}, 200);*/
	});
	
	$("#submenu li.current_page_item a, #submenu2 li.current_page_item a, #leftcolumn ul.sufiVersion li ul li:first a").mouseout( function () {
		$(this).animate({
			color: "#1b86c8"
		}, 150);
	});
	
	// submenu2 (Stand Up for Israel submenu) and 3rd level <ul> on submenu
	
	$("#submenu2 ul").css("display", "none");
	
	$("#submenu ul li:has(ul) > a").toggle(
		function() {
			$("#submenu ul ul").slideDown("slow");
		},
		function() {
			$("#submenu ul ul").slideUp("slow");
		}
	);
	
	// open close menu
	$("#submenu2 > li").each(function(i) {
		$("#submenu2 > li:eq(" + i + "):has(ul) > a").toggle( 
			function() {
				$("#submenu2 > li:eq(" + i + ") ul").slideDown("slow");
				$(this).parent().addClass('toggleArrow');
			},
			function() {
				$("#submenu2 > li:eq(" + i + ") ul").slideUp("slow");
				$(this).parent().removeClass('toggleArrow');
			}
		)
	});
	
	// hide arrow bg image if there are no nested ul
	$("#leftcolumn ul#submenu2 > li:not(:has(ul)) > a").css("backgroundImage", "none");
	
	// open second level <ul> when you are in cuurrent page
	$("#leftcolumn ul#submenu2 > li.current_page_parent ul").css('display', 'block');
	$("#leftcolumn ul#submenu2 > li.current_page_parent").addClass('toggleArrow');
	
	$("#submenu2 li a").mouseover( function() {
		$(this).animate({
			marginLeft: "5px",
			color: "#1b86c8"
		}, 150);
	});
	
	
	// This is only for submenu highlighting on sufi
	$("#leftcolumn ul.sufiVersion li ul li:first a").css("color", "#1b86c8");
	
	// side bar Search form
	var textfield = " Search...";
	
	$("#searchform input").val(textfield).css("color", "#999");
	
	$("#searchform input").blur(function() {
		$(this).css("color", "#999");
		if($("#searchform input").val() == textfield || $("#searchform input").val() == "") {
			$(this).val(textfield);
		}
	});
	
	$("#searchform input").focus(function() {
		$(this).css("color", "#444");
		if($("#searchform input").val() == textfield) {
			$(this).val("");
		}
	});
	
	$("#searchform").submit(function() {
      if($("#searchform input").val() == textfield) {
      	$("#searchform input").val("");
      }
   	});
	
	
	// When you are single.php for JCRC news and events, main menu will be highlighted
	if ($("div#content h2").hasClass("onJCRC")) {
		$("#menu > ul > li:eq(4)").addClass("current_page_item");	
	}
	
	// When you are single.php for SUFI news and events, main menu will be highlighted
	if ($("div#content h2").hasClass("onSUFI")) {
		$("#menu > ul > li:eq(1)").addClass("current_page_item");	
	}
	
	
	$('a[href*=#]').click(function() {
    	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body')
					.animate({scrollTop: targetOffset}, 1000);
					return false;
			    }
		}
	});

	

});