HDAChrome = {
  initialize: function() {
    this.hasReceivedNode=false;
    this.initialized=true
    this.videoOn=true
    this.buttons=new Array()
    this.controls=new Object()
    //var btns = this._getElementsByClassName('hda-button');
    /*for(var i=0; i < btns.length; i++) {
      this.buttons.push(btns[i])
    }*/
    // If we are using a table for the buttonset, add a void tr just to add the footer
 /*   bs = document.getElementById('hda-buttonset')
    tf = document.createElement('TBODY')
    tf.id='hda-buttonset-body'
    bs.appendChild(tf)
    if (bs.tagName=='tbody') {
      tf=document.createElement('tr')
      tf.id='hda-buttonset-bottom'
      tftd=document.createElement('td')
      tf.appendChild(tftd)
      bs.appendChild(tf)
    }
    //this._setupTextWidget()*/

    this._setupPlayerControls()
    //this._unsetMenu()
  },

  setupTextWidget: function(text) {
    if (!text)
        text=''
    if (text == '*')
        return;
    if (t=document.getElementById('hda-textArea')) {
      t.innerHTML=text
    }
  },

  // Partially from prototype.js
  _getElementsByClassName: function(className) {
    var children = document.body.getElementsByTagName('*');
    var elements=[]
    for (var i=0; i < children.length; i++) {
      if (children[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
        elements.push(children[i]);
    }
    return elements;
  },

  setupMenu: function(menuActions) {
  	return/*
    if (!this.initialized)
      return

    this._unsetMenu()

    if (!menuActions)
      return
    this.menuActions=menuActions
    for (var i=0; i< menuActions.length;i++){
    	this._createButton()
    }
    if (menuActions.length!=0){
    	this._createButton('footer');
    }
    for(var i=0; i < menuActions.length; i++) {
      this.buttons[i].innerHTML=menuActions[i].titl
      //this.buttons[i].style.backgroundPosition='0 0px'
      this.buttons[i].style.cursor='pointer'
      this.buttons[i].style.color='white'
      this.buttons[i].className = 'hdaMenuButton_' +i
      this.buttons[i].setAttribute('className','hdaMenuButton_' +i);
      this.buttons[i].onmouseover = function(e) {
        if (!e) e=event
        var t = e.target || e.srcElement;
        if (t.nodeType == 3) // defeat Safari bug
          t = t.parentNode;
        if (t.checked)
          return
        t.style.backgroundPosition='0 -57px'
        t.style.color = '#676868'
      }
      this.buttons[i].onmouseout = function(e) {
        if (!e) e=event
        var t = e.target || e.srcElement;
        if (t.nodeType == 3) // defeat Safari bug
          t = t.parentNode;
        if (t.checked)
          return
        t.style.backgroundPosition='0 0px'
        t.style.color = 'white'
      }
      this.buttons[i].onmousedown = function(e) {
        HDAChrome._deactivateMenu()
        if (!e) e=event
        var t = e.target || e.srcElement;
        if (t.nodeType == 3) // defeat Safari bug
          t = t.parentNode;
          if (t.className)
        		mv=HDAChrome.menuActions[t.className.replace('hdaMenuButton_','')];
          else
          	if(t.getAttribute('className')){
          		var str = t.getAttribute('className');
        		mv=HDAChrome.menuActions[str.replace('hdaMenuButton_','')];
        	}else
        		alert('Je trouve pas');

        if (mv.node || mv.rule) { // we want to keep the menu when opening an URL
          HDAChrome._deactivateMenu()
           t.style.backgroundPosition='0 0px'//'0px -126px'
          t.style.cursor='wait'
          t.checked=true
        }
        if (mv.node) {
          HDAPlayer.sendLoad(mv.node,null,mv.attr)
        } else if (mv.rule) {
          HDAPlayer.sendLoad(null,mv.rule,mv.attr)
        } else if (mv.url) {
          HDAChrome.openURL(mv.url)
        }
      }
    }*/
  },

 /* urlData is made of...
   *     url: the actual address to open
   *     target: the url target, can be any valid html target.
   *         a special value of "popup" means the url should be opened in a
   *         popup window
   *     popup: {
   *       width: the popup window width
   *       height: the popup window height
   *     }
   */
  openURL: function(urlData) {
    if (urlData.target=='popup') {
      popup=window.open(urlData.url,'name','height='+urlData.popup.height+',width='+urlData.popup.width);
      if (popup && window.focus) {popup.focus()}
    } else if (urlData.target=='self') {
      top.location.href = urlData.url;
    } else if (urlData.target=='sx') {
      top.frames['impersonate'].location.href = urlData.url;
    } else if (urlData.target=='parent') {
      document.location.href = urlData.url; // top.opener.location.href = urlData.url;
    } else if (urlData.target=='blank') {
      popup=window.open(urlData.url);
      if (popup && window.focus) {popup.focus()}
    } else if (urlData.target=='rien') {

    }
     else {
      var doc = top.frames[urlData.target];
      if (doc) {
        doc.location.href = urlData.url;
      } else {
        popup=window.open(urlData.url);
        if (popup && window.focus) {popup.focus()}
      }
    }
  },

  setupControlsOnVideoStart: function() {
    this.controls.cRepeat.activate()
    this.controls.cSkip.activate()
    this.controls.cStop.activate()
  },

  setupControlsOnVideoStop: function() {
    this.controls.cSkip.deactivate()
    this.controls.cStop.deactivate()
	if(!HDAChrome.videoOn) {
		this.controls.cRepeat.deactivate()
	}
  },

  /* Private methods: please don't touch anything below this point */
  _unsetMenu: function() {
    if (!this.initialized)
      return

    bs=document.getElementById('hda-buttonset-body')
    if(bs!=null)
    if (bs.tagName=='TBODY') {
    	var element = document.getElementById('hda-buttonset-body')
    	element.parentNode.removeChild(element)
    }
    /*
    for(var i=0; i < this.buttons.length; i++) {
      this._deactivateMenu()
      this.buttons[i].style.backgroundPosition='0 0'
      this.buttons[i].style.color = 'black'
      this.buttons[i].innerHTML=""
      this.buttons[i].checked=false
    }
    */
  },

  _deactivateMenu: function() {
    if (!this.initialized)
      return
    for(var i=0; i < this.buttons.length; i++) {
      this.buttons[i].style.cursor='default'
      this.buttons[i].onmouseover = function(){}
      this.buttons[i].onmouseout = function(){}
      this.buttons[i].onmousedown = function(){}
      this.buttons[i].onmouseup = function(){}
    }
  },

  _createButton: function(index){
  	tbody = document.getElementById('hda-buttonset-body')
  	if (tbody == null){
		bs=document.getElementById('hda-buttonset')
    	tf = document.createElement('TBODY')
    	tf.id='hda-buttonset-body'
   		bs.appendChild(tf)
   		tbody = document.getElementById('hda-buttonset-body')
  	}
  		tf=document.createElement('tr')
     	tftd=document.createElement('td')
     if (index =='footer')
     	tftd.id='hda-button-end'
     else
     	tftd.id='hda-button'
    	tf.appendChild(tftd)
    	tbody.appendChild(tf)
    	this.buttons.push(tftd)
  },

  /*_setupTextWidget: function() {
    if (!this.initialized)
      return
    // Create a new scroller widget for the text area
    var scroller = new hda_scroller({
      windowWidth: 170,
      windowHeight: 120,
      container: 'hda-textArea'
    })

    // Bind scroll action to widget
    scrollUp=document.getElementById('hda-textWidgetScrollUp')
    scrollDown=document.getElementById('hda-textWidgetScrollDown')
    scrollUp.onmouseover   = function() {scroller.scrollUp()}
    scrollDown.onmouseover = function() {scroller.scrollDown()}
    scrollDown.onmouseout  = function() {scroller.stopScroll()}
    scrollUp.onmouseout    = function() {scroller.stopScroll()}
    document.getElementById('hda-textArea').style.visibility='visible'
  },*/

  _setupPlayerControls: function() {

    if (!this.initialized)
      return

    this.controls.cRepeat = document.getElementById('hda-controlRepeat')
    this.controls.cSkip = document.getElementById('hda-controlSkip')
    this.controls.cStop = document.getElementById('hda-controlStop')
    this.controls.cPlay = document.getElementById('hda-controlStop')
	if(this.controls.cRepeat!=null){
	    this.controls.cRepeat.activate=function() {
	      HDAChrome.controls.cRepeat.style.cursor='pointer'
	      HDAChrome.controls.cRepeat.onclick=function() {
	        if (HDAChrome.videoOn) {
		    	HDAChrome.controls.cSkip.activate()
			    HDAChrome.controls.cStop.activate()
			}else{
			    HDAChrome.controls.cStop.deactivate()
		    	HDAChrome.controls.cSkip.deactivate()
			}
			HDAPlayer.repeat()
	      }
	      HDAChrome.controls.cRepeat.style.backgroundPosition='0 0px'
	      HDAChrome.controls.cRepeat.onmouseover=function() {
	        HDAChrome.controls.cRepeat.style.backgroundPosition='0 -18px'
	      }
	      HDAChrome.controls.cRepeat.onmouseout=function() {
	        HDAChrome.controls.cRepeat.style.backgroundPosition='0 0px'
	      }
	    }
	    this.controls.cRepeat.deactivate=function() {
	      HDAChrome.controls.cRepeat.style.cursor='default'
	      HDAChrome.controls.cRepeat.onclick=function(){}
	      HDAChrome.controls.cRepeat.onmouseover=function(){}
	      HDAChrome.controls.cRepeat.onmouseout=function(){}
	      HDAChrome.controls.cRepeat.style.backgroundPosition='0 0px'
	    }
	   	this.controls.cRepeat.deactivate()
    }

    if(this.controls.cSkip!=null){
	    this.controls.cSkip.activate=function() {
	      HDAChrome.controls.cSkip.style.cursor='pointer'
	      HDAChrome.controls.cSkip.onclick=function() {HDAPlayer.skip()}
	      HDAChrome.controls.cSkip.style.backgroundPosition='0 0px'
	      HDAChrome.controls.cSkip.onmouseover=function() {
	        HDAChrome.controls.cSkip.style.backgroundPosition='0 -18px'
	      }
	      HDAChrome.controls.cSkip.onmouseout=function() {
	        HDAChrome.controls.cSkip.style.backgroundPosition='0 0px'
	      }
	    }
	    this.controls.cSkip.deactivate=function() {
	      HDAChrome.controls.cSkip.style.cursor='default'
	      HDAChrome.controls.cSkip.onclick=function(){}
	      HDAChrome.controls.cSkip.onmouseover=function(){}
	      HDAChrome.controls.cSkip.onmouseout=function(){}
	      HDAChrome.controls.cSkip.style.backgroundPosition='0 0'
	    }
    	this.controls.cSkip.deactivate()
    }

	if(this.controls.cStop!=null){
	    this.controls.cStop.activate=function() {
	      HDAChrome.controls.cStop.style.cursor='pointer'
	      HDAChrome.controls.cStop.onclick=function() {
		    HDAChrome.controls.cStop.activatePlay();
	      	HDAPlayer.stop()
	      }
	      HDAChrome.controls.cStop.style.backgroundPosition='0 0px'
	      HDAChrome.controls.cStop.onmouseover=function() {
	        HDAChrome.controls.cStop.style.backgroundPosition='0 -18px'
	      }
	      HDAChrome.controls.cStop.onmouseout=function() {
	        HDAChrome.controls.cStop.style.backgroundPosition='0 0px'
	      }
	    }
	    this.controls.cStop.deactivate=function() {
	      HDAChrome.controls.cStop.style.cursor='default'
	      HDAChrome.controls.cStop.onclick=function(){}
	      HDAChrome.controls.cStop.onmouseover=function(){}
	      HDAChrome.controls.cStop.onmouseout=function(){}
	      HDAChrome.controls.cStop.style.backgroundPosition='0 0px'
	    }

	    this.controls.cStop.activatePlay=function() {
	      HDAChrome.controls.cStop.style.cursor='pointer'
	      HDAChrome.controls.cStop.onclick=function() {
	      	HDAChrome.controls.cStop.activate()
	      	HDAPlayer.play()
	      }
	      HDAChrome.controls.cStop.style.backgroundPosition='0 0px'
	      HDAChrome.controls.cStop.onmouseover=function() {
	        HDAChrome.controls.cStop.style.backgroundPosition='0 -18px'
	      }
	      HDAChrome.controls.cStop.onmouseout=function() {
	        HDAChrome.controls.cStop.style.backgroundPosition='0 0px'
	      }
	    }
		this.controls.cStop.deactivate()
	}


  }
}

function hda_scroller(options) {
  this.windowWidth=options.windowWidth
  this.windowHeight=options.windowHeight
  this.container=document.getElementById(options.container)
  this.offsetTop=this.container.offsetTop
  this.amount=options.amount || 10
  this.time=options.time || 100
  this.container.style.clip = 'rect(0,'+this.windowWidth+'px, '+this.windowHeight+'px,0)';
  this._top=0
  this._bottom=this.windowHeight
}

hda_scroller.prototype.scrollDown = function() {
  if (this._bottom > this.container.offsetHeight) return;
	this._top    += this.amount;
	this._bottom += this.amount;
	this.offsetTop -= this.amount;
	this.scroll()
	obj=this
	this.to = setTimeout(function() {
	  obj.scrollDown()
	}, this.time);
}

hda_scroller.prototype.scrollUp = function() {
  if (this._top <= 0) return
	this._top    -= this.amount;
	this._bottom -= this.amount;
	this.offsetTop += this.amount;
	this.scroll()
	obj=this
	this.to = setTimeout(function() {
	  obj.scrollUp()
	}, this.time);
}

hda_scroller.prototype.scroll = function() {
  this.container.style.clip = 'rect('+this._top+'px,'+this.windowWidth+'px,'+this._bottom+'px,0)';
  this.container.style.top = this.offsetTop + 'px';
}

hda_scroller.prototype.stopScroll = function() {
	if (this.to) clearTimeout(this.to);
}

