function open_url(url) {
	if (url != "") {
		window.location.href = url;		
		return true;
	}
	return false;
}

function addItem(id, title, price) {
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    if (item) {
        var item_parts = item.split('_');
        $.cookies.set('cart[' + id + ']', (parseFloat(item_parts[0]) + 1) + '_' + item_parts[1], {path : '/'});
        
        $('.cart-content .item_' + id).find('.count i').html(parseFloat(item_parts[0]) + 1);
        
        var price_parts = $('.cart-content').find('.price span').html().split(' ');
        var price_value = parseFloat(price_parts[0]) + parseFloat(item_parts[1]);
        $('.cart-content').find('.price span').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((parseFloat(price_value) / 100) * DISCOUNT);
            $('.cart-content').find('.discount span').html(discount_value + ' ' + price_parts[1]);
            $('.cart-content').find('.result span').html((parseFloat(price_value) - parseFloat(discount_value)) + ' ' + price_parts[1]);
        }
        
    } else {
        var new_item = 
            '<li class="item_' + id + '">' +
                '<div class="item"><div class="name">' + title + '</div><div class="count"><i>1</i> &times; ' + price + '</div></div>' +
                '<div class="options">' +
                    '<div class="plus" style="height: 10px; position: relative; margin-top: 10px" onClick="plusItem(\'' + id + '\',' + price + ')"></div>' + 
                    '<div class="minus" style="height: 10px; position: relative; margin-top: 10px" onClick="minusItem(\'' + id + '\',' + price + ')"></div>' + 
                    '<div class="delete" style="height: 10px; position: relative; margin-top: 10px" onClick="deleteItem(\'' + id + '\',' + price + ')"></div>' +
                '</div><div style="clear: both;"></div>' +
            '</li>';
        
        price = parseFloat(price);
        if ($('.cart-content').find('li').size()) {
            $('.cart-content li:first').before(new_item);            
            var price_parts = $('.cart-content').find('.price span').html().split(' ');
            var price_new = parseFloat(price_parts[0]) + price;
            $('.cart-content').find('.price span').html(price_new + ' ' + price_parts[1]);

            if (DISCOUNT) {
                var discount_value = Math.ceil((price_new / 100) * DISCOUNT);
                $('.cart-content').find('.discount span').html(discount_value + ' ' + price_parts[1]);
                $('.cart-content').find('.result span').html((price_new - discount_value) + ' ' + price_parts[1]);
            }
            
            var block_pos = $("#cart").offset().top + $("#cart").outerHeight();
            var margin = parseInt($("#content").css('margin-bottom'));
            if (block_pos >= $("#footer-inner").offset().top)
                $("#content").css('margin-bottom', block_pos - $("#footer-inner").offset().top + 20 + margin);
            
        } else {
            var cart_content = '<ul>' + new_item + '</ul><div class="price">' + PRICE_TITLE + ': <span>' + price + ' ' + CURRENCY + '</span></div>';
            if (DISCOUNT) {
                var discount_value = Math.ceil((price / 100) * DISCOUNT);
                cart_content += '<div class="discount">' + DISCOUNT_TITLE + ': <span>' + discount_value + ' ' + CURRENCY + '</span></div>';
                cart_content += '<div class="result">' + TOTAL_TITLE + ': <span>' + (price - discount_value) + ' ' + CURRENCY + '</span></div>';
            }
            cart_content += '<div class="order" onclick="open_url(\'/' + LANG + '/order\')"></div>';
            $('.cart-empty').hide().before(cart_content);
            $('.cart-content .item_' + id).addClass('last');
        }
        $.cookies.set('cart[' + id + ']', 1 + '_' + price, {path : '/'});
    }
    
    $('.cart-content .item_' + id + ' .item').animate({backgroundColor: "#232323"}, 750, function() {
        $('.cart-content .item_' + id + ' .item').animate({backgroundColor: "black"}, 750, function() {
            $('.cart-content .item_' + id + ' .item').css('background-color', 'transparent');
        });
    });
}

function plusItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        var item_parts = item.split('_');
        $.cookies.set('cart[' + id + ']', (parseFloat(item_parts[0]) + 1) + '_' + price, {path : '/'});
        
        var item_position = $('.cart-content .item_' + id);
        item_position.find('.count i').html(parseFloat(item_parts[0]) + 1);
        
        var price_parts = $('.cart-content').find('.price span').html().split(' ');
        var price_value = parseFloat(price_parts[0]) + price;
        $('.cart-content').find('.price span').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
            $('.cart-content').find('.discount span').html(discount_value + ' ' + price_parts[1]);
            $('.cart-content').find('.result span').html((price_value - discount_value) + ' ' + price_parts[1]);
        }
        
    }
}

function minusItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        var item_parts = item.split('_');
        if (item_parts[0] < 2)
            return;
        
        $.cookies.set('cart[' + id + ']', (parseFloat(item_parts[0]) - 1) + '_' + price, {path : '/'});
        
        var item_position = $('.cart-content .item_' + id);
        item_position.find('.count i').html(parseFloat(item_parts[0]) - 1);
        
        var price_parts = $('.cart-content').find('.price span').html().split(' ');
        var price_value = parseFloat(price_parts[0]) - price;
        $('.cart-content').find('.price span').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
            $('.cart-content').find('.discount span').html(discount_value + ' ' + price_parts[1]);
            $('.cart-content').find('.result span').html((price_value - discount_value) + ' ' + price_parts[1]);
        }
        
    }
}

function deleteItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        jaaulde.utils.cookies.del('cart[' + id + ']', {path: '/'});
        var count = $('.cart-content .item_' + id).find('.count i').html();
        $('.cart-content .item_' + id).remove();
        if ($('.cart-content').find('li').size()) {
            var price_parts = $('.cart-content').find('.price span').html().split(' ');
            var price_value = parseFloat(price_parts[0]) - (price * parseFloat(count));
            $('.cart-content').find('.price span').html(price_value + ' ' + price_parts[1]);

            if (DISCOUNT) {
                var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
                $('.cart-content').find('.discount span').html(discount_value + ' ' + price_parts[1]);
                $('.cart-content').find('.result span').html((price_value - discount_value) + ' ' + price_parts[1]);
            }
        } else {
            $('.cart-content ul').remove();
            $('.cart-content .price').remove();
            if (DISCOUNT) {
                $('.cart-content .discount').remove();
                $('.cart-content .result').remove();
            }
            $('.cart-content .order').remove();
            $('.cart-empty').css('display', 'block');
        }
        
        var block_pos = $("#cart").offset().top + $("#cart").outerHeight();
        var margin = parseInt($("#content").css('margin-bottom'));
        if (margin > 0 && block_pos < $("#footer-inner").offset().top) {
            var new_margin = margin - ($("#footer-inner").offset().top - block_pos) + 20;
            $("#content").css('margin-bottom', (new_margin > 0) ? new_margin : 0);
        }        
    }
}

// ORDER funtions

function orderPlusItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        var item_parts = item.split('_');
        $.cookies.set('cart[' + id + ']', (parseFloat(item_parts[0]) + 1) + '_' + price, {path : '/'});
        
        var item_position = $('.order-items .item_' + id);
        item_position.find('input').val(parseFloat(item_parts[0]) + 1);
        
        var price_parts = $('.order-items').find('.price_count').html().split(' ');
        var price_value = parseFloat(price_parts[0]) + price;
        $('.order-items').find('.price_count').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
            $('.order-items').find('.discount_count').html(discount_value + ' ' + price_parts[1]);
            $('.order-items').find('.result_count').html((price_value - discount_value) + ' ' + price_parts[1]);
        }
        
    }
}

function orderMinusItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        var item_parts = item.split('_');
        if (item_parts[0] < 2)
            return;
        
        $.cookies.set('cart[' + id + ']', (parseFloat(item_parts[0]) - 1) + '_' + price, {path : '/'});
        
        var item_position = $('.order-items .item_' + id);
        item_position.find('input').val(parseFloat(item_parts[0]) - 1);
        
        var price_parts = $('.order-items').find('.price_count').html().split(' ');
        var price_value = parseFloat(price_parts[0]) - price;
        $('.order-items').find('.price_count').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
            $('.order-items').find('.discount_count').html(discount_value + ' ' + price_parts[1]);
            $('.order-items').find('.result_count').html((price_value - discount_value) + ' ' + price_parts[1]);
        }
        
    }
}

function orderDeleteItem(id, price) {
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        jaaulde.utils.cookies.del('cart[' + id + ']', {path: '/'});
        var count = $('.order-items .item_' + id).find('input').val();
        $('.order-items .item_' + id).remove();
        if ($('.order-items').find('tr').size()) {
            var price_parts = $('.order-items').find('.price_count').html().split(' ');
            var price_value = parseFloat(price_parts[0]) - (price * parseFloat(count));
            $('.order-items').find('.price_count').html(price_value + ' ' + price_parts[1]);

            if (DISCOUNT) {
                var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
                $('.order-items').find('.discount_count').html(discount_value + ' ' + price_parts[1]);
                $('.order-items').find('.result_count').html((price_value - discount_value) + ' ' + price_parts[1]);
            }
        } else {
            $('.order-items .price').remove();
            $('.order-items .price_count').remove();
            $('.order-items .line').remove();
            $('.order-items .clear').remove();
            $('.order-items table').remove();
            if (DISCOUNT) {
                $('.order-items .discount').remove();
                $('.order-items .discount_count').remove();
                $('.order-items .result').remove();
                $('.order-items .result_count').remove();
                $('.order-items .result_line').remove();
            }
            $('.order-items .thickbox').remove();
            $('.cart-empty').css('display', 'block');
        }
    }
}

function orderChangeCountItems(row, id, price)
{
    var count = $(row).val();
    if (count < 1) {
        count = 1
        $(row).val(count);
    }
    
    price = parseFloat(price);
    var cart = $.cookies.filter('cart', {path : '/'});
    var item = cart['cart['+id+']'];
    
    if (item) {
        var item_parts = item.split('_');
        $.cookies.set('cart[' + id + ']', count + '_' + price, {path : '/'});
        
        var price_parts = $('.order-items').find('.price_count').html().split(' ');
        var price_value = parseFloat(price_parts[0]) - (parseFloat(item_parts[0]) * price) + (count * price);
        $('.order-items').find('.price_count').html(price_value + ' ' + price_parts[1]);
        
        if (DISCOUNT) {
            var discount_value = Math.ceil((price_value / 100) * DISCOUNT);
            $('.order-items').find('.discount_count').html(discount_value + ' ' + price_parts[1]);
            $('.order-items').find('.result_count').html((price_value - discount_value) + ' ' + price_parts[1]);
        }
        
    }
}
