/*
 * TVKaista Feedback form
 * - Dependent on boxy
 */
$(function() {
    var contactBoxy;

    var feedbackCategories = [
        _('feedback_question'),
        _('feedback_feedback'),
        _('feedback_idea'),
        _('feedback_problem'),
        _('feedback_praise'),
        _('feedback_other')
    ];

    function boxyContent( subject, message, options, error ) {
        var categoyOptions = [];
        for (var i=0, l=options.length; i<l; i++) {
            categoyOptions.push('<option value="' + options[i] + '">' + options[i] + '</option>');
        }
        return [
            '<div style="width:300px; height:300px">',
                error,
                '<form id="feedback">',
                    '<p style="font-size: 12px">',
                        _('feedback_subject'),':<br />',
                        '<input type="text" name="subject" id="subject" style="width: 290px;"',
                            ( subject !== '' ? ' value="' + subject + '"' : '' ), ' />',
                    '</p>',
                    '<p style="font-size: 12px">',
                        _('feedback_category'),':<br />',
                        '<select id="category" name="category" style="width: 295px;">',
                            categoyOptions.join(''),
                        '</select>',
                    '</p>',
                    '<p style="font-size: 12px">',
                        _('feedback_message'),':<br />',
                        '<textarea name="message" id="message" style="width: 290px;" rows="5">',
                            message,
                        '</textarea>',
                    '</p>',
                    '<br />',
                    '<input type="submit" name="submit" value="',
                        _('general_send'),
                    '" />',
                    '<button class="close" style="margin-left: 172px; width: 60px;">',
                        _('general_cancel'),
                    '</button>',
                '</form>',
            '</div>'].join('');
    }

    function getBrowserInfo() {
        var info = '';

        info = info + 'Screen resolution: ' + screen.width + 'x' + screen.height + '\n';
        if(navigator) {
            info = info + 'Browser: ' + navigator.appName + ' (' + navigator.appVersion + ')' + '\n';
            info = info + 'User agent: ' + navigator.userAgent + '\n';
            if(navigator.mimeTypes["application/x-shockwave-flash"]) {
                info = info + 'Flash MIME handler: Yes';
                if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
                    info = info + ' (' + navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin.description + ')';
                }
                info = info + '\n';
            }
            if(navigator.plugins["VLC Multimedia Plug-in"]) {
                info = info + 'VLC plugin: ' + navigator.plugins["VLC Multimedia Plug-in"].description + '\n';
            }
            if(navigator.mimeTypes["application/x-vlc-plugin"]) {
                info = info + 'VLC MIME handler: Yes';
                if(navigator.mimeTypes["application/x-vlc-plugin"].enabledPlugin){
                    info = info + ' (' + navigator.mimeTypes["application/x-vlc-plugin"].enabledPlugin.description + ')';
                }
                info = info + '\n';
            }
            if(navigator.mimeTypes["video/mp4"]) {
                info = info + 'MPEG4 MIME handler: Yes';
                if(navigator.mimeTypes["video/mp4"].enabledPlugin){
                    info = info + ' (' + navigator.mimeTypes["video/mp4"].enabledPlugin.description + ')';
                }
                info = info + '\n';
            }
            info = info + 'View cookie: ' + $.cookie('format') + '\n';
            info = info + 'Preferred servers cookie: ' + $.cookie('preferred_servers') + '\n';
        }

        // Remove any tags
        return info.replace(/<\/?[^>]+(>|$)/g, '');
    }

    $('.sendfeedback, .reportproblem').live( 'click', function() {
        contactBoxy = new Boxy( boxyContent( '', '', feedbackCategories, '' ), {
            title: _('feedback_send_feedback'),
            draggable: false,
            modal: true,
            closeText: _('feedback_close_txt'),
            behaviours: function(c) {
                c.find('#feedback').submit( function() {
                    Boxy.get(this).setContent('<div style="width: 300px; height: 300px">' + _('feedback_sending') + '</div>');
                    if ( c.find("input[name='subject']").val() == '' || c.find("textarea[name='message']").val() == '') {
                        contactBoxy.setContent( boxyContent(
                                c.find("input[name='subject']").val(),
                                c.find("textarea[name='message']").val(),
                                feedbackCategories,
                                '<div style="color: red;">' + _('feedback_subject_and_message_required') + '</div>'
                            ) );
                    } else {
                        var browserInfo = getBrowserInfo();
	                    TVKaistaCentreServer.sendFeedback(
	                        c.find("input[name='subject']").val(),
	                        c.find("select[name='category']").val(),
	                        c.find("textarea[name='message']").val() + '\n--\n' + browserInfo, {
	                            callback: function() {
	                                    contactBoxy.setContent( [
	                                        '<div style="width: 300px; height: 300px">',
	                                            _('feedback_thank_you'),
	                                            '<div style="margin-top: 30px; text-align: center;">',
	                                                '<button class="close">',
	                                                    _('feedback_close_window'),
	                                                '</button>',
	                                            '</div>',
	                                        '</div>'].join('') );
	                            }
	                        });
                   }
                   return false;
                });
            }
        });

        // Adds a report problem link to programs
        // Autofills some fields
        if( $(this).hasClass('reportproblem') ) {
            $("select[name='category']").val(_('feedback_problem'));
            // Read name and date from page. Nice chained way of getting the text of an element with children.
            var programName = $('a','#pid' + $(this).attr('id').substr(3) ).text();
            var programDate = $.trim( $('#toolbarcalendar').html() );
            $("input[name='subject']").val(_('recordings_recording') + ': ' + $(this).attr('id').substr(3) + " (" + $('#format').val() + ") " + programName + " (" + programDate + ")" );
        }
        return false;
    });
});

