
_TABBLO_LOADED = false;
var tabblo_rotator = {};
tabblo_rotator.data = {};
tabblo_rotator.html_chunk = '<table style="background-color:#ffffff;border:1px solid #000000;" width="##width##" cellpadding="0" cellspacing="0" border="0"><tr><td style="height:19px;width:20px;background-color:#000;color:#6699CC;padding-left:1px;text-decoration:none;font-size:9px;font-family:verdana;">' +
'<a href="http://www.tabblo.com/studio/"><img border="0" src="http://www.tabblo.com/bitty-static/images/ernst_18x17.gif"/></a></td><td style="background-color:#000000;color:#6699CC;padding:4px;text-decoration:none;font-size:9px;font-family:verdana;"><a onmouseover="addTline(this)" onmouseout="removeTline(this)" href="http://www.tabblo.com/studio/" style="font-weight:normal;color:#6699CC">Try Tabblo</a></td>' +
'<tr><td colspan="2" style="background-color:#000;"><a id="tabblo_link_##link_id##" href=""><img src="" id="##img_id##" border="0" title="" style="width:##width##px;height:##height##px;z-index:2;"/></a></td></tr>' +
'<tr><td colspan="2" style="padding-left:2px;height:15px;line-height:10px;font-size:9px;font-family:verdana;color:#fff;background-color:#000000;">##category## &#xbb;</td></tr>' + 
'</table>';

tabblo_rotator.register = function(divId, data, duration, width, height, category) {
	var el = document.getElementById(divId);
	el.style.display = 'none';
	var html = this.html_chunk.replace(/##width##/g, width).replace(/##height##/g, height);
	html = html.replace(/##category##/g, category).replace(/##img_id##/g, 'img_' + divId).replace(/##link_id##/g, divId);
	el.innerHTML = html;
	this.data[divId] = {theIndex: 0, tabblos: data, rotate: 1, width: width, height: height, category: category};
	setTimeout('tabblo_rotator.stopRotating("' + divId + '");', 15 * 60 * 1000);
	this.rotate(divId, duration);
};

tabblo_rotator.setUp = function(divId, username, list_type, pars) {
	var script = document.createElement('script');
	
	var url = 'http://www.tabblo.com/studio/js/rotate/';
	if (!username) {
		url += 'recent/';
	} else {
		url += username + '/' + list_type + '/';
	}
	var el = document.getElementById(divId);
	if (!_TABBLO_LOADED) {
		_TABBLO_LOADED = true;
		if (!self.YAHOO || !self.YAHOO.util || !self.YAHOO.util.Easing) {
			var yscript = document.createElement('script');
			yscript.setAttribute('src', 'http://www.tabblo.com/bitty-static/javascripts/yui_2.5.1/utilities/utilities.js');
			el.parentNode.appendChild(yscript);
		}
	}
	url += '?id=' + divId;
	if (pars != "" && pars.substring(0,1) != "&") {
		url += '&';
	}
	url += pars;
	script.setAttribute('src', url);
	el.parentNode.appendChild(script);
};

tabblo_rotator.rotate = function(divId, seconds) {
	var el = document.getElementById(divId);
	var d = this.data[divId];
	if (d.rotate == 0) return;
	if (d.tabblos.length == 0) return;
	if (d.theIndex >= d.tabblos.length) {
		d.theIndex = 0;
	}
	var n = d.tabblos[d.theIndex];
	var img = document.getElementById('img_' + divId);
	var link = document.getElementById('tabblo_link_' + divId);
	if (el.style.display == 'none') {
		changeLink(link, img, n[0], n[1].src, n[2]);
		el.style.display = '';
	} else {
		Simple_Fade(img, 0.5, 0.01, function() {changeLink(link, img, n[0], n[1].src, n[2]); Simple_Appear(img, 0.5)});
	}
	d.theIndex += 1;
	
	setTimeout('tabblo_rotator.rotate("' + divId + '", ' + seconds + ');', seconds * 1000);
};

tabblo_rotator.stopRotating = function(divId) {
	this.data[divId].rotate = 0;	
};

function changeLink(link, img, url, src, title) {
	link.href = url; 
	img.src = src; 
	img.title = title;
}

function addTline(obj) {obj.style.textDecoration = "underline";}
function removeTline(obj) {obj.style.textDecoration = "none";}
function Simple_Appear(elementRef, durationSeconds, optCallback) 
{
	var attributes = {
      opacity: { from: 0.01, to: 1.0 }
   	};
   	
	YAHOO.util.Dom.setStyle(elementRef, 'opacity', 0.0);
	YAHOO.util.Dom.setStyle(elementRef, 'visibility', 'visible');
	YAHOO.util.Dom.setStyle(elementRef, 'display', '');
   	var myAnim = new YAHOO.util.Anim(elementRef, attributes, durationSeconds, YAHOO.util.Easing.easeOut);
   	
   	if (optCallback) 
   	{
		myAnim.onComplete.subscribe(optCallback);
   	}
	myAnim.animate();
}

function Simple_Fade(elementRef, durationSeconds, to, optCallback) 
{
	if (!to) { to = 0.01; }
	var attributes = {
      opacity: { from: 1.0, to: to }
   	};
   	
	YAHOO.util.Dom.setStyle(elementRef, 'opacity', 1.0);
	YAHOO.util.Dom.setStyle(elementRef, 'visibility', 'visible');
	YAHOO.util.Dom.setStyle(elementRef, 'display', '');
   	var myAnim = new YAHOO.util.Anim(elementRef, attributes, durationSeconds, YAHOO.util.Easing.easeOut);

   	if (optCallback) 
   	{
		myAnim.onComplete.subscribe(optCallback);
   	}
	myAnim.animate();
}


