function video(id, report_spam_url, delete_url, delete_comment_url, add_comment_url, comments_url, update_url, popup, console_adverts, upload_url, patterns)
{
        this.id = id;
        this.report_spam_url = report_spam_url;
        this.delete_url = delete_url;
        this.delete_comment_url = delete_comment_url;
        this.add_comment_url = add_comment_url;
        this.comments_url = comments_url;
        this.update_url = update_url;
        this.popup = popup;
        this.console_adverts = console_adverts;
        this.upload_url = upload_url;
        this.patterns = patterns;

        this.req = new Object();
        this.comment_req = new Object();
        this.element = "download_video";
        this.popup_win;

        this.show = function()
        {
                var win = $( "#" + this.element );
                if( win.css('display') == 'none' || !win.css('display') )
                {
                        var blocks = ['favorite_video','share_video','report_video'];
                        for(var i in blocks ) $( "#" + blocks[ i ] ).hide();
                        win.show();
                }
                else win.hide();
                return false;
        };

        this.hide = function()
        {
                $( "#" + this.element ).hide();
                return false;
        }


        this.deleteVideo = function()
        {
                showConfirmDialog( $("#delete_video_question").text(), function(){
                        sendRequest( video.req, 
                                         video.delete_url, 
                                         {id: video.id},
                                         function()
                                         {
                                                $("#ajax_loading").show();
                                         },
                                         function( data )
                                         {
                                                $("#ajax_loading").hide();
                                                showMessageFromDiv("video_deleted");
                                         },
                                         function( data )
                                         {
                                                $("#ajax_loading").hide();
                                                showMessageFromDiv("deleting_failed");
                                         });
                });
        }

        this.reportSpam = function ( id )
        {
                if( !id ) return false;
                showConfirmDialog($( "#report_confirm" ).text(), function(){
                sendRequest( video.req,
                             video.report_spam_url,
                             {id: id, type: 'VIDEO'},
                             function()
                             {
                                $("#ajax_loading").show();
                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                $("#report_" + id).hide();
                                showMessageFromDiv("reporting_success");

                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                showMessageFromDiv("reporting_failed");
                             });
                });
                return false;
        }

        this.sendComment=function()
        {
                var message = $("#message").attr( 'value' );
                if(message) sendRequest( video.req, 
                                         this.add_comment_url, 
                                         {id: this.id, message: message}, 
                                         function()
                                         {
                                                $("#ajax_loading").show();
                                         },
                                         function( data )
                                         {
                                                $("#ajax_loading").hide();
                                                $("#message").attr( 'value', '' );
                                                if( comment ) comment.checkCharacterCount();
                                                video.updateCommentsList( data );
                                         },
                                         function( data )
                                         {
                                                $("#ajax_loading").hide();
                                                showMessageFromDiv("message_error");
                                         });
        }

        this.deleteComment=function( id )
        {
                if( !id ) return false;
                showConfirmDialog( $( "#delete_comment_question" ).text(), function(){
                sendRequest( video.comment_req, 
                             video.delete_comment_url, 
                             {id: id}, 
                             function() {
                                        $("#ajax_loading" + id).show();
                             },
                             function( data ) {
                                        $("#ajax_loading" + id).hide();
                                        $("#comment_" + id).hide();
                                        //video.updateCommentsList( data );
                             },
                             function( data ) {
                                        $("#ajax_loading" + id).hide();
                                        showMessageFromDiv( "delete_comment_failed" );
                             });
                });
        }

        this.updateCommentsList = function( data )
        {
                if( !data ) return false;
                this.showCommentsList( data.comments, data.count );
                this.showPagination( data.pagination );
        }

        this.showPagination = function( pages )
        {
                if( !pages ) return false;
                var container = $("#pagination");
                if( !container ) return false;
                container.empty();
                container.append( pages );
        }

        this.showCommentsList = function( comments, count )
        {
                if( !comments || !count) return false;
                var container = $("#comments");
                if( container )
                {
                        container.empty();
                        container.append( comments );
                }
                var container = $("#comments_count");
                if( container )
                {
                        container.empty();
                        //container.append( count );
                        container.text( '('+count+')' );
                        container.show();
                }
        }

        /**
                Upload video
        */

        this.submitUploadForm = function( id, ignore_file_checking )
        {
                var mode = this.getMode();
                if( mode == 'download' || mode == 'remote_upload' ) ignore_file_checking = true;
                var title = $("#title").attr('value');
                if( !title ) 
                {
                        showMessage( "Title is empty" );
                        return false;
                }
                var tags = $("#tags").attr('value');
                if( !tags ) 
                {
                        showMessage( "Tags is empty" );
                        return false;
                }
                var description = $("#description").attr('value');
                if( !description ) 
                {
                        showMessage( "Description is empty" );
                        return false;
                }
                var download_url = '';
                if( mode == 'download' ) download_url = trim( $("#download_url").attr('value') );
                else if( mode == 'remote_upload' ) download_url = trim( $("#remote_upload_url").attr('value') );
                if( mode == 'download' || mode == 'remote_upload' )
                {
                        if( !download_url ) 
                        {
                                showMessageFromDiv( 'empty_url' );
                                return false;
                        }
                }
                if( download_url )
                {
                        if( mode == 'download' )
                        {
                                if( !this.patterns )
                                {
                                        showMessageFromDiv( 'incorrect_url' );
                                        return false;
                                }
                                var match_res = false;
                                for( var i in this.patterns )
                                {
                                        var regexp = new RegExp( this.patterns[ i ], 'gi' );
                                        if( regexp.test( download_url ) )
                                        {
                                                match_res = true;
                                                break;
                                        }
                                }

                                if( !match_res )
                                {
                                        showMessageFromDiv( 'incorrect_url' );
                                        return false;
                                }
                        }
                        if( mode == 'remote_upload' )
                        {
                                if( !checkVideoFile( download_url ) )
                                {
                                        showMessageFromDiv( 'incorrect_url' );
                                        return false;
                                }

                                if( !checkURL( download_url ) )
                                {
                                        showMessageFromDiv( 'incorrect_url' );
                                        return false;
                                }
                        }
                }

                if( !ignore_file_checking )
                {
                        var file = $("#upload_file").attr('value');
                        if( !file ) 
                        {
                                showMessage( "Please select video file" );
                                return false;
                        }
                        if(!checkVideoFile( file ))
                        {
                                showMessage( "Incorrect file extension" );
                                return false;
                        }
                }
                var category_id = $("#category_id").val();
                if( !category_id )
                {
                        showMessage( "Please select category" );
                        return false;
                }
                if( $( "#id" ).attr( 'value' ) || mode == 'download' || mode == 'remote_upload' )
                {
                        $( "#upload_form" ).removeAttr( 'action' )
                                           .removeAttr( 'target' )
                                           .attr( 'action', '' )
                                           .attr( 'target', '' )
                                           .submit(); 
                        return true;
                }
                else
                {
                        uploader.startUpload();
                        return false;
                }
                
        }

        /**
                Download video
        */
        this.downloadFile = function( file )
        {
                if( !file ) return false;
                var iframe = $( "<iframe src=\"" + file + "\"></iframe>" );
                iframe.attr( 'src', file );
                iframe.appendTo( $( document.body ) );
        }

        /**
                Load comments
        */
        this.loadVideoComments = function( page )
        {
                sendRequest( this.req, 
                             this.comments_url, 
                             {id: this.id, page: page}, 
                             function()
                             {
                                $("#ajax_loading").show();
                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                video.updateCommentsList( data );
                             },
                             function( data )
                             {
                                $("#ajax_loading").hide();
                                showMessageFromDiv( "error_loading_comments" );
                             });
                return false;
        }

        this.updateVideo = function( popup_id, console_adv )
        {
                var anticheat_params = 'null';
                if( anticheat ) anticheat_params = anticheat.getBrowserParam();
                sendRequest( this.req, 
                             this.update_url, 
                             {id: this.id, popup_id: popup_id, console_adv: console_adv, anticheat: anticheat_params},
                             function() {},
                             function( data ) {
                                //$( "#views_count" ).text( parseInt($( "#views_count" ).text()) + 1 );
                                $( "#views_count" ).text( data.views_count );
                             },
                             function( data ) {});
                return false;
        }

        this.showAdv = function()
        {
                var adv = '';
                var console_adv = '';

                for( var i in this.console_adverts ) console_adv +=  (console_adv ? '|' : '') + this.console_adverts[ i ];

                for( var i in this.popup )
                {
                        if( this.popup[ i ].type == 'popup' ) var conf = 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1';
                        else if( this.popup[ i ].type == 'cross_popup' ) var conf = 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=5px,height=5px';
                        //else if( !this.popup[ i ].type || this.popup[ i ].type == 'undefined') 

                        var show = getCookie( this.popup[ i ].name );
                        if( !show )
                        {
                                this.popup_win = window.open(this.popup[ i ].url, '_blank', conf);
                                window.focus();
                                this.popup_win.blur();
                                adv = this.popup[ i ].id;
                                var today = new Date();
                                today.setTime( today.getTime() ); 
                                var expires_day = new Date(today.getTime() + (1000 * 60 * 60 * this.popup[ i ].freq));
                                setCookie(this.popup[ i ].name, "1", expires_day.toGMTString(), "/", '', '');
                                break;
                        }
                }
                if( video ) video.updateVideo( adv, console_adv )
        }

        this.getMode = function()
        {
                return $(":radio[name=mode]").filter(":checked").val();
        }

        this.changeMode =function()
        {
                switch( this.getMode() )
                {
                        case 'upload':
                                $( "#download_slots" ).hide();
                                $( "#remote_upload_slots" ).hide();
                                $( "#upload_slots" ).show();
                                $( "#video_type" ).attr('value', 'UPLOADED');
                                $( "#upload_form" ).attr( 'target', 'upload_iframe' );
                        break;
                        case 'download': 
                                $( "#upload_slots" ).hide();
                                $( "#remote_upload_slots" ).hide();
                                $( "#download_slots" ).show();
                                $( "#video_type" ).attr('value', 'DOWNLOADED');
                                $( "#upload_form" ).attr( 'target', '' );
                        break;
                        case 'remote_upload': 
                                $( "#upload_slots" ).hide();
                                $( "#download_slots" ).hide();
                                $( "#remote_upload_slots" ).show();
                                $( "#video_type" ).attr('value', 'REMOTE_UPLOADED');
                                $( "#upload_form" ).attr( 'target', '' );
                        break;
                }
                return false;
        }
}

