/*
 * Copyright(c) 2009
 *
 */

// create application
bh.behaviour = function() {

    // do NOT access DOM from here; elements don't exist yet

    // private variables

    // private functions

    // public space
    return {
        // public properties, e.g. strings to translate

        // public methods
        init : function() {
            this.create_flv_players();
            this.create_video_links();
            this.autocomplete();
        },

        /*
        * create player instances in page.
        */
        create_flv_players: function(){
            $('.mediaplayer').each(function(cnt){
                var id = $(this).attr('id');
                var file = $(this).attr('data-file');
                var image = $(this).attr('data-preview');
                var screencolor = $(this).attr('data-screencolor');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                createPlayer(id, file, "false", image, screencolor, "450", height);
                if(window.console) console.log("Player " + id + " found, File " + file + " loaded");
            });
        },
        create_video_links: function(){
            $('a.videolink').click(function(){
                var playerID = $(this).attr("data-player");
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var preview = $(this).attr('data-preview');
                var player = document.getElementById(playerID);
                var file = player.getPlaylist()[0].file;
                player.sendEvent("STOP");
                var url = "/live/internet/satellitewin.php?file=" + file + "&width=" + width + "&height=" + height + "&image=" + preview;
                var popWin = window.open(url, "VIDEO", "width=" + width + ",height=" + height + ",left=100,top=200,resizableno");
                if(window.popWin) popWin.focus();
            });
        },
        autocomplete: function(){
            $("#ac_query").autocomplete('/live/internet/autocomplete.php', {
                extraParams: {
                    query: function(){ return $("#ac_query").val(); },
                    indexID: 36,
                    addPossibleHitCount: false,
                    addThesaurusTerms: false
                },
                /* width: 182, */
                minChars: 3,
                max: 20,
                multiple: false,
                matchContains: true,
                selectFirst: false,
                formatItem: function(item) {
                    return item.value;
                },
                parse: function(xml) {
                    var rows = new Array();
                    $(xml).find('term').each(function(i){
                        var value = $(this).attr('value');
                        var field = $(this).attr('field');
                        var occurence = $(this).attr('occurence');
                        var data = {field: field, occurence: occurence, value: value};
                        rows[i] = {
                            data: data,
                            value: value,
                            result: value
                        };
                    });
                    return rows;
                }
            });
        }

    }
}(); // end of app

// end of file
