(function ($) {
	$ (document).ready (function () {
		navigationChilds.init ();							  
	});
	
	var navigationChilds = {
		preferences: {
			showTimeout: 500,
			hideTimeout: 300,
			jsonPath: '/businesscenter/cpe/html0/173690.json'			
		},		
		
		init: function () {
			// search for any 'ul' inside primary links - these are children links, we will not touch them
			$ ('#ibm-primary-links ul').each (function () {
				var parent = $ (this).parent ();
				parent.addClass ('ibm-do-not-touch');
			});
			
			// IE 6+ bug fix - causing no background at the end of navigation
			$ ('#ibm-navigation').css ({ overflow: 'visible' });
			
			// get structure of navigation from JSON
			$.getJSON (navigationChilds.preferences.jsonPath, function (json) {
          		var childs = json['navigation'];
				
				var links = $ ('#ibm-primary-links > li > a').each (function (i) {
				
					var text = $ (this).text (),
						exists = false,
						html = [];
					// create child link navigation
					html.push ('<ul style="display: none">');
					
					var obj = childs [text];
					for (var j in obj) {
						exists = true;
						html.push ('<li><a href="' + obj[j] + '">' + j + '</a></a>');
					}
					
					html.push ('</ul>');
					var parent = $ (this).parent ();
					
					if (exists && $('ul', parent).length == 0) {
						
						parent.append (html.join (''));
					}					
				});
				
				navigationChilds.handlers ();				
        	});
		},
		
		handlers: function () {
			var li = $ ('#ibm-primary-links > li').not ('.ibm-do-not-touch'),
				handler = null,
				outside = null,
				ul = null;
				
			
				
			li.bind ('mouseenter', function() {
				ul = $ ('ul', this);
     			handler = window.setTimeout ( function () {
					ul.addClass ('ibm-childLinks-shown');
					ul.slideDown ( function () {
						// IE 6+ bug fix - causing no background at the end of navigation
						$ ('#ibm-navigation ul:last').css ({ backgroundImage: 'url(//www.ibm.com/i/c.gif)', paddingBottom: '1px' });
					});						  
				}, navigationChilds.preferences.showTimeout);   		
				
			}).bind ('mouseleave', function() {
				window.clearTimeout (handler);     
    		});	
			
			$ ('#ibm-primary-links').bind ('mouseleave', function () {
				outside = window.setTimeout ( function () {
					$ ('#ibm-primary-links ul.ibm-childLinks-shown').slideUp ().removeClass ('ibm-ChildLinks-shown');	
				}, navigationChilds.preferences.hideTimeout );
			}).bind ('mouseenter', function () {
				window.clearTimeout (outside);
			});
		}
	};
}) (jQuery);