
function init(){
    $('main').setOpacity(0);
    $('bottom_nav').setOpacity(0);
    $('back_button').setOpacity(0);
    $('content').hide();
    $('overlay').clonePosition('main_nav');
    $('overlay').setOpacity(0);
    $('overlay').setStyle({
        background:'black',
        position:'absolute',
        left:'0px',
        top:'0px'
    });
    $('overlay').hide();
    loadIntroHandler();

    //add event listeners

    document.observe('dom:loaded',function(){
        if(readCookie('skipIntro') == 'true'){
            setTimeout(function(){endIntro()},2000);
        }
        preloadImages();
        $$('.nav_link').each(function(item){
            item.observe('click', function(){
                dimHome();
                showBackButton();
                loadHash(item);
            })
        });
    });

    document.observe('keyup',function(e){

                if (e.keyCode == Event.KEY_ESC){
                        undimHome()
                        Event.stop(e);
                 
                }
        
    });

    

    $('frame_news').observe('mouseover', function(){
        new Effect.Move('frame_news', {
            x: 35,
            y: 100,
            mode: 'absolute',
            duration:0.1
        });
    });
    $('frame_news').observe('mouseout', function(){
        new Effect.Move('frame_news', {
            x: 35,
            y: 90,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('frame_menu').observe('mouseover', function(){
        new Effect.Move('frame_menu', {
            x: 375,
            y: 210,
            mode: 'absolute',
            duration:0.1
        });
    });
    $('frame_menu').observe('mouseout', function(){
        new Effect.Move('frame_menu', {
            x: 375,
            y: 200,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('frame_aboutus').observe('mouseover', function(){
        new Effect.Move('frame_aboutus', {
            x: 590,
            y: 80,
            mode: 'absolute',
            duration:0.1
        });
    });
    $('frame_aboutus').observe('mouseout', function(){
        new Effect.Move('frame_aboutus', {
            x: 590,
            y: 70,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('phone_bottom').observe('mouseover', function(event){
        if(!!(document.createElement('audio').canPlayType)){
            $('phone_ring').currentTime=0;
            $('phone_ring').play();
        }
        event.stop();

        setTimeout(function(){
            
                    $('phone_top').shake({
                        distance:3,
                        duration:0.4
                    });
                
            
        },500);
    });

    $('reserved').observe('mouseover',function(){
        new Effect.Move('reserved', {
            x: 310,
            y: 435,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('reserved').observe('mouseout',function(){
        new Effect.Move('reserved', {
            x: 310,
            y: 438,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('press_hat').observe('mouseover',function(){
        new Effect.Move('press_hat', {
            x: 660,
            y: 430,
            mode: 'absolute',
            duration:0.1
        });
    });

    $('press_hat').observe('mouseout',function(){
        new Effect.Move('press_hat', {
            x: 650,
            y: 450,
            mode: 'absolute',
            duration:0.1
        });
    });

}

function endIntro(videocheck){
    $('intro').fade({
        afterFinish:function(){
            $('intro').remove();
            $$('footer').each(function(i){i.appear();});
            $('bottom_nav').appear();
        }
    });
    $('main').appear();
    $('bottom_nav').appear();
    clearInterval(videocheck);
}

function dimHome(){
    
    $('overlay').appear({
        to:0.60
    });

    
}

function undimHome(){
    $('overlay').fade();
    $('back_button').fade({
        duration:0.25
    });
    $('content').fade({
        afterFinish:function(){
            $('content').hide();
            $('content').update('');
        }
    });

}

function showBackButton(){
    $('back_button').appear();
}

function loadIntroHandler(){

        if(!document.createElement('video').canPlayType){
            endIntro(videocheck);
        }else{

            $('intro_video').play();
            var videocheck = setInterval(function(){
                if($('intro_video') && $('intro_video').ended === true){
                    endIntro(videocheck);
                }
            },1000);

        }
}

function loadHash(user_hash){

    var hash;

    //if hash is explicitly set, pull it from the href of link...otherwise use the location.hash
    if(user_hash){
        hash=user_hash.readAttribute('href');
    }else{
        hash=location.hash;
    }

    //if we have a hash load it...otherwise go to the home section
    if (hash){
        if(hash.length > 1){
            category = hash.substr(1);
            loadContent(category+'.php','');
        }else{
    //do nothing
    }
    }else{
        loadContent('index.php');
    }

    return false;
}

function loadContent(url,post_vars){

    var fade_time = 0.5;

    $('content').show();
    //$('content').setOpacity(0);
    $('content').fade({
        duration:fade_time,
        afterFinish: function(){$('content').update('');}
    });
    //NOTE: this could possibly go into an afterFinish callback in the fading of the content
    setTimeout(function(){
        new Ajax.Updater('content',url,{
            parameters:post_vars,
            evalScripts: true,
            onComplete: function(){

                //refresh all cufon fonts
                Cufon.refresh();

                //fade in the content
                
                $('content').appear({
                    to:1,
                    duration:fade_time,
                    queue:'end'
                });

            }
        });
    },1000*fade_time);
}

function preloadImages(){
    //backgrounds
    new Element('img',{
        'src':'images/menu_background.png'
    });
    new Element('img',{
        'src':'images/contact_background.png'
    });

    //owl's club photo'
    new Element('img',{
        'src':'images/form.jpg'
    });

    //about us animation
    new Element('img',{
        'src':'images/owl_body.png'
    });
    new Element('img',{
        'src':'images/owl_wing_left.png'
    });
    new Element('img',{
        'src':'images/owl_wing_right.png'
    });
    new Element('img',{
        'src':'images/tree_branch.png'
    });

    //contact highlights
    new Element('img',{
        'src':'images/contact_highlight.png'
    });
    new Element('img',{
        'src':'images/contact_highlight_1.png'
    });
    new Element('img',{
        'src':'images/contact_highlight_2.png'
    });
    new Element('img',{
        'src':'images/contact_highlight_3.png'
    });
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


