function PhotosAJAX(sid, tr, tr_popup, albums, groups, collections) {
    var oThis = this;
    
    this.sid = sid;
    this.album_menu = null;
    
    this.smiles = {
        ':)': '<img src="http://m1.kefche.com/icons/c/smile.gif" alt=":)" />',
        ':-)': '<img src="http://m1.kefche.com/icons/c/smile.gif" alt=":-)" />',
        ':(': '<img src="http://m1.kefche.com/icons/c/sadsmile.gif" alt=":(" />',
        ':-(': '<img src="http://m1.kefche.com/icons/c/sadsmile.gif" alt=":-(" />',
        ':D': '<img src="http://m1.kefche.com/icons/c/bigsmile.gif" alt=":D" />',
        ':d': '<img src="http://m1.kefche.com/icons/c/bigsmile.gif" alt=":D" />',
        '8)': '<img src="http://m1.kefche.com/icons/c/cool.gif" alt="8)" />',
        ':O)': '<img src="http://m1.kefche.com/icons/c/surprised.gif" alt=":O)" />',
        ':o)': '<img src="http://m1.kefche.com/icons/c/surprised.gif" alt=":O)" />',
        ';)': '<img src="http://m1.kefche.com/icons/c/wink.gif" alt=";)" />',
        ';-)': '<img src="http://m1.kefche.com/icons/c/wink.gif" alt=";-)" />',
        ';(': '<img src="http://m1.kefche.com/icons/c/crying.gif" alt=";(" />',
        ';-(': '<img src="http://m1.kefche.com/icons/c/crying.gif" alt=";-(" />',
        ':*': '<img src="http://m1.kefche.com/icons/c/kiss.gif" alt=":*" />',
        ':P': '<img src="http://m1.kefche.com/icons/c/tongueout.gif" alt=":P" />',
        ':p': '<img src="http://m1.kefche.com/icons/c/tongueout.gif" alt=":P" />',
        ':-P': '<img src="http://m1.kefche.com/icons/c/tongueout.gif" alt=":-P" />',
        ':-p': '<img src="http://m1.kefche.com/icons/c/tongueout.gif" alt=":-P" />',
        '|-)': '<img src="http://m1.kefche.com/icons/c/sleepy.gif" alt="|-)" />', 
        'x=(': '<img src="http://m1.kefche.com/icons/c/angry.gif" alt="x=(" />',
        'X=(': '<img src="http://m1.kefche.com/icons/c/angry.gif" alt="x=(" />',
        ':-S': '<img src="http://m1.kefche.com/icons/c/worried.gif" alt=":-S" />',
        ':-s': '<img src="http://m1.kefche.com/icons/c/worried.gif" alt=":-S" />',
        '8-|': '<img src="http://m1.kefche.com/icons/c/nerd.gif" alt="8-|" />'
    };
    
    this.createAlbumMenu = function (cont, album_id, link, item, owner) {
        var move = document.createElement('a'),
            combine = document.createElement('a'),
            group_add = document.createElement('a'),
            coll_add = document.createElement('a'),
            fav_add = document.createElement('a'),
            spacer = document.createElement('hr');
        
        if(owner) {
            move.appendChild(document.createTextNode(tr_popup['PHOTOS_MOVE_ALBUM_PHOTOS_TO']));
            move.href = '#';
            move.onclick = function () {
                toggleEffect(oThis.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
                oThis.showMoveForm(album_id, item);
                return false;
            };
            cont.appendChild(move);
            
            combine.appendChild(document.createTextNode(tr_popup['PHOTOS_COMBINE_ALBUM_PHOTOS']));
            combine.href = '#';
            combine.onclick = function () {
                toggleEffect(oThis.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
                oThis.showCombineForm(album_id, item);
                return false;
            };
            cont.appendChild(combine);
            
            cont.appendChild(spacer);
        }
        
        fav_add.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_ALBUM_PHOTOS_TO_FAVORITES']));
        fav_add.href = '#';
        fav_add.onclick = function () {
            toggleEffect(oThis.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
            oThis.showAddFavoritesForm(album_id, item);
            return false;
        };
        cont.appendChild(fav_add);
        
        coll_add.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_ALBUM_PHOTOS_TO_COLLECTION']));
        coll_add.href = '#';
        coll_add.onclick = function () {
            toggleEffect(oThis.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
            oThis.showAddCollectionForm(album_id, item);
            return false;
        };
        cont.appendChild(coll_add);
        
        group_add.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_ALBUM_PHOTOS_TO_GROUP']));
        group_add.href = '#';
        group_add.onclick = function () {
            toggleEffect(oThis.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
            oThis.showAddGroupForm(album_id, item);
            return false;
        };
        cont.appendChild(group_add);
        
        
        
    }
    this.showAlbumMenu = function (e, album_id, link, owner) {
        if (!this.album_menu) {
            
            this.album_menu = document.createElement('div');
            this.album_menu.className = 'select_type';
            
            $('main_wraper').appendChild(this.album_menu);
            hide(this.album_menu);
            this.album_menu.style.height = '0px';
            this.createAlbumMenu(this.album_menu, album_id, link, link.parentNode.parentNode.parentNode, owner);
        } else {
            if (this.album_menu.last_open != link) {
                hide(this.album_menu);
                this.album_menu.style.height = '0px';
                document.onclick = null;
            }
            this.album_menu.innerHTML = '';
            this.createAlbumMenu(this.album_menu, album_id, link, link.parentNode.parentNode.parentNode, owner);
        }
        
        var link_width = (document.all) ? link.clientWidth : link.scrollWidth,
			menu_width = (document.all) ? this.album_menu.clientWidth : this.album_menu.scrollWidth;
        
        setPosTo(this.album_menu, link, true);
        this.album_menu.style.left = parseInt(this.album_menu.style.left) - menu_width + link_width + 'px';
        
        if (!e) e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        
        this.album_menu.last_open = link;
        toggleEffect(this.album_menu, function(){EffectSquashIn(oThis.album_menu);}, function(){EffectSquashOut(oThis.album_menu, 144, link, 'vertical', 5, false);});
    }
    
    this.showCombineForm = function(album_id, item) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        var selects = [new DropDown(tr_popup['PHOTOS_SELECT_ALBUM'], 'dropdown-std', null), new DropDown(tr_popup['PHOTOS_SELECT_ALBUM'], 'dropdown-std', null)];
        var title = document.createElement('input');
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_COMBINE_ALBUMS_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_COMBINE_ALBUMS_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.albums_combine(title.value, selects, item, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array(),
                clear = document.createElement('div'),
                br = document.createElement('br');
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_COMBINE_ALBUMS_DESC']));
            
            var options = new Array();
            for(var i in albums) {
                var opt = new DropDownOption(albums[i], i, function (event) {selects[0].selectOption(event, this.v, this.t);});
                options.push(opt);
                
                if(i == album_id) {
                    var opts = [i, albums[i]];
                }
            }
            
            selects[0].setOptions(options);
            selects[0].init();
            cont.appendChild(selects[0].get());
            if(opts) {
                selects[0].selectOption(null, opts[0], opts[1]);
                selects[0].toggleMenu(null);
            }
            
            
            var options = new Array();
            for(var i in albums) {
                var opt = new DropDownOption(albums[i], i, function (event) {selects[1].selectOption(event, this.v, this.t);});
                options.push(opt);
            }
            
            selects[1].setOptions(options);
            selects[1].init();
            cont.appendChild(selects[1].get());
            
            clear.className = 'clear';
            cont.appendChild(clear);
            
            cont.appendChild(br);
            
            title.className = 'input-std';
            title.value = tr_popup['PHOTOS_ALBUM_TITLE'];
            title.onfocus = function () {if (this.value == tr_popup['PHOTOS_ALBUM_TITLE']) this.value = '';};
            title.onblur = function () {if (this.value == '') this.value = tr_popup['PHOTOS_ALBUM_TITLE'];};
            
            cont.appendChild(title);
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
        this.albums_combine = function (title, selects, item, btn) {
            if(title == '' || title == tr_popup['PHOTOS_ALBUM_TITLE']) {
                this.groups_popup.showError(tr_popup['PHOTOS_COMBINE_ALBUMS_ERROR_ENTER_TITLE']);
                btn.disabled = false;
                return;
            }
            
            
            var selected = {}, data = {};
            data['albums'] = new Array();
            
            for(var i=0; i<selects.length; i++) {
                if(selects[i].getValue() == '') {
                    this.groups_popup.showError(tr_popup['PHOTOS_COMBINE_ALBUMS_ERROR_SELECT_ALBUM']);
                    btn.disabled = false;
                    return;
                } else if(selected[selects[i].getValue()]) {
                    this.groups_popup.showError(tr_popup['PHOTOS_COMBINE_ALBUMS_ERROR_SELECT_DIFFERENT']);
                    btn.disabled = false;
                    return;
                }
                selected[selects[i].getValue()] = 1;
                data['albums'].push(selects[i].getValue());
            }
            data['title'] = title;
            
            this.groups_popup.showWait();
            
            var request = {
                url: '/photos/ajax_handle.php?action=combine_albums',
                data: data,
                onSuccess: function (res) {
                    location.href = location.href;
                },
                onFailure: function (res) {
                    oThis.groups_popup.hideWait();
                    setTimeout(function () {btn.disabled = false;}, 2000);
                }
            };
            myAJAX(request);
        }
    
    /* ADD ALBUM TO FAVORITES */
    this.showAddFavoritesForm = function(album_id, item) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_ADD_ALBUM_TO_FAVORITES_TITLE'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_ADD_TO_FAVORITES_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_album_to_favorites(album_id, item, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_ALBUM_TO_FAVORITES_DESC']));
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
        
        this.add_album_to_favorites = function(album_id, item, btn) {
            
            var request = {
                url: '/photos/ajax_handle.php?action=add_album_to_favorites',
                data: { album_id: album_id },
                onSuccess: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 1000);
                    oThis.groups_popup.showMessage(res);
                },
                onFailure: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 2000);
                    oThis.groups_popup.showError(res);
                }
            };
            myAJAX(request);
        }
    
    /**/
    
    
    
    /* ADD ALBUM TO COLLECTION */
    this.showAddCollectionForm = function(album_id, item) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        var select = new DropDown(tr_popup['PHOTOS_SELECT_COLLECTION'], 'dropdown-std', null);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_ADD_ALBUM_TO_COLLECTION_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_ADD_TO_COLLECTION_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_album_to_collection(album_id, select.getValue(), item, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array(),
                add_coll = document.createElement('input');
            
            add_coll.className = 'input-std';
            add_coll.value = tr_popup['PHOTOS_CREATE_COLLECTION'];
            add_coll.onfocus = function () {if (this.value == tr_popup['PHOTOS_CREATE_COLLECTION']) this.value = '';};
            add_coll.onblur = function () {if (this.value == '') this.value = tr_popup['PHOTOS_CREATE_COLLECTION'];};
            add_coll.onkeydown = function (event) {var value = this.value;oThis.get_key(event, function () {oThis.add_photo_collection(value, select)});};
            
            add_elements.push(add_coll);
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_TO_COLLECTION_DESC']));
            
            var options = new Array();
            for(var i in collections) {
                var opt = new DropDownOption(collections[i].title, i, function (event) {select.selectOption(event, this.v, this.t);});
                options.push(opt);
            }
            
            select.setOptions(options);
            select.init();
            select.addElements(add_elements);
            cont.appendChild(select.get());
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
        
        this.add_album_to_collection = function(album_id, coll_id, item, btn) {
            
            var request = {
                url: '/photos/ajax_handle.php?action=add_album_to_collection',
                data: {
                    album_id: album_id,
                    coll_id: coll_id
                },
                onSuccess: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 1000);
                    oThis.groups_popup.showMessage(res);
                },
                onFailure: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 2000);
                    oThis.groups_popup.showError(res);
                }
            };
            myAJAX(request);
        }
    
    /**/
    
    
    /* ADD ALBUM TO GROUP */
    this.showAddGroupForm = function(album_id, item) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        var select = new DropDown(tr_popup['PHOTOS_SELECT_GROUP'], 'dropdown-std', null);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_ADD_ALBUM_TO_GROUP_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_ADD_TO_GROUP_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_album_to_group(album_id, select.getValue(), item, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            if (!groups) {
                cont.appendChild(document.createTextNode(tr_popup['PHOTOS_YOU_DONT_HAVE_GROUPS']));
            } else {
                cont.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_ALBUM_TO_GROUP_DESC']));
                
                var options = new Array();
                for(var i in groups) {
                    var opt = new DropDownOption(groups[i], i, function (event) {select.selectOption(event, this.v, this.t);});
                    options.push(opt);
                }
                
                select.setOptions(options);
                select.init();
                cont.appendChild(select.get());
            }
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
        
        this.add_album_to_group = function(album_id, group_id, item, btn) {
            
            var request = {
                url: '/photos/ajax_handle.php?action=add_album_to_group',
                data: {
                    album_id: album_id,
                    group_id: group_id
                },
                onSuccess: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 1000);
                    oThis.groups_popup.showMessage(res);
                },
                onFailure: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 2000);
                }
            };
            myAJAX(request);
        }
    
    /**/
    
    /* MOVE ALBUM TO ALBUM */
    this.showMoveForm = function(album_id, item) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        var select = new DropDown(tr_popup['PHOTOS_SELECT_ALBUM'], 'dropdown-std', null);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_MOVE_ALBUM_TO_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_MOVE_ALBUM_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.move_album(album_id, select.getValue(), item, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array();
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_MOVE_ALBUM_TO_DESC']));
            
            
            add_coll = document.createElement('input');
            add_coll.className = 'input-std';
            add_coll.value = tr_popup['PHOTOS_CREATE_ALBUM'];
            add_coll.onfocus = function () {if (this.value == tr_popup['PHOTOS_CREATE_ALBUM']) this.value = '';};
            add_coll.onblur = function () {if (this.value == '') this.value = tr_popup['PHOTOS_CREATE_ALBUM'];};
            add_coll.onkeydown = function (event) {var value = this.value;oThis.get_key(event, function () {oThis.add_photo_album(value, select)});};
            
            add_elements.push(add_coll);
            
            var options = new Array();
            for(var i in albums) {
                if(i != album_id) {
                    var opt = new DropDownOption(albums[i], i, function (event) {select.selectOption(event, this.v, this.t);});
                    options.push(opt);
                }
            }
            
            select.setOptions(options);
            select.init();
            select.addElements(add_elements);
            cont.appendChild(select.get());
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
        this.add_photo_album = function(title, select) {
            var request = {
                url: '/photos/ajax_handle.php?action=add_album',
                data: {
                    title: title
                },
                onSuccess: function (res) {
                    var album_id = eval('('+res+')');
                    select.addOption(title, album_id, function (event) {select.selectOption(event, this.v, this.t);});
                    select.selectOption (null, album_id, title);
                    
                    if (!albums) albums = {};
                    
                    albums[album_id] = title;
                }
            };
            myAJAX(request);
        }
        this.move_album = function(album_id, to_album_id, item, btn) {
            
            var request = {
                url: '/photos/ajax_handle.php?action=move_album',
                data: {
                    album_id: album_id,
                    to_album_id: to_album_id
                },
                onSuccess: function (res) {
					location.reload();
                },
                onFailure: function (res) {
                    setTimeout(function () {btn.disabled = false;}, 2000);
                }
            };
            myAJAX(request);
        }
    /**/
    
    
    this.RemoveAlbum = function (album_id, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=remove_album',
            data: {
                album_id: album_id
            },
            onSuccess: function (res) {
                delete albums[album_id];
                fadeOut(obj, 60, function () {obj.parentNode.removeChild(obj);});
            }
        };
        myAJAX(request);
    }
    
    this.showRemovePhoto = function (photo_id) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_REMOVE_PHOTO_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_REMOVE_PHOTO_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.RemovePhoto(photo_id);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTO_REMOVE_PHOTO_CONFIRM']));
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
    this.RemovePhoto = function (photo_id, obj) {
        if (oThis.groups_popup)
            oThis.groups_popup.showWait();
        var request = {
            url: '/photos/ajax_handle.php?action=remove_photo',
            data: {
                photo_id: photo_id
            },
            onSuccess: function (res) {
                if (oThis.groups_popup)
                    oThis.groups_popup.hideWait();
                
                if (obj) {
                    oThis.setOpacity(obj, 100);
                    oThis.fadeOut(obj, 60);
                } else {
                    location.reload();
                }
            }
        };
        myAJAX(request);
    }
    
    this.RemoveCollections = function (photo_id, coll_id, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=remove_collections',
            data: {
                photo_id: photo_id,
                coll_id: coll_id
            },
            onSuccess: function (res) {
                oThis.fadeOut(obj, 100);
            }
        };
        myAJAX(request);
    }
    
    this.AddFavorites = function (photo_id, obj, link, text, text2) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_favorites',
            data: {
                photo_id: photo_id
            },
            onSuccess: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
                setTimeout(
                function () {
                    oThis.fadeOut(obj, 100);
                }, 5000);
                if (link) {
                    link.innerHTML = '<span></span>'+text;
                    link.onclick = function () {
                        oThis.RemoveFavorites(photo_id, obj, link, text2, text);
                    }
                }
            },
            
            onFailure: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
            }
        };
        myAJAX(request);
    }
    
    
    this.RemoveFavorites = function (photo_id, obj, link, text, text2) {
        var request = {
            url: '/photos/ajax_handle.php?action=remove_favorites',
            data: {
                photo_id: photo_id
            },
            onSuccess: function (res) {
                if (link) {
                    
                    obj.innerHTML = res;
                    oThis.setOpacity(obj, 100);
                    obj.style.display = 'block';
                    setTimeout(
                    function () {
                        oThis.fadeOut(obj, 100);
                    }, 5000);
                    link.innerHTML = '<span></span>'+text;
                    link.onclick = function () {
                        oThis.AddFavorites(photo_id, obj, link, text2, text);
                    }
                    
                } else {
                    oThis.fadeOut(obj, 100);
                }
                
            },
            
            onFailure: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
            }
        };
        myAJAX(request);
    }
    
    this.AddAbuse = function (photo_id, type_id, obj, abuse_panel) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_abuse',
            data: {
                photo_id: photo_id,
                abuse_id: type_id
            },
            onSuccess: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
                abuse_panel.style.display = 'none';
                setTimeout(
                function () {
                    oThis.fadeOut(obj, 100);
                }, 5000);
            },
            
            onFailure: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
            }
        };
        myAJAX(request);
    }
    
    this.AddVote = function (photo_id, vote, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_vote',
            data: {
                photo_id: photo_id,
                vote: vote
            },
            onSuccess: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
                setTimeout(
                function () {
                    oThis.fadeOut(obj, 100);
                }, 5000);
            },
            
            onFailure: function (res) {
                obj.innerHTML = res;
                oThis.setOpacity(obj, 100);
                obj.style.display = 'block';
            }
        };
        myAJAX(request);
    }
    
    this.AddSpam = function (comment_id, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_spam',
            data: {
                comment_id: comment_id
            },
            onSuccess: function (res) {
                $('spam_'+comment_id).innerHTML = res;
            },
            onFailure: function (res) {
                alert(tr_popup['PHOTOS_COMMENTS_YOU_CAN_VOTE_ONCE_PER_DAY']);
                return false;
            }
        };
        myAJAX(request);
    }
    
    this.RemoveSpam = function (comment_id, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=remove_spam',
            data: {
                comment_id: comment_id
            },
            onSuccess: function (res) {
                $('spam_'+comment_id).innerHTML = res;
            },
            onFailure: function (res) {
                alert(tr_popup['PHOTOS_COMMENTS_YOU_CAN_VOTE_ONCE_PER_DAY']);
                return false;
            }
        };
        myAJAX(request);
    }
    
    this.DelComment = function (comment_id, obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=del_comment',
            data: {
                comment_id: comment_id
            },
            onSuccess: function (res) {
                oThis.setOpacity(obj.parentNode.parentNode.parentNode, 100);
                oThis.fadeOut(obj.parentNode.parentNode.parentNode, 100, function () {obj.parentNode.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode.parentNode);});
            },
            
            onFailure: function (res) {
                return false;
            }
        };
        myAJAX(request);
    }
    
    this.editComment = function (msg_id, link) {
        var msg = link.parentNode.parentNode.getElementsByTagName('p')[0];
        
        if(!link.taEdit) {
            var br = document.createElement('br');
            link.formEdit = document.createElement('div');
            link.taEdit = document.createElement('textarea');
            link.saveBtn = document.createElement('input');
            
            link.saveBtn.type = 'button';
            link.saveBtn.value = tr['COMMENTS_SAVE_COMMENT'];
            link.saveBtn.onclick = function () {
                oThis.save_comment(msg_id, link);
            };
            
            link.taEdit.className = 'comment_ta';
            
            msg.parentNode.appendChild(link.formEdit);
            link.formEdit.appendChild(link.taEdit);
            link.formEdit.appendChild(br);
            link.formEdit.appendChild(link.saveBtn);
            
            show(msg);
            
            var comm = msg.innerHTML;
            comm = comm.replace(/<img[^>]+alt="([^"><]+?)".*?>/gi, '$1');
            link.taEdit.value = br2nl(comm);
        }
        
        toggle(link.formEdit);
        toggle(msg);
    }
        this.save_comment = function (msg_id, link) {
            var request = {
                url: '/photos/ajax_handle.php?action=save_comment',
                data: {
                    msg_id: msg_id,
                    comment: link.taEdit.value
                },
                onSuccess: function (res) {
                    var comm = link.taEdit.value,
                        msg = link.parentNode.parentNode.getElementsByTagName('p')[0];
                    
                    hide(link.formEdit);
                    show(msg);
                    
                    for(var smilie in oThis.smiles) {
                        var regex = new RegExp(RegExp.escape(smilie), "g");
                        comm = comm.replace(regex, oThis.smiles[smilie]);
                    }
                    
                    msg.innerHTML = nl2br(comm);
                }
                
            };
            myAJAX(request);
        }
    
    this.AddComment = function (photo_id, comment, obj, msg_obj) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_comment',
            data: {
                photo_id: photo_id,
                comment: comment
            },
            onSuccess: function (res) {
                var info = eval('('+res+')');
                    comm = document.createElement('div'),
                    panel = obj.parentNode;
                
                comm.innerHTML = info.comment;
                panel.parentNode.insertBefore(comm, nextNode(panel));
                if (obj.parentNode.reset) obj.parentNode.reset();
                
                msg_obj.style.display = 'block';
                msg_obj.innerHTML = info.msg;
                
                if(tooltip) tooltip.initElements();
                
                oThis.setOpacity(comm, 10);
                setTimeout(
                    function () {
                        oThis.fadeIn(comm, 20);
                    }, 500);
                
                oThis.setOpacity(msg_obj, 100);
                setTimeout(
                    function () {
                        oThis.fadeOut(msg_obj, 100);
                    }, 5000);
            },
            
            onFailure: function (res) {
                msg_obj.style.display = 'block';
                msg_obj.innerHTML = res;
                
                oThis.setOpacity(msg_obj, 100);
                setTimeout(
                    function () {
                        oThis.fadeOut(msg_obj, 100);
                    }, 5000);
                
                return false;
            }
        };
        myAJAX(request);
    }
    
    this.ShowReply = function (photo_id, comment_id, obj) {
        var cont = obj.parentNode.parentNode.parentNode;
            panel = $('comment_reply_'+comment_id);
        
        if (!panel) {
            var panel = document.createElement('div'),
                comm = document.createElement('textarea'),
                btn_ok = document.createElement('input'),
                btn_cancel = document.createElement('input'),
                msg = document.createElement('div');
            
            comm.className = 'comment_ta';
            
            btn_ok.type = 'button';
            btn_ok.value = tr['COMMENTS_SAVE_COMMENT'];
            btn_ok.onclick = function () {
                oThis.AddReply(photo_id, comment_id, comm.value, this.parentNode, msg);
            }
            
            btn_cancel.type = 'button';
            btn_cancel.value = tr['COMMENTS_CANCEL_COMMENT'];
            btn_cancel.onclick = function () {
                hide(panel);
            }
            
            panel.id = 'comment_reply_'+comment_id;
            panel.style.display = 'none';
            panel.appendChild(comm);
            
            if($('emote_list')) {
                /* Kopirame i ikonkite */
                var emote = document.createElement('div');
                emote.innerHTML = $('emote_list').innerHTML;
                emote.className = $('emote_list').className;
                
                var elink = nextNode($('emote_list'));
                var link = document.createElement('a');
                
                link.innerHTML = elink.innerHTML;
                link.className = 'emote-link';
                link.href='#';
                link.onclick = function () {toggle(prevNode(this)); return false;};
                
                panel.appendChild(emote);
                panel.appendChild(link);
                
                var list = emote.getElementsByTagName('a');
                for(var i=0; i<list.length; i++) {
                    list[i].onclick = function (){
                        var msgbox = prevNode(this.parentNode),
                            img = this.getElementsByTagName('img')[0];
                        
                        msgbox.value += "" + img.getAttribute('alt') + " ";
                        msgbox.focus();
                        
                        hide(this.parentNode);
                    }
                }
            }
            
            panel.appendChild(btn_ok);
            panel.appendChild(document.createTextNode(' '));
            panel.appendChild(btn_cancel);
            panel.appendChild(msg);
            cont.appendChild(panel);
        }
        
        toggle(panel);
        
        if(panel.style.display != 'none') {
            panel.getElementsByTagName('textarea')[0].focus();
        }
    }
    
    this.AddReply = function (photo_id, comment_id, comment, obj, msg) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_reply',
            data: {
                photo_id: photo_id,
                comment_id: comment_id,
                comment: comment
            },
            onSuccess: function (res) {
                var info = eval('('+res+')');
                    comm = document.createElement('div'),
                    bf = obj.parentNode.getElementsByTagName('div')[0];
                
                comm.innerHTML = info.comment;
                bf.appendChild(comm);
                
                hide(obj);
                if(tooltip) tooltip.initElements();
                
                oThis.setOpacity(comm, 10);
                setTimeout(
                    function () {
                        oThis.fadeIn(comm, 20);
                    }, 500);
            },
            onFailure: function (res) {
                msg.style.display = 'block';
                msg.innerHTML = res;
                
                oThis.setOpacity(msg, 100);
                setTimeout(
                    function () {
                        oThis.fadeOut(msg, 100);
                    }, 5000);
                
                return false;
            }
        };
        myAJAX(request);
    }
    
    this.ShowReplys = function (obj) {
        var list = obj.parentNode.getElementsByTagName('div');
        
        for(var i=0; i<list.length; i++) {
            if (list[i].className == 'comment-item hide') {
                list[i].style.display = 'block';
                fadeIn(list[i], 10);
            }
        }
    }
    
    
    this.ShowGroups = function (photo_id, link) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        var select = new DropDown(tr_popup['PHOTOS_SELECT_GROUP'], 'dropdown-std', null);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_ADD_TO_GROUP'], 'dialog-std');
        
        
        function buttons() {
            if (!groups) {
                return [new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} )];
            }
            
            return [new Button(tr_popup['PHOTOS_ADD_TO_GROUP_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_to_group(photo_id, select.getValue(), link, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            if (!groups) {
                cont.appendChild(document.createTextNode(tr_popup['PHOTOS_YOU_DONT_HAVE_GROUPS']));
            } else {
                cont.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_TO_GROUP_DESC']));
                
                var options = new Array();
                for(var i in groups) {
                    var opt = new DropDownOption(groups[i], i, function (event) {select.selectOption(event, this.v, this.t);});
                    options.push(opt);
                }
                
                select.setOptions(options);
                select.init();
                cont.appendChild(select.get());
            }
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
    this.add_to_group = function (photo_id, group_id, link, btn) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_to_group',
            data: {
                photo_id: photo_id,
                group_id: group_id
            },
            onSuccess: function (res) {
                oThis.groups_popup.showMessage(res);
                setTimeout(function () {btn.disabled = false;}, 2000);
            },
            onFailure: function (res) {
                oThis.groups_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 2000);
            }
        };
        myAJAX(request);
        
    }
	
	this.showRemoveAllPhotos = function () {
        this.groups_popup && this.groups_popup.hide() && (delete this.groups_popup);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_REMOVE_ALL_PHOTOS_BTN'], 'dialog-std');
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_REMOVE_ALL_PHOTOS_BTN'], 'input-submit', function () {this.disabled = true; oThis.remove_all_favorites(this)} ),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.friends_popup;} ) ];
        }
        
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_REMOVE_ALL_PHOTOS_CONFIRM']));
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
        this.remove_all_favorites = function (btn) {
            var request = {
                url: '/photos/ajax_handle.php?action=remove_all_favorites',
                data: {},
                onSuccess: function (res) { location.reload(); },
                onFailure: function (res) { oThis.groups_popup.showError(res); btn.disabled = false; }
            };
			
            myAJAX(request);
        }
    
    
    
    this.ShowCollections = function (photo_id, link) {
        this.groups_popup && this.groups_popup.hide() && (delete this.groups_popup);
        
        var select = new DropDown(tr_popup['PHOTOS_SELECT_COLLECTION'], 'dropdown-std', null);
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_ADD_TO_COLLECTION'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_ADD_TO_COLLECTION_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_to_collection(photo_id, select.getValue(), link, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div'),
                add_elements = new Array(),
                add_coll = document.createElement('input');
            
            add_coll.className = 'input-std';
            add_coll.value = tr_popup['PHOTOS_CREATE_COLLECTION'];
            add_coll.onfocus = function () {if (this.value == tr_popup['PHOTOS_CREATE_COLLECTION']) this.value = '';};
            add_coll.onblur = function () {if (this.value == '') this.value = tr_popup['PHOTOS_CREATE_COLLECTION'];};
            add_coll.onkeydown = function (event) {var value = this.value;oThis.get_key(event, function () {oThis.add_photo_collection(value, select)});};
            
            add_elements.push(add_coll);
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_ADD_TO_COLLECTION_DESC']));
            
            var options = new Array();
            for(var i in collections) {
                var opt = new DropDownOption(collections[i].title, i, function (event) {select.selectOption(event, this.v, this.t);});
                options.push(opt);
            }
            
            select.setOptions(options);
            select.init();
            select.addElements(add_elements);
            cont.appendChild(select.get());
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
    
    this.add_photo_collection = function (title, select) {
         var request = {
            url: '/photos/ajax_handle.php?action=add_collection',
            data: {
                title: title
            },
            onSuccess: function (res) {
                var cat_id = eval('('+res+')');
                select.addOption(title, cat_id, function (event) {select.selectOption(event, this.v, this.t);});
                select.selectOption (null, cat_id, title);
                
                if (!collections) collections = {};
                
                collections[cat_id] = {"title": title};
            }
        };
        myAJAX(request);
    }
    
    this.add_to_collection = function (photo_id, coll_id, link, btn) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_to_collection',
            data: {
                photo_id: photo_id,
                coll_id: coll_id
            },
            onSuccess: function (res) {
                oThis.groups_popup.showMessage(res);
                oThis.groups_popup.removeButtons();
                oThis.groups_popup.addButton(new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ));
            },
            onFailure: function (res) {
                oThis.groups_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 2000);
            }
        };
        myAJAX(request);
    }
    
    this.showHideSpam = function (obj, link) {
        var n = obj;
        
        do n = n.nextSibling;
        while (n && n.nodeType != 1);
        
        if (n.style.display == 'none' || n.style.display == '') {
            link.className = 'hide-btn';
            show(n);
            fadeIn(n, 50);
        } else {
            link.className = 'show-btn';
            fadeOut(n, 50, function () {hide(n);});
        }
    }
    
    this.ShowPromo = function (photo_id, link) {
        if (this.groups_popup) {
            this.groups_popup.hide();
            delete this.groups_popup;
        }
        
        this.groups_popup = new PopupWindow(tr_popup['PHOTOS_SUGGEST_PROMO_TITLE'], 'dialog-std');
        
        
        function buttons() {
            return [new Button(tr_popup['PHOTOS_SUGGEST_PROMO_BTN'], 'input-submit', function () {
                        this.disabled=true;
                        oThis.add_to_promo(photo_id, link, this);
                    }),
                    new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ) ];
        }
        function content() {
            var cont = document.createElement('div');
            
            cont.appendChild(document.createTextNode(tr_popup['PHOTOS_SUGGEST_PROMO_DESC']));
            
            return cont;
        }
        
        this.groups_popup.display($('main_wraper'), content, buttons, 'center');
    }
    this.add_to_promo = function(photo_id, link, btn) {
        var request = {
            url: '/photos/ajax_handle.php?action=add_to_promo',
            data: {
                photo_id: photo_id
            },
            onSuccess: function (res) {
                oThis.groups_popup.showMessage(res);
                oThis.groups_popup.removeButtons();
                oThis.groups_popup.addButton(new Button(tr_popup['PHOTOS_CLOSE_BTN'], 'input-button', function () {oThis.groups_popup.hide(); delete oThis.groups_popup;} ));
            },
            onFailure: function (res) {
                oThis.groups_popup.showError(res);
                setTimeout(function () {btn.disabled = false;}, 1000);
            }
        };
        myAJAX(request);
    }
    
    
    
    this.fadeOut = function(obj, opacity, onComplete) {
        
        if (opacity < 20) {
            if (obj) obj.style.display = 'none';
            if (onComplete) onComplete();
        } else {
            oThis.setOpacity(obj, opacity);
            opacity -= 10;
            setTimeout(
                function () {
                    oThis.fadeOut(obj, opacity, onComplete);
                }, 150);
        }
    }
    
    this.fadeIn = function(obj, opacity, onComplete) {
        
        if (opacity > 100) {
            oThis.setOpacity(obj, 100);
            if (onComplete) onComplete();
        } else {
            oThis.setOpacity(obj, opacity);
            opacity += 10;
            setTimeout(
                function () {
                    oThis.fadeIn(obj, opacity, onComplete);
                }, 150);
        }
    }
    
    this.setOpacity = function (obj, opacity) {
        obj.style.opacity = opacity / 100;
        obj.style.filter = 'alpha(opacity=' + opacity + ')';
    }
    
    this.get_key = function(e, okFunc) {
        if (!e) e = window.event;
        
        var asc = !e.keyCode ? (!e.which ? e.charCode : e.which) : e.keyCode;
        
        if (asc != 17) {
            // натиснат е клавиш
            if (asc == 13 || (this.is_control_pressed && asc == 83)) {
                // CTRL + ENTER или CTRL+S
                this.is_control_pressed = false;
                if(okFunc) okFunc();
            }
        } else if (asc == 17) {
            this.is_control_pressed = 1;
        } else {
            this.is_control_pressed = false;
        }
        
        
    }
}