$(document).ready(function(){
    
    /**
     * init.
     */
    $('#partners_fader').cycle({
        fx: 'fade'
    });
    $('#search_string').bind('click', function(){
        $(this).attr({
            value: ''
        });
    });
    $('div.box.item6 input').bind('click', function(){
        $(this).attr({
            value: ''
        });
    });
    //$('#product-gallery a').lightBox();
    $('a#neweshopclick').bind('click', function(){

        $('div#neweshopform').toggle();
    });
    $('a#show_eshop_form').bind('click', function(){
        $('div#neweshopform').show();
    })

});


$('a.favhandler').live('click', function(){

    var $this = this;
    $pid = parseInt($($this).attr('name'));
    if($($this).hasClass('fav_append')){
        $mode = 1;
    }else if($($this).hasClass('fav_remove')){
        $mode = 0;
    }
    favourite($pid, $mode);
});

$('a.importinput').live('click', function(){
    var $this = this;
    $pid = parseInt($($this).attr('name'));
    if($($this).hasClass('addprod')){
        $mode = 1;
    }else if($($this).hasClass('delprod')){
        $mode = 0;
    }
    importinput($pid, $mode);
    
});

function importinput(pid, mode){
    if(typeof mode != 'number' || typeof pid != 'number'){
        alert('error');
        return;
    }

    $.ajax({
        url: '/prods/products/importinput/',
        type: "POST",
        data: {
            id: parseInt(pid),
            mode: parseInt(mode)
        },
        success: function($data) {
            if($data){
                if($mode){ $('#add_'+$pid).fadeOut(100, function(){$('#del_'+$pid).fadeIn(300);}); }
                else { $('#del_'+$pid).fadeOut(100, function(){$('#add_'+$pid).fadeIn(300);}); }
            }
        },
        beforeSend: function(){

        }
    });

}

function favourite(pid, mode){


    if(typeof mode != 'number' || typeof pid != 'number'){
        alert('error');
        return;
    }

    $.ajax({
        url: '/products/addtofavourite/',
        type: "POST",
        data: {
            id: parseInt(pid),
            mode: parseInt(mode)
        },
        success: function($data) {
            if($data == '1'){
                if(0 == mode){
                    $remove = 'fav_remove';
                    $append = 'fav_append';
                }else{
                    $append = 'fav_remove';
                    $remove = 'fav_append';
                }
                $('a.favhandler').each(function(){
                    if(parseInt($pid) == parseInt($(this).attr('name'))){
                        if($(this).hasClass('favtext')){
                            $(this).html((0 == mode)?'Dodaj do obserwowanych':'Usuń z obserwowanych');
                        }
                        $(this).removeClass($remove);
                        $(this).addClass($append);
                    }
                });
            }
        },
        beforeSend: function(){

        }
    });


}


$('a.eshop_brandhandler').live('click', function(){
    
   
    var data = $(this).attr('name');
    var d = data.split('_');
    var eid = d[0];
    var bid = d[1];

    if($(this).hasClass('brand_append')){
        var mod = 1;
    }else if($(this).hasClass('brand_remove')){
        var mod = 0;
    }    
    
    brand(eid, bid, mod);    

});

function brand(eid, bid, mod){

    $.ajax({
        url: '/eshops/brands/remarkbrand/',
        type: "POST",
        data: {
            eid: parseInt(eid),
            bid: parseInt(bid),
            mod: parseInt(mod)
        },
        success: function($data) {
            if($data == '1' || parseInt($data) > 1){
                if(0 == mod){
                    $remove = 'brand_remove';
                    $append = 'brand_append';
                }else{
                    $append = 'brand_remove';
                    $remove = 'brand_append';
                }
                $('a.eshop_brandhandler').each(function(){
                    var nm = $(this).attr('name');
                    nm = nm.split('_');
                    var eid1 = nm[0];
                    var bid1 = nm[1];
                    if(parseInt(eid) == parseInt(eid1)){
                        if(parseInt(bid) == parseInt(bid1)){
                            $(this).removeClass($remove);
                            $(this).addClass($append);
                        }
                    }
                });
            }
        },
        beforeSend: function(){

        }
    });


}



