/**
 * Created by .
 * User: И
 * Date: 29.07.11
 * Time: 11:34
 * To change this template use File | Settings | File Templates.
 */

ccl = true;

$(document).ready(function() {
    // coin slider


    $('#types .item').click(function() {
        $('#types .item').removeClass('active');
        $(this).addClass('active');
        ccl = true;
        changer();
    });

    if ($(document).height()>$('#container').height()) {
        ots = $(document).height()-$('#container').height();
        $('#footer').css('margin-top', ots+'px');
    }


    $('#choice').animate({opacity:0}, 0);

    function changer() {

        var colors = '';
         if (ccl == false) {
        $('#colors .item').each(function() {
           if ($(this).is('.active')) {
               colors+=$(this).find('.name').html()+', ';
           }
        });}

        if ($('#buyer').attr('checked')){
                            ra = true;
                        }
        else {
           ra = false;
        }

        $.getJSON('/api/product/', {'id': $('#types .active').attr('rel'), qty: val, colors:colors, ra:ra}, function(data) {
         if (data) {
                        if (data.description) {
                            $('#description').animate({opacity:1}, 150);
                            $('#choice').animate({opacity:1}, 150);
                            $('#description').html('<h3>Описание:</h3>' + data.description);
                        }
                        else {
                            $('#description').animate({opacity:0}, 150);
                        }
                        if (ccl){
                        $('#colors .item').detach();
                        for (var i = 0; i < data.colors.length; i++) {
                            $('#colors').append('<div class="item"><div class="image"><img src="" alt=""></div><div class="name"></div></div>');
                        }
                        s = 0;
                            
                        $('#colors .item').each(function() {
                            $(this).find('.name').html(data.colors[s]);
                            $(this).find('img').attr('src', data.colors_img[s]);
                            s++;
                        });

                        $('#colors .item').click(function() {
                            ccl = false;
                            $(this).toggleClass('active');
                            changer();
                        });}

                        if ($('#buyer').attr('checked')){
                            price = data.price_ra;
                        }
                        else {
                            price = data.price;
                        }

                        $('#price_one').html(price+' руб.');

                        $('#price_total').html(price*val+' руб.');


                    } });
    }


    $('#colors .item').click(function() {
        ccl = false;
        $(this).toggleClass('active');
        changer();
    });

    $('#buyer').click(function(){

        changer();
    });

    var def = 0;
    var val = 0;

    $("#qty").slider({
        value:0,
        animate: true,
        min: 0,
        max: 28,
        step: 1,
        slide: function(event, ui) {

            if (ui.value<=9) {
                val = ui.value * 10;
            }
            else {
                 if (ui.value<=19) {
                val = (ui.value-9) * 100;
            }
                else {
                     if (ui.value<=30) {
                val = (ui.value-18) * 1000;}
                 }
            }

            $('.ui-slider-handle').html('<span>' + val) + '</span>';

        },
        create: function(event, ui) {
            $('.ui-slider-handle').html('<span>' + 0 + '</span>');
        },
        change: function(event, ui) {
            ccl = false;
            changer();
        }

    });

    $("a[rel^='gallery']").prettyPhoto({
        allow_resize: true,
        overlay_gallery: false,
        changepicturecallback: function() {
            $(".pp_overlay").css("height", $(document).height());
        }
    });

    $('#coin-slider').coinslider({
        width: 934, // width of slider panel
        height: 298 // height of slider panel
    });


});

$(document).ready(function() {
    // coin slider

    $("#types").jCarouselLite({
                btnNext: "#ar_right",
                btnPrev: "#ar_left",
                mouseWheel: true,
                visible:6
            });
});
