
String.prototype.startsWith = function(str) {
    return (this.indexOf(str) === 0);
}
var customMenuRotation = 0;
/*
Commented By : Kumarchand R. Tripathi
Commented On : 27 May 2011
Purpose      : To display the menu
*/
$(document).ready(function() {
    var parent, level, count, functioncallcount = 0;
    id = "#minNavigation";
    $root = $(id + ' li');

    $loc = window.location.toString();
    if ($loc.indexOf('index.htm') > 0) {
        level = '';
        parent = 'home';
        count = -1;
    }

    function RKMathNavigation(currentActiveUrl) {
        for (var k = 0; k < $root.length; k++) {
            $($root[k]).find('a').attr('rev', k);
        }
        //debugger;
        loadNavigation(currentActiveUrl);
        $(id).find('li').click(function(event) {
            window.location.href = $(this).children('a:first').attr('href');
        });
    }

    // Used to get the parent of the current url
    function getCurrentURLParent(currentActiveUrl) {
        var currentURL = currentActiveUrl.replace('http://' + window.location.host, '').replace('https://' + window.location.host, '');
        if (currentURL == "/")
            currentURL = "/home.aspx";

        var anchors = $('#minNavigation li a');
        //Removes all elements from the set of matched elements that do not match the specified function.
        anchors = anchors.filter(function() {
            if (this.href.replace('http://' + window.location.host, '').replace('https://' + window.location.host, '').toLowerCase().endsWith(currentURL.toLowerCase()))
                return true;
            else
                return false;
        });
        if (anchors.length > 0) {
            //once we get the current url anchor tag then do the actions for respective <li> which we are doing on click event of li tag.
            var selectedAnchor = anchors[anchors.length - 1];
            if ($(selectedAnchor).attr('rel') == 'parent' || $(selectedAnchor).attr('rel') != 'level') {
                count = $(selectedAnchor).attr('rev');
                level = '';
            }
            else if ($(selectedAnchor).attr('rel') == 'level') {
                level = $(selectedAnchor).attr('rev');
                var anchorParentLi = selectedAnchor.parentNode.parentNode;
                while (anchorParentLi.tagName != 'LI') {
                    anchorParentLi = anchorParentLi.parentNode;
                }
                count = $(anchorParentLi).find('a:first').attr('rev');
            }

            //get the parent node
            while (anchors.length > 0) {
                var jAnchor = $(anchors[0]);
                if (jAnchor.attr('rel') == 'parent') {
                    return $(anchors[0].parentNode).attr('class');

                }
                var parentli = anchors[0].parentNode.parentNode;
                while (parentli.tagName != 'LI') {
                    if (parentli.tagName == 'BODY') {
                        return "home";
                    }
                    parentli = parentli.parentNode;
                }
                anchors = $(parentli).find('a:first');
            }
        }
        return null;
    }

    function loadNavigation(currentActiveUrl) {
        $(id).find('li a').css({ 'font-weight': 'normal' });
        parent = getCurrentURLParent(currentActiveUrl);

        $parentNode = parent;
        $currentPos = count;

        if (level != undefined) {
            $levelPos = level;
            $($root[$levelPos]).find('a:first').css({ 'font-weight': 'bold' });
        }

        if ($parentNode == null) {
            if (customMenuRotation == 0) {
                //If ActivateParentMenuShownInFrontEnd doesn't find the parent menu, it will fetcth same URL what has been sent
                //and it keeps calling the function. To avoid this, we are restricting the function call only one time
                if (functioncallcount < 1) {
                    functioncallcount += 1;
                    var pagewithpid = currentActiveUrl.replace('http://' + window.location.host, '').replace('https://' + window.location.host, '').split("&")[0];
                    ActivateParentMenuShownInFrontEnd(pagewithpid);
                }
            }

            return;
        }
        else if ($parentNode != null && $parentNode != '' && $parentNode != 'home' && $parentNode != undefined) {
            $(id).attr('class', '');
            $($root).find('a').removeClass('active');
            activateParent($parentNode)
            if ($($root[$currentPos]).children('a:first').attr('rel') != 'parent') {
                if (!$($root[$currentPos]).find('ul li a:first')) {
                    $($root[$currentPos]).find('ul li a:first').addClass('active');
                }
                else {
                    if ($($root[$currentPos]).children('a:first').attr('rel') != 'level') {
                        $($root[$currentPos]).find('a:first').addClass('active');
                    }
                }
            }
            else {
                $($root[$currentPos]).find('ul li a:first').addClass('active');
            }
        } else {
            if ($parentNode == 'home') {
                activateParent($parentNode)
                $($root).find('a').removeClass('active');
                if ($($root[$currentPos]).children('a:first').attr('rel') != 'parent') {
                    if (!$($root[$currentPos]).find('ul li a:first')) {
                        $($root[$currentPos]).find('ul li a:first').addClass('active');
                    }
                    else {
                        $($root[$currentPos]).children('a:first').addClass('active');
                    }
                }
            }
            $(id).find('li.home').find('ul:first').show();
        }
    }

    function activateParent(nodeValue) {
        $(id).attr('class', '');
        $(id).addClass($parentNode + '_main');
        $(id).find('li ul').hide();
        $('li.' + nodeValue).find('ul:first').show();
        if ($($root[$currentPos]).children('a:first').attr('rel') == 'sub') {
            $($root[$currentPos]).children('ul:first').show();

            //Call function to set the session variable which is used to highlight the right menu
            var pageName = $($root[$currentPos]).children(0)[0].pathname;
            //pageName is prefixed with / in firefox but not in IE. So remove / to make compatible
            if (pageName.charAt(0) === '/')
                pageName = pageName.slice(1);
                
            //Highlight right menu
            $('div.rightLinks li').removeClass('active');
            $('div.rightLinks li a:[href$=' + pageName + ']').parent().addClass('active');

            if (level == undefined) {
                $($root[$currentPos]).find('ul li a:first').css({ 'font-weight': 'bold' });
            }
        } else {
            $('li.' + nodeValue).find('ul:first').find('li.sub:first > ul').show();
            if (level == undefined)
                $('li.' + nodeValue).find('ul:first').find('li.sub:first li a:first').css({ 'font-weight': 'bold' });
        }
    }


    RKMathNavigation($loc);


    function ActivateParentMenuShownInFrontEnd(currentUrl) {
        try {
            $.ajax({
                type: "POST",
                url: "/WebService.asmx/GetParentMenuShownInFrontEnd",
                data: "{currentUrl:'" + currentUrl + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    try {
                        var response = (msg.d != null && msg.d != undefined) ? msg.d : msg;
                        //debugger;
                        if (response != null) {
                            if (response != "") {
                                if (response.toLowerCase() != currentUrl.toLowerCase())
                                    RKMathNavigation(response);
                            }
                            else
                            { RKMathNavigation("/home.aspx"); }
                        } else { RKMathNavigation("/home.aspx"); }

                    } catch (e)
                { }
                },
                error: function(msg) {
                    var response = (msg.d != null && msg.d != undefined) ? msg.d : msg;
                    RKMathNavigation("/home.aspx");
                }
            });
        } catch (e) {
            alert(e);
        }
    }

});


function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) { return unescape(y); }
    }
}

