$(document).ready(function(){

    // Search initialisation
    var _reset_search = function(){
        $('#search_f').val('');
    }
      
    $('#search_f').click(function(){
        switch($('#search_f').val()){
            case 'tražite nešto?':
                _reset_search();
                break;
        }
    });

    $('#search_f').blur(function(){
        switch($('#search_f').val()){
            case '':
                $(this).val('tražite nešto?');
                break;
        }
    });

    //Master slide on each page
    
    var counter = $('#inner-box').children('.product-box').length;
    var box_width = $('#inner-box').children('.product-box').width();
    var margin_size = $('#inner-box').children('.product-box').css('margin-right');
    var whole_move = parseInt(box_width)+parseInt(margin_size);
    var allowed_clicks = counter - 3;
    var clicked=0;
    //Making moves
    
    
    setInterval(function(){
        if(allowed_clicks == clicked){
            $('#inner-box').animate({
                left:0
            })
            clicked=0;
        }else{
            $('#inner-box').animate({
                left:'-=' + whole_move
            })
            clicked++;
        }
    }, 5000);
    /**************************************/
    if(counter >= 3){

        //inicialisation inner content
        $('#inner-box').width((counter*box_width)+(counter+margin_size));

        //getting left
        $('#go-left').click(function(){
            if(allowed_clicks == clicked){
            //alert('Nema vise');
            }else{
                $('#inner-box').animate({
                    left:'-=' + whole_move
                })
                clicked++;
            }
        })

        //inicialisation inner content
        $('#go-right').click(function(){
            if(clicked == 0){
            //alert('Nema vise');
            }else{
                $('#inner-box').animate({
                    left:'+=' + whole_move
                })
                clicked--;
            }
        })
        
    }
    //getting slider details
    $('.product-box').click(function(){
        window.location = $(this).attr('link');
    });
    
    //Making priducts

    $('.product').each(function(e){
        e++;
        if(e % 3){
        //Have a rest just leave it
        }else{
            $(this).css('margin-right','0px');
        }
    });

    //Add to cart

    $('.product-buttons a.assign, .product-buttons a.assign-detail').click(function(){

        $(this).parent().parent().append('<div class="loader"><div class="loader-text">Molimo sačekajte...</div></div>');
        var baseurl = $(this).attr('baseurl');
        var id = $(this).attr('productid');
       
        $.ajax({
            type: 'POST',
            url:  baseurl + 'ajax_frontend/cart_ajax_controller/add_to_cart/' + id + '.phtml',
            success: function(){
                $.ajax({
                    type: 'POST',
                    url:  baseurl + 'ajax_frontend/cart_ajax_controller/get_total_cart_num.phtml',
                    success: function(rez){
                        $('.loader').fadeOut(2000);
                        $('#cart_total').html(rez);
                    }
                });

            }

        });
            
        return false;
    })

    /*
     *Shoping cart update product
     */

    $('.saze-me').keyup(function(e){
        /* inits */
        var _width = $('#checkout-frame table').width();
        var _height = $('#checkout-frame table').height();
        
        var new_val = $(this).val();
        var baseurl = $(this).attr('url');

        if(parseInt(e.keyCode) == 13){
            var master_id = $(this).attr('row');
            $.ajax({
                type: 'POST',
                url:  baseurl + 'ajax_frontend/cart_ajax_controller/update_cart/' + master_id + '/' + new_val + '.phtml',
                success: function(data){
                    $('#table-rendering').html(data);
                    $.ajax({
                        type: 'POST',
                        url:  baseurl + 'ajax_frontend/cart_ajax_controller/get_total_cart_num.phtml',
                        success: function(rez){
                            $('.loader').fadeOut(2000);
                            $('#cart_total').html(rez);
                        }
                    });
                }

            });

        }

    })

    /*
     *Shoping cart delete product
     */
    $('.get-image').click(function(){
        
        var baseurl = $(this).attr('url');
        var value   = $(this).attr('row');

        $.ajax({
            type: 'POST',
            url:  baseurl + 'ajax_frontend/cart_ajax_controller/delete_row_cart/' + value + '.phtml',
            success: function(data){
                $('#table-rendering').html(data);
                $.ajax({
                    type: 'POST',
                    url:  baseurl + 'ajax_frontend/cart_ajax_controller/get_total_cart_num.phtml',
                    success: function(rez){
                        $('.loader').fadeOut(2000);
                        $('#cart_total').html(rez);
                    }
                });
            }

        });

    })

    /*Getting full text*/
    $('.get-full-text').click(function(){

        
        $('.get-full-text').parent().parent().prev().prev().slideDown(400);
        $('.get-full-text').parent().parent().prev().slideUp(400);

        $(this).parent().parent().prev().prev().slideUp(400);
        $(this).parent().parent().prev().slideDown(400);
        return false;
    })

    /*Categories and sub categorijes*/

    
})
