$(document).ready(function() {
    $('a.konfig_prev').click(function(){
        var pozice = $('ul#config_palette').scrollTop();
        $('ul#config_palette').animate({scrollTop: pozice - 27}, 400);
    });
    $('a.konfig_next').click(function(){
        var pozice = $('ul#config_palette').scrollTop();
        $('ul#config_palette').animate({scrollTop: pozice + 27}, 400);
    });

    $('#config_palette > li').click(
        function(){
            if(!$(this).hasClass("otevreno"))
            {
                $(this).addClass('otevreno');
                $(this).animate({'right':'0'},600);
                $(this).children('img').attr('src', './images/konfig_right.png');
                $(this).children('div.config_color_selected').fadeOut(0).css('right','4px').fadeIn(600);
                $(this).children('ul.palette').fadeIn(600);
                $(this).css('z-index', 5);
            }
        });

    var myTimer = false;
    
    $('#config_palette > li').hover(function(){
        if($(this).hasClass("otevreno")) {
            clearTimeout(myTimer);
        }
    },
    function(){
        //mouse leave
        var li_actual = this;
        myTimer = setTimeout(function(){
            hidePalette(li_actual);
        },500)
    });

    function hidePalette(id_palette) {
        if($(id_palette).hasClass("otevreno")) {
            $(id_palette).removeClass('otevreno');
            $(id_palette).children('ul.palette').fadeOut('slow');
            $(id_palette).children('div.config_color_selected').fadeOut(0).css('right','479px').fadeIn(500);
            $(id_palette).children('img').attr('src', './images/konfig_left.png');
            $(id_palette).animate({'right':'-475px'},500);
            $(id_palette).fade(0).css('z-index', 5);
        }

    };

    $('.palette > li').click(function () {
        if(!$('#click_block').hasClass('block')) {
            if($(this).attr('class') == 'no_background') {
                var bg = $(this).css('background');
                $(this).parents("li:eq(0)").children('.config_color_selected').css('background', bg);
                $(this).parents("li:eq(0)").children('.config_color_selected').attr('value', 'no_background');
            } else {
                var barva = $(this).css('background-color');
                var barva_id = $(this).attr('value');
                $(this).parents("li:eq(0)").children('.config_color_selected').css('background', '');
                $(this).parents("li:eq(0)").children('.config_color_selected').css('background-color', barva);
                $(this).parents("li:eq(0)").children('.config_color_selected').attr('value', barva_id);
            };
            load_photo();
        };
    });
});

function load_photo(rotation){
    if(typeof rotation == 'undefined') rotation = false;


    //$('.palette > li').attr('disabled', 'disabled');

    //$('#click_block').addClass('block');


    var rt = $('#rotation').attr('value');
    if(rotation) {
        if(rt == 'front') {
            rotation_new = 'back';
        } else {
            rotation_new = 'front';
        }
    } else {
        rotation_new = rt;
    }

    //alert(rotation_new);

    $('#rotation').attr('value', rotation_new);
    
    showLoader('configurator_loader', document.getElementById('conf_left'), 'center center', 'large', true);
    var href = $('#product_photo_lupa').attr('href');
    $('#conf_left').fadeTo("fast", 0.5);

    var Data = new Array();
    var poc = 0;
    $('td.pal_selected').each(function() {
        Data[poc] = $(this).attr('name') + "=" + $(this).attr('value');
        poc = poc + 1;
    });

    var data_s = Data.join('&');
    $.ajax({
        type: "POST",
        url: 'functions/photo_create.php',
        data: "photo_config=1&rotation_config="+rotation_new+"&"+data_s,
        success: function(value) {
            //alert(value);
            //$("#resultt").html(value);
            value = $.trim(value);

            if(value != 'error') {
                value = value.split('&');
                for(x in value) {
                    var par = value[x].split('=');
                    switch(par[0]) {
                        case 'height':
                            height = par[1];
                            break;
                        case 'width':
                            width = par[1];
                            break;
                        case 'url':
                            url = par[1];
                            break;
                        default:
                            break;
                    }
                }

                var filename = url.split('/');
                filename = filename[filename.length - 1].split('.');

                $('#product_photo_config_dialog').attr('src', url);
                $('#product_photo_config_dialog').show();
                $('#photo_file_name').attr('value', filename[0]);
                //$('#product_photo_zoom > a:first').attr('href', url);
            } else {
                alert('Při vykonávání příkazu se vyskytla chyba');
            }

            $('#conf_left').fadeTo("fast", 1);
            //$('#click_block').removeClass('block');
            hideLoader('configurator_loader');
            $("#loading").attr("value", "false");
          
        },
        error: function() {
            $('#conf_left').fadeTo("fast", 1);
            //$('#product_photo_config_wrap').fadeTo("fast", 1);
            hideLoader('configurator_loader');
            $("#loading").attr("value", "false");
            alert('Při vykonávání příkazu se vyskytla chyba');
            //$('#click_block').removeClass('block');
        }
    });
};


