function getUpdate(typ,pr1,pr2,pid) {
    $.log(pid + '/' + typ + ':' + pr1);
    getUpdate.setValue(pid, typ, pr1);
    /*if(typ == "time") { getUpdate.setValue(pid, 'time', pr1); }
    else if(typ == "volume") { getUpdate.setValue(pid, 'volume', pr1); }
    else if(typ == "item") { getUpdate.setValue(pid, 'item', pr1); }
    else if(typ == "state") { getUpdate.setValue(pid, 'state', pr1); }*/
}

getUpdate.setValue = function(pid, name, val) {
    if(typeof getUpdate.vals == 'undefined') getUpdate.vals = {};
    if(!getUpdate.vals[pid]) getUpdate.vals[pid] = {};
    getUpdate.vals[pid][name] = val;
}

getUpdate.getValue = function(pid, name) {
    if(typeof this.vals == 'undefined') return undefined;
    if(!this.vals[pid]) return undefined;
    return this.vals[pid][name];
}

SWFPlayer = function() {
    this.name = '';
    this.ready = false;
    this.started = false;
    this.state = {
        state: 0,
        time: 0,
        item: 0,
        volume: 85
    };
    this.volumeX = 0;
    this.volumeY = 0;
    this.volume_width = 0;
}

SWFPlayer.prototype = {
    create: function(elem_id, player_url, name, play_list, auto_play/*, play_item, volume, autostart*/) {
		var so = new SWFObject(player_url,name,'1','1','8');
		so.addParam("allowfullscreen","true");
		so.addVariable("file",play_list);
		so.addVariable("enablejs","true");
		so.addVariable("javascriptid",name);
		so.addVariable("displayheight","1");
		so.addVariable("repeat","true");
		so.addVariable("shuffle","false");
		so.write(elem_id);
        this.name = name;
        this.ready = false;
        this.started = false;
        this.initEvents();
        this.loadState();
        if(!this.loadState() && auto_play) this.state.state = 1;
    },
    
    initEvents: function() {
        var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
        if (root){
            var oThis = this;
            var func = function() { oThis.onUnload(); }
            if (root.addEventListener) root.addEventListener("unload", func, false);
            else if (root.attachEvent) root.attachEvent("onunload", func);
        }
    },
    
    addVolumeControl: function(id, control_width) {
        this.volume_width = control_width;
        var oThis = this;
        $('#' + id).mousemove(function(evt) { oThis.volumeX = evt.clientX; oThis.volumeY = evt.clientY;}).
            click(function() { 
                var volume = Math.round((oThis.volumeX - $(this).coords().x) / oThis.volume_width * 100);
                oThis.setVolume(volume);
            });
    },
    
    onUnload: function() {
        this.saveState();
    },
    
    saveState: function() {
        var state = getUpdate.getValue(this.name, 'state');
        if(typeof state == 'undefined') return;
        setCookie('play_' + this.name, state, 0, '/');
        var time = getUpdate.getValue(this.name, 'time');
        if(typeof time != 'undefined')
            setCookie('starttime_' + this.name, time, 0, '/');
        var item = getUpdate.getValue(this.name, 'item');
        if(typeof item != 'undefined')
            setCookie('item_' + this.name, item, 0, '/');
        var volume = getUpdate.getValue(this.name, 'volume');
        if(typeof volume != 'undefined')
            setCookie('volume_' + this.name, volume, 0, '/');
    },
    
    loadState: function() {
        var state = getCookie('play_' + this.name);
        if(state == null) return false;
        $.log('startup state: ' + state);
        this.state.state = parseInt(state) ? 1 : 0;
        var time = getCookie('starttime_' + this.name);
        if(time != null) this.state.time = time;
        var item = getCookie('item_' + this.name);
        if(item != null) this.state.item = item;
        var volume = getCookie('volume_' + this.name);
        if(volume != null) this.state.volume = volume;
        return true;
    },
    
    start: function() {
        this.setReady();
        $.log('startup state: ' + this.state.state);
        if(this.state.state) this.startFromState();
    },
    
    setReady: function() {
        if(typeof getUpdate.getValue(this.name, 'state') != 'undefined') {
            this.ready = true;
        } else {
            $.log('player not ready');
            var oThis = this;
            setTimeout(function() { oThis.setReady() }, 1000);
        }
    },

    startFromState: function() {
        this.started = true;
        if(this.ready) {
            $.log('start from state');
            this.setVolume(0);
            this.playItem(this.state.item);
            this.startUpPosition(this.state.time);
        } else {
            var oThis = this;
            setTimeout(function() { oThis.startFromState() }, 1000);
        }
    },
    
    startUpPosition: function(pos) {
        if(getUpdate.getValue(this.name, 'time') > 0) {
            this.setPosition(pos);
            this.setVolume(this.state.volume);
        } else {
            var oThis = this;
            setTimeout(function() { oThis.startUpPosition(pos) }, 1000);
        }
    },
    
    getPlayer: function() {
	    if($.browser.msie) {
			return window[this.name];
		} else {
			return document[this.name];
		}
    },
    
    playPause: function() {
        if(!this.started) {
            this.startFromState();
            return;
        }
        if(!this.ready) return;
        $.log('playpause');
        this.sendEvent('playpause');
    },
    
    setPosition: function(t) {
        if(!this.ready) return;
        $.log('Set position: ' + t);
        this.sendEvent('scrub', t);
    },
    
    nextTrack: function() {
        if(!this.ready) return;
        this.started = true;
        this.sendEvent('next');
    },
    
    prevTrack: function() {
        if(!this.ready) return;
        this.started = true;
        this.sendEvent('prev');
    },
    
    playItem: function(item) {
        if(!this.ready) return;
        this.started = true;
        this.sendEvent('playitem', item);
    },
    
    setVolume: function(volume) {
        if(!this.ready) return;
        $.log('Set volume: ' + volume);
        this.sendEvent('volume', volume);
    },
    
    sendEvent: function(typ, prm) {
        this.getPlayer().sendEvent(typ, prm);
    }
}

PlayerControls = function(player_name) {
    this.player_name = player_name;
    this.play_state = false;
    this.play_elem = null;
    this.play_img = null;
    this.pause_img = null;
    this.active_image = null;
    
    var oThis = this;
    setInterval(function() { oThis.tick(); }, 500);
}

PlayerControls.prototype = {
    tick: function() {
        this.updatePlayButton();
    },
    
    setPlayButton: function(jexpr, play_img, pause_img, state) {
        this.play_elem = $(jexpr).get(0);
        this.play_img = play_img;
        this.pause_img = pause_img;
        this.play_state = state;
        var oThis = this;
        if(play_img instanceof Array || pause_img instanceof Array) {
            $(this.play_elem).hover(
                function() {
                    if(oThis.active_image instanceof Array) this.src = oThis.active_image[1];
                },
                function() {
                    if(oThis.active_image instanceof Array) this.src = oThis.active_image[0];
                }
            );
        }
        this.active_image = state ? play_img : pause_img;
    },
    
    updatePlayButton: function()
    {
        if(!this.play_elem) return;
        var state = getUpdate.getValue(this.player_name, 'state') ? true : false;
        if(state == this.play_state) return;
        this.play_state = state;
        var play_img = this.play_img instanceof Array ? this.play_img[0] : this.play_img;
        var pause_img = this.pause_img instanceof Array ? this.pause_img[0] : this.pause_img;
        this.play_elem.src = state ? play_img : pause_img;
        this.active_image = state ? this.play_img : this.pause_img;
    }
}

