//
//title=eShops JS;version=0.0.0.4;date=2010-12-20

//
function rawurlencode (str) { 
    str = (str+'').toString();         
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A'); 
} 

function ee_shop_ajax_reload_bucket(){
    $.ajax({
        url: SD + 'ee_ajax_transport.php',
        data: 't=h&f=ee_eshop/ajax&o=reloadbucket&il='+IL,
        type: 'POST',
        dataType : 'html',
        async:false,
        global: false,
        success: function(msg){
            $('#eshopbacket').parent().html(msg);
        }
    });
}

function ee_shop_ajax_buy (reluri) {
    $.ajax({
        url: SD + 'ee_ajax_transport.php',
        data: 't=h&f=ee_eshop/ajax&o=buy&uri='+rawurlencode(reluri)+'&il='+IL,
        type: 'POST',
        dataType : 'html',
        async:false,
        global: false,
        success: function(msg){
            alert('ok!');
            ee_shop_ajax_reload_bucket();
        }
    });
}

function ee_delivery_ajax () {
    $.ajax({
        url: SD + 'ee_ajax_transport.php',
        data: 't=h&f=ee_delivery/ajax&id='+$('#deliverygeoid').val()+'&il='+IL,
        type: 'POST',
        dataType : 'html',
        async:false,
        global: false,
        success: function(msg){
            $('#deliveryajax').empty().append(msg);
        }
    });
}

function ee_refresh_buy_href_link(countid) {
    var eebch = $('#b'+countid.attr('id')).attr('href');
    $('#b'+countid.attr('id')).attr('href', eebch.slice(0,eebch.lastIndexOf("/")) + '/' + countid.val() + '.html');
}

$(document).ready(function(){
    $('.eebc').keyup(function(){
//        var eebch = $('#b'+$(this).attr('id')).attr('href');
//        $('#b'+$(this).attr('id')).attr('href', eebch.slice(0,eebch.lastIndexOf("/")) + '/' + $(this).val() + '.html');
//        alert($('#b'+$(this).attr('id')).attr('href'));
        ee_refresh_buy_href_link($(this));
    });

    $('.buybutton a').click(function () {
        ee_shop_ajax_buy ($(this).attr('href'));
        return false;
    });
    
    //counter add
    var items = $('div.shopitemlist ul').children('li');
    var buttons = '<a href="#" class="eshopcountup"></a><a href="#" class="eshopcountdown"></a>';
    $.each(items, function(index, element) {
        $(element).find('div.itemscount').append(buttons);
    });
    $('#shoptopblock .eeeshopcolumn .itemscount').append(buttons);
    
    $('div.itemscount').find('a').bind('click', function(){
        var input = $(this).parent().find('input:text'),
        count = parseInt(input.val(), 10);
        if ($(this).hasClass('eshopcountup')) {
            count +=1;
        } else if(count > 1) {
            count -=1;
        }
        input.val(count);
        ee_refresh_buy_href_link(input);
        return false;
    });
    
    //визначення доставки
    if ($('#deliverygeoid').length) {
        if ($('#deliverygeoid').val()) {
            ee_delivery_ajax ();
        }
        $('#deliverygeoid').change(function(){
            ee_delivery_ajax ();
        });
    }
});
