$(document).ready(function () {

    //$("a.lightbox").lightBox();

    $("a#mini_thumbnail").click(function () {
        $(this).attr('class','');
        $("#full_image").attr('href', $(this).attr('href'));
        var image_thumbnail_html = '<img src="' + $(this).attr("rel") + '">';
        $("#full_image").animate({
            opacity:0.0
        }, 200, function () {
            $("#full_image").html(image_thumbnail_html);
            $("#full_image").animate({
                opacity:1.0
            }, 200);
        });

        return false;
    });

    $("a.del_photo").live("click", function () {
        var photo_id = $(this).attr('rel');
        $.ajax({
            type:'GET',
            data:'',
            url:'/addtechnique/delphoto/' + photo_id + '/',
            beforeSend:function () {

            },
            success:function () {
                $.get('/addtechnique/thumbnails', {}, show_thumbnails);
            },
            error:function () {
            }
        });
        return false;
    });

    $("a.set_main").live("click", function () {
        var photo_id = $(this).attr('rel');
        $.ajax({
            type:'GET',
            data:'',
            url:'/addtechnique/setmainphoto/' + photo_id + '/',
            beforeSend:function () {

            },
            success:function () {
                $("a.set_main").css('display', 'block');
                $(this).css('display', 'none');
                $("span.main_photo").css('display', 'none');
                $("span.main_photo[rel=" + photo_id + "]").css('display', 'block');
            },
            error:function () {
            }
        });

        return false;
    });

    $('#technique_region').change(function () {
        adjustregion();
    });


});

function adjustregion() {
    var technique_region_id = $('#technique_region').val();
    var tmpSelect = $('#technique_city');

    if ($('#technique_region').attr('class') == 'noactive') {
        $('#technique_region').attr('class', 'active');
        return false;
    }

    $.ajax({
        url:'/location/citylist/' + technique_region_id,
        dataType:"text",
        success:function (data) {
            json = eval("(" + data + ")");
            tmpSelect.fillSelect(json).attr('disabled', false);
        }
    });
}


(function ($) {
    // очищаем select
    $.fn.clearSelect = function () {
        return this.each(function () {
            if (this.tagName == 'SELECT') {
                this.options.length = 0;
                $(this).attr('disabled', true);
            }
        });
    }
    // заполняем select
    $.fn.fillSelect = function (dataArray) {
        return this.clearSelect().each(function () {
            if (this.tagName == 'SELECT') {
                var currentSelect = this;
                $.each(dataArray, function (index, data) {
                    var option = new Option(data.text, data.value);
                    if ($.support.cssFloat) {
                        currentSelect.add(option, null);
                    } else {
                        currentSelect.add(option);
                    }
                });
            }
        });
    }
})(jQuery);
