function sure()
{
    return confirm('Are you sure ?');
}


function openWin(url, name, width, height)
{
  if (! width) {
           width = screen.width*0.9;
  }
  if (! height) {
           height = screen.height*0.75;
  }
  var newWindow = window.open(url, name, 'left=' + Math.ceil((screen.width - width)/2) + ',top=' + Math.ceil((0+screen.height - height)/2) + ',width=' + width + ',height=' + height + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,channelmode=0,fullscreen=0');
  newWindow.focus();
  return false;
}

function selectAll(name, val)
{
    a = document.getElementsByName(name);
    for(i=0;i<a.length;++i) a[i].checked =   val;
}

function viewBlock(block, value) {
  document.getElementById(block).style.display = (value == 0) ? 'none' : '';
}


function crz_go_vote(nSiteId, nElIdx){
    if (typeof(nSiteId) == 'undefined'){
        nSiteId = crz_go_vote.nSiteId;
    }
    else{
        crz_go_vote.nSiteId = nSiteId;
    }
    if (typeof(nElIdx) == 'undefined'){
        nElIdx = crz_go_vote.nElIdx;
    }
    else{
        crz_go_vote.nElIdx = nElIdx;
    }
    $.post('ajax/index/star/', {site_id : nSiteId, 'star' : nElIdx}, function(data){
        if (data.result == 'ok'){
            $("span.star").attr('title', data.new_rating + ' / ' + data.new_rating_count);
            new_rating = Math.round(data.new_rating);
            $("span.star > span").removeClass('on').slice(0, new_rating).addClass('on');
            $('span.star_message').show();
        }
        else if (data.result == 'error' && data.code != 'auth'){
            alert(data.error);
        }
        else if (data.code == 'auth'){
            crz_auth.back = crz_go_vote;
            crz_auth.show_auth_dialog();
        }
    }, 'json');
}


crz_comment = {
    nSiteId : 0,
    nPageId : 0,
    nHM : 10,
    nPageCount : 0,

    show_comment_form : function (nSiteId){
        $('body').css('cursor', 'wait');
        if (typeof(nSiteId) != 'undefined'){
            crz_comment.nSiteId = nSiteId;
        }

        $.post('ajax/index/comment/', {object_id : crz_comment.nSiteId, mode : 'check'}, function(data){
            $('body').css('cursor', '');
            if (data.result == 'ok'){
                $('body').css('cursor', 'wait');
                $('#crz_comments_form').load('ajax/index/comment/', {object_id : crz_comment.nSiteId, mode : 'load'}, function(){$('body').css('cursor', '');}).show();
            }
            else if (data.result == 'error' && data.code != 'auth'){
                alert(data.error);
            }
            else if (data.code == 'auth'){
                crz_auth.back = crz_comment.show_comment_form;
                crz_auth.show_auth_dialog();
            }
        }, 'json');
    },

    go_comment : function(){
        $('body').css('cursor', 'wait');
        comment_data = {
            comment_author : $('#crz_comments_form input[name="comment_author"]').val(),
            comment_email : $('#crz_comments_form input[name="comment_email"]').val(),
            comment_text : $('#crz_comments_form textarea[name="comment_text"]').val(),
            object_id : crz_comment.nSiteId,
            user_cpt : $('#crz_comments_form input[name="user_cpt"]').val()
        }
        
        $.post('ajax/index/gocomment/', comment_data, function(data){
            if (data.result == 'ok'){
                crz_comment.append_comment(data.comment_id);
                $('#crz_comments_form').html('').hide();
            }
            else if (data.result == 'error'){
                $('#crz_comments_form #crz_comment_message').text(data.error).css('color', 'red');
            }
            $('body').css('cursor', '');
        }, 'json');
    },

    append_comment : function(comment_id){
        $.post('/ajax/index/getcomment/', {comment_id : comment_id, object_id : crz_comment.nSiteId}, function(data){
            comments_els = $('#crz_comments > div')
            if (crz_comment.nPageId == 0 && comments_els.size() >= crz_comment.nHM){
                comments_els.last().remove();
            }
            if (crz_comment.nPageId == 0){
                comments_els = $('#crz_comments').prepend(data.html);
            }
            else{
                crz_comment.load_page(0);
            }
        }, 'json');
    },

    load_page : function(mPageId){
        $('body').css('cursor', 'wait');
        if (typeof(mPageId) == 'string'){
            if (mPageId == 'next'){
                mPageId = crz_comment.nPageId + 1;
                if (mPageId > crz_comment.nPageCount){
                    mPageId = crz_comment.nPageCount;
                }
            }
            else if (mPageId == 'prev'){
                mPageId = crz_comment.nPageId - 1;
                if (mPageId < 0) mPageId = 0;
            }
        }
        $.post('ajax/index/getcomment', {page : mPageId, object_id : crz_comment.nSiteId}, function(data){
            $('#crz_comments').html(data.html);
            crz_comment.nPageId = mPageId;
                if (data.counts > crz_comment.nPageCount){
                    sStr = "";
                    for (nI = crz_comment.nPageCount; nI < data.counts; nI++){
                        sStr = sStr + '<span>' + (1 + nI) + '</span>';
                    }
                    $('#crz_comments_nav').append(sStr);
                    crz_comment.nPageCount = data.counts;
                }
            $('#crz_comments_nav > span').removeClass('sel').eq(crz_comment.nPageId).addClass('sel');
            $('body').css('cursor', '');
            
        }, 'json');
    },
    
    close : function(){
        $('#crz_comments_form').html('').hide();
    }
}

crz_auth = {
    back : "",

    go_auth : function(){
        $('body').css('cursor', 'wait');
        auth_data = {
            login : $('#crz_auth_form input[name="login"]').val(),
            passw : $('#crz_auth_form input[name="passw"]').val()
        };
        $.post('ajax/index/goauth/', auth_data, function(data){
            if (data.result == 'ok'){
                if (crz_auth.back != ''){
                    crz_auth.back();
                }
                $.modal.close();
            }
            else if (data.result == 'error'){
                $('#crz_auth_message').text(data.error).css('color', 'red');
            }
            $('body').css('cursor', '');
        }, 'json');
    },

    show_auth_dialog : function (){
        $('body').css('cursor', 'wait');
        if (!crz_auth.check_modal(crz_auth.show_auth_dialog)) return;
        $.get('ajax/index/auth/', function(data){
            $.modal(data, {
                onShow: function (){
                           $('body').css('cursor', '');
                       },
                overlayClose:true,
                opacity:0
            });
        });
    },

    show_reg_dialog : function(){
        if (!crz_auth.check_modal(crz_auth.show_reg_dialog)) return;
        $.modal.close();
        $.get('ajax/index/regform/', function(data){
            $.modal(data, {
                onShow: function (){
                    $('body').css('cursor', '');
                },
                overlayClose:true,
                opacity:0
            });
        });
    },

    go_reg : function(){
        $('body').css('cursor', 'wait');
        reg_data = {
            user_login : $('#crz_reg_form input[name="user_login"]').val(),
            user_passw : $('#crz_reg_form input[name="user_passw"]').val(),
            user_email : $('#crz_reg_form input[name="user_email"]').val(),
            user_cpt : $('#crz_reg_form input[name="user_cpt"]').val()
        };
        $.post('ajax/index/goreg/', reg_data, function(data){
            if (data.result == 'ok'){
                $.modal.close();
                if (crz_auth.back != ''){
                    crz_auth.back();
                }

            }
            else if (data.result == 'confirm'){
                crz_auth.show_confirm_dialog();
            }
            else if (data.result == 'error'){
                $('#crz_reg_message').text(data.error).css('color', 'red');
            }
            $('body').css('cursor', '');
        }, 'json');
    },

    show_confirm_dialog : function(){
        if (!crz_auth.check_modal(crz_auth.show_confirm_dialog)) return;
        $.modal.close();
        $.get('ajax/index/confirmform/', function(data){
            $.modal(data, {
                onShow: function (){
                    $('body').css('cursor', '');
                },
                overlayClose:true,
                opacity:0
            });
        });
    },

    go_confirm : function(){
        $('body').css('cursor', 'wait');
        $.post('ajax/index/goconfirm/', {user_code : $('#crz_confirm_form input[name="user_code"]').val()}, function(data){
            if (data.result == 'ok'){
                $.modal.close();
                if (crz_auth.back != ''){
                    crz_auth.back();
                }

            }
            else if (data.result == 'error'){
                $('#crz_confirm_message').text(data.error).css('color', 'red');
            }
            $('body').css('cursor', '');
        }, 'json');
    },

    go_logout : function(){
        $.post('ajax/index/gologout/');
    },

    check_modal : function(func){
        if (typeof($.modal) == 'undefined'){
            $.getScript('js/jquery.simplemodal.1.4.1.min.js', function(){
                if (typeof(func) == 'function'){
                    func();
                }
            });
            return false;
        }
        return true;
    }
}
