function HeaderTimerMinutesHandler()
{
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();
    if(hours.toString().length == 1) hours = '0' + hours.toString();
    if(minutes.toString().length == 1) minutes = '0' + minutes.toString();
    $("#header_timer_time > span.hour").html(hours);
    $("#header_timer_time > span.minutes").html(minutes);
    setTimeout(HeaderTimerMinutesHandler, 60000);
}

function InitializeHeaderTimer()
{
    setTimeout(HeaderTimerMinutesHandler, 60000);
}

function InitializeMenu()
{
    $("#menu_inner_1 div.menu_item:last").addClass("last_menu_item");
    $("#menu_inner_1 div.active_menu_item").prev().addClass("before_active_menu_item");
}

function InitializeLeftMenu()
{
    $("#left_menu_inner div.menu_item:last").addClass("last_menu_item");
    $("#left_menu_inner div.menu_item, #left_menu_inner div.submenu_item").unbind("mouseenter");
    $("#left_menu_inner div.menu_item, #left_menu_inner div.submenu_item").unbind("mouseleave");
}

function InitializeContentInternetTvBlock()
{
    $("#content_internet_tv_block_items tr > td:odd").addClass("even_content_internet_tv_block_item");
}

function InitializeContentPromoBlock()
{
    $("div.content_promo_block_item:last-child").addClass("last_content_promo_block_item");
}

function InitializeContentPhotos()
{
    $("#content_main_block_inner img[class!=cap_img]").each(function(){
        if($(this).parent("a").size() > 0)
            $(this).parent().wrap("<div class='content_photo inline_block' />");
        else
            $(this).wrap("<div class='content_photo inline_block' />");
    });
    $("div.content_photo > img").each(function(){
        var title = $(this).attr("title");
        if(title !== undefined && title !== "")
            $(this).after("<div class='content_photo_text inline_block'>" + title + "</div>");
        if($(this).hasClass("float_left") || $(this).attr("align") === "left" || $(this).css("float") === "left")
            $(this).parent().addClass("content_photo_with_float_left");
        else if($(this).hasClass("float_right") || $(this).attr("align") === "right" || $(this).css("float") === "right")
            $(this).parent().addClass("content_photo_with_float_right");
        $(this).removeClass("float_left").removeClass("float_right").removeAttr("align").css("float", "none");
    });
    $("div.content_photo > a > img").each(function(){
        var title = $(this).attr("title");
        if(title !== undefined && title !== "")
            $(this).parent().after("<div class='content_photo_text inline_block'>" + title + "</div>");
        if($(this).hasClass("float_left") || $(this).attr("align") === "left" || $(this).css("float") === "left")
            $(this).parent().parent().addClass("content_photo_with_float_left");
        else if($(this).hasClass("float_right") || $(this).attr("align") === "right" || $(this).css("float") === "right")
            $(this).parent().parent().addClass("content_photo_with_float_right");
        $(this).removeClass("float_left").removeClass("float_right").removeAttr("align").css("float", "none");
    });
    $("p > div.content_photo").unwrap();
}

function InitializeContentDocuments()
{
    $("#content_wrapper_td a.content_document_doc, #content_wrapper_td a.content_document_xls, #content_wrapper_td a.content_document_pdf, #content_wrapper_td a.content_document_rar").addClass("content_document");
    $("#content_wrapper_td p > a.content_document").unwrap();
}

function InitializeAjaxFolkNewsForm()
{
    $("#send_folk_news_form").submit(function(){SendFolkNews();return false;});
    $("#send_folk_news_form #folk_news_button").click(function(){SendFolkNews();return false;});
}

function SendFolkNews(step, success)
{
    var info_id = "#send_folk_news_info";
    var form_id = "#send_folk_news_form";
    var data_row = 'script=add_folk_news';
    if(step === undefined) step = 1;
    if(success === undefined) success = false;
    if(step == 1)
        {
        data_row += get_all_form_data(form_id);
        ajax_query(info_id, form_id, data_row);
    }
    else if(step == 2)
        {
        if(success)
            {
            $(form_id + " input[type='text'], " + form_id + " input[type='password']").attr("value","");
            $(form_id + " textarea").attr("value","");
        }
    }
}

function InitializeFolkNews()
{
    $('#file_upload').uploadify({
        'uploader'  : '/uploadify/uploadify.swf',
        'script'    : '/uploadify/uploadify.php',
        'cancelImg' : '/uploadify/cancel.png',
        'folder'    : '/upload/uploadify',
        'auto'      : true,
        'fileDesc'  : 'Images',
        'fileExt'   : '*.jpg;*.jpeg;*.gif;*.png',
        'buttonText': 'Select',
        /*'sizeLimit' : 1024*1024*10,*/
        'onComplete': function(event, ID, fileObj, response, data){
            $('#file_uploaded').val(fileObj.name);
        }
    });
}

function InitializeBulletinBoard()
{
    $('#bulletinBoardRubricSelect').change(function(){
        var value = $(this).val();
        if(value != 0)
            {
            if($(this).find('option[value=' + value + ']').parent().get(0).tagName === 'OPTGROUP')
                {
                var parentId = $(this).find('option[value=' + value + ']').parent().attr('rel');
                window.location = window.location + parentId + '/' + value + '/';
            }
            else
                window.location = window.location + value + '/';
        }
    });
}

function InitializeVideo()
{
    $(".specialVideoPlayerDiv").each(function (i) {
        var id = $(this).attr("id");
        var file = $(this).attr("data-filename");
        var preview = $(this).attr("data-preview");
        var v_comment = $(this).attr("data-comment");
        var player_path = "/players/";
        swfobject.embedSWF(player_path + "uppod.swf", id, 450, 380, "9.0.115.0", player_path + "expressInstall.swf",
        {
            wmode: "transparent",
            file: file,
            poster: preview,
            comment: v_comment,
            st: player_path+"styles/etver.txt"
        },
        {
            wmode: "transparent",
            allowFullScreen: "true",
            allowScriptAccess: "always",
            movie: player_path + "uppod.swf"
        }
        );
    });
}

function CutBulletingTextArea(){
    var maxLen = 250;
    $('#m_descrition11').keyup( function(){
        var $this = $(this);
        if($this.val().length > maxLen)
            $this.val($this.val().substr(0, maxLen));            
    });
};


$(document).ready(function(){
    InitializeHeaderTimer();
    InitializeMenu();
    InitializeLeftMenu();
    InitializeContentInternetTvBlock();
    InitializeContentPromoBlock();
    InitializeContentPhotos();
    InitializeContentDocuments();
    InitializeAjaxFolkNewsForm();
    InitializeFolkNews();
    InitializeVideo();
    InitializeBulletinBoard();
    CutBulletingTextArea();
});
