var fetched_videos = new Array();
function more_videos() {
    $('#more_videos > ul a').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        var id = $(this).attr('class').substr(2);
        if (fetched_videos[id] == null)
        {
            // get video via ajax
            $('#more_videos .wrap_video').loading().load('default.html?ajax=1&video=' + id, function() {
                fetched_videos[id] = $(this).html();
            });
        }
        else
        {
            // get video preview from cache
            $('#more_videos .wrap_video').html(fetched_videos[id]);
        }
        // prevent from redirecting to the href link
        update_nav('more_videos', $(this).attr('class'));
        return false;
    });
}

var fetched_galleries = new Array();
function galleries() {
    $('#gallery > ul a').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        var id = $(this).attr('class').substr(2);
        if (fetched_galleries[id] == null)
        {
            // get article via ajax
            $('#gallery .wrap').loading().load('default.html?ajax=1&gallery=' + id, function() {
                fetched_galleries[id] = $(this).html();
            });
        }
        else
        {
            // get article preview from cache
            $('#gallery .wrap').html(fetched_galleries[id]);
        }
        // prevent from redirecting to the href link
        update_nav('gallery', $(this).attr('class'));
        return false;
    });
}

function videosHR() {
    $('#last_videos_hr').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        $('#highest_rated .wrap_video').loading().load('default.html?ajax=1&module=videos&type=hr', function() {
        });
        // prevent from redirecting to the href link
        $('#last_videos_hr').addClass('link_off');
        $('#last_videos_l').removeClass('link_off');
        return false;
    });
}

function videosL() {
    $('#last_videos_l').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        $('#highest_rated .wrap_video').loading().load('default.html?ajax=1&module=videos&type=l', function() {
        });
        // prevent from redirecting to the href link
        $('#last_videos_l').addClass('link_off');
        $('#last_videos_hr').removeClass('link_off');
        return false;
    });
}

function articles() {
    $('#last_articles').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        $('#latest_articles .wrap_articles').loading().load('default.html?ajax=1&module=articles&type=articles', function() {
        });
        // prevent from redirecting to the href link
        $('#last_articles').addClass('link_off');
        $('#last_events').removeClass('link_off');
        return false;
    });
}

function return_page() {
    $('#pagination a').livequery('click',function (event) {
        $("#highest_rated .wrap_video").loading().load($(this).attr('href'), function () { return_page(); });
        return false;
    });
}

function events() {
    $('#last_events').click(function () {
        // each link now fetches the preview via ajax
        // get the id that we need to pass to ajax
        $('#latest_articles .wrap_articles').loading().load('default.html?ajax=1&module=articles&type=events', function() {
        });
        // prevent from redirecting to the href link
        $('#last_events').addClass('link_off');
        $('#last_articles').removeClass('link_off');
        return false;
    });
}


function update_nav(id, link_class) {
    $('#'+id+' > ul a').each(function() {
        if ( $(this).attr('class') == link_class )
        {
            $(this).addClass('selected');
        }
        else
        {
            $(this).removeClass('selected');
        }
    });
}

$(document).ready(function(){
    //latest_articles();
    more_videos();
    galleries();
    videosHR();
    videosL();
    return_page();
    articles();
    events();
});
