// for menu
var timeout             = 500;
var close_timer         = 0;
var ddm_item            = 0;

function ddm_open()
{
    ddm_cancel_timer();
    ddm_close();
    ddm_item = $(this).find('ul').css('visibility', 'visible');
}

function ddm_close()
{
    if(ddm_item)
    {
        ddm_item.css('visibility', 'hidden');
    }
}

function ddm_timer()
{
    close_timer = window.setTimeout(ddm_close, timeout);
}

function ddm_cancel_timer()
{
    if(close_timer)
    {
        window.clearTimeout(close_timer);
        close_timer = null;
    }
}

function set_input_focus()
{
    // set focus high-light for all input[text | password]
    $('input:text,input:password,textarea').focus(function()
    {
        $(this).css('border', 'solid 1px #3B7DB7');
        $(this).css('background-color', '#FFFFDF');
    });
    
    $('input:text,input:password,textarea').blur(function()
    {
        $(this).css('border', 'solid 1px #aaa');
        $(this).css('background-color', '#FFF');
    });

    $('input:text:first').focus();
}

function set_alternative_rows()
{
    if ($('.list tr:even'))
    {
        $('.list tr:even').addClass('even');
    }
}

function bind_change_cat()
{
    if ($('select[name=category_id]'))
    {
        $('select[name=category_id]').change(function(){
            load_news();
        });
    }
}

function load_cat(lang, is_onchange)
{
    request_uri = '/member/categories/' + lang;

    $.ajax(
    {
        type:   'post',
        url:    request_uri,
        data:   {
            'is-ajax': 1
        },
        success: function(responseText) {
            if ($('#categories'))
            {
                $('#categories').html(responseText);

                if (is_onchange) bind_change_cat();
            }
        }
    });
}

function load_news()
{
    var lang = $('input:radio:checked').val();
    var category_id = $('select[name=category_id]').val();

    if (category_id != -1)
    {
        var request_uri = '/member/news/' + lang;

        $.ajax(
        {
            type:   'post',
            url:    request_uri,
            data:   {
                'category-id'   : category_id,
                'is-ajax'       : 1
            },
            success: function(responseText) {
                if ($('#news_list'))
                {
                    $('#news_list').html(responseText);
                    set_alternative_rows();
                }
            }
        });
    }
}

function confirm_delete_news(news_id)
{
    if (confirm('Bạn có thực sự muốn xóa tin này không?'))
    {
        var lang        = $('input:radio:checked').val();
        var category_id = $('select[name=category_id]').val();
        var request_uri = '/member/news/delete/' + news_id;

        $.ajax(
        {
            type:   'post',
            url:    request_uri,
            data:   {
                'is-ajax'       : 1,
                'category-id'   : category_id,
                'lang'          : lang
            },
            success: function(responseText) {
                if ($('#news_list'))
                {
                    $('#news_list').html(responseText);
                    set_alternative_rows();
                }
            }
        });
    }
}

function enable_wysiwyg(selector)
{
    tinyMCE.init({
        mode : "textareas",
        editor_selector : selector,
        theme : "advanced",
        invalid_elements : "div,script,abbr,acronym,address,applet,area,bdo,big,blockquote,button,caption,cite,code,col,colgroup,dd,del,dfn,iframe,input,ins,isindex,kbd,label,legend,map,menu,noscript,object,optgroup,option,param,textarea,var,ruby,samp,select,rtc",
        height: "300px",
        plugins : "paste,inlinepopups,contextmenu,table,heading,preview",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,h1,h2,h3,|,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,|,image,table",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        //theme_advanced_path_location : "bottom",
        entity_encoding : "raw",
        language : 'en',
        theme_advanced_buttons1_add : "forecolor,backcolor,separator,link,unlink,,pasteword,removeformat,preview",
        plugin_preview_width : "640",
        plugin_preview_height : "480"
    });
}

function fix_ie_z_index()
{
    var zIndex = 1000;
    
    $('div').each(function(){
       $(this).css('zIndex', zIndex);
       zIndex -= 10;
    });
}

$(document).ready(function()
{
    $('.dropdown > li').bind('mouseover', ddm_open);
    $('.dropdown > li').bind('mouseout',  ddm_timer);

    // add banner functionalities
    $('.gallery1').gallery();

    // set focus high-light for all input[text | password]
    set_input_focus();

    set_alternative_rows();

    bind_change_cat();

//    fix_ie_z_index();
});

document.onclick = ddm_close;
/*
 * End of file jpost.js
 * Path: scripts/jpost.js
 */