/**
* forum/pulse.js
*
*
*
* Dependencies
* - YUI
*
* @author Fermin <fsimeon@gaiaonline.com>
**/


YAHOO.namespace('gaia.app.Forum');
YAHOO.gaia.app.Forum.Pulse = function(settings){
	
	
	this._pulse_delay = 1;
	this._pulse_running = 1;
	this._pulse_list =  null;
	this._pulse_container = null;
	this._pulse_list_elem = null;
	this._child_nodes_start = 0;
	this._pulse_string = '';
	this._is_hidden=0;
	this._waitTimer=null;
	this._ajaxTimer=0;	
	this._settings = null;
	this._ajax = '';
	this._ajaxRequest = null;
	this._newThreads = new Array();
	this._parsing = 0;
	this._is_collapsed = 0;
	this._tr_on = 0;
	this._array_locked = 0;
	this._initializing = 0;
	this._last_timestamp = 0;
	this._userid = '';
	this._request_count = 0;
	this._first_loaded = 0;
	
}


	
YAHOO.gaia.app.Forum.Pulse.prototype._timestamp = function(){
		var now = new Date();
		return Math.round((new Date().getTime()/1000.0) -this._settings.timeout_delay);
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._createElement = function(tag,item){
		
		var el = document.createElement(tag);
		el.innerHTML = item;
		
		return el;
}
	
	
YAHOO.gaia.app.Forum.Pulse.prototype._fadeIn = function(el,duration){
		
	var attributes = {
			
		opacity: { from:0,to: 1 } 
	};
	var anim = new YAHOO.util.Anim(el,attributes,duration);
	anim.animate();
}
YAHOO.gaia.app.Forum.Pulse.prototype._fadeOut = function(el,duration){
		
	var attributes = {
			
		opacity: { from:1,to: 0 } 
	};
	var anim = new YAHOO.util.Anim(el,attributes,duration);
	anim.animate();
}


YAHOO.gaia.app.Forum.Pulse.prototype._collapse = function(initial){
	this._is_collapsed=1;
	
	var myObj = this;
	
	window.setTimeout(function(){
		
		myObj._pulse_list.setStyle('display','none');
		var footer = new YAHOO.util.Element(myObj._settings.footer);
		footer.setStyle('display','none');
		},300);
	
	var el = document.getElementById(this._settings.container);
	
	var attributes = {
			
		height: { from:this._settings.collapse_max,to: this._settings.collapse_min} 
	};
	var anim = new YAHOO.util.Anim(el,attributes,4);
	anim.animate();
	
	YAHOO.util.Dom.replaceClass(this._settings.expand_collapse,'expand','collapse');	
	var button = new YAHOO.util.Element(this._settings.expand_collapse);	
	button.set('title','maximize');
	document.getElementById(this._settings.expand_collapse).className='collapse';
	if(this._isIE6()){
		button.setStyle('background','url(/images/forum/pulse/pulse_expand_collapse.gif) bottom center no-repeat');
	}
	if(!this._initializing){
		this._setAjaxData('collapsed',1)
	}
		
	
}

YAHOO.gaia.app.Forum.Pulse.prototype._expand = function(initial){
	
	this._is_collapsed=0;
	
	
	var myObj = this;
	window.setTimeout(function(){
		myObj._pulse_list.setStyle('display','inline');
		var footer = new YAHOO.util.Element(myObj._settings.footer);
		footer.setStyle('display','inline');
		},300);
		
	var el = document.getElementById(this._settings.container);
	var attributes = {
			
		height: { from:this._settings.collapse_min,to: this._settings.collapse_max} 
	};
	var anim = new YAHOO.util.Anim(el,attributes,4);
	anim.animate();
	
	YAHOO.util.Dom.replaceClass(this._settings.expand_collapse,'collapse','expand');

	var button = new YAHOO.util.Element(this._settings.expand_collapse);
	button.set('title','minimize');
	document.getElementById(this._settings.expand_collapse).className='expand';
	if(this._isIE6()){
		button.setStyle('background','url(/images/forum/pulse/pulse_expand_collapse.gif) top center no-repeat');
	}
	if(!this._initializing){
		this._setAjaxData('collapsed',0);
	}
		
	
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._fadeEachDown = function(){
		
		
	var fadeStartPos =  this._settings.limit - this._settings.fade_last ;
	var s = this._pulse_list_elem.childNodes.length+this._child_nodes_start - fadeStartPos;
		
	for(var i=0; i < this._pulse_list_elem.childNodes.length -1; i++){

		var childNode = this._pulse_list_elem.childNodes[i];
		var elem = new YAHOO.util.Element(childNode);
		if(i >= fadeStartPos){
			var o = 1 - (s == 1 ? 0.5 : 0.85/s*(i-fadeStartPos+1));
			if (window.ActiveXObject){
				elem.set('filter',"alpha(opacity=" + o*100 + ")" );
					
			}
			elem.setStyle("opacity",o);
		}
			
			
	}
}
	
	
YAHOO.gaia.app.Forum.Pulse.prototype._push = function(item,elem_id){
		
			
		var el = this._createElement('div',item);
		
		var elem = new YAHOO.util.Element(el);
		
		elem.set('id',"pulse_"+elem_id);
		if(this._getTrState()){
			elem.addClass("pulse_tron");
		}else{
			elem.addClass("pulse_troff");
		}
		
		
		elem.setStyle('opacity', 0);
		
			
		if(this._pulse_list_elem.childNodes.length > 0){
			var first = this._pulse_list_elem.childNodes[this._child_nodes_start];
			this._pulse_list_elem.insertBefore(el,first);
			
			
				
		}
		else{
			this._pulse_list_elem.appendChild(el);
		}
		this._fadeIn(el,1);
				
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._remove = function(){
		
	if(this._pulse_list_elem.hasChildNodes() && this._pulse_list_elem.childNodes.length > this._settings.maxsize+this._child_nodes_start){
			
		this._pulse_list_elem.removeChild(this._pulse_list_elem.lastChild);
			
	}	
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._addThread = function(item,elem_id){
	
	this._remove();
	this._fadeEachDown();
	this._push(item,elem_id);
}
	
	

YAHOO.gaia.app.Forum.Pulse.prototype._play_pause = function(){
	
	if(this._pulse_running){
		this._stop_parse();

	}
	else{
		this._start_parse();
		this._request_count = 0;
	}	
	
	
	
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._speedUp = function(){

	if(this._pulse_delay != 2 && this._pulse_delay > 2){
		this._pulse_delay -= 2;
		if(this._pulse_running){ //restart only if running
			this._stop_parse();
			this._start_parse();
		}
		else{ //start again if paused
			this._start_parse();
		}
		if(this._pulse_delay == 2){
			YAHOO.util.Dom.replaceClass(this._settings.faster,'enabled','disabled');
			if(this._isIE6()){
				var fbutton = new YAHOO.util.Element(this._settings.faster);//IE 6 fix
				fbutton.setStyle('background','url(/images/forum/pulse/pulse_control_faster.gif) bottom center no-repeat');
			}
		}
		YAHOO.util.Dom.replaceClass(this._settings.slower,'disabled','enabled');
		if(this._isIE6()){
			var sbutton = new YAHOO.util.Element(this._settings.slower); //IE 6 fix
			sbutton.setStyle('background','url(/images/forum/pulse/pulse_control_slower.gif) top center no-repeat');
		}
	}
		
	
	
		
		
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._slowDown = function(){
	if(this._pulse_delay != 6 && this._pulse_delay <6 ){
		this._pulse_delay += 2;
		if(this._pulse_running){ //restart only if running
			this._stop_parse();
			this._start_parse();
		}
		else{
			//start again if paused
			this._start_parse();
		}
		if(this._pulse_delay == 6){
			YAHOO.util.Dom.replaceClass(this._settings.slower,'enabled','disabled');
			if(this._isIE6()){
				var sbutton = new YAHOO.util.Element(this._settings.slower); //IE 6 fix
				sbutton.setStyle('background','url(/images/forum/pulse/pulse_control_slower.gif) bottom center no-repeat');
			}
		}
		
		YAHOO.util.Dom.replaceClass(this._settings.faster,'disabled','enabled');
		if(this._isIE6()){
			var fbutton = new YAHOO.util.Element(this._settings.faster);//IE 6 fix
			fbutton.setStyle('background','url(/images/forum/pulse/pulse_control_faster.gif) top center no-repeat');
		}
	}
}



YAHOO.gaia.app.Forum.Pulse.prototype._toggleCollapse = function(){	
	if(this._is_collapsed){
		this._expand();
	}
	else{
		
		this._collapse();	
	}
}
		
YAHOO.gaia.app.Forum.Pulse.prototype._close = function(initial){
	
	var s_link = new YAHOO.util.Element(this._settings.show);
	s_link.setStyle('display','inline');
			
	this._pulse_container.setStyle('display','none');
	this._stop_parse();
	this._stop_ajax();
	
	if(!this._initializing){
		this._setAjaxData('closed',1);
	}	
}

YAHOO.gaia.app.Forum.Pulse.prototype._open = function(initial){
	
	var s_link = new YAHOO.util.Element(this._settings.show);
	s_link.setStyle('display','none');
	this._pulse_container.setStyle('display','inline');
	if(!this._first_loaded){ //check if already initialized
	    this._start_parse();
	    this._start_ajax();
	}
	else{
	    this._start_ajax();
	    this._start_parse();
    }
	
	if(!this._initializing){
		this._setAjaxData('closed',0);
	}
}

YAHOO.gaia.app.Forum.Pulse.prototype._getTrState = function(){
		
	if(this._tr_on){
		this._tr_on = 0;
		
	}else{
		this._tr_on = 1;
	}
	return this._tr_on;
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._isDupe = function(elem_id){
	if(document.getElementById("pulse_"+elem_id)){
		
		return true;
	}
	else{
			
		return false;
	}
		
		
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._parseItem = function(){
	
	if(this._array_locked || this._parsing || !this._newThreads.length){ //check for array lock
		return;
		
	}
	
	this._parsing = 1;
	if(this._newThreads.length){			
		var i = this._newThreads.shift();
		if(!this._isDupe(i.tid)){
			var link = '<a class="gotonewposts" title="'+i.topic+'" href="'+i.url+'">'+i.topic+'</a>';
			var profile_link = '<a href="'+i.profile+'" title="view '+i.author+'\'s profile">'+i.author+'</a>';
			var item = '<div class="corner_bd">&nbsp;</div>'+
					   '<div class="pulse_data">'+
							'<div class="pulse_td topic" >'+link+'</div>'+
							'<div id="replies_'+i.tid+'"class="pulse_td replies" >'+i.replies+'</div>'+
							'<div class="pulse_td creator">'+profile_link+'</div>'+
							'<div class="pulse_td lastupdated"></div>'+
							'<div class="clear"></div>'+
						'</div>'+
						'<div class="corner_bd">&nbsp;</div>'+
						'<div class="clear">&nbsp;</div>';
			this._addThread(item,i.tid);
			
		}else{
			var rc = document.getElementById('replies_'+i.tid);
			this._fadeOut(rc,1);
			this._fadeIn(rc,4);
			rc.innerHTML=i.replies;
			
		}
	}
	this._parsing = 0;
}

YAHOO.gaia.app.Forum.Pulse.prototype._start_parse = function(){
	var myObj = this;
	this._waitTimer = window.setInterval(function(){ myObj._parseItem();},1000*this._pulse_delay);
	this._pulse_running = 1;
	YAHOO.util.Dom.replaceClass(this._settings.play_pause,'pause','play'); //change button state
	var button = new YAHOO.util.Element(this._settings.play_pause);
	button.set('title','pause');
	if(this._isIE6()){
		button.setStyle('background','url(/images/forum/pulse/pulse_control_play_pause.gif) center center no-repeat');
	}
}

YAHOO.gaia.app.Forum.Pulse.prototype._stop_parse = function(){
	window.clearInterval(this._waitTimer);
	this._pulse_running = 0;
	YAHOO.util.Dom.replaceClass(this._settings.play_pause,'play','pause'); //change button state
	var button = new YAHOO.util.Element(this._settings.play_pause);
	button.set('title','play');
	if(this._isIE6()){
		button.setStyle('background','url(/images/forum/pulse/pulse_control_play_pause.gif) top center no-repeat');
	}
} 


	//Connection Manager for ajax call
YAHOO.gaia.app.Forum.Pulse.prototype._handleSuccess = function(o){
		
	var ajaxThreads = eval("(" + o.responseText + ")");
	ajaxThreads.reverse();
	if(ajaxThreads.length > 0){
		this._array_locked= 1; //lock to stop parsing
		if( this._newThreads.length > this._settings.cache_size){
			this._newThreads.splice(0,ajaxThreads.length);
		}
		for(var i=0; i<ajaxThreads.length; i++){
			this._newThreads.push(ajaxThreads[i]);
			if( ajaxThreads[i].rawtime > this._last_timestamp ){
				this._last_timestamp = ajaxThreads[i].rawtime; 
			}
			
		}
		
		this._array_locked=0;
		
		if(o.argument.initializing){
			for(var i=0; i<32; i++){
				this._parseItem();
			}
		}
	
	}
					
}

	//Connection Manager for ajax call
YAHOO.gaia.app.Forum.Pulse.prototype._handlePrefSuccess = function(o){
	var ajaxThreads = eval("(" + o.responseText + ")");				
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._handleFailure = function(o){
		
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._setAjaxData = function(key,val){	
	var callback = {
		success: function(o){

			this._handlePrefSuccess(o);
		},
		failure: function(o){

			this._handleFailure(o);
		},
		timeout:5000,
		scope: this
	}
	if(this._settings.userid != ''){
		var args = 'pulse_userid='+this._settings.userid+'&pulse_pref='+key+'&pref_value='+ val+'&ts='+this._timestamp();
		var ajax_url = this._settings.ajax+'/settings';
		this._ajaxRequest =  YAHOO.util.Connect.asyncRequest('POST',ajax_url, callback,args);
	}
}
	
YAHOO.gaia.app.Forum.Pulse.prototype._getAjaxData = function(initial){
			
	var callback = {
		success: function(o){
			this._handleSuccess(o);
		},
		failure: function(o){
			this._handleFailure(o);
		},
		timeout:20000,
		scope: this,
		argument: {initializing:this._initializing}
		
	}
	var args = '?id='+ this._settings.pulse_topic_id+'&ts='+this._timestamp();
	if(!initial){
		args+='&timestamp='+this._last_timestamp;
	}
	var ajax_url = this._settings.ajax + args;
	if(this._request_count < this._settings.request_limit ){
		if(this._pulse_running){
			this._ajaxRequest =  YAHOO.util.Connect.asyncRequest('GET',ajax_url, callback);
		}
		this._request_count++;
	}
	else{
	    this._stop_parse();
	}
}

YAHOO.gaia.app.Forum.Pulse.prototype._start_ajax = function(){
    this._first_loaded = 1;
	this._getAjaxData(true);//initial load
	var myObj = this;
	this._ajaxTimer = window.setInterval(function(){
		myObj._getAjaxData(false);
		},this._settings.timeout);
}

YAHOO.gaia.app.Forum.Pulse.prototype._stop_ajax = function(){
	
	window.clearInterval(this._ajaxTimer);
}
YAHOO.gaia.app.Forum.Pulse.prototype._isIE6 = function(){
	
	var version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1){
	var temp=navigator.appVersion.split("MSIE");
	var version=parseFloat(temp[1]);
	}
	if(version == 6){
		return true;
	}
	else{
		return false;
	}
}
	
YAHOO.gaia.app.Forum.Pulse.prototype.init = function(settings){
			
			
		this._settings = {

			limit: (settings.limit || 10),
			ajax: settings.ajax || this._ajax,
			userid: (settings.pulse_userid || ''),
			timeout: (settings.timeout || 30000),
			timeout_delay: (settings.timeout_delay || 60),
			pulse_delay: (settings.pulse_delay || 1),
			timestamp: (settings.timestamp || this._timestamp),
			isDupe: (settings.isDupe || this._isDupe),
			footer: (settings.footer || 'pulse_footer'),
			list: (settings.container || 'pulse_list'),
			container: (settings.container || 'pulse_container'),
			play_pause:(settings.play_pause || 'pulse_play_pause'),
			slower:(settings.slower || 'pulse_slower'),
			faster:(settings.faster || 'pulse_faster'),
			close:(settings.close || 'pulse_close'),
			show:(settings.show || 'pulse_show'),
			expand_collapse:(settings.expand_collapse || 'pulse_expand_collapse'),
			fade_last: (settings.fade_last || 2),
			pulse_topic_id: (settings.pulse_topic_id || ''),
			maxsize: (settings.maxsize || 10),
			cache_size:(settings.cache_size || 60),
			collapse_max:(settings.collapse_max || 350),
			collapse_min:(settings.collapse_min || 40),
			default_closed: settings.default_closed,
			request_limit:(settings.request_limit || 2),
			default_collapsed: settings.default_collapsed
		};
		
		
		this._pulse_delay = this._settings.pulse_delay;
		this._pulse_list =  new YAHOO.util.Element(this._settings.list);
		this._pulse_container = new YAHOO.util.Element(this._settings.container);
		this._pulse_list_elem = document.getElementById(this._settings.list);
		this._child_nodes_start = this._pulse_list_elem.childNodes.length;
		this._pulse_string = settings.pulse_list;
		this._pulse_running=1;
		this._userid = this._settings.userid;
			
			
		YAHOO.util.Event.addListener(this._settings.close,'click',this._close,this,true);
		YAHOO.util.Event.addListener(this._settings.expand_collapse,'click',this._toggleCollapse,this,true);
		YAHOO.util.Event.addListener(this._settings.play_pause, 'click', this._play_pause,this,true);
		YAHOO.util.Event.addListener(this._settings.faster, 'click', this._speedUp,this,true);
		YAHOO.util.Event.addListener(this._settings.slower, 'click', this._slowDown,this,true);
		YAHOO.util.Event.addListener(this._settings.show, 'click', this._open,this,true);
			
	
		this._initializing =1;
		if(this._settings.default_closed){
			this._close();
		}else{
			this._open();
			
		}
		
		if(this._settings.default_collapsed){
			this._collapse();
		}else{
			var footer = new YAHOO.util.Element(this._settings.footer);
			footer.setStyle('display','inline');
		}
		this._initializing =0;
		
}


function initPulse(settings){
	
	pulse = new YAHOO.gaia.app.Forum.Pulse();
	pulse.init(settings);
}
