var abrutis_player = {
    width: 601,
    height: 473,
    target_container: "pub_player_container",
    target: "abrutis-player-div",
    target_pub: "abrutis-player-pub",
    pub_file: "http://www.abrutis.com/img/player_banners/a7505ef3819be9890c4b25556010bdd6.gif",
    pub_link: "http://acces.direction-x.com/a.php?t=29&o=if&pgid=4&tc1=abrutis&tc2=300x250&n=24&pg_b_format=300x250&"
};

var currentState = "NONE";
var previousState = "NONE";
var player = null;
var link_pub = null;
abrutis_player.pub = new Image();
abrutis_player.pub.src = abrutis_player.pub_file;

var displayPub = function() {
    var mg_left = ( abrutis_player.width - abrutis_player.pub.width ) / 2;
    var mg_top = ( abrutis_player.height - abrutis_player.pub.height ) / 2;

    link_pub = document.createElement( "a" );
    link_pub.href = abrutis_player.pub_link;
    link_pub.target = "_blank";

    document.getElementById( abrutis_player.target_container ).insertBefore( link_pub, document.getElementById( "mediaspace" ) ) ;

    var img_pub = document.createElement( "img" );
    img_pub.width = abrutis_player.pub.width;
    img_pub.height = abrutis_player.pub.height;
    img_pub.src = abrutis_player.pub.src;
    img_pub.border = 0;
    img_pub.style.position = "absolute";
    img_pub.style.left = Math.round( mg_left ) + "px";
    img_pub.style.marginTop = Math.round( mg_top ) + "px";
    link_pub.appendChild( img_pub );
};

var hidePub = function() {
    if( link_pub ) {
        document.getElementById( abrutis_player.target_container ).removeChild( link_pub ) ;
        link_pub = null;
    }
};

var playerReady = function(p) {
    player = document.getElementById(p.id);
    addListeners();
};

var addListeners = function() {
    if (player) {
        player.addModelListener("STATE", "stateListener");
    } else {
        setTimeout("addListeners()", 100);
    }
};

var stateListener = function(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;
    if( ( currentState == "COMPLETED" ) ) {
        //displayPub();
    } else {
        hidePub();
    }
};
