/* PRODUCT FUNCTIONS  ------------------------------------------------------- */
var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
function switchRegion(rid, img)
{
    $('table.pregion').hide();
    $('li.ril').removeClass('here');
    $('#ri-'+rid).show();
    $('#ril-'+rid).addClass('here');
    $('.bigpic-img').hide();
    if ($('span.bpr-'+rid).length > 0) {
        $('span.bpr-'+rid).css('display', 'inline-block');
    } else {
        $('img.bpr-'+rid).show();
    }
}
function addToCart(pid)
{
    cart_add(pid, $('#ca-size').val(), $('#ca-qty').val());
    return false;
}

function productSave()
{
    if (!$('#sa-email').val()) {
        showMessage('Введите Ваш e-mail!');
        return false;
    }
    $.getJSON(
        '/my/save/', 
        $('#sa').serialize(),
        function(data) {
            if (data['code']) {
                $('#sa').fadeOut();
            } else {
                showMessage(data['msgs']);
            }
        }
    );
}


/* CART MANAGEMENT  --------------------------------------------------------- */
function cart_repaint(data)
{
    var num = 0;
    var sum = 0;
    for(var k in data)  {
        num += 1*data[k]['qty'];
        sum += 1*data[k]['qty']*data[k]['price'];
    }
    
    // if cart is empty - hide-show controls
    if (!num) {
        $('.korzina,.bf').hide();
        $('#cart-empty-msg').show();
    }
    return;
}

function cart_add(pid, qty)
{
    if (!pid) {
        showMessage(['Ошибка! Не указан товар.'], 'error');
        return false;
    }
    
    if (!qty) {
        showMessage(['Ошибка! Не указано количество.'], 'error');
        return false;
    }
    
    $.getJSON('/cart/add/', {'pid': pid, 'qty': qty}, 
		function(data) {
			if (data['error']) {
				return alert(data['error']);
			}
			else {
    			location.href = '/cart/';
			}
		}
    );
}

function cart_update(pid, qty)
{
    if (!pid) {
        showMessage(['Ошибка! Не указан товар.'], 'error');
        return false;
    }
    
    if (!qty) {
        showMessage(['Ошибка! Не указано количество.'], 'error');
        return false;
    }
    $.getJSON(
        '/cart/update/', 
        {'pid': pid, 'qty': qty},
        //cart_repaint
        function() {
        	location.href = '/cart/';
        }
    );
}

function cart_remove(pid, size)
{
    if (!pid) {
        showMessage(['Ошибка! Не указан товар.'], 'error');
        return false;
    }
    
    $.getJSON(
        '/cart/remove/', 
        {'pid': pid},
        cart_repaint
    );
    
    $('#item-'+pid).remove();
}


/* ORDER MANAGEMENT  -------------------------------------------------------- */
function order_update_total()
{
    var total = subtotal;
    
    // repaint delivery
    var el = $('input[name=id_deliveries]');
    var delivery = undefined;
    for(var i=0; i < el.length; i++) {
        var row = el.get(i);
        if (row.checked) {
            delivery = deliveries[row.value];
        }
    }
    if (delivery) {
        var delivery_price = delivery['price'];
        total += delivery_price;
        total = Math.round(100*total) / 100;
    }
    if (coupon != $('#coupon').val()) {
        coupon = $('#coupon').val();
        coupon_value = 0;
        if (coupon != '') {
            $.getJSON(
                '/order/coupon/?coupon='+coupon, 
                function(data) {
                    if (data['code']) {
                        data['value'] = data['value']*1.00;
                        msg = '';
                        
                        if(data['type'] == 'deliveries') {
                        	msg += 'Скидка на доставку: ' + delivery_price + ' y.e' ;
                        	coupon_value = delivery_price;
                        } else if (data['value']) {
                            msg += 'Скидка на '+data['value']+' '+(data['type'] == 'fixed' ? 'у.е.' : '%');
                            if (data['type'] == 'fixed') {
                                coupon_value = data['value'];
                            } else {
                                coupon_value = subtotal*data['value']/100;
                            }
                            coupon_value = Math.round(100*coupon_value)/100;
                            if (data['notes']) {
                                msg += '<br/>';
                            }
                        }
                        if (data['notes']) {
                            msg += '<strong>'+data['notes']+'</strong>';
                        }
                        $('#coupon_info').html(msg);
                    } else {
                        $('#coupon_info').html(data['msgs'][0]);
                    }
                }
            );
        } else {
            $('#coupon_info').html('');
        }
    }
    total -= coupon_value;
    total = Math.round(100*total)/100;
    if (!(total > 0)) {
        total = '0.00';
    }
    
    $('#total').html(total);
}




