function onair(params)
{ 
  this.onairBlockID=null;
  this.onairArtistID=null;
  this.onairTitleID=null;
  this.onairVoteID=null;
  this.onairRatingID=null;
    
  this.onairStartID=null;
  this.onairArtistStyle=null;
  this.onairNoArtistStyle=null;
  this.onairTitleStyle=null;
  this.onairOnlyTitleStyle=null;
  this.onairEndID=null;
  
  this.onairURL=null;
  this.voteURL=null;
	this.timeDiff=null; 
	this.defaultInterval=10000;
	this.maxInterval=300000;
  this.objName=null;
  
  this.title="";
  this.artist="";
  this.titleid=0;
  this.topicid=0;
  this.time=null;
  this.start=null;
  this.end=null;
  this.type=0;//0 without artist, 1 with
    
  this.VoteDIVHeight = 40;
  this.EffectInterval = 2;
  
  this.current=1;
  this.next=2;
  this.stars=5;//total amount of stars
  this.rating=0;
  
  this.voted=false;
  this.voteBlockHidden=true;
  
  this.randomNum=Math.random();
   
  for (property in this) 
  { if(params[property] !== undefined)
      this[property] = params[property];
  }
  
  this.filterURI='domain=' + this.domain;  
  this.titleObj = document.getElementById('title');
}
onair.prototype.initTimer = function()
{ this.start= new Date;
	dateVal=parseInt(document.getElementById(this.onairStartID).value,10) - this.timeDiff;
	this.start.setTime( dateVal );
	
	this.end=new Date;
	dateVal=parseInt(document.getElementById(this.onairEndID).value,10) - this.timeDiff;
	this.end.setTime(dateVal);
	
	//reikia nustatyti type-a cia baigiau
	this.updateObjectsReference();
	this.type = parseInt(document.getElementById("type").value, 10);;
	if(this.type)
	{ this.ratingObj.style.visibility = "visible";
		this.voteObj.style.visibility = "visible";
		this.titleid=parseInt(document.getElementById("titleid").value, 10);
		this.rating=parseInt(document.getElementById("hrating").value, 10);
		this.topicid=parseInt(document.getElementById("topicid").value, 10);
		this.displayRating(this.current,this.rating);
		if(this.voteBlockHidden)
  		this.voteBlockToggle();
	}
	
	tillEnd=this.tillEnd();
	this.nextUpdate( tillEnd );	
}
onair.prototype.tillEnd = function()
{ now=new Date;
	tillEnd = parseInt( (this.end - now), 10);
	//register(tillEnd,'time left: ');
	if( tillEnd < this.maxInterval && tillEnd > 0 && this.type > 0 )
	  return(tillEnd);//return(this.defaultInterval);
	else
		return(this.defaultInterval);	
  
}
onair.prototype.update = function()
{ 
	new Ajax.Request(this.onairURL + "?seq=" + this.randomNum++,
  {
    method:'get',
    onSuccess: function(response) {
    	onairObj.parseData(response.responseXML);
  	}
  });
  
}
onair.prototype.parseData = function(responseXML)
{ titleid=responseXML.getElementsByTagName('titleid')[0].childNodes[0].nodeValue;
	titleid=parseInt(titleid,10);
	if(this.titleid == titleid )
	{ this.nextUpdate(this.defaultInterval);
	  return false;
	}
	
	this.titleid=titleid;
	this.title=responseXML.getElementsByTagName('title')[0].childNodes[0].nodeValue;
	
	if(responseXML.getElementsByTagName('artist').length > 0)
	{ this.artist=responseXML.getElementsByTagName('artist')[0].childNodes[0].nodeValue;
	  this.type=1;
	}
	else
	{	this.artist="";
	  this.topicid=0;
		this.type=0;
	}
	
	if(responseXML.getElementsByTagName('rating').length > 0)
	{ this.rating=responseXML.getElementsByTagName('rating')[0].childNodes[0].nodeValue;
	
	}
  
  
	dateVal=parseInt( responseXML.getElementsByTagName('start')[0].childNodes[0].nodeValue,10 ) - this.timeDiff;
	this.start.setTime( dateVal );
	
	dateVal=parseInt(responseXML.getElementsByTagName('end')[0].childNodes[0].nodeValue,10) - this.timeDiff;
	this.end.setTime( dateVal );
	
	tillEnd = this.tillEnd();
	this.nextUpdate(tillEnd);
	
	this.prepare();
	//this.exchange();
}
onair.prototype.updateObjectsReference = function()
{
  this.artistObj = document.getElementById(this.onairArtistID);
  this.titleObj = document.getElementById(this.onairTitleID);
  this.ratingObj = document.getElementById(this.onairRatingID); 
  this.voteObj = document.getElementById(this.onairVoteID); 
  
}
onair.prototype.displayRating = function(rating)
{ 
  for(i=1; i < rating + 1; i++)
    document.getElementById("star" + i).className="sg";
  starsLeft= this.stars - rating;
  for(i= this.stars - starsLeft + 1; i < this.stars + 1; i++)
    document.getElementById("star" + i).className="ss";
}
onair.prototype.prepare = function()
{	
    
  this.updateObjectsReference();
  
  switch(this.type)
  {
    case 0:
    	//noartist
    	this.titleObj.innerHTML = "<b>" + this.title + "</b>";
    	if(!this.voteBlockHidden)
  			this.voteBlockToggle();
      break;
    case 1:
    	//artist
    	this.activateVote();//nes gali but kad jau balsavo
    	if(this.voteBlockHidden)
  			this.voteBlockToggle();
			this.titleObj.innerHTML = "<b>" + this.artist + "</b> <br/>" + this.title;
  		this.displayRating(this.rating);
      break;
  }
}
onair.prototype.exchange = function()
{ 
	new Effect.Move("onair" + this.next, { x: 0, y: this.DIVHeight, mode: 'relative', transition: Effect.Transitions.sinoidal});
  //new Effect.Opacity(conair, {from: 0, to: 1, duration: this.EffectInterval+1} );  
  new Effect.Move("onair" + this.current, { x: 0, y: this.DIVHeight, mode: 'relative' , duration: this.EffectInterval , duration: this.EffectInterval, transition: Effect.Transitions.sinoidal});
  //new Effect.Opacity("nonair", {from: 1, to: 0, duration: this.EffectInterval+1} );
	
	//conairObj.style.top="-80px";
}
onair.prototype.voteBlockToggle = function()
{ height=this.VoteDIVHeight;
	if(this.voteBlockHidden)
	{	this.voteBlockHidden=false;
		height=height*-1;
	}
	else
	{	this.voteBlockHidden=true;
	}
	
	new Effect.Move("vote", { x: 0, y: height, mode: 'relative', transition: Effect.Transitions.sinoidal});
	
  //new Effect.Opacity("nonair", {from: 1, to: 0, duration: this.EffectInterval+1} );
	
	//conairObj.style.top="-80px";
}

onair.prototype.nextUpdate = function(interval)
{ 
	setTimeout("onairObj.update()", interval);
}

onair.prototype.vote = function(vote)
{ 
  params="titleid=" + this.titleid + "&vote=" + vote;
	new Ajax.Request(this.voteURL,
	{
    method:'post',
    postBody: params,
    onSuccess: function(response) 
    { onairObj.parseRating(response.responseXML);
  	}
  });
  this.voted=true;
  this.deactivateVote();
}
onair.prototype.deactivateVote = function()
{ document.getElementById('votebad').className='badbw';
	document.getElementById('votegood').className='goodbw';
}
onair.prototype.activateVote = function()
{ document.getElementById('votebad').className='bad';
	document.getElementById('votegood').className='good';
}

onair.prototype.parseRating = function(responseXML)
{ 
  this.rating=parseInt(responseXML.getElementsByTagName('rating')[0].childNodes[0].nodeValue,10);
  
  this.displayRating(this.current,this.rating);
}
onair.prototype.discuss = function()
{ document.location.href = this.forumUrl + this.topicid;
}
