function attachment( url, wysiwyg )
{
        this.url = url;
        this.req = new Object();
        this.win = '';
        this.attach = '';
        this.wysiwyg = wysiwyg;
        this.video_buttons = ['favourite_videos', 'playlist', 'videos'];
        this.photo_buttons = ['favourite_photos', 'photoalbums'];


        this.showPhotoPanel = function()
        {
                $("#attach_video").hide();
                $("#attach_photo").show(400);
                this.showAlbums();
                return false; 
        }

        this.closePhotoPanel = function()
        {
                $("#attach_photo").hide();
                return false;
        }

        this.closeVideoPanel = function()
        {
                $("#attach_video").hide();
                return false;
        }

        this.hidePhotoPanel = function()
        {
                for(var i in this.photo_buttons) $( '#' + this.photo_buttons[ i ] ).css('font-weight', 'normal');
                return false;
        }

        this.showVideoPanel = function()
        {
                $("#attach_photo").hide();
                $("#attach_video").show(400);
                this.showVideos();
                return false;
        }

        this.hideVideoPanel = function()
        {
                for(var i in this.video_buttons) $( '#' + this.video_buttons[ i ] ).css('font-weight', 'normal');
                return false;
        }

        this.getMedia = function( url, params, type )
        {
                if( !url ) return false;
                $( "#attachment_container" ).html( "" );
                params[ 'wysiwyg' ] = this.wysiwyg;
                if(type=='video') empty = 'No video';
                else if(type=='photo') empty = 'No photo';
                sendRequest( this.req, 
                             url, 
                             params, 
                             function()
                             {
                                $("#ajax_loading").show();
                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                if( data.content ) $( "#"+type+"_attachment_container" ).html( data.content );
                                else $( "#"+type+"_attachment_container" ).html( "<div class=\"box\">" + empty + "</div>" );
                                $( "#"+type+"_attachment_pagination" ).html( data.pagination );
                                $("#message_result").show();
                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                $("#attachment_error").show();
                             });
        }

        this.showFavouritePhotos = function( page )
        {
                if( !page ) page = 1;
                this.hidePhotoPanel();
                $( '#favourite_photos' ).css('font-weight', 'bold');

                this.getMedia(this.url, {type: 'favourite_photos', page: page}, 'photo');
                return false;
        }

        this.showAlbums = function( page )
        {
                if( !page ) page = 1;
                this.hidePhotoPanel();
                $( '#photoalbums' ).css('font-weight', 'bold');
                this.getMedia(this.url, {type: 'albums', page: page}, 'photo');
                return false;
        }

        this.showPhotos = function( album_id, page )
        {
                if( !album_id ) return false;
                if( !page ) page = 1;
                this.hidePhotoPanel();
                $( '#photoalbums' ).css('font-weight', 'bold');
                this.getMedia(this.url, {type: 'photos', album_id: album_id, page: page}, 'photo');
                return false;
        }

        this.showFavouriteVideos = function( page )
        {
                if( !page ) page = 1;
                this.hideVideoPanel();
                $( '#favourite_videos' ).css('font-weight', 'bold');
                this.getMedia(this.url, {type: 'favourite_videos', page: page}, 'video');
                return false;
        }

        this.showVideos = function( page )
        {
                if( !page ) page = 1;
                this.hideVideoPanel();
                $( '#videos' ).css('font-weight', 'bold');
                this.getMedia(this.url, {type: 'videos', page: page}, 'video');
                return false;
        }

        this.showPlaylist = function( page )
        {
                if( !page ) page = 1;
                this.hideVideoPanel();
                $( '#playlist' ).css('font-weight', 'bold');
                this.getMedia(this.url, {type: 'playlist', page: page}, 'video');
                return false;
        }

        this.addPhotoTag = function( id )
        {
                $("#message").attr('value', $("#message").attr('value') + "&nbsp;&nbsp;[photo]" + id + "[/photo]&nbsp;&nbsp;");
        }

        this.addVideoTag = function( id )
        {
                $("#message").attr('value', $("#message").attr('value') + "&nbsp;&nbsp;[video]" + id + "[/video]&nbsp;&nbsp;");
        }

        this.bindPhoto2Message = function( id )
        {
                if( !id ) return false;
                $("#photo_id").attr('value', id);
        }

        this.bindVideo2Message = function( id )
        {
                if( !id ) return false;
                $("#video_id").attr('value', id);
        }

        this.unbindPhotoFromMessage = function()
        {
                $("#photo_id").attr('value', '');
        }

        this.unbindVideoFromMessage = function()
        {
                $("#video_id").attr('value', '');
        }

        /**
                Blog
        */

        this.wysiwyg_init = function()
        {
                tinyMCEPopup.requireLangPack();

                this.attach = {
                        init : function(ed) {
                                tinyMCEPopup.resizeToInnerSize();
                        },
                        attachPhoto : function( src, id ) {
                                var ed = tinyMCEPopup.editor, dom = ed.dom;
                                tinyMCEPopup.execCommand('mceInsertPhoto', false, dom.createHTML('img', {
                                        id : "photo_"+id,
                                        src : src
                                }));
                                tinyMCEPopup.close();
                        },
                        attachVideo : function( src, id ) {
                                var ed = tinyMCEPopup.editor, dom = ed.dom;
                                tinyMCEPopup.execCommand('mceInsertVideo', false, dom.createHTML('img', {
                                        id : "video_"+id,
                                        src : src
                                }));
                                tinyMCEPopup.close();
                        }
                };

                tinyMCEPopup.onInit.add(this.attach.init, this.attach);
        }

        this.attachPhoto = function( src, url, id, title )
        {
                //var element = tinyMCE.activeEditor.dom.createHTML('img', {id : "photo_"+id, src : src, alt: title, href : url});
                //var link = tinyMCE.activeEditor.dom.createHTML('a', {href : url, alt: title}, '');
                //var res = tinyMCE.activeEditor.dom.add(link, element);
                //tinyMCE.activeEditor.selection.setContent( element );
                tinyMCE.execCommand('mceInsertContent',false, "<a id='linkphoto_" + id + "' href='" + url + "'><img id='photo_" + id + "' src='" + src + "' alt='" + title + "'></a>");
        }

        this.attachVideo = function( src, url, id, title )
        {
                //var element = tinyMCE.activeEditor.dom.createHTML('img', {id : "video_"+id, src : src, alt: title, href : url});
                //var link = tinyMCE.activeEditor.dom.createHTML('a', {href : url, alt: title});
                //tinyMCE.activeEditor.selection.setContent( element );
                tinyMCE.execCommand('mceInsertContent',false, "<a id='linkvideo_" + id + "' href='" + url + "'><img id='video_" + id + "' src='" + src + "' alt='" + title + "'></a>");
        }
}
