var $baseUrl = '/';
var $login = {pop: undefined, showed: false, url: $baseUrl+'?module=ajax&action=login'};

$(document).ready(function(){
    $("#search").autocomplete($baseUrl+"?module=ajax&action=search_context", {
        delay:2,
        minChars:1,
        matchSubset:1,
        autoFill:false,
        matchContains:1,
        cacheLength:10,
        selectFirst:true,
        maxItemsToShow:10,
        width:236
    });
});

function addToFavorite(id, type, task) {
    if ($logged == false) {
        showAlert('Что бы добавить клип или видео в избранное нужно быть <a href="'+$baseUrl+'?module=users&action=register" target="_blank">зарегистрированым</a> на нашем сайте!');
        showLogin();
        return false;
    }
    var url = $baseUrl+'?module=ajax&action=favorite&id='+id;
    if (type !== undefined) {
        url += '&type='+type;
    }
    if (task !== undefined) {
        url += '&task='+task;
    }
    $.get(url, {}, function(data){showAlert(data)}, 'html');
    return true;
}

function setRate(id, rate, idTag) {
//    if ($logged == false) {
//        showAlert('Ошибка! Возможно надо<a href="'+$baseUrl+'?module=users&action=login" target="_blank">залогиниться</a>');
//        return false;
//    }
    var url = $baseUrl+'zend_index.php?module=ajax&action=rate&vote='+rate;
    if (idTag == undefined) {
        url += '&cid='+id;
    } else {
        url += '&'+idTag+'='+id;
    }
    $.get(url, {}, function(data){
        if (data == 'Ok') {
            curVotes = $('#rateVotes').html();
            $('#rateVotes').html(parseInt(curVotes) + 1);
        }
        showAlert(data);
    }, 'html');
    return true;
}

function postComment(frm, _showAlert) {
    if ($logged == false) {
        if (_showAlert !== false) {
            showAlert('Что бы комментировать нужно быть <a href="'+$baseUrl+'?module=users&action=register" target="_blank">зарегистрированым</a> на нашем сайте!');
        }
        showLogin();
        return false;
    }
    if ($('#commText').val() == '') {
        showAlert('Введите текст комментария');
        return false;
    }

    if ($('#commText').val().length < 4) {
        showAlert('Текст комментария слишком короткий');
        return false;
    }

    dataString = $(frm).serialize().replace('%5B', '[').replace('%5D', ']');
    $.post($baseUrl+'?module=ajax&action=addcomment', dataString, function(message){
        showAlert(message);
        frm.reset();
    });
    return true;
}

function showAlert(msg) {
    //alert(msg);
    if (msg !== undefined) {
        $("#flashMessages").html('<li>'+msg+'</li>');
    }

    $("#flashMessages").slideDown("slow");
    setTimeout(function() {$("#flashMessages").slideUp("fast");}, 5000);
}

function showLogin() {
    if ($login.showed == false) {
        if ($login.pop == undefined) {
            $('body').append('<div id="loginPop"></div>');
            $login.pop = $('#loginPop');
        }
        $.get($login.url, function(loginForm){
            $login.pop.html(loginForm).show();
            $login.showed = true;
        });
    }
}

function checkLogin(frm) {
    dataString = $(frm).serialize().replace('%5B', '[').replace('%5D', ']');
    $.post($login.url, dataString, function(result){
        if (result == 'true') location.reload(true); else $login.pop.html(result);
    }, "html")
}
