var menuTimer = 0;
var optionTimer = 0;
var imageTimeout = 0;
var imageTimeoutLength = 9500;
var current_image = 0;
var quoteTimeout = 0;
var quoteTimeoutLength = 21000;
var quotes;
var current_quote = 0;

holdOn = function () {
	$(this).children('ul').addClass('hover');
};
leaveSub = function () {
	$(this).children('ul').removeClass('hover');
	menuTimer = window.setTimeout(hideSubMenu, 200);	
};
hideSubMenu = function () {
	$("#new-menu ul li ul:not('.hover')").fadeOut();
};
clearOptions = function () {
	//$("#new-menu ul li:not('.selected') a img.on").fadeOut('fast');
};
menuOn = function () {
	$(this).unbind('mouseleave');
	$("#new-menu ul li").removeClass('selected');
	$(this).parent().addClass('selected');
	$(this).children('img').each(function() {
		$(this).css('display', 'inline');
		if ($(this).parent().parent().children('ul').length > 0) {
			$(this).parent().parent().children('ul').children('li').each(function(){
				//$(this).css('display', 'block');
			});
			$(this).parent().parent().children('ul').fadeIn('fast');
		}
		$(this).parent().bind('mouseleave', menuOff);
		if (optionTimer) {
			window.clearTimeout(optionTimer);
			optionTimer = null;
		}
		optionTimer = window.setTimeout(clearOptions, 50);
	});
};
menuOff = function () {

	$(this).unbind('mouseover');
	menuTimer = window.setTimeout(hideSubMenu, 200);
	$(this).children('img').each(function() {
		//$(this).parent().parent().css('background-position-y', '0');
		$(this).parent().mouseover(menuOn);
	});
};
subMenuOn = function () {
	$(this).unbind('mouseleave');
	$("#new-menu ul li ul li").removeClass('selected');
	$(this).parent().addClass('selected');
	//$("#new-menu ul li ul li:not('.selected') img").css('background-position-x', '0');
	$(this).children('img').each(function() {
		//$(this).css('display', 'block');
		if (optionTimer) {
			window.clearTimeout(optionTimer);
			optionTimer = null;
		}
		
	});
};
subMenuOff = function () {
	optionTimer = window.setTimeout(clearOptions, 50);
	$(this).unbind('mouseover');
	$(this).children('img').fadeOut('fast', function() {
		$(this).parent().mouseover(menuOn);
	});
};
imageRotate = function () {
	var image_count = $(".header-image").length;
	var last_image = parseInt($(".header-image:visible").attr('id').replace("header-image-", ""));
	if ( last_image == image_count ) {
		current_image = "1";
	}
	else {
		current_image = last_image + 1;
		current_image = current_image.toString();
	}
	$("#header-image-" + current_image).css( 'z-index', 5 ).show();
	$("#header-image-" + last_image).fadeOut( 800, function () {
		$("#header-image-" + current_image).css( 'z-index', 10 );
	});
};
imageInit = function () {
	imageTimeout = setInterval ( imageRotate, imageTimeoutLength );
};
quoteRotate = function () {
	var quotes_length = 0;
	for (var i in quotes) {
		quotes_length++;
	}
	current_quote++;
	if (current_quote >= quotes_length) {
		current_quote = 0;
	}
	var new_quote = quotes[current_quote];
	$(".quote-text").fadeOut('fast', function () {
		$(this).text( new_quote.quote );
		$(this).fadeIn('fast');
	});
	$(".quote-url").fadeOut('fast', function () {
		$(this).text( new_quote.link_text ).attr('href', new_quote.url);
		$(this).fadeIn('fast');
	});
	$(".quote-author").fadeOut('fast', function () {
		$(this).html( new_quote.author );
		$(this).fadeIn('fast');
	});
};
quoteInit = function ( quotes_json ) {
	quotes = quotes_json;
	quoteTimeout = setInterval ( quoteRotate, quoteTimeoutLength );
};

$(document).ready( function () {
	$("#new-menu ul li a:not('#new-menu ul li ul li a')").mouseover(menuOn);
	$('#new-menu ul li ul li a').mouseover(subMenuOn);
	$("#new-menu ul li").mouseover(holdOn);
	$("#new-menu ul li").bind('mouseleave', leaveSub);
	$("#new-menu ul li a").css('cursor', 'pointer');
	imageInit();
});

