if (window.stop && window.location.hash) {
	var loc = window.location.hash.replace('#','');
    if (loc.length > 0) {
        window.location.replace('/'+loc + '.html');
    }
}


(function($) {

    /**
     * Document ready.
     */
$(function() {
    
	if(!$('body').hasClass('vorwind')) {
		var $columns = $('.column');
	    var $current = $columns.filter('.initial-column');
	    var width = $current.outerWidth();

	    var idx = $columns.index($current);

	    var left = width * idx * - 1;

	    $('#content').css({left: left+'px'});
	    $columns.show();

	    $('#background .img').show();
	    var bgLeft = ($('#background .img').eq(idx+2).position().left - $('#background .img').eq(2).position().left) * -1;
	    $('#background').css({left: bgLeft+'px'});

	    $columns.filter(':not(.initial-column)').each(function() {
	        var $col = $(this);
	        $.get($col.attr('data-href'),function(html) {
	            $col.css({opacity:0}).html(html).fadeTo(10,1);
	        });
	    });


	    var $images = $('#background .img');
	    $images.each(function() {
	        var $bg = $(this);
	        var src = $bg.attr('data-src');
	    
	        if (src && src != '' && src != 'false') {
	            var img = new Image();
	            img.onload = function() {
	                $(img).css({opacity:0}).appendTo($bg).fadeTo(500,1);
	            }
	            img.src = src;
	        }
	    });
	    
	    

	    var $navItems = $('.js #navigation>ul>li>a.viertel-kreis, .js #navigation>ul>li>a.vorwind-catering, .js #navigation>ul>li>a.oslo-centro');
	}
    
    if ($('body').hasClass('js') && !$('body').hasClass('vorwind')) {
    	        
        var openpage = function(link) {        
            var $link = $(link);
            var href = $link.get(0).getAttribute('href');
            var localhost = window.location.protocol+ "//" + window.location.host;
        
            href = href.replace(localhost,'');
            if (!href) return true;
    
            if (href.substring(0,1) != '/') {
                window.open(href);
                return false;
            }
            href = href.split('.html')[0];
            
            window.location.hash = href.substring(1);
            var $a = $navItems.filter('[href$="'+href+'.html"]');
    
            var idx = $navItems.index($a);
            
            var width = $('#content').find('.column').eq(idx).outerWidth();
            
            var bgLeft = ($('#background .img').eq(idx+2).position().left - $('#background .img').eq(2).position().left) * -1;

            try {
                document.title = $columns.eq(idx).attr('data-title');
            } catch (e) {}
    
            var currentLeft = parseInt($('#content').css('left'));
            var left = width * idx * - 1;
    
            var diff = Math.abs(currentLeft - left);
            var duration = Math.round(diff/4.0) + 500;
    
            var $container = $('#content-container');
            var scroll = function() {
                $('#content').stop().animate({left: left+'px'},duration);
                $('#background').stop().animate({left: bgLeft+'px'},duration);
            }
    
            if ($container.scrollTop() != 0) {
                $('#content-container').animate({scrollTop: 0},200, function() {
                    scroll();
                });
            }
            else {
                scroll();
            }
    
            return false;
        };
    
        $('#main a').live('click',function() {
            return openpage(this);
        });
    
        $('#navigation a.viertel-kreis, #navigation a.vorwind-catering, #navigation a.oslo-centro').click(function(event) {
        	return openpage(this);
        });
    } 
    if($('#footer').length != 0){
    	
    	$('#footer a img').each(function(index, element){
    		$(element).css('left', '0px');
    	})
    	
    	$('#footer a img').mouseenter(function() {
        	$('#footer a img').each(function(index, element) {
				$(element).stop(true).animate({left : index*parseInt($(element).css('width')) + 'px'});
			});
        });
        
        $('#footer').mouseleave(function(){
        	$('#footer a img').each(function(index, element) {
        		$(element).stop(true).animate({left : '-' + index*parseInt($(element).css('width')) + 'px'});
        	});
        });
    }
});
    


})(jQuery);
