// Author: mail@aivanov.com

LinkDialog = new Class({
    Extends: FWindow,
    initialize: function(link, imgPath, linkPath, host) {
        this.parent(link);
        this.imgPath  = imgPath;
        this.linkPath = linkPath;
        this.host = host;
        this.setHTML('\
            <form id="link_dialog_form">\
                <div class="link_block">\
                    <label for="blog_code">Код для сайта или блога</label>\
                    <input type="text" readonly="readonly" class="text" id="blog_code" onclick="this.focus();this.select();"/>\
                </div>\
                <div class="link_block">\
                    <label for="link_code">Ссылка</label>\
                    <input type="text" readonly="readonly" class="text" id="link_code" onclick="this.focus();this.select();"/>\
                </div>\
                <strong class="photosize_header">размер изображения</strong>\
                <ul class="photosize">\
                    <li>\
                        <input type="radio" name="photosize" id="photosize_1" checked="checked" value="625x130"/>\
                        <label for="photosize_1">Малый</label>\
                    </li>\
                    <li>\
                        <input type="radio" name="photosize" id="photosize_2" value="625x200"/>\
                        <label for="photosize_2">Средний</label>\
                    </li>\
                    <li>\
                        <input type="radio" name="photosize" id="photosize_3" value="625x1000"/>\
                        <label for="photosize_3">Большой</label>\
                    </li>\
                    <li>\
                        <input type="radio" name="photosize" id="photosize_4" value=""/>\
                        <label for="photosize_4">Пользовательский</label>\
                        &nbsp;&nbsp;&nbsp;\
                        <input type="text" class="text" id="photosize_w" value="400" size="3"/>&nbsp;x&nbsp;<input type="text" class="text" id="photosize_h" value="300" size="3"/>\
                    </li>\
                </ul>\
            </form>'
        );

        this.form = $('link_dialog_form');

        var refreshFunction = this.refresh.bind(this);

        document.getElements('.photosize input[type="radio"]', this.form).each (
            function(obj) {
                obj.addEvent('click', refreshFunction);
            }
        )

        document.getElements('.photosize input[type="text"]', this.form).each (
            function(obj) {
                obj.addEvent('change', refreshFunction)
            }
        )

        this.refresh();
    },

    refresh: function() {
        var size = $('photosize_1').checked ?
            $('photosize_1').value : ($('photosize_2').checked ?
                $('photosize_2').value : ($('photosize_3').checked ?
                    $('photosize_3').value : ''));
        if ($('photosize_4').checked) {
            var w = parseInt($('photosize_w').value) || 0;
            $('photosize_w').value = w || 'авто';
            var h = parseInt($('photosize_h').value) || 0;
            $('photosize_h').value = h || 'авто';
            size = w + 'x' + h;
        }

        var url  = 'http://' + this.host + '/photos/' + size + '/' + this.imgPath;
        var link = 'http://' + this.host + '/' + this.linkPath;

        $('blog_code').value = '<a href="' + link + '"><img src="' + url + '" border="0" alt="flagatrip image"></a>';
        $('link_code').value = url;
    }

});


SelectFotoDialog = new Class({
    Extends: FWindow,
    initialize: function(link, folderId, className) {
        this.parent(link, 'fwindow_selectphoto' + (className ? ' ' + className : ''));
        this.addCloseButton();
        this.ulPhotos  = new Element('ul', {'id' : 'fPhotoalbumPhotos'}).injectInside(this.contentElement);
        new Element('a', {
            'class' : 'js_button ready_btn',
            'events': {
                'click' : this.okClickHandler.bind(this)
            }
        }).injectInside(this.contentElement);
        this.photos = [];
        this.folderId = folderId;
        this.folderImage = $('folder_' + folderId);
        this.prevSelected = null;
        this.selectedPhotoData = null;
        this.load(folderId);
    },

    load: function(folderId) {
        new FAjax('/ajax.php?action=photoalbum.folder&id=' + folderId, {
            onSuccess: function(data){
                this.loadHandler(data)
            }.bind(this)
        }).send({});
    },

    loadHandler: function(data) {
        if (data.result) {
            this.photos = data.result;
            this.refresh();
        } else {
            ulFolders.set('html','<li class="error">Креативная ошибка</li>');
        }
        this.refresh();
    },

    refresh: function() {
        this.ulPhotos.empty();
        for (var i = 0; i < this.photos.length; ++i) {
            var p = this.photos[i];
            var li = new Element('li').injectInside(this.ulPhotos);
            var div = new Element('div', {'class' : 'photo'}).injectInside(li);
            new Element('img', {
                'src'    : 'http://flagatrip.ru/photos/110!x82!/' + p.id + '.' + p.ext,
                'width'  : 110,
                'height' : 82
            }).injectInside(div);
            new Element('a', {
                'events' : { 'click' : this.photoClickHandler.bindWithEvent(this, [p]) }
            }).injectInside(div);
        }
    },

    photoClickHandler: function(e, p) {
        e.stop();
        if (this.prevSelected) {
            this.prevSelected.removeClass('active');
        }
        this.prevSelected = e.target;
        e.target.addClass('active');
        this.selectedPhotoData = p;
    },

    okClickHandler: function() {
        if (this.selectedPhotoData) {
            if (this.folderImage) {
                this.folderImage.src = '/photos/182!x182!/' + this.selectedPhotoData.id + '.' + this.selectedPhotoData.ext;
            }
            new FAjax("/ajax.php?action=photoalbum.setfoldercover&folder=" + this.folderId + "&cover=" + this.selectedPhotoData.id, {
                'method' : 'POST'
            }).send({});
        }
        this.remove();
    }

});



/**
 * Привязка папки
 * @param HTMLElement link
 * @param String subj
 * @param String view
 * @example new CountryLink($(anchor_element), folderId)
 */
CountryLink = new Class({

    initialize: function(link, folderId, options) {
        this.link     = link;
        this.folderId = folderId;
        this.load(folderId);
    },

    load: function(folderId) {
        new FAjax('/ajax.php?action=photoalbum.folderplace&id=' + folderId, {
            onSuccess: function(data){
                this.loadHandler(data)
            }.bind(this)
        }).send({});
    },

    loadHandler: function(data) {
        if (data.place) {
            this.place    = data.place;
            this.placeId  = data.placeId;
            this.folderId = data.folderId;
            this.refresh();
        } else {
            this.place    = '';
            this.placeId  = 0;
            this.refresh();
        }
    },

    refresh: function() {
        this.inputPlace = new Element('input', {
            'class' : 'autocompleter autocomp_field',
            'id'    : 'ctrl_city_name' + this.folderId,
            //'style' : 'width: 170px; position: absolute; left: 0px; top: 0px;',
            'type'  : 'text',
            'value' : this.place
        }).injectAfter(this.link);
        this.inputPlaceHidden = new Element('input', {
            'name'  : 'city_id' + this.folderId,
            'id'    : 'city_id' + this.folderId,
            'type'  : 'hidden',
            'value' : this.placeId
        }).injectAfter(this.link);
        this.aOk = new Element('a', {
            'class' : 'loclink_icon_ok',
            'events': {
                'click' : this.okClickHandler.bind(this)
            }
        }).injectAfter(this.link);

        var url = '/ajax.php?action=cities.autocomplete';
        new Autocompleter.Ajax.Json2($('ctrl_city_name' + this.folderId), $('city_id'+ this.folderId), url, { useSelection: false });
    },

    okClickHandler: function() {
        new Request({url: 'ajax.php?action=photoalbum.setfolderplace&id=' + this.folderId + '&placeid=' + $('city_id'+ this.folderId).value
                    ,onSuccess: function(data){
                                    data = JSON.decode(data);
                                    if(data.result == 'false')
                                    {
                                        this.link.className = 'loclink_icon_none';
                                    }else
                                    {
                                        this.link.className = 'loclink_icon';
                                    }
                                }.bind(this)
                    , method:'get'}).send();
        this.inputPlace.dispose();
        this.inputPlaceHidden.dispose();
        this.aOk.dispose();
    }
});

