var send_to_friend_v=false,
send_pm=false,
init_font=10,
captcha_img='http://'+Uhost+'/captcha.php';
var butts1={
    "Ok":function(){
        $(this).dialog('close')
    }
},
butts2={
    "Cancel":function(){
        $(this).dialog('close')
    },
    "Sign Up":function(){
        signup({
            'email':$('.l_email').val(),
            'password':$('.l_password').val(),
            'password2':$('.l_password2').val(),
            'name':$('.l_name').val(),
            'about':$('.l_about').val(),
            'gender':$('.l_gender:checked').val(),
            'country':$('.l_country').val()
        });
    }
};
butts3={
    "Cancel":function(){
        $(this).dialog('close')
    },
    "Send":function(){
        send_to_friend({
            'my_email':$('.my_email').val(),
            'friend_email':$('.friend_email').val(),
            'captcha_img':$('.captcha_img').val()
        });
    }
},
butts4={
    "Ok":function(){
        window.location.href='http://'+Uhost+'/member_cp/browse/';
    }
};
function dialogg(obj,title,content,buttons) {
    $(obj).dialog({
        autoOpen:false,
        bgiframe:true,
        closeOnEscape:true,
        draggable:false,
        modal:true,
        width:300,
        stack:true
    });
    $(obj).dialog('option','title',title);
    $(obj).html(content);
    $(obj).dialog('option','buttons',buttons);
    $(obj).dialog('open');
}
var newwindow;
var ajaxthreads=0;
function ajaxOn() {
    $('#ajaxInd').fadeIn(500);
    ajaxthreads++;
}
function ajaxOff() {
    ajaxthreads--;
    if(ajaxthreads==0)
        $('#ajaxInd').fadeOut(500);
}
function popper(url)
{
    newwindow=window.open(url,'name','height=320,width=400,scrollbars=1');
    if (window.focus) {
        newwindow.focus()
    }
    return false;
}
function send_to_friend(obj) {
    if(send_to_friend_v===true) return;
    send_to_friend_v=true;
    ajaxOn();
    $.post(AJAXURL,{
        act:'send-to-friend',
        my_email:obj['my_email'],
        friend_email:obj['friend_email'],
        captcha_img:obj['captcha_img'],
        artid:artID
    },function(data){
        send_to_friend_v=false;
        ajaxOff();
        if(data==1) {
            $('#send-to-friend').dialog('close');
        }
        else {
            dialogg('.ui_dialog','Login error',data,butts1);
        }
    });
}
function getCaptcha(funcname){
    dialogg('.ui_dialog','Captcha verification','<div id="captcha"><span style="font-size:10px;display:block">Enter characters below for security reasons:</span><br/><img src="'+(captcha_img+'?'+Math.random())+'" border="0"/><br/><input class="lR rR" type="text" id="captcha-text"/></div>',{
        "Ok":function(){
            eval(funcname+'("'+$('#captcha-text').val()+'");');
            $(this).dialog('close');
        }
    });
    $('#captcha-text').focus();
};
function sendPM(captcha) {
    if(send_pm===true) return;
    send_pm=true;
    ajaxOn();
    $.post(AJAXURL,{
        act:'save-pm',
        subject:$('#pm-subject').val(),
        content:$('#pm-content').val(),
        captcha:captcha,
        auth_id:$('#pm-auth_id').val()
    },function(data){
        send_pm=false;
        ajaxOff();
        if(data['status']==1) {
            $('#pm-box').dialog('close');
            dialogg('.ui_dialog','PM delivered','Your private message has been sent!',butts1);
        }
        else {
            dialogg('.ui_dialog','Login error',data['status'],butts1);
        }
    },'json');
}
$(document).ready(function(){
    $('.tfchoose a').click(function(){
        ajaxOn();
        var obj=$(this);
        $.post(AJAXURL,{
            act:'getPopular',
            rel:$(this).attr('rel')
        },function(data){
            ajaxOff();
            if(data.status==1) {
                obj.parent().siblings('.ajx').html(data.data).hide().fadeIn(200);
            }
            else {
                dialogg('.ui_dialog','Error',data['message'],butts1);
            }
            },'json');
            return false;
    });
    $('a').live('mousedown',function(){
        $(this).fadeTo(100,0.8)
    }).live('mouseup',function(){
        $(this).fadeTo(100,1);
    });
    $('.hCategories').hover(function(){
        w=$(this).outerWidth()-4;
        obj=$(this).find('.wrapup').first();
        obj.show().css({
            left:w+'px'
        });
    },function(){
        obj=$(this).find('.wrapup').first();
        obj.hide();
        $(this).find('.wrapup ul ul').hide();
        $(this).find('span').removeClass('thelihover');
    });
    $('.wrapup>ul>li>span').toggle(function(){
        $(this).addClass('thelihover');
        $(this).next('ul').show();
    },function(){
        $(this).removeClass('thelihover');
        $(this).next('ul').hide();
    });
    $('#widget-latest li').click(function(){
        $(this).siblings().removeClass('wlis');
        $(this).addClass('wlis');
    });
    $('#widget-latest .w-right div:first').show();
    $('#widget-latest li').click(function(){
        $('#widget-latest .w-right div.hidden').hide();
        $('.'+$(this).attr('rel')).show();
    });
    $('.popper').click(function(){
        popper($(this).attr('href'));
        return false;
    });
    $('.send-to-friend').click(function(){
        $('#captcha_img').attr('src',captcha_img+'?'+Math.random());
        dialogg('#send-to-friend',artTitle,$('#send-to-friend').html(),butts3);
        return false;
    });
    $('.change-font').click(function(){
        if(init_font%20!=0) {
            init_font=init_font+2;
        }
        else {
            init_font=10;
        }
        $('.article-body').css('font-size',init_font+'px');
        return false;
    })
    $('#fav-heart').hover(function(){
        $(this).find('span').hide();
        $(this).find('img').show();
    },function(){
        $(this).find('span').show();
        $(this).find('img').hide();
    }).click(function(){
        $(this).fadeTo(60,0.6,function(){
            $(this).fadeTo(60,1)
        });
        ajaxOn();
        $.post(AJAXURL,{
            artid:artID,
            act:'favorite-article'
        },function(data){
            ajaxOff();
            $('#fav-heart span').html(data);
        });
    });
    if(window.isOnPage) {
        if(isOnPage=='read') {
            $('#comments').html(disqus_comments_box);
            $('#send-to-friend').after(disqus_script_core);
        }
    }
    $('.h-sorter').toggle(function(){
        $(this).next('ul').show();
    },function(){
        $(this).next('ul').hide();
    });
    $('.pm_author').click(function(){
        dialogg('#pm-box','Send PM to '+$(this).attr('name'),$('#pm-box').html(),{
            "Cancel":function(){
                $(this).dialog('close');
            },
            "Send":function(){
                getCaptcha('sendPM');
            }            
        });
        $('#pm-auth_id').val($(this).attr('rel'));
        return false;
    });
    $('.delete-message').click(function(){
        message_id=$(this).attr('rel');
        dialogg('.ui_dialog','Please confirm','Are you sure you want to delete this message?',{
            "Ok":function(){
                deletePM(message_id);
                $(this).dialog('close');
            },
            "Cancel":function(){
                $(this).dialog('close');
            }
        });
        return false;
    });
    $('.read-pm').click(function(){
        ajaxOn();
        message_id=$(this).attr('rel');
        $.post(AJAXURL,{
            act:'read-message',
            message_id:message_id
        },function(data){
            ajaxOff();
            if(data['status']==1) {
                dialogg('.ui_dialog', 'Reading message...', data['message'], butts1);
                $('#tr'+message_id+' .hasStatus').html('READ');
            }
            else {
                dialogg('.ui_dialog', 'Error deleting message.', data['status'], butts1);
            }
        },'json');
        return false;
    });
    $('.block-sender').live('click',function(){
        object=$(this);
        dialogg('.ui_dialog', 'Confirm action.', 'Are you sure you want to block the sender of the current message?', {
            "Ok":function(){
                $(this).dialog("close");
                ajaxOn();
                $.post(AJAXURL,{
                    act:'block-sender',
                    message_id:object.attr('rel')
                },function(data){
                    ajaxOff();
                    if(data['status']==1) {
                        object.removeClass('block-sender').addClass('unblock-sender').find('img').removeClass('i-lock').addClass('i-unlock').attr({
                            title:'Unblock sender.'
                        });
                    }
                    else {
                        dialogg('.ui_dialog', 'Error deleting message.', data['status'], butts1);
                    }
                },'json');
            },
            "Cancel":function(){
                $(this).dialog("close");
            }
        });
    });
    $('.unblock-sender').live('click',function(){
        object=$(this);
        dialogg('.ui_dialog', 'Confirm action.', 'Are you sure you want to unblock the sender of the current message?', {
            "Ok":function(){
                $(this).dialog("close");
                ajaxOn();
                $.post(AJAXURL,{
                    act:object.hasClass('isblocked')?'unblock-sender2':'unblock-sender',
                    message_id:object.attr('rel')
                },function(data){
                    ajaxOff();
                    if(data['status']==1) {
                        object.removeClass('unblock-sender').addClass('block-sender').find('img').removeClass('i-unlock').addClass('i-lock').attr({
                            title:'Block sender.'
                        });
                    }
                    else {
                        dialogg('.ui_dialog', 'Error deleting message.', data['status'], butts1);
                    }
                },'json');
            },
            "Cancel":function(){
                $(this).dialog("close");
            }
        });
    });
});
function deletePM(message_id) {
    ajaxOn();
    $.post(AJAXURL,{
        act:'delete-message',
        message_id:message_id
    },function(data){
        ajaxOff();
        if(data['status']==1) {
            $('#tr'+message_id).fadeOut(400,function(){
                $(this).remove();
                if($('table tr').length==1)
                    window.location.reload();
            });
        }
        else {
            dialogg('.ui_dialog', 'Error deleting message.', data['status'], butts1);
        }
    },'json')
}