(function($){
	$(function() {
		var TAB = 9;
		var ARROW_UP = 38;
		var ARROW_DOWN = 40;
		var ARROW_LEFT = 37;
		var ARROW_RIGHT = 39;		
		var _hideMenu = true;
				
		initTabIndex();
		initTemaSelector();
		initLanguageSelector();
		initFontSizeSelector();
		initFindEmployee();
		initSelectBoxes();
		initPPSelectBoxes();
		
		$('.logo > a')
			.css('outline', 'none')
			.focus();
		
		$('.big-menu > li > a')
			.focus(function(e) {
				$(this).trigger('mouseover');
			})
			.blur(function(e) {
				if (_hideMenu) {
					$(this).trigger('mouseout');
				}
			})
			.keydown(function(e) {
				if (e.keyCode == ARROW_DOWN) {
					e.preventDefault(); // to stop scrolling down
					_hideMenu = false;
					$(this).next('.big-popup').find('.pp-menu.mn-left a:first').focus();
				}
		});
		
		// first 8 links
		$('.pp-menu.mn-left a').keydown(function(e) {
			_hideMenu = true;
			
			if (e.shiftKey && e.keyCode == TAB) {
				$(this).parents('.big-popup').prev().focus();
				return false;
			}
			if (e.keyCode == TAB) {
				findLastLinkInTheMenu($(this)).focus();
			}
			else if (e.keyCode == ARROW_UP) {
				e.preventDefault();
				var prevA = $(this).parent('li').prev('li').find('a');
				if ($(prevA).size() > 0) {
					$(prevA).focus();
				} else {
					$(this).parents('.box-left').find('.pp-menu.mn-left a:last').focus();
				}
			}
			else if (e.keyCode == ARROW_DOWN) {
				e.preventDefault();
				var nextA = $(this).parent('li').next('li').find('a');
				if ($(nextA).size() > 0) {
					$(nextA).focus();
				} else {
					$(this).parents('.box-left').find('.pp-menu.mn-left a:first').focus();
				}
			}
			else if (e.keyCode == ARROW_RIGHT) {
				var rightA = $(this).parents('.box-left').find('.pp-menu.mn-rgt.pd-bott a:first');
				if ($(rightA).size() > 0) {
					$(rightA).focus();
					return;
				} 
				rightA = $(this).parents('.box-left').find('.pp-menu.pos-abs a:first');
				if ($(rightA).size() > 0) {
					$(rightA).focus();
				}
			}
		});
		
		$('.pp-menu.mn-rgt.pd-bott a').keydown(function(e) {
			_hideMenu = true;
			
			if (e.shiftKey && e.keyCode == TAB) {
				$(this).parents('.big-popup').prev().focus();
				return false;
			}
			if (e.keyCode == TAB) {
				findLastLinkInTheMenu($(this)).focus();
			}
			else if (e.keyCode == ARROW_UP) {
				e.preventDefault();
				var prevA = $(this).parent('li').prev('li').find('a');
				if ($(prevA).size() > 0) {
					$(prevA).focus();
				} else {
					$(this).parents('.box-left').find('.pp-menu.pos-abs a:last').focus();
				}
			}
			else if (e.keyCode == ARROW_DOWN) {
				e.preventDefault();
				var nextA = $(this).parent('li').next('li').find('a');
				if ($(nextA).size() > 0) {
					$(nextA).focus();
					return;
				} 
				var nextA = $(this).parents('.box-left').find('.pp-menu.pos-abs a:first');
				if ($(nextA).size() > 0) {
					$(nextA).focus();
				} 
			}
			else if (e.keyCode == ARROW_LEFT) {
				$(this).parents('.box-left').find('.pp-menu.mn-left a:first').focus();
			}
		});
		
		$('.pp-menu.pos-abs a').keydown(function(e) {
			_hideMenu = true;
			
			if (e.shiftKey && e.keyCode == TAB) {
				$(this).parents('.big-popup').prev().focus();
				return false;
			}
			if (e.keyCode == TAB) {
				findLastLinkInTheMenu($(this)).focus();
			}
			else if (e.keyCode == ARROW_UP) {
				e.preventDefault();
				var prevA = $(this).parent('li').prev('li').find('a');
				if ($(prevA).size() > 0) {
					$(prevA).focus();
				} else {
					$(this).parents('.box-left').find('.pp-menu.mn-rgt.pd-bott a:last').focus();
				}
			}
			else if (e.keyCode == ARROW_DOWN) {
				e.preventDefault();
				var nextA = $(this).parent('li').next('li').find('a');
				if ($(nextA).size() > 0) {
					$(nextA).focus();
				} else {
					$(this).parents('.box-left').find('.pp-menu.mn-rgt.pd-bott a:first').focus();
				}
			}
			else if (e.keyCode == ARROW_LEFT) {
				$(this).parents('.box-left').find('.pp-menu.mn-left a:first').focus();
			}
		});
		
		$('.pp-search-bar .txt').keydown(function(e) {			
			if (e.shiftKey && e.keyCode == TAB) {
				$(this).parents('.big-popup').find('.pp-menu.mn-left a').focus();
				return false;
			}
		});
		
		function findLastLinkInTheMenu(currA) {
			var lastA = $(currA).parents('.big-popup').find('.pp-menu.pos-abs a:last');
			if ($(lastA).size() > 0) {
				return lastA;
			} 
			lastA = $(currA).parents('.big-popup').find('.pp-menu.mn-rgt.pd-bott a:last').focus();
			if ($(lastA).size() > 0) {
				return lastA;
			}
			return $(currA).parents('.big-popup').find('.pp-menu.mn-left a:last');
		}
		
		function initTabIndex() {
			var index = 1;
			$('.logo > a').attr('tabindex', index);
			
			$('.m-sites a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
			
			$('.lang-select a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
			
			$('.fsizer a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
			
			$('.left-menu a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
		
			$('.right-menu a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
			
			$('.big-menu > li > a').each(function() {
				index++;
				$(this).attr('tabindex', index);
			});
			
			index++;
			$('#q-text').attr('tabindex', index);
		}
		
		function initTemaSelector() {
			$('.komm-title a')
				.focus(function(e) {
					e.preventDefault();
					$(this).mouseover();
					$('.komm-list a:first').focus();
				}
			);
			
			$('.komm-list-inner a').keydown(function(e) {
				if (e.shiftKey && e.keyCode == TAB) {
					e.preventDefault();
					$('.logo > a').focus();
					$('.komm-list').hide();
					$('.komm-title').removeClass('over');
					return;
				}
				if (e.keyCode == TAB) {
					$('.act-lang a').focus();
				}
				else if (e.keyCode == ARROW_UP) {
					e.preventDefault();
					var prevA = $(this).parent('li').prev('li').find('a');
					if ($(prevA).size() > 0) {
						$(prevA).focus();
					} else {
						$(this).parents('.komm-list-inner').find('a:last').focus();
					}
				}
				else if (e.keyCode == ARROW_DOWN) {
					e.preventDefault();
					var nextA = $(this).parent('li').next('li').find('a');
					if ($(nextA).size() > 0) {
						$(nextA).focus();
					} else {
						$(this).parents('.komm-list-inner').find('a:first').focus();
					}
				}
			});
		}
		
		function initLanguageSelector() {
			$('.act-lang a')
				.focus(function() {
					$(this).mouseover();
					$('.lang-inner a:first').focus();
				}
			);
			
			$('.lang-list a').keydown(function(e) {
				if (e.shiftKey && e.keyCode == TAB) {
					e.preventDefault();
					$('.komm-title a').focus();
					return;
				}
				if (e.keyCode == TAB) {
					e.preventDefault();
					$('.lang-list').hide();
					$('.act-lang').removeClass('over');
					$('#fs_sml').focus();
				}
				else if (e.keyCode == ARROW_UP) {
					e.preventDefault();
					var prevA = $(this).parent('li').prev('li').find('a');
					if ($(prevA).size() > 0) {
						$(prevA).focus();
					} else {
						$(this).parents('.lang-inner').find('a:last').focus();
					}
				}
				else if (e.keyCode == ARROW_DOWN) {
					e.preventDefault();
					var nextA = $(this).parent('li').next('li').find('a');
					if ($(nextA).size() > 0) {
						$(nextA).focus();
					} else {
						$(this).parents('.lang-inner').find('a:first').focus();
					}
				}
			});
		}
		
		function initFontSizeSelector() {
			$('.fsizer a')
				.focus(function(e) {
					$(this).css('outline', '1px dotted #666');
				})
				.blur(function() {
					$(this).css('outline', 'none');
				})
				.keypress(function(ev) {
					if (ev.keyCode == 13) {
						$(this).click();
					}
				});
		}
		
		function initFindEmployee() {
			$('.searchbar-link').focus(function() {
				$(this).mouseover();
				$('#search-text').focus();
			});
			
			$('#search-text').keydown(function(e) {
				if (e.shiftKey && e.keyCode == TAB) {
					e.preventDefault();
					$('.search-over').hide();
					$('.searchbar-link').parent('li').prev('li').find('a').focus();
					return;
				}
				if (e.keyCode == TAB) {
					e.preventDefault();
					$('.search-over').hide();
					$('.searchbar-link').parent('li').next('li').find('a').focus();
				}
			});
		}
		
		function initSelectBoxes() {
			$('.select-scroll,')
				.prepend('<a class="fake-focus-for-selectors" href="#"><img src="/admin/images/nothing.gif" /></a>');
			$('.fake-focus-for-selectors')
				.focus(function() {
					$(this).parent('.select-scroll').find('.act-select').click();				
			});
			
			$('.fake-focus-for-selectors')
				.keydown(function(e) {
					if (e.shiftKey && e.keyCode == TAB) {
						$(this).parent('.select-scroll').find('.act-select').outerClick();
						return;
					}
					if (e.keyCode == TAB) {
						$(this).parent('.select-scroll').find('.act-select').outerClick();
					}
					else if (e.keyCode == ARROW_DOWN) {
						e.preventDefault();
						$(this).parent('.select-scroll').find('.dropdown-list ul a:first').focus();
					}
			});
			
			$('.dropdown-list ul a').keydown(function(e) {
					if (e.shiftKey && e.keyCode == TAB) {
						//e.preventDefault();
						$(this).parents('.select-scroll').find('.act-select').outerClick();
						return;
					}
					if (e.keyCode == TAB) {
						//e.preventDefault();
						$(this).parents('.select-scroll').find('.act-select').outerClick();
					}
					else if (e.keyCode == ARROW_UP) {
						e.preventDefault();
						var prevA = $(this).parent('li').prev('li').find('a');
						if ($(prevA).size() > 0) {
							$(prevA).focus();
						} else {
							$(this).parents('.lang-inner').find('a:last').focus();
						}
					}
					else if (e.keyCode == ARROW_DOWN) {
						e.preventDefault();
						var nextA = $(this).parent('li').next('li').find('a');
						if ($(nextA).size() > 0) {
							$(nextA).focus();
						} else {
							$(this).parents('.lang-inner').find('a:first').focus();
						}
					}
			});
		}
		
		function initPPSelectBoxes() {
			$('.pp-select-scroll,')
				.append('<a class="fake-focus-for-selectors" href="#"><img src="/admin/images/x.gif" /></a>');
			$('.fake-focus-for-selectors')
				.focus(function() {
					$(this).parent('.pp-select-scroll').find('.pp-act-select').click();				
			});
			
			$('.fake-focus-for-selectors')
				.keydown(function(e) {
					if (e.shiftKey && e.keyCode == TAB) {
						//e.preventDefault();
						$(this).parent('.pp-select-scroll').find('.pp-act-select').outerClick();
						$(this).focus();
						return;
					}
					if (e.keyCode == TAB) {
						$(this).parent('.pp-select-scroll').find('.pp-act-select').outerClick();
						$(this).focus();
					}
					else if (e.keyCode == ARROW_DOWN) {
						e.preventDefault();
						$(this).parent('.pp-select-scroll').find('.pp-dropdown-list ul a:first').focus();
					}
			});
			
			$('.pp-dropdown-list ul a').keydown(function(e) {
					if (e.shiftKey && e.keyCode == TAB) {
						//e.preventDefault();
						$(this).parents('.pp-select-scroll').find('.pp-act-select').outerClick();
						return;
					}
					if (e.keyCode == TAB) {
						//e.preventDefault();
						$(this).parents('.pp-select-scroll').find('.pp-act-select').outerClick();
					}
					else if (e.keyCode == ARROW_UP) {
						e.preventDefault();
						var prevA = $(this).parent('li').prev('li').find('a');
						$(prevA).focus();
					}
					else if (e.keyCode == ARROW_DOWN) {
						e.preventDefault();
						var nextA = $(this).parent('li').next('li').find('a');
						$(nextA).focus();
					}
			});
		}
	});
})(jQuery);	

