var newMsg = null;
var userInfo = null;
var notice = null;

$(document).ready( function() {
    bindAjaxLoader();
    
    var options = {
        beforeSubmit : preLogin,
        success : postLogin,
        dataType : 'json'
    };

    $('#login-form').ajaxForm(options);

    newMsg = setInterval("newMsgNotify()", 4000);
    userInfo = setInterval("getUserInfo()", 300000);

    if ($('#login-nick').length > 0 && $('#login-pass').length > 0) {

        $('#login-nick').val($('#login-nick-txt').html());
        $('#login-pass').val($('#login-pass-txt').html());

        $('#login-nick').focus( function() {
            if ($(this).val() == $('#login-nick-txt').html()) {
                $(this).val('');
                $(this).css( {
                    'color' : '#000000'
                });
            }
        });

        $('#login-nick').blur( function() {
            if ($(this).val() == '') {
                $(this).val($('#login-nick-txt').html());
                $(this).css( {
                    'color' : '#999999'
                });
            }
        });

        $('#login-pass').focus( function() {
            if ($(this).val() == $('#login-pass-txt').html()) {
                $(this).val('');
                $(this).css( {
                    'color' : '#000000'
                });
            }
        });

        $('#login-pass').blur( function() {
            if ($(this).val() == '') {
                $(this).val($('#login-pass-txt').html());
                $(this).css( {
                    'color' : '#999999'
                });
            }
        });
    }

    $('#search-input').focus( function() {
        if ($(this).val() == $('#search-txt').html()) {
            $(this).val('');
        }
    });

    $('#search-input').blur( function() {
        if ($(this).val() == '') {
            $(this).val($('#search-txt').html());
        }
    });
});

function bindAjaxLoader() {
    $("#loading").bind("ajaxSend", function() {
        $(this).show();
    }).bind("ajaxComplete", function() {
        $(this).hide();
    });
}

function unbindAjaxLoader() {
    $("#loading").unbind("ajaxSend").unbind("ajaxComplete");
}

function preLogin(formData, jqForm, options) {
    $('#signin-err-email-cnt').hide();
    return true;
}

function newMsgNotify() {
    if ($("#new-user-msg").length > 0) {
        if ($("#new-user-msg").html() != '0') {
            $('#msg-link').fadeOut('slow').fadeIn('slow');
        }
    }
}

function getUserInfo() {
    if ($("#my-avatar-top").length > 0) {
        $("#loading").unbind("ajaxSend").unbind("ajaxComplete");

        var url = '/ro/users/axinfo';
        $.post(url, {
            x : 1
        }, function(data) {
            if (data.status == 'ok') {
                $('#new-user-msg').html(data.unread);
            }

            bindAjaxLoader();
        }, 'json');
    }
}

function postLogin(respObj, statusText) {
    try {
        if (respObj.status == "ok") {
            $('#pers-menu-ctr').html(respObj.html);

            if ($("#comm-frm").length > 0) {
                $('#cnt-login-form-comm').fadeOut('slow', function() {
                    $('#comm-frm').fadeIn('fast');
                });
            }
			
            if (respObj.rebind == '1') {
                try {
                    bindViewVideoButtons();
                } catch (e) {
                }
            }

            if ($("#reg-form").length > 0 || $("#pass-form").length > 0) {
                window.location = '/';
            }
        } else {
            $('#' + respObj.msg_id).html(respObj.message);
            $('#signin-err-email-cnt').fadeIn('fast');
        }
    } catch (e) {
    }
}

function showTopLogin() {
    $('#login-def').hide();
    $('#login-form-top').show();
    $('#login-forgot').show();
}

function delayedRedirect(url, sec) {
    setTimeout('redirectPage(\'' + url + '\')', sec * 1000);
}

function redirectPage(url) {
    window.location = url;
}

function storeClientSideData(name, val) {
    $.cookie(name, val, {
        expires: 365,
        path: '/'
    });
}

function getClientSideData(name) {
    return $.cookie(name);
}

function addToQuickList(id, from1) {
    unbindAjaxLoader();
    $('#ql-add-' + id).hide();
    var url = '/' + $('#lang-code').html() + '/quicklist/add';
    var nobtn_s = '0';
    $.post(url, {
        vid : id,
        from : from1
    }, function(data) {
        if (data.status == 'ok') {
        	if (data.from == 'v') {
        		$('.ql-link').toggle();
        		noticeDisplay(data.msg, 'ok', true);
        	} else {	
        		$('#ql-total').html(data.msg);
        		$('#ql-del-' + id).show();
        	}
        	
        	if ($('#new-pl-cnt-input').length > 0) {
        		$('#' + id).animate({opacity: 1}, "fast");
        	}
        }

        bindAjaxLoader();
    }, 'json');
}

function delFromQuickList(id, from1) {
    unbindAjaxLoader();
    $('#ql-del-' + id).hide();
    var url = '/' + $('#lang-code').html() + '/quicklist/del';
    $.post(url, {
        vid : id,
        from : from1
    }, function(data) {
        if (data.status == 'ok') {
            $('#ql-total').html(data.msg);
            $('#ql-add-' + id).show();
            if ($('#new-pl-cnt-input').length > 0) {
        		$('#' + id).animate({opacity: 0.3}, "fast");
        	}
        }

        bindAjaxLoader();
    }, 'json');
}

function noticeDisplay(message, type, autohide) {
	noticeHide();
	
    var image = 'info.png';
    if (!type) {
        type = 'info';
    }

    if (type == 'ok') {
        image = 'ok.png';
    } else if (type == 'error') {
        image = 'error.png';
    } else if (type == 'warn') {
        image = 'warn.png';
    }

    $('#notice-msg').html(message);
    $('#notice').html($('#notice').html().replace(/ok.png/, image));
    $('#notice').hide().fadeIn('slow');

    if (autohide) {
    	notice = setTimeout('noticeHide()', 4000);
    }
}

function noticeHide() {
	clearInterval(notice);
    $('#notice').fadeOut('slow');
}
