$(document).ready(function() {
	/* navigation */
	$(".sections").find("li").hover(function() {
			var menu = $("ul", this);
			if (menu.length) {
				var position = $(this).position();
				menu.css({"top": (position.top - 7) + "px", "left": (position.left + $(this).innerWidth() - 15) + "px"})
					.stop(true, true)
					.fadeIn("slow");
			}
		}, function() {
			var menu = $("ul", this);
			if (menu) {
				menu.stop(true, true)
					.animate({zIndex: 99}, 500)
					.fadeOut("slow");
			}
		});
	
	/* mainmenu effects */
	$("#main_menu").find("a").each(function() {
		var normalClassName = "normal";
		var activeClassName = "active";
		var el = $(this);
		// not for active links
		if (el.parent().hasClass("current_page_item")) {
			return;
		}
		el.css('backgroundColor', 'rgba(171, 35, 38, 0.0)');
		el.hover(function() {
				el.stop(true, true)
					.removeClass(normalClassName);
				
				if (el.css('backgroundColor') == "transparent") {
					el.css('backgroundColor', "#AB2326");
				}
				
				// for ie :-(
				try {
					el.animate({backgroundColor: '#FFF', color: '#AB2326'}, 900, function() {
						el.addClass(activeClassName)
					});
				} catch(e) {}
			}, function() {
				el.stop(true, true)
					.removeClass(activeClassName);
				// for ie :-(
				try {
					el.animate({backgroundColor: 'rgba(171, 35, 38, 0.0)', color: "#FFF"}, 900, function() {
							el.addClass(normalClassName);
						});
				} catch(e) {
					el.animate({backgroundColor: "#AB2326", color: "#FFF"}, 900, function() {
							el.addClass(normalClassName);
						});
				}
			});
	});
	
	/* sections */
	$(".sections").find("a").each(function() {
		var el = $(this);
		el.hover(function() {
			el.stop(true, true);
			el.animate({color: '#e29d9e'}, 400);
		}, function() {
			el.animate({color: '#992224'}, 400);
		});
	});
	
	/* newsbox */
	var news = {
		// html elements
		listElem: 'li',
		navPrevClass: 'prev',
		navNextClass: 'next',
		headingElem: 'h3',
		textElem: 'p',
		// slideshow
		slideShow: true,
		slideShowTimeout: 5000,
		_slideStatus: true,
		// components
		_newsElem: null,
		_prevButton: null,
		_nextButton: null,
		_heading: null,
		_textBox: null,
		// news
		_messages: [],
		_actual: 0,
		// optimalization
		_headingHeight: 0,
		_textHeight: 0,
		/**
		 * Init function
		 * @param elemId - news element
		 * @return this
		 */
		init: function(elemId) {
			var context = this;
			context._newsElem = $(elemId);
			context._newsElemHeight = context._newsElem.height();
			
			// find components
			var components = context._newsElem.find(context.listElem + ":first");
			context._prevButton = components.find("." + context.navPrevClass);
			context._prevButton.click(function() {
					context.show(context._actual - 1, "fast");
					return false;
				})
				.detach();
			
			context._nextButton = components.find("." + context.navNextClass);
			context._nextButton.click(function() {
					context.show(context._actual + 1, "fast");
					return false;
				})
				.detach();
			
			context._textBox = components.find(context.textElem);
			context._heading = $("<span></span>");
			context._heading.html(components.find(context.headingElem).html());
			
			// find messages
			// hide all
			context._newsElem.find(context.listElem).hide();
			context._newsElem.find(context.listElem).each(function(index) {
				// get max height
				var li = $(this);
				// remove arrows
				if (index > 0) {
					li.find(".nav").detach();
				}
				
				li.show();
				li.find(context.headingElem).show();
				context._headingHeight = Math.max(context._headingHeight, li.find(context.headingElem).height());
				context._textHeight = Math.max(context._textHeight, li.find(context.textElem).height());
				li.hide();
				
				// message
				var m = {
					heading: li.find(context.headingElem).html(),
					text: li.find(context.textElem).html()
				};
				context._messages.push(m);
			});
			context._newsElem.find(context.listElem + ":first").show();
			
			components.find(context.headingElem)
					.empty()
					.append(context._prevButton)
					.append(context._nextButton)
					.append(context._heading);
			
			// set height
			context._newsElem.css('height', (context._headingHeight + context._textHeight + 10) + 'px');
			
			var imgHeight = context._prevButton.find('img').height();
			var imgTopMargin = Math.round((context._headingHeight - imgHeight) / 2);
			context._prevButton.css({'height': context._headingHeight + 'px'});
			context._nextButton.css({'height': context._headingHeight + 'px'});
			context._prevButton.find('img').stop().animate({'margin-top': imgTopMargin + 'px'}, "fast");
			context._nextButton.find('img').stop().animate({'margin-top': imgTopMargin + 'px'}, "fast");
			
			context.show(0, "fast");
			
			// slideShow
			if (context.slideShow && context._messages.length > 1) {
				context._newsElem.mouseenter(function() {
						context._slideStatus = false;
					})
					.mouseleave(function() {
						context._slideStatus = true;
					});
				
				// slideshow callback
				var slideshow = function() {
					if (context._slideStatus) {
						context.show(context._actual + 1, "slow");
					}
					setTimeout(slideshow, context.slideShowTimeout);
				};
				setTimeout(slideshow, context.slideShowTimeout);
			}
			
			return context;
		},
		show: function(i, speed) {
			var context = this;
			// no news
			if (!context._messages.length) {
				return;
			}
			
			// get shown message
			if (!i) {
				i = 0;
			} else if (i < 0 && context._messages.length) {
				i += context._messages.length;
			}
			if (!speed) {
				speed = "slow";
			}
			context._actual = i % context._messages.length;
			
			var newText = context._messages[context._actual].text;
			var newHeading = context._messages[context._actual].heading;

			// hide not animated elements
			context._textBox.parent().find(context.textElem).not(":first").stop().hide();
			
			var newTextBox = $("<" + context.textElem + "></" + context.textElem + ">");
			newTextBox.html(newText).hide();
			context._textBox.after(newTextBox);
			
			context._textBox.stop().fadeOut(speed, function() {
				newTextBox.stop().fadeIn(speed, function() {
					// manipulate elements
					context._textBox.detach();
					context._textBox = newTextBox;
					
					// set max size
				});
				
				// change heading
				context._heading.html(newHeading);
				
				// set navigation position
				var headingHeight = context._heading.height();
				var headingTopMargin = Math.round((context._headingHeight - headingHeight) / 2);
				context._heading.css({'display': 'block', 'padding-top': headingTopMargin + 'px', 'padding-bottom': headingTopMargin + 'px'});
			});
		}
	};
	news.init("#newsbox");
	
	/* archieve */
	$("#archieve").find("li li ul").slideUp();
	$("#archieve").find("li li h3 a").click(function() {
		$(this).parent().parent().find("ul").slideToggle('slow');
		return false;
	});
});
