/* main js */

var timeout = null;
var flag = true;

$(document).ready(function() {
    if ($('#order_delivery')) {
        showDeliveryProperties();
    }
});

function showDeliveryProperties() {
    $('.delivery_properties').hide();
    if ($('#select_delivery :selected').attr('value') == 'courier') {
        $('.delivery_properties_courier').fadeIn('slow');
    } else {
        var selectedDelivery = $('#select_delivery :selected').attr('value');
	if (selectedDelivery == 'ukrpost' || selectedDelivery == 'new_post') {
            $("#select_payment option[value='afterwards']").attr('selected', 'selected');
            $('.delivery_properties_ukrpost').fadeIn('slow');
        }
    }
}
function delItem(itemId) {
    rememberState('del_' + itemId);
    hideCartRow(itemId);
}
function returnItem(itemId) {
    removeState('del_' + itemId);
    showCartRow(itemId);
}
function showCartRow(itemId) {
    showCart();
    $('#r-' + itemId).fadeIn('slow');
    $('.order_cart_amount').html(
            $('.order_cart_amount').html() * 1 + $('#r-' + itemId + ' .item_cart_amount').html() * 1);
    $('#r-' + itemId + ' input').attr('value', $('#r-' + itemId + ' .item_cart_count').html());
    $('.order_cart_count').html($('.order_cart_count').html() * 1 + $('#r-' + itemId + ' .item_cart_count').html() * 1);
    $('#cart_notification').slideUp('slow');
}
function hideCartRow(itemId) {
    $('.order_cart_amount').html(
            $('.order_cart_amount').html() * 1 - $('#r-' + itemId + ' .item_cart_amount').html() * 1);
    $('.order_cart_count').html($('.order_cart_count').html() * 1 - $('#r-' + itemId + ' .item_cart_count').html() * 1);
    hideCart();
    $('#r-' + itemId).fadeOut('slow');
    $('#cart_notification_link').attr('href', '/cart');
    $('#cart_notification_link').unbind('click').click(function() {
        returnItem(itemId);
        return false;
    });
    $('#cart_notification').slideDown();
}
function showCart() {
    $('#cart_table_empty').slideUp('slow');
    $('#cart_form').slideDown('slow');
    $('#order_form').slideDown('slow');
}
function hideCart() {
    if (!($('.order_cart_count').html() * 1)) {
        $('#cart_form').slideUp('slow');
        $('#order_form').slideUp('slow');
        $('#cart_table_empty').fadeIn('slow');
    }
}
function removeState(stateName) {
    rememberState(stateName, true);
}
function rememberState(stateName, remove) {
    var states;
    if (tmp = $.cookie("states"))
        states = tmp.split('|');
    else
        states = [];
    if ((undefined != remove)) {
        if ((idx = $.inArray(stateName, states)) != -1)
            states.splice(idx, 1);
        else
            return false;
    } else {
        if ($.inArray(stateName, states) == -1)
            states.push(stateName);
    }
    $.cookie("states", states.join("|"), {
        expires : 365,
        path : '/'
    });
    return true;
}
function getState(stateName) {
    if (tmp = $.cookie("states")) {
        var states = tmp.split('|');
        if ($.inArray(stateName, states) != -1)
            return true;
        else
            return false;
    } else
        return false;
}
function switchEnterForm() {
    if ($('#enter').is(":visible")) {
        $('#enter').hide();
        $('#enter_open').show();
    } else {
        $('#enter_open').hide();
        $('#enter').show();
    }
}
$(document).ready(function() {
    var temp;
    $('#enter_email_open').focus(function() {
        if (!temp) {
            temp = $('#enter_email_open').val();
        }
        if (temp == $('#enter_email_open').val()) {
            $('#enter_email_open').val('');
        }
    });
    $('#enter_email_open').blur(function() {
        if (!$('#enter_email_open').val()) {
            $('#enter_email_open').val(temp);
        }
    });
    $('#enter_password_replace').focus(function() {
        $('#enter_password_replace').remove();
        $('#enter_password_open').show();
        $('#enter_password_open').focus();
    });
    // ---- ����� -----
        var options = {
            // �������, ������� ����� �������� �� ������ �������
            // target: "#output",
            beforeSubmit : showRequest, // �������, ���������� ����� ���������
            success : showResponse, // �������, ���������� ��� ��������� ������
            timeout : 3000
        // ����-���
        };

        // ����������� ������� submit � �����
        $('#reg').submit(function() {
            $(this).ajaxSubmit(options);
            // !!! ����� !!!
                // ������ ���������� false, ����� ������������ �����������
                // �������� �������� (������� �� �������� form.php)
                return true;
            });
        // ---- ����� -----
    });

// ����� ����� ��������� ������
function showRequest(formData, jqForm, options) {
    // formData - ������; ����� ������������ $.param ����� ������������� ��� �
    // ������ ��� ������ � alert(),
    // (������ � ���������������� �����), �� � ����� ������� jQuery Form ���
    // ����������� �������������.
    var queryString = $.param(formData);
    // jqForm ��� jQuery ������, ���������� �������� �����.
    // ��� ������� � ��������� ����� �����������
    // var formElement = jqForm[0];
    alert('��� ��� �� ��������: \n\n' + queryString);
    // ����� ����� ������� false ����� ��������� �������� �����;
    // ����� �������� �� fals �������� �������� �������� �����.
    return true;
}

// ����� ����� ��������� ������
function showResponse(responseText, statusText) {
    // ��� �������� html ������, ������ �������� - �������� responseText
    // ������� XMLHttpRequest

    // ���� ����������� ����� ajaxSubmit (��� ajaxForm) � �������������� �����
    // dataType
    // ������������� � 'xml', ������ �������� - �������� responseXML
    // ������� XMLHttpRequest

    // ���� ����������� ����� ajaxSubmit (��� ajaxForm) � �������������� �����
    // dataType
    // ������������� � 'json', ������ �������� - ����� json, ������������
    // ��������.

    alert('������ ������ �������: ' + statusText + '\n\n����� ������ �������: \n' + responseText
            + '\n\n������� ������� div ���������� ���� �������.');
}

function submitThisForm() {

}
