var weltNavigation = (function($) {
    var keepNavVis = false;
    var hideIt;

    function showNav(obj) {
        var pos = obj.position();
        obj.next().css({
            top: pos.top + 24 + 'px',
            left: pos.left + 10 + 'px'
        });
        obj.next().show();
        obj.addClass('act');
    }

    function hideNav(obj) {
        obj.next().hide();
    }

    return {
        init: function() {
            $('#topNav li a.hasChildren').hover(function() {
                var id = $(this).parent().attr('class').split('nav_')[1];
                showNav($(this));
            }, function() {
                var id = $(this).parent().attr('class').split('nav_')[1];
                var _that = $(this);
                if(!keepNavVis) {
                    hideIt = setTimeout(function() {
                        hideNav($(_that));
                        $(_that).removeClass('act');
                    }, 10);
                }
            });

            $('.subNav').hover(function() {
                clearTimeout(hideIt);
                keepNavVis = true;
            }, function() {
                $(this).hide();
                $(this).prev().removeClass('act');
                keepNavVis = false;
            });

            $('.subNav').each(function() {
                // Upgrade to Primefaces 7.0 => upgrade to jquery 3.0, issue with size() or length():
                //    now this error occurs: "$(...).find(...).size is not a function"
                //    now this error occurs: "$(...).find(...).length is not a function"
                // This variable cnt is NOT used anywhere, so simply do not calculate the size/length here.
                //var cnt = $(this).find('.inner').size();
                //$(this).css('width', parseInt($('.inner').css('width'), 10) * cnt + 'px');
            });


            $('#siteSearch input').focus(function() {
                if($(this).val() == $(this)[0].defaultValue) {
                    $(this).val("");
                }
            });

            $('#siteSearch input').blur(function() {
                if($(this).val() === "") {
                    var defVal = $(this)[0].defaultValue;
                    $(this).val(defVal);
                }
            });

            if ($.browser.msie) {
                $('#topNav').add('#leftNav').css('zoom', 1);
            }
			
			$('#bottomNav a[href="/welt_rb2012/en/index.html"]').click(function(e){
				e.preventDefault();
				var selfLoc=self.location.href;
				var destLoc=selfLoc.replace('/de/','/en/');
				var href=$(this).attr('href');
				fileExists(destLoc,href);
			})
			
			$('#bottomNav a[href="/welt_rb2012/de/index.html"]').click(function(e){
				e.preventDefault();
				var selfLoc=self.location.href;
				var destLoc=selfLoc.replace('/en/','/de/');
				var href=$(this).attr('href');
				fileExists(destLoc,href);
			})
			
			function fileExists(fileLocation,href) {
				var response = $.ajax({
					url: fileLocation,
					type: 'HEAD',
					async: false
				}).status;
				if(response==200){
					self.location.href=fileLocation;
				}else{
					self.location.href=href;
				}
			}
			
        }
    }
}(jQuery));
