var the_path;
$(document).ready(function(){

	$("ul.sf-menu").supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
    }).superfish();  
		
	$.address.init(function(event) {
		
		//$('a:not([href^=http])').address();
		$('.sf-menu a, .trans').address();	
	}).change(function(event) {
		var qsr =  $.address.queryString();
		the_path = event.path;
		if(qsr){
			the_path = '/';
		}		
		var page = event.path == '/' ? 'home.php' : event.path + '.php';
		if(qsr){
			page += '?' + qsr;
		}
		$.ajaxSetup ({cache: false});

		/*$('#main-inner').hide('elastic',function(){
			$(this).load('/wtf/' + page, function(){
				init();
				$(this).show('elastic');
			});
		});*/
		switch(event.path){
			case '/services':
				$('#main-inner').animate({
				    opacity: 0
				}, 1000, function () {
						$('#main-inner').load('/eng/' + page,function(){
				        init();
				    });
				    $('#main-inner').animate({
				        height: '450px'
				    }, "fast").animate({
				        opacity: 1
				    }, 1500);
				});
			break;	
			case '/projects':
				$('#main-inner').animate({
				    opacity: 0
				}, 1000, function () {
						$('#main-inner').load('/eng/' + page,function(){
				        init();
				    });
				    $('#main-inner').animate({
				        height: '500px'
				    }, "fast").animate({
				        opacity: 1
				    }, 1500);
				});
			break;
			case '/contact':
				$('#main-inner').animate({
				    opacity: 0
				}, 1000, function () {
						$('#main-inner').load('/eng/' + page,function(){
				        init();
				    });
				    $('#main-inner').animate({
				        height: '450px'
				    }, "fast").animate({
				        opacity: 1
				    }, 1500);
				});
			break;	
			case '/careers':
				$('#main-inner').animate({
				    opacity: 0
				}, 1000, function () {
				    $('#main-inner').load('/eng/' + page, function () {
				        init();					
				        var nheight = ($('.toggle-fancy').length) * 145;
						
						/* Adjust if not that many careers are live */
						nheight = nheight < 500 ? 500 : nheight;
				        
						$(this).animate({
				            height: nheight
				        }, "fast").animate({
				            opacity: 1
				        }, 1500);
				    });

				});			
			break;	
			case '/company_info':
				$('#main-inner').animate({
				    opacity: 0
				}, 1000, function () {
						$('#main-inner').load('/eng/' + page,function(){
				        init();
				    });
				    $('#main-inner').animate({
				        height: '450px'
				    }, "fast").animate({
				        opacity: 1
				    }, 1500);
				});
			break;													
			default:				
				$('#main-inner').animate({
				    opacity: 0
					}, 'fast', function () {
				    $(this).html('<img src="./images/preloader.gif" class="main-inner-preloader"/>').animate({
				        height: '650px'
				    }).animate({
				        opacity: 1
				    }, function () {
				        $('#main-inner').load('/eng/' + page, function () {
				            init();
				            $('#homepage-slideshow').css({
				                opacity: 0
				            });
				            $('.slide').css({
				                opacity: 0
				            });
				            $('#footer-wrapper, #footer-left, #footer-right').css({
				                opacity: 0
				            });
				            $('.home-news').css({
				                opacity: 0
				            });
				            $('.home-contact-info').css({
				                opacity: 0
				            });
				            $('.slideshow-bottom').css({
				                opacity: 0
				            });
				            $('#homepage-slideshow').delay(250).animate({
				                opacity: 1
				            }, 250, function () {
				                $('#footer-wrapper, #footer-left, #footer-right').delay(250).animate({
				                    opacity: 1
				                }, 250, function () {
				                    $('.slideshow-bottom').delay(250).animate({
				                        opacity: 1
				                    }, 250);
				                    $('.slide').delay(250).animate({
				                        opacity: 1
				                    }, 250, function () {
				                        $('.home-news').delay(250).animate({
				                            opacity: 1
				                        }, 250, function () {
				                            $('.home-contact-info').delay(250).animate({
				                                opacity: 1
				                            }, 250, function () {});
				                        });

				                    });
				                });
				            });
				        })
				    });
				});
			break;
		}
	});
});

function init()
{
	/* Fix for back button on projects/ */
	
	if($.colorbox){
		$.colorbox.remove();
		$.colorbox.init();	
	}
	$('#homepage-slideshow, div.page').addClass('top_bg');
    $(".themeblvd-toggle").each(function(){
		$(this).find(".box").hide();
	});
	
	$(".themeblvd-toggle").each(function(){
		$(this).find(".trigger").click(function() {
			$(this).toggleClass("active").next().stop(true, true).slideToggle("slow");
	    	return false;
		});
	});
	
	$('#language_switcher').click(function(){
		window.location.href='/fra/#' + the_path;
	});

	
}

