(function ($) {
    $(document).ready(function () {

        /*-----------------Main Menu------------------------*/

        //Initializing the default menu(remove default styles)
        $("#zz1_TopNav div").removeAttr("class");
        $("#zz1_TopNav ul li").removeAttr("class");
        $("#zz1_TopNav ul").removeAttr("class");
        $("#zz1_TopNav ul ul").removeAttr("class");
        $("#zz1_TopNav ul ul").removeAttr("style");

        //Display second level navigation for a particular time range. This feature is not currently visible 
        $('#zz1_TopNav > div > ul > li > ul > li').each(function () {
            var pagename = $(".breadcrumbs td:last").children('a').html();

            if ($(this).children('a').find('.menu-item-text').html() == pagename) {
                $(this).parent().show().delay(0).hide();
            }
        });

        //Hilight first level navigation 
        $('#zz1_TopNav > div > ul > li').each(function () {
            var pagename = $(".breadcrumbs td:eq(1)").children('a').html();

            if ($(this).children('a').find('.menu-item-text').html() == pagename) {
                $(this).children('a').find('.menu-item-text').addClass('select');
            }
        });

        //Remove breadcrumb node events
        $("td[id*='PlaceHolderTitleBreadcrumb']").removeAttr("onmouseover");
        $("td[id*='PlaceHolderTitleBreadcrumb']").removeAttr("onkeyup");
        $("td[id*='PlaceHolderTitleBreadcrumb']").removeAttr("onmouseout");

        var ismouseout = true;

        $('#zz1_TopNav > div > ul').mouseleave(function () {
            ismouseout = true;

            if (ismouseout)
                $('#zz1_TopNav > div > ul li ul').delay(5000).fadeOut(1000,function(){$('#header').css('z-index','5');});
        });

        $('#zz1_TopNav > div > ul').mouseenter(function () {
	     $('#header').css('z-index','100');
	     
            $('#zz1_TopNav > div > ul li ul').css('z-index', '1');
            $(this).children('ul').clearQueue().stop().css('z-index', '2000').fadeTo(1, 10);
        });

        $('#zz1_TopNav > div > ul > li').mouseenter(function () {
		 $('#header').css('z-index','100');
		 
            ismouseout = false;

            $('#zz1_TopNav > div > ul li ul').css('z-index', '1');
            $(this).children('span').css('color', '#ffffff');
            $(this).children('ul').clearQueue().stop().css('z-index', '2000').fadeTo(1, 10, function () {
                $('#zz1_TopNav > div > ul li ul').not(this).hide();
                $(this).show();
            });

            if ($(this).children('ul').length < 1) {
                $('#zz1_TopNav > div > ul li ul').clearQueue().stop().hide();
            }
        });

        $('#zz1_TopNav > div > ul li ul').mouseenter(function () {
		 $('#header').css('z-index','100');
            ismouseout = false;

            $('#zz1_TopNav > div > ul li ul').clearQueue().stop();
        });

        $('#zz1_TopNav > div > ul li ul').mouseleave(function () {
            ismouseout = true;

            if (ismouseout)
                $('#zz1_TopNav > div > ul li ul').delay(4000).fadeOut(1000,function(){$('#header').css('z-index','5');});
        });

        $('#zz1_TopNav > div > ul > li').click(function () {
            $('#zz1_TopNav > div > ul > li').children('a').find('.menu-item-text').removeClass('select').addClass('deselect');
            $(this).children('a').find('.menu-item-text').removeClass('deselect').addClass('select');
        });


        /*-----------------Left Menu------------------------*/
        if ($(".menu-vertical ul li:has(ul)")) {
		
            //Add plus image, find class and hide all ul's
            $(".menu-vertical ul li").has("ul").prepend("<div class='PlusImg'></div>");
            $(".menu-vertical ul li").has("ul").addClass('find');
            $(".menu-vertical ul li").not(".find").prepend("<div></div>");

		$(".menu-vertical>ul>li>ul").each(function(){
			if($(this).children('.find').length == 0){
				$(this).children('li').children('div').remove();
			}
		});
            var n = 0;
            //Expand menu 		
            $("div.PlusImg").live("click", function () {
                //remove plus image of clicked item and add minus image
                $(this).removeClass('PlusImg').addClass('MinusImg');


                $(this).parent("li").filter(".open").removeClass('open');

                $(this).parents("li").filter(".close").removeClass('close');

                $(this).parents('.find').filter(':first').addClass('open');

		//$(this).parent().filter('.open').children('ul').css('display', 'block');
                $(this).parent().children('ul').css('display', 'block');


                if ($(this).parent().parent('ul').filter('.root').attr('id') == '') {
                    $(this).parent().parent('ul').attr('id', 1);
                    $(this).parent().children('ul').attr('id', 2);
                }
                else {
                    var no = $(this).parent().parent('ul').attr('id');
                    $(this).parent().children('ul').attr('id', ++no);
                }

                if ($(this).parent().children('ul').attr('id') == 100) {
                    $(this).parent().children('ul').children('li').children('div').removeClass('PlusImg');
                }

                $(this).parent().siblings('.open').removeClass('open').addClass('close').children('ul').hide();
                $(this).parent().siblings('.close').children('div').removeClass('MinusImg').addClass('PlusImg');

//                if ($(this).parent().children('ul').children('li').children('div').attr('class') == '') 
//                {
//                        $(this).parent().children('ul').children('li').children('div').each(function () {
//                        if (!$(this).hasClass("MinusImg") && !$(this).hasClass("PlusImg")) {
//                           $(this).parent().children('ul').children('li').css('padding', '0px');
//                           $(this).css('display', 'none');
//                           $(this).parent().children('ul').children('li').children('a').css('margin', '0px');
//                           $(this).parent().children('ul').children('li').children('a').css('margin-left', '15px');
//                        }
//                    });
//                }

            });

            //Minimise menu 
            $("div.MinusImg").live("click", function () {
                $(this).removeClass('MinusImg').addClass('PlusImg');

                $(this).parent('.find').filter(':first').removeClass('open').addClass('close');


                $('.close').children('ul').hide();


            });
			
			expandNode($('ul.root'));   
        }
		
	     

	function expandNode(nodeul){
		var currentBreadNode = 1;
		var breadNodeString = $('.breadcrumbs:last > table > tbody > tr > td:last a').text().trim();
		var nodeCount = $('.breadcrumbs:last > table > tbody > tr > td').length-1;
		var matchedNode = nodeul;
		var found = false;
		
		if(breadNodeString != "" && nodeCount > 0)
		{
			while(currentBreadNode != nodeCount){
				//incerement node count to get the node from the breadcrumb
				currentBreadNode++;
				
				//get the next node string to match
				breadNodeString = $('.breadcrumbs:last > table > tbody > tr > td:eq('+currentBreadNode+') a').text().trim();
				
				if(breadNodeString == "")
					continue;
					
				matchedNode.children('li').each(function(){
					if($(this).children('a').find('span.menu-item-text:eq(0)').text().trim() == breadNodeString)
					{
						$(this).children('div.PlusImg').removeClass('PlusImg').addClass('MinusImg');
						$(this).children('ul').css('display','block');
						//alert(breadNodeString + "---" + $(this).children('a').text());	
						matchedNode = $(this).children('ul');
					}
				});
			}
		}
	}
	
       //Display the left navigation according to the breadcrumb
       /* var lastBreadcrumbNode = $('.breadcrumbs:last > table > tbody > tr > td:last a').text().trim();
		
        $('.menu-vertical span.menu-item-text').each(function () {
            if ($(this).text().trim() == lastBreadcrumbNode) {
                $(this).parents('ul').show();
                $(this).parents('ul').filter('div.PlusImg').removeClass('PlusImg').addClass('MinusImg');
		  $(this).parents('li').filter(':first').children('ul').show();
                $(this).parents('ul').siblings().filter('div.PlusImg').removeClass('PlusImg').addClass('MinusImg');
		  $(this).parents('li').filter(':first').children('ul').show().siblings().filter('div.PlusImg').removeClass('PlusImg').addClass('MinusImg');
            }
        });*/


	if(jQuery("table[id*='PlaceHolderTitleBreadcrumb'] td:last span").length > 0){
		var lastBreadcrumText = jQuery("table[id*='PlaceHolderTitleBreadcrumb'] td:last span").text();
		if(lastBreadcrumText.length > 60)
		{
			jQuery("table[id*='PlaceHolderTitleBreadcrumb'] td:last span").text(lastBreadcrumText.substring(0,58) + "..");
		}
	}
    });

})(jQuery);
