// JavaScript Document


// Get the menu item
$(document).ready(function(){


	var selectedClass = 'ui-tabs-selected';
	var ItemidString = jQuery.url.param("Itemid");
	if (ItemidString) {
		process(ItemidString);
	}


	function process(thisEle) {
		var Itemid = parseInt(thisEle);
		var endUrlString = "Itemid="+Itemid; // current url paramter
		var clickA = $(".nav_txt").find("a[href$="+endUrlString+"]"); // The clicked a tag
		if (clickA.length == 0) { // if not the mainmenu
			$('.menu_ui-tabs-nav li').addClass(selectedClass);

		} else { // else is in the mainmenu
			var firstChildren = clickA.find('span:first').html(); // set the first children to itself


			var childrenEle = clickA.next();
			if (childrenEle.html() != null) {
				var firstChildrenNew = childrenEle.find('span:first').html();
				clickA = childrenEle.find("li:first > a");
			}

			var clickId = getClickItemid(clickA); // get the current Itemid then pass to the view page
			updateItemUrl(clickId);

			firstChildren = firstChildrenNew ? firstChildrenNew : firstChildren;

			setSubMenuStyle(firstChildren);

			setSubmenuUrl(clickA);// set the click responsiable sub menu url
			
		}

	}

	/**
	 * automatic change the current select tab style
	 */
	function setSubMenuStyle(currentName) {
		$('.menu_ui-tabs-nav').find('a').each(function(){
			var parent = $(this).parent();
			var aTag = parent.find("a");
			var url = aTag.attr("href");
			var url_0 = url.split("Itemid=");
			url_0 = url_0[0];
			if($(this).find('span').html() == currentName) {
				parent.addClass('ui-tabs-selected');

			} else {
				$(this).parent().removeClass(selectedClass);
			}
		})
	}

	function setSubmenuUrl(ele) {
		var topUl = ele.parent().parent(); // top menu ul element
		$('.menu_ui-tabs-nav').find('span').each(function(){
			var subA = $(this).parent();
			var menuName = $(this).html(); // each sub menu name
			topUl.children("li").each(function(){
				var topAUrl = $(this).children("a:first").attr("href");
				var topMenuName = $(this).find("span").html();
				if(topMenuName == menuName) {
					subA.attr("href", topAUrl);
				}
			})
		})
	}

	function getClickItemid(clickA) {
		var Url = clickA.attr("href");
		var Itemid = Url.split("Itemid=");
		Itemid = parseInt(Itemid[1]);// the parent Itemid

		return Itemid;
	}

	// modify the article title link url
	function updateItemUrl(Itemid) {
		// Iterate get module item title
		$(".moduleItemTitle").each(function(){
//			var thisUrl = $(this).attr("href//");
//			var newUrl = thisUrl + "&Itemid=//"+Itemid
//			$(this).attr("href//", newUrl);
		})
	}

	/**
	 * make the more link the same as the title link
	 */
	$(".moduleItemReadMore").click(function(){
//		var parent = $(this).parent().parent();// get the parent element
//		var titleEle = parent.find('a:first//');// get the title element
//		$(this).attr("href//", titleEle.attr('href//'));// pass the title href to more href
	})

	// update all the url links to the articles position
	$("#left").find("a").each(function(){
		//if($(this).attr("href").indexOf("mailto")==-1&&$(this).attr("href").indexOf("javascript")==-1) $(this).attr("href", $(this).attr("href")+"#articles")
	})
	$("#substmap").find("a").each(function(){
		//if($(this).attr("href").indexOf("mailto")==-1&&$(this).attr("href").indexOf("javascript")==-1) $(this).attr("href", $(this).attr("href")+"#articles")
	})

})
