$().ready(function() {

    $.jGrowl.defaults.position = 'center';

    $('#reg_popup').jqm('#registration');
    $('#stock_popup').jqm('a.stock_popup');
    $(function(){
        $(".find_form form").jqTransform(({imgPath:'img/'}));
        $(".profile1-left").jqTransform(({imgPath:'img/'}));
    });

    $(".menu_sub-item a:last").addClass("last");

});

function show_regform(){
    $('#reg_popup').jqmShow();
}
	
function stock_popup(cur_user, id_security){
    $('#id_security').val(id_security);
    $('#cur_user').val(cur_user);
    //$('#stock_confirm').hide();
    var data = "&cur_user="+ cur_user +"&id_security=" + id_security;
    $.ajax({
        type: "POST",
        url: "ajax_stock.php",
        //скрипт получает значения для формы в формате json
        dataType: "json",
        data: data,
        error: function(){
            alert('Error!');
        },
        success: function(data){
            if(data){
                var error = data.error;
                if (error) {
                    $.jGrowl(data.error);
                } else {
                    // полученные данные заносим по ид в попап-форму
                    $.each(data, function(label, val) {
                        $('#' + label).html(val);
                    });
                    $('#allocation').html(data.allocation);
                    $('#stock_confirm').hide();
                    // показываем скрытую форму
                    $('#stock_popup').jqmShow();
                }
            } else window.location = "/user_login.php";
        }
    });
}
	
function invest_popup(cur_user, id_expert){
    $('#id_expert').val(id_expert);
    $('#cur_user').val(cur_user);
    //$('#stock_confirm').hide();
    var data = "&cur_user="+ cur_user +"&id_expert=" + id_expert;
    $.ajax({
        type: "POST",
        url: "ajax_invest.php",
        //скрипт получает значения для формы в формате json
        dataType: "json",
        data: data,
        error: function(){
            $('#stock_popup').jqmShow('Error');
        },
        success: function(data){
            if(data){
                var error = data.error;
                if (error) {
                    $.jGrowl(data.error);
                } else {
                    // полученные данные заносим по ид в попап-форму
                    $.each(data, function(label, val) {
                        $('#' + label).html(val);
                    });
                    $('#change_weight').html(data.weight);
                    $('#stock_confirm').hide();
                    // показываем скрытую форму
                    $('#stock_popup').jqmShow();
                }
            } else window.location = "/user_login.php";
        }
    });
}	
function change_weight(delta){
    $('#stock_confirm').show();
		
    var new_allocation = $('#new_allocation').html();
    var current_allocation = $('#current_allocation').html();
    var free_cash = $('#free_cash_percent').html();
    var max = 1*current_allocation + 1*free_cash;
    var result = 1*new_allocation + 1*delta;
    if (result > max) result = max;

    var new_val = result.toFixed(2);
    if (0 > new_val) new_val = 0;
		
    $('#new_allocation').html(new_val);
    //$('#input_change_weight').val(new_val);
    return false;
}
	
function expert_transaction_submit(){
    $('#stock_confirm').hide();
    var cur_user = $('#cur_user').val();
    var id_security = $('#id_security').val();
    var new_allocation = $('#new_allocation').html();
    var data = "&cur_user=" + cur_user + "&submit=1&id_security=" + id_security + "&new_allocation=" + new_allocation;
    $.ajax({
        type: "POST",
        url: "ajax_stock.php",
        //скрипт получает значения для формы в формате json
        dataType: "json",
        data: data,
        error: function(){
            alert('Error!');
        },
        success: function(data){
            //				$('#free_cash_percent').html(data.free_cash_percent);
            //				$('#weight').html(data.weight);
            $('#stock_popup').jqmHide();
            var message = data.message;
            if (message) {
                $.jGrowl(data.message);
            }
            //$.jGrowl("Done! You have " + data.free_cash_percent +"% free cash left");
        }
    });
}
	
function investor_transaction_submit(){
    $('#stock_confirm').hide();
    var cur_user = $('#cur_user').val();
    var id_expert = $('#id_expert').val();
    var new_allocation = $('#new_allocation').html();
    var data = "&cur_user=" + cur_user + "&submit=1&id_expert=" + id_expert + "&new_allocation=" + new_allocation
    $.ajax({
        type: "POST",
        url: "ajax_invest.php",
        //скрипт получает значения для формы в формате json
        dataType: "json",
        data: data,
        error: function(){
            alert('Error!');
        },
        success: function(data){
            $('#stock_popup').jqmHide();
            var message = data.message;
            if (message) {
                $.jGrowl(data.message);
            }
            //$.jGrowl("Done! You have " + data.free_cash_percent +"% free cash left");
        }
    });
}
	
function follow(cur_user, id_user){
    $.ajax({
        type: "POST",
        url: "ajax_follow.php",
        dataType: "json",
        data: "cur_user=" + cur_user + "&id_user=" + id_user,
        error: function(){
            alert('Error!');
        },
        success: function(data){
            if(data){
                var result = data.result;
                var td = data.td;
                var error = data.error;
                var message = data.message;
                if (message){
                    $.jGrowl(data.message);
                }
                if(result == 1 && td != '') {
                    // succesfuly added user to followers list
                    $("#td_" + id_user).html(td);
                }
            } else window.location = "/user_login.php";
        }
    });
}
	
function delete_follower(cur_user, id_user){
    $.ajax({
        type: "POST",
        url: "ajax_delete_follow.php",
        dataType: "json",
        data: "cur_user=" + cur_user + "&id_user=" + id_user,
        error: function(){
            alert('Error!');
        },
        success: function(data){
            if(data){
                var result = data.result;
                var error = data.error;
                var message = data.message;
                if (error) {
                    $.jGrowl(data.error);
                }
                if (message){
                    $.jGrowl(data.message);
                }
                if(result == 1) {
                    // succesfuly deleted user from follower list
                    $("#tr_follow_" + id_user).fadeOut('slow', function(){
                        $("#tr_follow_" + id_user).remove();
                    });
                }
            }
        }
    });
}
