var stcUpdate = function() {
	// Constructor
}

stcUpdate.prototype = {
	
	// Create the timer holder
	timer: "",
	
	// Set the interval in milliseconds
	interval: 20000,
	
	// Placeholder
	initTotal: "",
	
	// Placeholder
	bcTotal: "",
	
	// Initialization
	init: function() {
		// Initial Total Broadcasts
		this.timer = YAHOO.lang.later(this.interval, this, this.doUpdate, "", true);
	},
	
	doUpdate: function() {
		var params = new Array();
		
		params['responseFunction'] = this.chkNew;
		params['responseScope'] = this;
		params['templateName'] = 'ajax/getNewBroadcasts';
		params['action'] = 'loadTemplate';
		params['responseType'] = 'JSON';
		params['handlerName'] = 'one_ajax_templateLoader';
		params['requestType'] = 'class';
		params['sdb'] = 1;
		params['extraVars'] = this;
		
		var upv = YAHOO.util.Dom.getElementsByClassName("updateParamValues");
		for(var x=0;x<upv.length;x++) {
			var upvid = upv[x].id.substring(4);
			params[upvid] = upv[x].value;
		}
		
		OneAjax.request(params);
	},
	
	chkNew: function(o,scope) {
		var output = eval('('+o.output+')');
		//console.log(output, scope.getNew);
		if(output.num>0) {
			if(document.getElementById("newBroadcastUpdate")) {
				var box = document.getElementById("newBroadcastUpdate");
				box.innerHTML = "";
			}
			else {
				var box = document.createElement("div");
				box.id = "newBroadcastUpdate";
			}
			if(output.num>1) {
				box.innerHTML = "<span>"+output.num+" new broadcasts.</span>";
			}
			else {
				box.innerHTML = "<span>"+output.num+" new broadcast.</span>";
			}
			
			var bxLink = document.createElement("a");
			bxLink.id = "newBroadcastUpdateLink";
			bxLink.setAttribute("href", "#");
			if(output.num > 15) {
				bxLink.innerHTML = "[view newest 15]";
			} else {
				bxLink.innerHTML = "[view]";
			}
			
			box.appendChild(bxLink);
			
			if(!document.getElementById("newBroadcastUpdate")) {
				var cont = document.getElementById("bcContent");
				cont.insertBefore(box, cont.firstChild);
			}
			
			YAHOO.util.Event.addListener(bxLink, "click", scope.getNew, scope, true);
			scope.initTotal = output.num;
			scope.bcTotal = output.newTotal;
		}
	},
	
	getNew: function(e) {
		//console.log("clicked");
		YAHOO.util.Event.preventDefault(e);
		var params = new Array();
		
		params['responseFunction'] = this.dispNew;
		params['templateName'] = 'ajax/dispNewBroadcasts';
		params['action'] = 'loadTemplate';
		params['responseType'] = 'JSON';
		params['handlerName'] = 'one_ajax_templateLoader';
		params['requestType'] = 'class';
		params['sdb'] = 1;
		params['extraVars'] = this;
		
		var upv = YAHOO.util.Dom.getElementsByClassName("updateParamValues");
		for(var x=0;x<upv.length;x++) {
			var upvid = upv[x].id.substring(4);
			params[upvid] = upv[x].value;
		}
		
		params['numNew'] = this.initTotal;
		document.getElementById("upv_previous_total").value = this.bcTotal;
		
		OneAjax.request(params);
	},
	
	dispNew: function(o, scope) {
		var box = document.getElementById("newBroadcastUpdate");
		box.parentNode.removeChild(box);
		
		var allBroadcasts = YAHOO.util.Dom.getElementsByClassName("bcContentItem");
		var i = allBroadcasts.length-scope.initTotal;
		if(i<0) i = 0;
		for(i;i<allBroadcasts.length;i++) {
			allBroadcasts[i].parentNode.removeChild(allBroadcasts[i]);
		}
		
		var cont = document.getElementById("bcContent");
		var tempCont = document.createElement("div");
		tempCont.innerHTML = o.output;
		YAHOO.util.Dom.setStyle(tempCont, "position", "absolute");
		YAHOO.util.Dom.setStyle(tempCont, "left", "-1000px");
		cont.insertBefore(tempCont, cont.firstChild);
		var tr = YAHOO.util.Dom.getRegion(tempCont);
		var trH = tr.bottom-tr.top;
		YAHOO.util.Dom.setStyle(tempCont, "height", "0px");
		YAHOO.util.Dom.setStyle(tempCont, "position", "");
		YAHOO.util.Dom.setStyle(tempCont, "overflow", "hidden");
		
		var newAttributes = {
		   height: { from:0, to: trH }
		};
		
		var newAnim = new YAHOO.util.Anim(tempCont, newAttributes);
		newAnim.duration = 0.500;
		newAnim.method = YAHOO.util.Easing.easeBoth;
		newAnim.onComplete.subscribe(function(){
			YAHOO.util.Dom.setStyle(tempCont, "height", "auto");
			YAHOO.util.Dom.setStyle(tempCont, "left", "");
		}); 
		newAnim.animate();
	}
}

YAHOO.util.Event.onDOMReady(function(){
	var stcNewBc = new stcUpdate();
	stcNewBc.init();
});












