function breakingNewsRotationLauncher() {
    setTimeout('breakingNewsRotation()', 5000);
}

function breakingNewsRotation() {
    $('li.breaking_news:first').slideUp('slow', function(){
        $(this).appendTo('ul#breaking_news_list');
        $('li.breaking_news:last').show();
    });
    breakingNewsRotationLauncher();
}

function fetureBoxSelection(currentnode, automatic){
    $('li.feature').removeClass('selected').removeClass('auto');

    var addclasstonode = $(currentnode).parent().addClass('selected');
    if (automatic) {
        addclasstonode.addClass('auto');
    }

    $('div#display').html($(currentnode).next('.toinsert').html());
    return false;
}

function featureBoxAutoSelection() {
    if ($('li.feature').length == 0 || ($('li.feature').hasClass('selected') && !$('li.feature').hasClass('auto'))) {
        return;
    }

    var currentnode = $('li.auto').next('.feature').find('a');
    if (!currentnode.length) {
        $('li.feature').show();
        currentnode = $('li.feature:first a');
    } else {
        var previousnodes = $('li.auto').next('.feature').prevAll();
        if (!(previousnodes.length % 4)) {
            previousnodes.hide();
        }
    }

    fetureBoxSelection(currentnode, true);
    setTimeout('featureBoxAutoSelection()', 10000);
}

$(function(){
    breakingNewsRotationLauncher();

    featureBoxAutoSelection();

    $('li.feature').hover(function(){
        // is equal to passing: $(this).find('a').eq(0)
        return fetureBoxSelection($(this).find('a'), false);
    });

    //$('a.feature').click(function(){
    //    return fetureBoxSelection(this, false);
    //});

    $('a#scroll_up').click(function(){
        var previousnodes = $('li.feature.selected').prevAll();
        if (previousnodes.length >= 4) {
            var jumpfeature = $('li.feature').eq(previousnodes.length - (previousnodes.length % 4) - 4);
            jumpfeature.nextAll().andSelf().show();
            fetureBoxSelection(jumpfeature.find('a'), false);
        }
        return false;
    });

    $('a#scroll_down').click(function(){
        var nextnodes = $('li.feature.selected').nextAll();
        var previousnodes = $('li.feature.selected').prevAll();
        if ((previousnodes.length % 4) + nextnodes.length >= 4) {
            var jumpfeature = $('li.feature').eq(previousnodes.length - (previousnodes.length % 4) + 4);
            jumpfeature.prevAll().hide();
            fetureBoxSelection(jumpfeature.find('a'), false);
        }
        return false;
    });

    $('a.senttoself').click(function(){
        var current_link = this;
        $.getJSON($(this).attr('href'), function(data){
                $('p.not_link').html(data.status);
        });
        return false;
    });

    $('a.sendtofriend').click(function(){
        $('div.send_friend').attr('style','');
        return false;
    });

    $("form#contactbox").submit(function(){
        $('#notice').html('');
        var req = 'The following fields are required :';
        var send = true;

        if($('#email').val() == '') {
            req += ' - email';
            send = false;
        }
        if($('#message').val() == '') {
            req += ' - inquiry';
            send = false;
        }

        if($('#disclaimer:checked').val() != 'yes') {
            req += ' - disclaimer';
            send = false;
        }
        req += '.';

        if (send == false) {
            $('#notice').html(req);
        } else {
            var filter =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i


                if (filter.test($('#email').val())) {
                        $(this).ajaxSubmit(function(data) {
                                var json = eval('(' + data + ')');
                                if (json.status == 'success') {
                                    $('#inquire').html(json.message);
                                } else {
                                    $('#notice').html(json.message);
                                }
                        });
                } else{
                    alert("Please input a valid email address")
                }
        }

        // Prevent normal submit
        return false;
    });

    $("input:checkbox").click(function(){
        if($(this).attr('checked') == true){
            if($(this).attr('id') == "all_checkbox"){
                $('table.list tr').addClass('highlight');
                $('table.list tr.list_space').removeClass('highlight');
                $('table.list tr td.last_row').parent('tr').removeClass('highlight');

            } else {
                $(this).parents("tr").addClass('highlight');
            }
        }else{
            if($(this).attr('id') == "all_checkbox"){
                $('table.list tr').removeClass('highlight');
            } else {
                $(this).parents("tr").removeClass('highlight');
            }
        }
    });
});
