$(document).ready(

    function() {
           
        $('#news-category').children('.position').each(
            //
            function(i) {
                $(this).click (
                    function () {
                        //$('#news-list').hide();
                        //$('#news-load').show();

                        hash   = $(this).children('a').attr('href');
                        tmpId  = $(this).attr('id');
                        tmpId  = tmpId.split('-');
                        thisId = tmpId[1];
                        location.hash = hash;
                        hash = hash.replace("#", "");

                        urlToLoad = location.href;
                        urlToLoad = urlToLoad.replace(".html", "");
                        urlToLoad = urlToLoad.replace("#", "/");

                        $.get(
                            urlToLoad,
                            function(data){
                                if (data.search('error') != -1) {
                                    $('#news-load').hide();
                                    $('#news-list').show();
                                    return false;
                                } else {
                                    $('#news-list').html(data);
                                    $('#news-load').hide();
                                    $('#news-list').show();
                                    addClickEvent();
                                }
                            }
                        );
                        $('#news-category').children('.position').each(
                            function(i) { 
                            $(this).removeClass('select'); 
                            $(this).children('a').blur();
                            }
                        )
                        $('#newscat-' + thisId + '').addClass('select');
                        urlToLoad = location.href;
                        urlToLoad = urlToLoad.replace(".html", "");
                        urlToLoad = urlToLoad.replace("#", "/");
                        return false;
                    }
                )
            }
        )

        if  (location.hash != '') {
            hash   = location.hash;
            thisId = 0;
            tmpHash = hash.split(',')
            $('#news-category').children('.position').each(
                function(i) {
                    mHash   = $(this).children('a').attr('href');
                    mTmpId  = $(this).attr('id');
                    mTmpId  = mTmpId.split('-');
                    mThisId = mTmpId[1];
                    if (mHash == tmpHash[0]) { thisId = mThisId; }
                }
            )
            hash = hash.replace("#", "");

            urlToLoad = location.href;
            urlToLoad = urlToLoad.replace(".html", "");
            urlToLoad = urlToLoad.replace("#", "/");
            $.get(
                urlToLoad,
                function(data){
                    if (data.search('error') != -1) {
                        $('#news-load').hide();
                        $('#news-list').show();
                    } else {
                        $('#news-list').html(data);
                        $('#news-load').hide();
                        $('#news-list').show();
                        addClickEvent();
                    }
                }
            );
            $('#newscat-' + thisId + '').addClass('select');
            return false;
        } else {
            $('#news-load').hide();
            $('#news-list').show();
            $('#newscat-0').addClass('select');
            addClickEvent();
        }

    }

);

addClickEvent = function () {
    $('#page-list').children('a').each(
        function(i) {
            $(this).click (
                function () {
                    $('#news-list').hide();
                    $('#news-load').show();

                    hash = $(this).attr('href');
                    location.hash = hash;
                    hash = hash.replace("#", "");

                    urlToLoad = location.href;
                    urlToLoad = urlToLoad.replace(".html", "");
                    urlToLoad = urlToLoad.replace("#", "/");

                    $.get(
                        urlToLoad,
                        function(data){
                            if (data.search('error') != -1) {
                                $('#news-load').hide();
                                $('#news-list').show();
                            } else {
                                $('#news-list').html(data);
                                $('#news-load').hide();
                                $('#news-list').show();
                                addClickEvent();
                            }
                        }
                    );
                    return false;
                }
            )
        }
    );
    $('#news-list').children('.position').each(
        function (i) {
            $(this).mouseenter (
                function () {
                    $(this).addClass('select');
                }
            );
            $(this).mouseleave (
                function () {
                    $(this).removeClass('select');
                }
            );
            $(this).click (
                function () {
                    location.href = $(this).children('.prev').children('.more').children('a').attr('href');
                }
            );
        }
    );
}


