var browser_type = 'msie';
var W=navigator.userAgent.toLowerCase();
if(W.indexOf("opera")!=-1){
    browser_type='opera';
}else
if(W.indexOf("msie")!=-1){
    browser_type='msie';
} else
if(W.indexOf("mozilla")!=-1){
    browser_type='firefox';
}




var menu_qty = 6;
var noSubMenu = new Array();

noSubMenu[0] = 1;
noSubMenu[1] = 5;
noSubMenu[2] = 6;
//noSubMenu[2] = 3;
//noSubMenu[3] = 4;
//noSubMenu[4] = 5;
//noSubMenu[5] = 6;



function showSubMenu (mark) {
    var noShowSubmenu = false;
    for (i in noSubMenu) if (noSubMenu[i] == mark) noShowSubmenu = true;
	
    hideAllSubmenu();
    document.getElementById('td_menu_'+mark).className = "main_menu_sel";
	
    if (noShowSubmenu == false) {
        //document.getElementById('tbl_submenu').style.display = "block";
        document.getElementById('div_submenu_'+mark).style.display = "block";
    }
	
}



function hideAllSubmenu () {
    for (i = 1; i <= menu_qty; i++) {
        document.getElementById('td_menu_'+i).className = "main_menu";
        document.getElementById('div_submenu_'+i).style.display = "none";
    }
	
//document.getElementById('tbl_submenu').style.display = "none";
}


function selectMenu (mark) {
	
    document.getElementById('td_menu_'+mark).className = "main_menu_sel";
	
//document.getElementById('menu_img_'+mark).src = "/images/menu_rozd.jpg";
//document.getElementById('menu_img_'+(parseInt(mark) + 1)).src = "/images/menu_rozd_2.jpg";
	
}



var switch_to_rte = 0;
var  commentTimer = null;

function commentToPlain(id, tab) {
    $('#comment_form .tabs dt').removeClass('active');
    $(tab).parent().addClass('active');
    if (switch_to_rte == 1) toggleEditor(id);
    clearInterval( commentTimer );
    return false;
}


function commentToEditor(id, tab) {
    $('#comment_form .tabs dt').removeClass('active');
    $(tab).parent().addClass('active');
    if (switch_to_rte == 0) toggleEditor(id);
    commentTimer = setInterval(function check(){
        ret = checkCommentSimbols( tinyMCE.get(id).getContent() );
        if( ret.sub === true ){
            tinyMCE.get(id).setContent(ret.content)
        }
    }, 2000);
    return false;
}



function toggleEditor(id) {
	
    if (!tinyMCE.get(id)) {
        tinyMCE.execCommand('mceAddControl', false, id);
        switch_to_rte = 1;
    }
    else {
        tinyMCE.execCommand('mceRemoveControl', false, id);
        switch_to_rte = 0;
    }
	
}


function makeEditor(id){
    tinyMCE.execCommand('mceAddControl', false, id);
}


function unmakeEditor(id) {
    tinyMCE.execCommand('mceRemoveControl', false, id);
}



function chagePrandPrixBlock(obj) {
    var url = "/ajax_request/load_grand_prix_block_results.php?type="+obj.value;
    var jsel = document.createElement('SCRIPT');
    jsel.type = 'text/javascript';
    jsel.src = url;
    document.getElementById('js').appendChild(jsel);
}



//function showRegistrationForm() {
//    closeLogin();
//
//    document.getElementById('div_trans').style.height = document.body.scrollHeight+"px";
//    document.getElementById('div_trans').style.display = "block";
//    //alert(2);
//    var width = "330";
//    var height = "205";
//    var left = (screen.width  - width)  / 2;
//    var top = (screen.height - height) / 3 + document.body.scrollTop;
//
//    document.getElementById('registration_block_div').style.left = left+"px";
//    document.getElementById('registration_block_div').style.top = top+"px";
//
//    document.getElementById('registration_block_div').style.display = "block";
//}



//function closeRegistration() {
//    document.getElementById('div_trans').style.display = "none";
//    document.getElementById('registration_block_div').style.display = "none";
//}




//function showLogin() {
//    document.getElementById('div_trans').style.height = document.body.scrollHeight+"px";
//    document.getElementById('div_trans').style.display = "block";
//    //alert(2);
//    var width = "330";
//    var height = "200";
//    var left = (screen.width  - width)  / 2;
//    var top = (screen.height - height) / 3 + document.body.scrollTop;
//
//    document.getElementById('login_block_div').style.left = left+"px";
//    document.getElementById('login_block_div').style.top = top+"px";
//
//    document.getElementById('login_block_div').style.display = "block";
//}





//function closeLogin() {
//    document.getElementById('div_trans').style.display = "none";
//    document.getElementById('login_block_div').style.display = "none";
//}



//function showForgotPassword() {
//    closeLogin()
//
//    document.getElementById('div_trans').style.height = document.body.scrollHeight+"px";
//    document.getElementById('div_trans').style.display = "block";
//    //alert(2);
//    var width = "330";
//    var height = "200";
//    var left = (screen.width  - width)  / 2;
//    var top = (screen.height - height) / 3 + document.body.scrollTop;
//
//    document.getElementById('forgot_password_block_div').style.left = left+"px";
//    document.getElementById('forgot_password_block_div').style.top = top+"px";
//
//    document.getElementById('forgot_password_block_div').style.display = "block";
//}
//
//
//
//
//
//function closeForgotPassword() {
//    document.getElementById('div_trans').style.display = "none";
//    document.getElementById('forgot_password_block_div').style.display = "none";
//}




//function showNotice() {
//	document.getElementById('div_trans').style.height = document.body.scrollHeight+"px";
//	document.getElementById('div_trans').style.display = "block";
//	//alert(2);
//	var width = "300";
//	var height = "200";
//	var left = (screen.width  - width)  / 2;
//	var top = (screen.height - height) / 3 + document.body.scrollTop;
//
//	document.getElementById('notice_div').style.left = left+"px";
//	document.getElementById('notice_div').style.top = top+"px";
//
//	document.getElementById('notice_div').style.display = "block";
//
//	document.onkeypress = closeNoticeEscape;
//}



//function closeNotice() {
//    document.getElementById('div_trans').style.display = "none";
//    document.getElementById('notice_div').style.display = "none";
//}



function closeNoticeEscape(e) {
    if (!e) e = window.event;
    var k = e.keyCode;
	
    if (k == 27) closeNotice();

}



function confirmdel(url) {
    message = 'Вы уверены что хотите удалить?';
	
    if (confirm(message)) {
        if (browser_type == "firefox") {
            self.location.href = url;
        }
        else {
            document.getElementById('link').href = url;
            document.getElementById('link').click();
        }
    }
}



function changeForumBlock(mark) {
    document.getElementById('td_forum_block_new').className = "forum_block";
    document.getElementById('td_forum_block_popular').className = "forum_block";
	
    document.getElementById('td_forum_block_'+mark).className = "forum_block_sel";
	
    document.getElementById('div_form_block').innerHTML = '<img src="/images/loading.gif" width="50" height="50" alt="" style="padding: 90px 0px 90px 70px;" align="center">';
	
    var url = "/ajax_request/load_forum_block.php?mark="+mark;
    var jsel = document.createElement('SCRIPT');
    jsel.type = 'text/javascript';
    jsel.src = url;
    document.getElementById('js').appendChild(jsel);
	
}


// VOTING

function showVotingArchive(voting_id) {
    document.getElementById('div_voting_btn').style.display = 'none';
    document.getElementById('div_voting_data').innerHTML = '<img src="/images/loading.gif" width="50" height="50" alt="" style="padding:70px 0px 70px 70px;">';
	
    var url = base_url+"/ajax_request/load_voting_results.php?voting_id="+voting_id;
    var jsel = document.createElement('SCRIPT');
    jsel.type = 'text/javascript';
    jsel.src = url;
    document.getElementById('js').appendChild(jsel);
}



function votingMakeVote(voting_id) {
	
    var var_href = "";
	
    for (i = 0; i < answer_qty; i++) {
        if (document.getElementById('a_vote_'+i).checked == true) {
            var_href += "&answer_id[]="+document.getElementById('a_vote_'+i).value;
        }
    }
	
    document.getElementById('div_voting_btn').style.display = 'none';
    document.getElementById('div_voting_data').innerHTML = '<img src="/images/loading.gif" width="50" height="50" alt="" style="padding:70px 0px 70px 85px;">';
	
	
    var url = base_url+"/ajax_request/make_voting_vote.php?voting_id="+voting_id+var_href;
    var jsel = document.createElement('SCRIPT');
    jsel.type = 'text/javascript';
    jsel.src = url;
    document.getElementById('js').appendChild(jsel);
}

function commentSimbols( obj ){
    ret = checkCommentSimbols(obj.value)
    obj.value = ret.content ;
}

function checkCommentSimbols ( content ){
    var maxcommentsimbols = 3000;
    ret = {}
    ret.sub = false;
    ret.content = content;
    if( content.length > maxcommentsimbols){
        ret.sub = true;
        ret.content = content.substring(0, maxcommentsimbols);
    }
    document.getElementById('countS').innerHTML = maxcommentsimbols - ret.content.length;
    return ret;
}

function checkCommentForm() {
    var message = "";

    if (switch_to_rte == 0) {
        if (document.getElementById('comment_content').value == "") {
            if (message != "") message += "\n";
            message += 'Введите тело коментария';
        }
    }

    else {
        if (tinyMCE.get('comment_content').getContent() == "") {
            if (message != "") message += "\n";
            message += 'Введите тело коментария';
        }
    }



    if (document.getElementById('nick_name').value == "") {
        if (message != "") message += "\n";
        message += 'Выберите подпись';
    }


    if (message == "") {
        return true;
    }
    else {
        alert(message);
        add_comm = true;
        return false;
    }

}

var add_comm = true;

function addComment() {
    if (add_comm == true) {

        add_comm = false;

        if (document.getElementById('image').value) {
            if (checkCommentForm()) {
                document.getElementById('comment_form').submit();
            }
        }
        else {
            var var_data = "module_id="+document.getElementById('module_id').value;
            var_data += "&module_type="+document.getElementById('module_type').value;
            content = (switch_to_rte == 0) ? document.getElementById('comment_content').value : tinyMCE.get('comment_content').getContent();
            ret = checkCommentSimbols( content );
            if( switch_to_rte != 0 ){
                tinyMCE.get('comment_content').setContent( ret.content );
                ret.content = tinyMCE.get('comment_content').getContent();
            }
            content = encodeURIComponent(ret.content);
            var_data += "&content=" + content;
            //			if (switch_to_rte == 0) var_data += "&content="+encodeURIComponent(document.getElementById('comment_content').value);
            //			else var_data += "&content="+encodeURIComponent(tinyMCE.get('comment_content').getContent());
            var_data += "&convert_coding=1";
            var_data += "&parent_id="+document.getElementById('parent_id').value;
            var_data += "&nick_name="+encodeURIComponent(document.getElementById('nick_name').value);
            var_data += "&rte="+switch_to_rte;
            if (document.getElementById('sec_code')) var_data += "&sec_code="+document.getElementById('sec_code').value;

            var gotError = function(err) {
                alert("System Error");
                return;
            }

            var gotInfo = function(data) {
                if (data.success == "1") {
                    var loading = '<TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="160" HEIGHT="50" style="border:solid 1px #CCCCCC;" align="center">';
                    loading += '		<tr>';
                    loading += '			<td WIDTH="50"><img SRC="/images/loading.gif" WIDTH="50" HEIGHT="50"></td>';
                    loading += '			<td>Загружается...</td>';
                    loading += '		</tr>';
                    loading += '   </TABLE>';

                    document.getElementById('div_comment_list').innerHTML = loading;
                    reloadComment(data.id, document.getElementById('module_type').value);
                }

                if (data.error == "1") {
                    var messArr = data.message.split("*");
                    var mess = messArr.join("\n");
                    alert( mess );
                    add_comm = true;
                }
                return;
            }


            var url = document.getElementById('comment_form').action;
            var opts = {
                "data": var_data,
                "method": "POST",
                "url": url,
                "onError": gotError,
                "onData": gotInfo

            };

            HTTPReq.getJSON(opts);
        }

    }
}

function reloadComment(last_id, type) {
    switch( type ){
        case 'race':
            url = base_url+"/ajax_request/load_race_comment.php?id="+document.getElementById('module_id').value+"&last_id="+last_id;
            break;
        case 'mat':
            url = base_url+"/ajax_request/load_story_comment.php?id="+document.getElementById('module_id').value+"&last_id="+last_id;
            break;
        case 'sp':
            url = base_url+"/ajax_request/load_special_page_comment.php?id="+document.getElementById('module_id').value+"&last_id="+last_id;
            break;
        case "community":
            url = base_url+"/community/comments/" + document.getElementById('module_id').value + "/json/";
            break;
    }
    var jsel = document.createElement('SCRIPT');
    jsel.type = 'text/javascript';
    jsel.src = url;
    document.getElementById('js').appendChild(jsel);
}

function makeAnswer(parent_id) {
	document.getElementById('div_answer_content').style.display = "block";
	document.getElementById('div_answer_content').innerHTML = document.getElementById('comment_content_'+parent_id).innerHTML;
	document.getElementById('parent_id').value = parent_id;
	location.href = '#add_comment_form';
}

function clearForm() {
	document.getElementById('comment_content').value = "";
	if (switch_to_rte == 1) tinyMCE.get('comment_content').setContent("");
	document.getElementById('parent_id').value = '0';
	document.getElementById('div_answer_content').innerHTML = "";
	document.getElementById('div_answer_content').style.display = "none";
	add_comm = true;
}

function SetCookie(sName, sValue)
{
    var now = new Date();
    cookie_exp = new Date(now.getFullYear() + 1, now.getMonth(), now.getDay());
    document.cookie = sName + "=" + escape(sValue) + "; expires=" + cookie_exp.toGMTString() + "; path=/";
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++)
    {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}


Menu = {};
Menu.show = function ( number ){
    number = ( number )? (number - 1):0;
    $("#menu dd:not(dl.sub dd):eq(" + number + ")").addClass('active');
    $("#menu dd:not(dl.sub dd)").bind('mouseover',  function(){
        $("#menu dd:not(dl.sub dd)").removeClass('active');
        $(this).addClass('active');
    });
}

Rating = {}
Rating.rate = function( object, id ){
    url = object.href;
    $.get( url, {}, function ( data ){
        if( data.success == 1){
            ret = $("#rating_value_" + id).html();
            ret = parseInt(ret,10) + data.point;
            $("#rating_value_" + id).html(ret);
            Rating.showRateMessage(id, lang.rating.ok);
        } else {
           Rating.showRateMessage( id, data.error );
        }
    }, 'json');
//    if( object.id == "ra_ok_" + id ){
//        $(object).html( lang.rating.button_ok );
//    }
//    $("#ra_ok_" + id).attr('href', "#").click( function(){ return false});
//    $("#ra_bad_" + id).attr('href', "#").click( function(){ return false});
    
    return false;
}

Rating.hideRateMessage = function ( id ){
    $("#rat_mess_span_" + id ).fadeOut( 'slow' );
}
Rating.showRateMessage = function ( id, msg ){
    $("#rat_mess_span_" + id).html( msg );
    $("#rat_mess_span_" + id).fadeIn( 'slow' );

    setTimeout("Rating.hideRateMessage(" + id + ");",  3000);
}

var Lightbox = {}
Lightbox.config = {
    imageLoading:"/images/lightbox/lightbox-ico-loading.gif",
    imageBtnClose:"/images/lightbox/lightbox-btn-close.gif",
    imageBtnPrev:"/images/lightbox/lightbox-btn-prev.gif",
    imageBtnNext:"/images/lightbox/lightbox-btn-next.gif"
}

var Teams = {}
Teams.changeSeason = function ( obj ){
    location.href = cU + obj.value + '/';
}
Teams.setSeasonYear = function ( year){
    $("#season option[value=" + year + "]").attr( 'selected', true);
}

var Pilots = Teams;

//function getUserLogin( ){
//    var src = "/auth/getLogin/?" + Math.random();
//    var e = document.createElement("script");
//    e.src = src;
//    e.type="text/javascript";
//    document.getElementsByTagName("head")[0].appendChild(e);
//}