/**
 * Created by JetBrains PhpStorm.
 * User: mbakirov
 * Date: 24.05.11
 * Time: 15:29
 * To change this template use File | Settings | File Templates.
 */
function count( mixed_var, mode ) {	// Count elements in an array, or properties in an object
	var key, cnt = 0;
	if( mode == 'COUNT_RECURSIVE' ) mode = 1; if( mode != 1 ) mode = 0;
	for (key in mixed_var){
		cnt++;
		if( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
			cnt += count(mixed_var[key], 1);
		}
	}
	return cnt;
}

$(window).load(function(){
    /* hiding elemetns */
    $('.hideContent').each(function(){
        if($(this).children('.message').html() != null && ($(this).children('.message').html().length > 1)) {
            $(this).prev('.hideButton').switchClass('closed', 'opened', 0);
        } else {
            $(this).hide();
        }
    });

    $('.hideButton').click(function(){
        $hideContent = $(this).next('.hideContent');
        if( $hideContent.is(':visible') ) {
            $hideContent.hide('blind', {}, 'fast');
            $(this).switchClass('opened', 'closed', 'fast');
        } else {
            $hideContent.show('blind', {}, 'fast');
            $(this).switchClass('closed', 'opened', 'fast');
        }
    });
    /* hiding elements end */

    /* authorisation */
    $('.authorisation').dialog({
        autoOpen: false,
        width: 262,
        modal: true,
        closeOnEscape: true,
        closeText: ' ',
        draggable: true,
        dialogClass: 'auth',
        buttons: [
            {
                text: 'Отмена',
                click: function(){$(this).dialog("close");},
                'class': 'back-button'
            },
            {
                text: 'Войти',
                click: function(){
                    $current_person_type = $('a.personTypeSelect.checked').attr('name');
                    $.ajax({
                        url: '/cart/login.php',
                        global: false,
                        type: "GET",
                        data: {
                            USER_LOGIN: $('#login').val(),
                            USER_PASSWORD: $('#password').val(),
                            AJAX_CALL: 'Y',
                            JSON: 'Y',
                            CHECK_BASKET: 'Y',
                            action: 'auth'
                        },
                        dataType: "json",
                        error: function(){
                            alert('ajax error');
                        },
                        success: function(data){
                            $arResult = data.responseData.auth.arResult;
                            if($arResult.ERROR_MESSAGE) {
                                $('.authorisation .error').html($arResult.ERROR_MESSAGE.MESSAGE);
                            }
                            else if($arResult.USER_ID) {
                                $old_order_sum = $('input[name="ORDER_PRICE"]').val();
                                if($arResult.ORDER_PRICE != $old_order_sum)
                                    alert('В корзине пользователя '+$arResult.USER_LOGIN+' уже имеются товары\nОбратите внимание, сумма и состав заказа изменились!');
                                window.location.reload();
                            }
                        }
                    });
                },
                'class': "login"
            }
        ]
    });
});


