var isIE6 = !$.support.opacity && !window.XMLHttpRequest;

function drawContent(offsetWidth, wrapperHeight, windowHeight){
	var adjustedBackgroundPosition = -452 + offsetWidth + "px" + " 0px";
	var adjustedTopValue = ((windowHeight * 0.5) - (wrapperHeight * 0.5)) + "px";
	var adjustedTopNavValue = (((windowHeight * 0.5) - ($('#nav').height() * 0.5))*0.8) + "px";
	$('.block:first').css('margin-left', offsetWidth);
	$('#wrapper').css({
        'background-position': adjustedBackgroundPosition,
		'top': adjustedTopValue 
    });
	$('#nav').css({
		'top': adjustedTopNavValue 
    });
	//window.location.hash = "#home";
}

$(document).ready(function () {

    $('body').append('<div id="nav"><a href="#" id="nav-left"></a><a href="#" id="nav-right"></a></div>');

    var $menu = $('.menu ul');
    var $trigger = $('.menu ul li a');
    var $nav = $('#nav');
    var $navPrev = $('#nav-left');
    var $navNext = $('#nav-right');
    var target;

    //$('body').css('overflow-x', 'hidden');
	$('body, html').css('overflow', 'hidden');

    $(".menu ul li a").each(function (i) {
        $(this).attr('rel', (i));
    });
	
    //var offsetWidth = Math.round(($(window).width() - $('.block').width()) / 2);

    function setCurrentClass() {
        var currentHash = window.location.hash;
        var currentSection = currentHash.substring(1, currentHash.length);
        var incomingHash = "a.get-" + currentSection;
        if(!isIE6){
			$(incomingHash).addClass('current');
		}
        document.title = 'Duel Microfusioni - ' + currentSection.substr(0, 1).toUpperCase() + currentSection.substr(1);
    }

    function setNavigation() {
        var currentHash = parseFloat($('a[href="' + window.location.hash + '"]').attr('rel'));
        var prevHash = $('a[rel="' + (currentHash - 1) + '"]').attr('href');
        var nextHash = $('a[rel="' + (currentHash + 1) + '"]').attr('href');
        $navPrev.attr('href', prevHash);
        $navNext.attr('href', nextHash);
    }

    function navigate2(target) {
        $nav.hide();
		if(!isIE6){
			$trigger.removeClass('current');
		}
        $.scrollTo(target, 800, {
            axis: 'x',
            offset: -Math.round(($(window).width() - $('.block').width()) / 2),
            easing: 'easeOutQuint',
            onAfter: function () {
                if (target == $('.menu ul li a:first').attr('href')) {
                    $navPrev.hide();
                } else {
                    $navPrev.show();
                }
                if (target == $('.menu ul li a:last').attr('href')) {
                    $navNext.hide();
                } else {
                    $navNext.show();
                }
                //window.location.hash = target;
                //$(target).scrollIntoView(false);
                //window.location.href.hash = target;
                setCurrentClass();
                setNavigation();
				$nav.fadeIn('fast');
            }
        });
    }

    /*$('.menu ul li a, #nav-left, #nav-right').click(function () {
        var incomingHash = $(this).attr('href');
        navigate2(incomingHash);		
    });*/	
	
	$('.menu ul li a, #nav-left, #nav-right').bind("click", function() {
        var incomingHash = $(this).attr('href');
        navigate2(incomingHash);
	});
	

    //var windowWidth = $(window).width();
    drawContent(Math.round(($(window).width() - $('.block').width()) / 2), $('#wrapper').height(), $(window).height());
	
	if (!window.location.hash) {
    	window.location.hash = "#home";
    }
    navigate2(window.location.hash);
        

    $(window).bind('hashchange', function () {
        navigate2(window.location.hash);
    })


});



$(window).bind('resize', function () {
	var offsetWidth = Math.round(($(window).width() - $('.block').width()) / 2);
	var wrapperHeight = $('#wrapper').height();
	var windowHeight = $(window).height();
    drawContent(offsetWidth, wrapperHeight, windowHeight);
});