/* FOR VIDEO TOUTS */
function writeWidgetBoxVideo(numFrames,currShow,fade, restart_interval) {
	if(typeof(restart_interval) == "undefined")
	{
		var restart_interval = false;
	}
	for (var i=1; i<=numFrames; i++) {
		document.getElementById('tnVideo'+i).style.visibility = "hidden";
		document.getElementById('tnVideo'+i).style.display = "none";
		if (i==currShow) {
			document.getElementById('tnVideo'+i).style.visibility = "visible";
			document.getElementById('tnVideo'+i).style.display = "block";
		}
	}
	//if (fade==true) {shiftOpacity('tncontainerVideo', 500);}
	var sequenceInd = document.getElementById('widgetSeqNumsVideo');
	sequenceInd.innerHTML = '';
	sequenceInd.innerHTML = '\(' + currShow + '&nbsp;of&nbsp;' + numFrames + '\)';
	var fade = true;
	

	if(restart_interval == true)
	{
		cnnRotateWidgetBoxVideo.restart(currShow);
	}
	
}
function rotateMeVideo(total,curr) {
	var newRotate = curr+1;
	if ( newRotate > total ) { newRotate = 1; }
	writeWidgetBoxVideo(total,newRotate,true);
	setTimeout("rotateMeVideo("+total+","+newRotate+")",7000);
}

/**
 * Image rotate object
 * For reuse, make a class, add a callback capability
 */
var cnnRotateWidgetBoxVideo = {
	window_interval:null,
	total_frames:1,
	current_frame:1,
	
	init:function(total_frames, current_frame)
	{
		this.total_frames = total_frames;
		this.current_frame = current_frame;
	},
	restart:function(current_frame)
	{
		this.current_frame = current_frame;
		this.stop();
		this.start();
	},
	rotate:function()
	{
		this.current_frame++;
		if(this.current_frame > this.total_frames)
		{
			this.current_frame = 1;
		}
		writeWidgetBoxVideo(this.total_frames, this.current_frame, true, false);
	},
	start:function(total_frames, current_frame)
	{
		this.window_interval = window.setInterval('cnnRotateWidgetBoxVideo.rotate()', 7000);
	},
	stop:function()
	{
		if(this.window_interval != null)
		{
			window.clearInterval(this.window_interval);
			this.window_interval = null;
		}
	}
};


//----------------------
// TYPEAHEAD
//----------------------

var gTypeAhead = null;
var typeahead_maxrows = 18; //maxdisplay rows
var gData = null;

function init_typeahead()
{
	//typeahead
	var inputs = {
		'target_node'		:'targetdiv',
		'onClick'			:'typeahead_callback_click',
		'onComplete'		:'typeahead_callback_request_complete'
	};	
	gTypeAhead = new TypeaheadField('gTypeAhead', 'http://apps.money.cnn.com/bestplaces_2009/bpl2009.jsp', inputs);
	gTypeAhead.init();
}

/**
 * TypeAhead onclick callback
 */
function typeahead_callback_click(selected_index)
{
	var id = gData[selected_index].id;
	location.href = "/magazines/moneymag/bplive/2009/snapshots/" +id+ ".html";
}

/**
 * TypeAhead request complete callback
 */
function typeahead_callback_request_complete(return_obj)
{
	if(typeof(return_obj) == "undefined")
	{
		$('listdiv').innerHTML = '';
		return;
	}
	
	gData = return_obj;
		
	var html = '<table class="list" cellspacing="0">';

	if(return_obj.length > 0)
	{	
		var len = (return_obj.length > typeahead_maxrows)?typeahead_maxrows:return_obj.length;
		
		for(var i = 0; i < len; i++)
		{
			html += '<tr><td class="list" id="typeahead_row_'+i+'" onmouseover="gTypeAhead.callbacks.onMouseOver.call(this, '+i+');" onclick="location.href = \'/magazines/moneymag/bplive/2009/snapshots/'+return_obj[i].id+'.html\'"><a id="typeahead_link_'+i+'" href="/magazines/moneymag/bplive/2009/snapshots/'+return_obj[i].id+'.html">'+return_obj[i].name+'</a></td></tr>';
		}
	}
	else
	{
		html += '<tr><td width="100%" class="nolist" id="typeahead_row_none">No Cities Found</td></tr>';
	}
	html += '</table>';
	
	$('listdiv').innerHTML = html;
	$('listdiv').show();
}
/* FOR GALLERY TOUTS ON SNAPSHOTS */
function writeWidgetBox(numFrames,currShow,fade) {
	for (var i=1; i<=numFrames; i++) {
		document.getElementById('tn'+i).style.visibility = "hidden";
		document.getElementById('tn'+i).style.display = "none";
		if (i==currShow) {
			document.getElementById('tn'+i).style.visibility = "visible";
			document.getElementById('tn'+i).style.display = "block";
		}
	}
	//if (fade==true) {shiftOpacity('tncontainer', 500);}
	var fade = true;
}
function shiftOpacity(id, millisec) {
	document.getElementById(id).style.opacity = 0
	opacity(id, 0, 100, millisec);
}
function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	for(i = opacStart; i <= opacEnd; i++) {
		setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
		timer++;
	}
}
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
function rotateMe(total,curr) {
	var newRotate = curr+1;
	if ( newRotate > total ) { newRotate = 1; }
	writeWidgetBox(total,newRotate,true);
	setTimeout("rotateMe("+total+","+newRotate+")",7000);
}

function test_bit(bit, value)
{
	if ((bit & value) == bit)  return true;
	else return false;
}

/* FOR EXPAND/COLLAPSE DATA ON SNAPSHOTS */
function revealMoreData() {
	if (navigator.appName=="Microsoft Internet Explorer") {
		document.getElementById('bpliveMoreData').style.display = 'block';
		document.getElementById('bpliveLessData').style.display = 'none';
	} else {
		document.getElementById('bpliveMoreData').style.display = 'table';
		document.getElementById('bpliveLessData').style.display = 'none';
	}
	document.getElementById('expandIndustries').style.display='none';
	document.getElementById('expandIndustries2').innerHTML='<a href="javascript:revealLessData();">See less data</a>';
}
function revealLessData() {
	document.getElementById('bpliveMoreData').style.display = 'none';
	document.getElementById('expandIndustries').style.display='block';
	if (navigator.appName=="Microsoft Internet Explorer") {
		document.getElementById('bpliveLessData').style.display = 'block';
	} else {
		document.getElementById('bpliveLessData').style.display = 'table';
	}
	document.getElementById('expandIndustries2').innerHTML='<a href="javascript:revealMoreData();">See all</a>';
	scrollTo(0, 936);
}
function showNavTab(tabId,tabNo) {
	var tabCollection = document.getElementById(tabId);
	tabCollection.className='Tab'+tabNo+'Visible';
}
