﻿
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function () {
	log.history = log.history || [];   // store logs to an array for reference
	log.history.push(arguments);
	if (this.console) {
		arguments.callee = arguments.callee.caller;
		var newarr = [].slice.call(arguments);
		(typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
	}
};
// make it safe to use console.log always
(function (b) { function c() { } for (var d = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","), a; a = d.pop(); ) { b[a] = b[a] || c } })((function () {
	try
{ console.log(); return window.console; } catch (err) { return window.console = {}; } 
})());

// place any jQuery/helper plugins in here, instead of separate, slower script files.
(function ($) {

	// Has children class
	$('nav li').has('ul').children('a').addClass('has-children').append('<span class="arrow"></span>');

	if (Modernizr.touch) return;

	//$(function(){ // dom ready shortcut if added above menu

	$("nav ul").css({ paddingLeft: '0px' });
	$("nav li>ul").each(function (i, el) {
		$(this).data('originalWidth', $(this).width());
		//$(this).children('ul').css('width', $(this).width());
		//left: 174px;
		$(this).css({
			//width: $(this).width() + 'px',
			position: 'absolute',
			display: 'block',
			left: '174px'
		});
		log($(this));
	});

	$("nav li").css({ width: '170px' }); // helps it stay wider when extended

	$("nav li ul li").css({width: '180px'}); 
	$("nav li ul li ul").css({left: '184px'});
	
	$("nav li ul").css({
		width: '0',
		visibility: 'hidden'
	});


	var navTimeout = null;
	var navClosing = null;

	$("nav li").has('ul').hover(
			function () {

				//if (navClosing) log(navClosing.get(0) == $(this).get(0), navClosing.get(0), $(this).get(0));

				if (navClosing && navClosing.get(0) == $(this).get(0)) clearTimeout(navTimeout); // don't close the one we are on if we roll over it again
				if (navClosing && navClosing.get(0) != $(this).get(0)) {
					closeSubNav(navClosing); // If the closing item is there 
				}

				if (!$(this).children('ul').length) return;
				//log($(this).children('ul').eq(0));
				$(this).addClass("open");
				$(this).children('a').addClass("open");

				$(this).children('ul').eq(0)
					.css({ visibility: 'visible', zIndex: 1000 })
					.stop() // Stop previous animation
					.animate({
						width: $(this).children('ul').data('originalWidth') + 'px'
					}, {
						queue: false,
						duration: 500,
						easing: 'easeOutSine',
						complete: function () {
							$(this).css({
								overflow: 'visible'
							});
						}
					});
			}, function () {

				var sub_ul = $(this); // Get this one
				navClosing = sub_ul; // store it as closing nav item

				navTimeout = setTimeout(function () { closeSubNav(sub_ul) }, 400);


			});

	function closeSubNav(item) {

		item.removeClass("open");
		item.children('a').removeClass("open");
		item.children('ul')
				.css({ zIndex: 999 })
				.stop()
				.animate({
					width: '0px'
				}, {
					queue: false,
					duration: 200,
					easing: 'easeInSine',
					complete: function () {
						$(this).css({
							visibility: 'hidden',
							width: '0px',
							overflow: 'visible'
						});

						// Tell everybody we are closed
						navClosing = null;
					}
				});
	}


	//}); // dom ready 
})(jQuery);
