
$(document).ready(function() {
    var w = window, d = document, e = d.documentElement, g = d.getElementsByTagName('body')[0],
    vpw = w.innerWidth || e.clientWidth || g.clientWidth, vph = w.innerHeight || e.clientHeight || g.clientHeight;
    
    $('#container').css({
        'width': Math.round(vpw * .86) + "px", 
        'left': Math.round(vpw * .07) + "px"
    });
    
    $('#fb').fbjlike();
    
    $('#header').corner('bottom');
    $('#header').corner('left');
    $('#body').corner('top');
    
    $('#' + page + 'Link').addClass('aCurrent');
        
    var pages = new Array();
    pages[0] = "home";
    pages[1] = "gallery";
    pages[2] = "about";
    pages[3] = "contact";
        
    var lin, containerOffset = Math.round($('#container').offset().left);
        
    for (i = 0; i < pages.length; i++) {
        if (pages[i] == page) {
            lin = i;
        } else {
            $('#' + pages[i] + 'Link').corner();
        }
    }
        
    if (page == 'gallery') {
        $('.ad-gallery').adGallery({
            width: 600,
            height: 400,
            thumb_opacity: 0.7,
            start_at_index: 0,
            animate_first_image: false,
            animation_speed: 400,
            display_next_and_prev: true,
            display_back_and_forward: true,
            scroll_jump: 0,
            slideshow: {
                enable: true,
                autostart: true,
                speed: 5000,
                start_label: 'Start',
                stop_label: 'Stop',
                stop_on_scroll: true,
                countdown_prefix: '(',
                countdown_sufix: ')'
            },
            effect: 'fade',
            enable_keyboard_move: true,
            cycle: true                
        });
    }
        
    if (lin > 0) {
        lLink = $('#' + pages[lin - 1] + 'Link');
        nhdl = ((Math.round(lLink.offset().left) + lLink.width()) - containerOffset) + "px";
    } else {
        nhdl = "0px";
    }
    if (lin < (pages.length - 1)) {
        rLink = $('#' + pages[lin + 1] + 'Link');
        nhw = ((Math.round(rLink.offset().left) - parseInt(nhdl)) - containerOffset) + "px";
    } else {
        rLink = $('#' + pages[lin] + 'Link');
        nhw = (((Math.round(rLink.offset().left) + rLink.width()) - parseInt(nhdl)) - containerOffset) + "px";
    }
    nht = $('#header').offset().top + "px";
    nhh = $('#header').height() + "px";
    $('#navHighlight').css({
        'left': nhdl, 
        'top': nht, 
        'width': nhw, 
        'height': nhh, 
        opacity: 0.75
    });
        
    var bodyBottom = Math.round($('#body').offset().top) + $('#body').height();
        
    if (bodyBottom < (vph - Math.round(vph * 0.03))) {
        $('#body').height(((vph - Math.round(vph * 0.03)) - Math.round($('#body').offset().top)) + "px");
        bodyBottom = Math.round($('#body').offset().top) + $('#body').height();
    }
        
    $('#container').height(bodyBottom + Math.round(vph * 0.03) + "px");
    if (page == "home") { 
        $('#mapFrame').height(Math.round($('#mapFrame').width()) + "px");
    } else if (page == 'contact') {
        $('#mapFrame').height(Math.round($('#mapFrame').width()) + "px");
        var mfBottom = Math.round($('#mapFrame').height()) + Math.round($('#mapFrame').offset().top);
        if (bodyBottom < mfBottom) {
            bodyTop = $('#body').offset().top;
            $('#body').height(mfBottom - bodyTop + "px");
            $('#container').height(mfBottom + Math.round(vph * 0.03) + "px");
        }
    }
        
});
