$(document).ready(function(){

    // show slider nav / video link
    $("#promo-nav").show();
    $("#welcome-inner .link-video").show();

    // removes highlight classes on 
    function canvasRemoveOnClass() {
        $("#promo-nav ul li").each(function(){
            $(this).removeClass("on")
        })
    }

    // resets welcome tab stuff 
    function canvasResetWelcome() {
        // hide video inner again
        $('#welcome-video-inner').hide();
        // hide video
        $('#welcome-video').hide();
        $('#video-embed').empty();

        // show welcome inner
        $('#promo-sections .one').height(330);
        $('#welcome-inner').show().height(330);
    }

    // Resets video elements for show/hide
    function canvasResetWelcomeVideo() {
        // hide welcome inner
        $('#welcome-inner').hide();

        // reset main promo height
        $('#promo-sections .one').css({display:'none', height:'528px'});
        $('#promo-sections .one').slideDown();

        // show video
        $('#welcome-video').height(528).slideDown(528, function(){
            // re-embed video
            initVideo();
            // fade in stuff
            $('#welcome-video-inner').fadeIn('fast');
        })
    }

    // loop items and return the selected item number
    function canvasWorkOutItemNo() {
        var li_items = $("#promo-nav ul li").map(function(n){
            if ($(this).hasClass("on")) {
                return n;
            };
        })
        var li_item = li_items[0];
        return li_item;
    }

    // used to fade / show items based on item numbers
    function canvasFadeContent(item_no) {
        $("#promo-sections > div").each(function(n){
            if (n==item_no) {
                // fade item in.
                $(this).fadeIn(200);
                $(this).height(330);
            } else {
                // fade item out 
                $(this).hide();
            };
        })
    }

    // Check state per item
    $("#promo-nav ul li").each(function(){
//
//   Events for Hover and Focus states
// 
        // attach hover states
        $(this).hover(
            // Hover over
            function () {
                if (!$(this).hasClass("on")) {
                    $(this).addClass("hover");
                }
            },
            // Hover out
            function () {
                $(this).removeClass("hover");
            }
        );
        // attach focus states
        $(this).focusin(
            // Focus over
            function () {
                if (!$(this).hasClass("on")) {
                    $(this).addClass("hover");
                }
            }
        );
        $(this).focusout(
            // Focus out
            function () {
                $(this).removeClass("hover");
            }
        );

//
//   Events for Hover and Focus states
//
        // attach keypress events
        $(this).keypress(
            function(event) {
                if (event.keyCode == '13') {
                    event.preventDefault();
                    // remove all active classes
                    canvasRemoveOnClass();
                    // reset welcome tab, hiding video
                    canvasResetWelcome();

                    // set active classe for this item 
                    $(this).removeClass("hover");
                    $(this).addClass("on");

                    // get item number
                    item_no = canvasWorkOutItemNo();

                    // fade content for item
                    canvasFadeContent(item_no);
                }
            }
        );

        // attach click events
        $(this).bind('click',
            // on click
            function () {
                // remove all active classes
                canvasRemoveOnClass();
                // reset welcome tab, hiding video
                canvasResetWelcome();

                // set active classe for this item 
                $(this).removeClass("hover");
                $(this).addClass("on");

                // get item number
                item_no = canvasWorkOutItemNo();

                // fade content for item
                canvasFadeContent(item_no);
            }
        );

/* OLD TAB NAV STUFF
        // attach hover states
        $(this).hover(
            function () {
                if (!$(this).hasClass("on")) {
                    $(this).addClass("hover");
                }
            },
            function () {
                $(this).removeClass("hover");
            }
        );

        // attach click events
        $(this).click(
            // on click
            function () {
                // remove all active classes
                canvasRemoveOnClass();
                // reset welcome tab, hiding video
                canvasResetWelcome();

                // set active classe for this item 
                $(this).removeClass("hover");
                $(this).addClass("on");

                // get item number
                item_no = canvasWorkOutItemNo();

                // fade content for item
                canvasFadeContent(item_no);
            }
        );
*/
    })

    // Show video
    $("#welcome-inner .link-video").click(function(){
        canvasResetWelcomeVideo();
    })

    // hide video
    $("#welcome-video a.link-close").click(function(){
        canvasResetWelcome();
    })

});
