
var loc = {

    selectorId: null,

    loadRegion: function(this_){
        $('locationRegionArea').load('/ajax.php?do=loadLocationRegion&id=' + $(this_).get('value'));
        $('locationTownArea').getChildren().set('value', '');
        $('locationTownArea').getChildren().set('disabled', true);
    },

    loadTown: function(this_){
        $('locationTownArea').load('/ajax.php?do=loadLocationTown&id=' + $(this_).get('value'));
    },

    loadCountyNative: function(this_){
        $('locationCountyNativeArea').load('/ajax.php?do=loadLocationCountyNative&id=' + $(this_).get('value'));
        $('locationVillageNativeArea').getChildren().set('value', '');
        $('locationVillageNativeArea').getChildren().set('disabled', true);
    },

    loadVillageNative: function(this_){
        $('locationVillageNativeArea').load('/ajax.php?do=loadLocationVillageNative&id=' + $(this_).get('value'));
    },

    selector: function(this_) {
        if ($(this_).value == '0') {
            win.init('locationSelector', $(this_).getPrevious().get('text'), 400);
            win.gotoThis('locationSelector',$(this_),'bottom');
            var elem = new Element('div').inject(win.content('locationSelector'));
            elem.load('/ajax.php?do=loadLocation&id=1');
            $('locationSelector').getElement('a').addEvent('click', function(){ $(this_).value = ''; });
            selectorId = $(this_).id;
        }
    },


    loadNext: function(this_)
    {
        var thisdiv = $(this_).getParent();
        thisdiv.getAllNext().each(function(item, index){ item.destroy(); });

        var elem = new Element('div').inject(thisdiv, 'after');
        elem.load('/ajax.php?do=loadLocation&id='+ $(this_).value);
    },


    selectThis: function(this_)
    {
        var elem = $(this_).getParent().getPrevious().getChildren()[0];
        var i = elem.selectedIndex;
        var optionTitle = elem.getElements('option')[i].get('text');

        if ($('locationSelector'))
        {
            new Element('option', {'value': elem.value}).set('text', optionTitle).inject($(selectorId).getElement('option'), 'after');
            $(selectorId).selectedIndex = 1;
            $('locationSelector').destroy();
        }
        else if ($('transferPhoto'))
        {
            $(this_).getParent().load('/ajax.php?do=transferToLocationDialog&id='+ elem.value);

        }
    },


    cultEditor: function(id)
    {
        win.init('cultEditor', 'Культовое место', 600);
        win.gotoCenter('cultEditor');
        new Request.HTML({
            url: '/ajax.php?do=cultEditor',
            update: win.content('cultEditor'),
            onComplete: function() {
                tinyMCE.init({
                    mode : "textareas",
                    elements : "editable-2",
                    theme : "advanced",
                    skin : "o2k7",
                    skin_variant : "silver",
                    language : "ru",
                    plugins : "safari,inlinepopups,paste",

                    theme_advanced_buttons1 : "formatselect,bold,italic,strikethrough,|,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,|,cleanup",
                    theme_advanced_buttons2 : "",
                    theme_advanced_toolbar_location : "top",
                    theme_advanced_toolbar_align : "left",
                    theme_advanced_statusbar_location : "none",

                    content_css : "/css/editor.css"
                });
                win.gotoCenter('cultEditor');
                $('cultEditor').getElement('form').addEvent('submit', loc.sendCult);
        }}).get({'id': id});
    },


    sendCult: function(e)
    {
        e.stop();
        var form = $('cultEditor').getElement('form');
        form.location_id.value = $('postForm').id.value;
        form.set('send', {
            method: 'post',
            onComplete: function(response) {
                if (response) {
                    var row_id = 'cult'+ response;
                    if ($(row_id)) {
                        $(row_id).load('/ajax.php?do=getCultForEdit&id='+ response);
                    }
                    else {
                        var elem = new Element('li', {id: row_id}).inject($('cultListEditor').getElement('li'), 'before');
                        elem.load('/ajax.php?do=getCultForEdit&id='+ response);
                    }
                    win.close('cultEditor');
                }
            }
        });
        form.send();
    }


};

