$(document).ready(function() {
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop();
		
		if (scrollTop != 0) {
			$("#hd")
			.stop()
			.animate(
				{ opacity: ".3" },
				400
			);
		} else {
			$('#hd')
			.stop()
			.animate(
				{ opacity: "1" },
				400
			);
		}
	});
	
	$('#hd').hover(
		function() {
			var scrollTop = $(window).scrollTop();
			
			if (scrollTop != 0) {
				$(this)
				.stop()
				.animate(
					{ opacity: "1" },
					400
				);
			}
		},
		function() {
			var scrollTop = $(window).scrollTop();
			
			if (scrollTop != 0) {
				$(this)
				.stop()
				.animate(
					{ opacity: ".3"},
					400
				);
			}
		}
	);
	
	$("ul", "#hd").each(function(indexUL, elementUL) {
		var array_search	= function(needle, haystack, argStrict) {
			    // http://kevin.vanzonneveld.net
			    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			    // +      input by: Brett Zamir (http://brett-zamir.me)
			    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			    // *     example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'});
			    // *     returns 1: 'surname'
		
			    var strict = !!argStrict;
			    var key = '';
		
			    for (key in haystack) {
			        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			            return key;
			        }
			    }
				
			    return false;
			},
			aBlinking		= [
				[5], // przyciski w pierwszej linii menu
				[2] // przyciski w drugiej linii menu
			];
		
		$(elementUL)
		.children()
		.each(function(indexLI, elementLI) {
			if (array_search(indexLI + 1, aBlinking[indexUL])) {
				$(elementLI)
				.find("a")
				.bind(
					"toggleColor",
					function()
					{
						var aCl = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec($(this).css("color"));
						
						if (aCl[1] == 255 && aCl[2] == 0 && aCl[3] == 0) {
							$(this).animate(
								{ color: "rgb(116, 74, 0)" },
								{
									duration: 400,
									complete: function() { $(this).trigger("toggleColor"); }
								}
							);
						} else {
							$(this).animate(
								{ color: "rgb(255, 0, 0)" },
								{
									duration: 400,
									complete: function() { $(this).trigger("toggleColor"); }
								}
							);
						}
					}
				)
				.trigger("toggleColor");
			}
		});
	});
});
