function isMobileSafari() {
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {
        return true;
    } else {
        return false;
	}
}

jQuery(document).ready(function() {
    var _timer;
    
    $('#header').hide();
    $('.visual').hide();
    $('#main').hide();
    $('#sidebar').hide();
    $('.nav-holder').hide();
    $('#footer').hide();
    $('#text-intro').css('opacity', '0');
    $('#text-intro').css('visibility', 'visible');
    
    setTimeout(function() {
        $('#preload').fadeIn('fast');
    }, '800');
    
    
    $('#btn-intro').click(function(e){
        e.preventDefault();
    });
    
    function initAnimation(event) {
        if(isMobileSafari()){
            $.backstretch('images/intro/bg-intro-mobile-safari.jpg', {speed: 500}, function() {
                lineIntro();
            });
        } else {
            $.backstretch('images/intro/bg-intro.jpg', {speed: 500}, function() {
                lineIntro();
            });
        }
    }
    
    function lineIntro(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('bgIntro'), '500');
        $('#line-intro').animate({
            height:'800px',
            marginTop: "-=800"
        }, 1000, 'linear', null);
    }
    
    function bgIntro(event) {
        logoIntro();
        $("#bg-intro").slideDown(750);
    }
    
    function logoIntro(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('textIntro'), '750');
        $('#logo-intro').css('padding-top', '423px');
        $('#logo-intro').animate({
            paddingTop: '-=310'
        }, 400, 'linear', null);
        $('#logo-intro').fadeIn(400);
    }
    
    function textIntro(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('btnIntro'), '150');
        $('#text-intro').css('margin-top', '55px');
        $('#text-intro').animate({
            opacity: 1
        }, 500, 'linear', null);
    }
    
    function btnIntro(event) {
        $('#btn-intro').animate({
            'margin-top': '50px'
        },
        {
            duration: 500,
            easing: 'easeOutQuad',
            queue: false
        });
        
        if($.browser.msie && parseInt(jQuery.browser.version) <= 8) {
            $('#btn-intro').css('visibility', 'visible');
        }
        else
        {
            $('#btn-intro').fadeTo(0, 0).css('visibility', 'visible').fadeTo(1000, 1);
            $('#btn-intro').blend();
        }
        
        $('html').bind('mouseup', buildPage);
    }
    
    function buildPage(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildBackground'), '250');
        $('#wrapper').css('visibility', 'visible');
    }
    
    function buildBackground(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildHeader'), '250');
        $('#wrapper').css('display', 'block');
        $('#footer').css('display', 'block');
        $('#animation').css('display', 'none');
        $('#backstretch').remove();
        $('body').css('background', 'url(images/bg-body.gif) no-repeat 50% 0');
    }
    
    function buildHeader(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildNavigation'), '250');
        $('#header').show();
    }
    
    function buildNavigation(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildBanner'), '250');
        
   	    $('.nav-holder').fadeIn(250);
        $('html').css('background', '#fff url(../images/bg-html.gif) repeat-x 0 30px');
    }
    
    function buildBanner(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildContent'), '250');
        
        $('.visual').fadeIn(250);
    }
    
    function buildContent(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildSidebar'), '250');
        
   	    $('#main').fadeIn(250);
    }
    
    function buildSidebar(event) {
        clearTimeout(_timer);
        _timer = setTimeout(eval('buildFooter'), '250');
        
        $('#sidebar').fadeIn(250);
    }
    
    function buildFooter(event) {
        $('#footer').css('visibility', 'visible');
        $('#footer').fadeIn(250);
        $('html').unbind('mousedown', initPage);
        if ($.browser.msie) {
            if(parseInt(jQuery.browser.version) < 8) {
                $('html').css('overflow', 'auto');
            } else {
		   		$('body').css('overflow', 'auto');
            }
        } else {
            $('body').css('overflow', 'auto');
        }
        initNav();
    }
    initAnimation();
});
