﻿$(document).ready(function() {

    $(function() {
        $('.subNavigation').css({ 'display': 'none' });
        // read cookie if there is one
        var cookie = readCookie('IDNMenu');
  
        if (cookie != null && cookie != "root") {

            $('ul#' + cookie).parents('ul').not('ul#root').css({ 'display': 'block' });
            $('ul#' + cookie).css({ 'display': 'block'});
            $('ul#' + cookie).children().css({ 'display': 'block' });
        }


    });
    // preload images     
    $(function() {
        $('li > a').click(function(event) {

            if (this == event.target) {
                var state = $(this).next('ul').attr('id');
   
                eraseCookie('IDNMenu');
                createCookie('IDNMenu', state, 1);
                if ($(this).next('ul').is(':hidden')) {
                    $(this).next('ul').toggle('slow');
                    if ($(this).attr('href') == "") {
                        return false;
                    } else {
                        return true;
                    }

                } else {

                    $(this).next('ul').toggle('slow');
                    if ($(this).attr('href') == "") {
                        return false;
                    } else {
                        return true;
                    }
                }
            }
            return true;
        }).css({ cursor: 'pointer' }); // End query
    }); // End function 
    $.preloadCssImages();
    //    $.tinysort.defaults.order = "asc";
    //    $.tinysort.defaults.attr = "href";
    //    $(function() {
    //        // used to turn off/on navigation sorting

    //        var off = document.getElementById('off');
    //        if (off == null)
    //            var lists = $('ul#root > li ul').get();
    //        for (var i = 0; i < lists.length; i++) {
    //            var id = lists[i].getAttribute('id');
    //            $('ul#' + id + '>li').tsort('a', { order: 'asc', attr: 'href' });
    //        }
    //        $('ul#IMS-Training>li').tsort('a', { order: 'asc', attr: 'title' });
    //        //$('ul#After-Booking-Confirmation>li').tsort("a[title]", { orderby: "title" });
    //    });

});                     // End document ready

// Utility Cookie functions
function createCookie(name,value,days) {
    if (days) {
	    var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

