var isGet = 1;
$j(function() {
//    $j("#indicator-loading").dialog({
//			autoOpen: false,
//            resizable: false,
//			modal: true
//    });

    var menu1 = 0;
    var menu2 = 0;
    $j('.my-menu').click(function(){
        if (menu2 == 1) {
            $j('.my-conf-menu-ui').toggle('blind', {}, 300);
            menu2 = 0;
        }
        
        $j('.my-menu-ui').toggle('blind', {}, 300);
        menu1 = menu1 == 0 ? 1 : 0;
    });
    
    $j('.my-conf-menu').click(function(){
        if (menu1 == 1) {
            $j('.my-menu-ui').toggle('blind', {}, 300);
            menu1 = 0;
        }

        $j('.my-conf-menu-ui').toggle('blind', {}, 300);
        menu2 = menu2 == 0 ? 1 : 0;
    });

    $j(".dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 140,
        modal: true
    });

    function favClick() {
        val = $j('.fav-value').text();
        url = '/favorite/toggle?conferenceId=' + val;
        prepareAjax(url, "POST");

        $j('.fav img').attr('src', '/images/indicator-small.gif');
        $j.ajax({
            complete: function(complete) {
                if (complete.responseText == 'in-favorite') {
                    $j('.fav img').attr('src', '/images/favorite_disabled.gif');
                    $j('.fav-link').hide();
                } else {
                    $j('.fav img').attr('src', '/images/favorite.gif');
                }
                
                $j('.fav img').effect('highlight',{},1000);
            }
        });
    }
    $j('.fav').click(function(){
        favClick();
    });

});

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $j(window).height() - this.height() ) / 2+$j(window).scrollTop() + "px");
    this.css("left", ( $j(window).width() - this.width() ) / 2+$j(window).scrollLeft() + "px");
    return this;
}


function showPreloader() {
    $j("#main_td").html('<div style="width:100%;height:100%;"><br /><br /><br /><br /><center><h3><img src=/images/indicator-small.gif>&nbsp;&nbsp;Подождите, идет загрузка...</h3></center></div>');
//    $j("#indicator-loading").removeClass("ui-helper-hidden");
//    $j("#indicator-loading").addClass("ui-widget-overlay");
    
}

function hidePreloader() {
//    $j("#indicator-loading").dialog('close');
}

function linkTo(url) {
    showPreloader();
    var fullUri = '/'+url;
    document.location.href = fullUri;
}

function prepareAjax(fullUrl, _TYPE) {
    $j.ajaxSetup({
        url: fullUrl,
        type: _TYPE
    });
}

function do_ajax(url, noUpdate) {
    if (isGet == 1) {
        prepareAjax(url, "GET");
    } else {
        prepareAjax(url, "POST");
    }

    showPreloader();
    _ajax(noUpdate);

    isGet = 1;
    return false;
}

function submitForm(item, update) {
    var params = $j(item).serialize();
    isGet = 0;
    do_ajax($j(item).attr('action')+'?'+params, update);
    return true;
}

function _ajax(noUpdate) {
    $j.ajax({
        complete: function(complete) {
                    hidePreloader();
                    if (noUpdate) {
                        $j('#'+noUpdate).html(complete.responseText);
                        $j("#"+noUpdate).effect('highlight',{},1000);
                    }
        }
    });
}
function doSendFeedback(idUpdater, addr, params) {
    isGet = 2;

    url = addr +'?' + params
    prepareAjax(url, "POST");

    $j('#'+idUpdater).html("Идет отправка формы <img src='/images/indicator-small.gif' />");

    $j.ajax({
        complete: function(complete) {
            $j('#'+idUpdater).html(complete.responseText);
            $j("#"+idUpdater).effect('highlight',{},1000);
        }
    });

}

function getRandom(min, max) {
  return Math.random() * (max - min) + min;
}

function removeItem(url, item) {
    prepareAjax(url, "GET");
    _ajax();
    $j('.item-'+item).remove();
    return false;
}

var info = '';
function showListInfo() {
    if (info == '') {
        info = $j('#tt').html();
    }
    $j('#tt').html($j('.conf-list').html());
    $j('#tt').addClass('conf-list');

    $j('.info-add').hide();
}

function showConfInfo() {
    $j('#tt').html(info);
    $j('#tt').removeClass('conf-list');
    $j('.info-add').show();
}