$(document).ready(function() {
    
// Clear out search upon focus
    $('#searchBox').focus(function(){
        $(this).attr('value','');
    });
    
// Put back Search value if focus is moved and no value is present
    $('#searchBox').blur(function(){
        if ($(this).val() == '' ) {
            $(this).attr('value','Search');
        }
    });
    
// Remove class of 'on' from list items in the profiles section
    $('.profiles li').removeClass('on');
    
    $('.profiles li').hover(function(){
        if(!($(this).hasClass('on'))) {
        $(this).children('a').toggleClass('hover');
        }
    });
    $('.profiles li').click(function(){
        $(this).toggleClass('on');
        return false;
    });
    
// News | Events Toggle
    $('.eventList').hide();
    $('a.latestNews').addClass('latestNewsOn');
    
    $('a.latestNews').click(function(){
        $('a.latestNews').addClass('latestNewsOn');
        $('a.events').removeClass('eventsOn');
        $('.eventList').hide();
        $('.newsList').show();
        $('#newsBox').removeClass('newsBoxEvents');
        return false;
    });
    
    $('a.events').click(function(){
        $('a.latestNews').removeClass('latestNewsOn');
        $('a.events').addClass('eventsOn');
        $('.eventList').show();
        $('.newsList').hide();
        $('#newsBox').addClass('newsBoxEvents');
        return false;
    });
// News | Events Convert whole area to link
    $('#newsBox li').click(function(){
        $link_url = $(this).find('a.title:first').attr('href');
        window.location.href = $link_url;
    });
// Activate Link Hover on list item 
    $('#newsBox a').hover( 
            function() { 
                    $(this).parent().addClass('over'); 
            }, 
            function() { 
                    $(this).parent().removeClass('over'); 
            } 
    ) 
// Share box 
    $('a.share').click(function(){
        $('a.share').toggleClass('shareOn');
        $('#share').toggle();
        return false;
    });
    
// HP Story box
    var homeFeature = $('#homeFeature');
    //  Randomize if desired
    if( homeFeature.hasClass('randomized') == true ){
        randomizer(homeFeature, '.item');
    }
    var homeFeatureItems = homeFeature.find('.item');
    var homeFeatureNav = homeFeature.append('<ul id="featureNav"></ul>').find('#featureNav');
    //  Build featureNav, associate items with nav items
    for (var i=0; i < homeFeatureItems.length; i++) {
        var thisName = 'fn'+(i+1);
        $(homeFeatureItems[i]).attr('id', thisName);
        homeFeatureNav.append('<li class="'+thisName+'"><a href="#">'+i+'</a></li>');
    };
    //  Change element display depending on item position
    $('div.item:not(:first)').hide();
    $('div.item:first').show();
    $('.fn1 a').addClass('on');
    $('#featureImg').append( $('#homeFeature .item:first').find('.image').html() );
    //  Animate first item on page load
    $('#homeFeature .item .left, #homeFeature .item .right').animate({
        marginLeft: 0,
        marginRight: 0
    }, 1000);
    //  Nav click / Story switch / Animations
    $('#featureNav a').click(function(){
        var story = $(this).parent('li').attr('class');
        var image = $('div#'+story).find('.image').html();
        //  Animate image fades
        $('#featureImg').stop().animate({
            opacity: 0
        }, 500, function() {
            $('#featureImg').empty().append(image);
            $('#featureImg').animate({
                opacity: 1
            }, 500);
        });
        //  Animate item text elements (left)
        $('#homeFeature .item .left').stop().animate({
            marginLeft: -460
        }, 500, function(){
            $('div.item').hide();
            $('div#'+story).show();
            setTimeout(function() {
                $('.left').stop().animate({
                    marginLeft: 0
                }, 500);
            }, 1);
        });
        //  Animate item text elements (right)
        $('#homeFeature .item .right').stop().animate({
            marginLeft: 460,
            marginRight: -460
        }, 500, function(){
            setTimeout(function() {
                $('#homeFeature .item .right').stop().animate({
                    marginLeft: 0,
                    marginRight: 0
                }, 500);
            }, 1)
        });
        $('#featureNav a').removeClass('on');
        $(this).addClass('on');
        return false;
    });
    if( homeFeature.hasClass('autoPlay') == true ){
        autoPlay(homeFeature);
    };
    //  Item randomizer function
    function randomizer(container, item) {
        $(container).each(function() {
            var obj = $(this);
            var items = obj.find(item);
            items.remove();
            var i = 0;
            var random;
            while ( i < items.length ) {
                //  Generate a random number between 0 and (arraylength-1)
                random = Math.floor(Math.random()*items.length);
                //  If the element hasn't been "passed"
                if ( items[random] != 'passed' ){
                    obj.append(items[random]);
                    //  Mark this item as "passed"
                    items[random]='passed';
                    i++
                };
            };
        });
    }
    function autoPlay(el) {
        $(el).each(function() {
            var interval = 7000;
            var obj = $(el);
            var current = 1;
            var links = obj.find('#featureNav a');
            var timer;
            function autoPlayTimer() {
                timer = setInterval(function() {
                    $(links[current]).trigger('click');
                    if( current === (links.length)-1 ) {
                        current = 0;
                    } else {
                        current = current+1;
                    }
                }, interval);                
            };
            autoPlayTimer();
            obj.hover(
                function() {clearInterval(timer);},
                function() {autoPlayTimer();}
            );
        });
    }






});
