addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
            	//alert('yip');
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func); 
    }
})();


//jsonp func//
(function(){
  var id = 0, head = document.getElementsByTagName('head')[0], global = this;
  global.getJSON = function(url, callback) {
    var script = document.createElement('script'), token = '__jsonp' + id;
    
    // callback should be a global function
    global[token] = callback;
    
    // url should have "?" parameter which is to be replaced with a global callback name
    script.src = url.replace(/\?(&|$)/, '__jsonp' + id + '$1');
    
    // clean up on load: remove script tag, null script variable and delete global callback function
    script.onload = function() {
      script.parentNode.removeChild(script);
      script = null;
      delete global[token];
    };
    head.appendChild(script);
    
    // callback name should be unique
    id++;
  }
})();


var Poll = function(widgetId, serviceUrl, votingTimeout) {
            var ajax = function(params, callback) {
                var timeouted = false;
                var timer = setTimeout(function() {
                    timeouted = true;
                    //alert("Poll TIMEOUT "+params.url)
                    if (callback) callback();
                }, votingTimeout==null ? 60000 : votingTimeout);
            
                var success = function(response) {
                    if (timeouted) return;
                    if (timer) {
                        clearTimeout(timer);
                        timer = null;
                    }
                    if (callback) callback(response);
                };
 
                var error = function(e) {
                    var s="";
                    for (var p in e)
                    	s+=p+"="+e[p]+" ";
                    //alert("Poll ERROR: "+s);
                    if (timeouted) return;
                    if (timer) {
                        clearTimeout(timer);
                        timer = null;
                    }
                    if (callback) callback();
                };
                
                //params.type= "POST";
                params.dataType = "jsonp";
                params.success = success;
                params.error = error;
               
                if (params.url.indexOf("?")!=-1)
            		params.url += "&callback=?"
            	else
            		params.url += "?callback=?"
                params.url += "&method="+ params.type;
                params.type = "GET";
//                $.ajax(params);
                getJSON(params.url, success);
                
//                document.getElementById("status").innerHTML += params.type+" "+params.url+"<br/>";
            };

            this.sendVote = function(name, callback) {
                ajax({
                    type: 'POST',
                    url: serviceUrl+'/'+widgetId+'/'+name
                }, callback);
            };
            
            this.requestStats = function(callback) {
                ajax({
                    type: 'GET',
                    url: serviceUrl+'/'+widgetId
                }, callback);
            };
            
            this.requestStatsWithPrefix = function(pre, callback) {
                ajax({
                    type: 'GET',
                    url: serviceUrl+'/'+widgetId+"?prefix="+pre
                }, callback);
            };
 
            this.requestCounter = function(name, callback) {
                ajax({
                    type: 'GET',
                    url: serviceUrl+'/'+widgetId+'/'+name
                }, callback);
            };
 
            this.setCounter = function(name, value, callback) {
                ajax({
                    type: 'PUT',
                    data: { value: value },
                    url: serviceUrl+'/'+widgetId+'/'+name
                }, callback);
            };
 
            this.removeCounter = function(name, callback) {
                ajax({
                    type: 'DELETE',
                    url: serviceUrl+'/'+widgetId+'/'+name
                }, callback);
            };
 
            this.removeAllCounters = function(callback) {
                ajax({
                    type: 'DELETE',
                    url: serviceUrl+'/'+widgetId
                }, callback);
            };
};



var img_base = config.script_base;

var DISABLE_FB_CONNECT = false;
var DISABLE_FB_CONNECT_IS_CONNECTED = true;
function set_html_if_fb_connected(html_if_connected,html_if_not_connected,div)
{	
	
	if(DISABLE_FB_CONNECT)
	{
		if(DISABLE_FB_CONNECT_IS_CONNECTED)
			div.innerHTML = html_if_connected;
		else
			div.innerHTML = html_if_not_connected;
	}
	else
	{
		FB.Connect.ifUserConnected
		(
		      function() {div.innerHTML = html_if_connected;}, 
		      function() {div.innerHTML = html_if_not_connected;}
		);
	}

}



function aw_build_page(tab)
{
	
	if(GET_COOKIE('internal_reload') == null)
		SET_COOKIE(config.ID+'_sub_idx',0);
	else
		ERASE_COOKIE('internal_reload');	


	var tab_idx = GET_URL_PARAM('t');
	if(tab_idx != null)
		SET_COOKIE(config.ID+'_tab_idx',tab_idx);

	if(tab != null)
	{
		SET_COOKIE(config.ID+'_tab_idx',tab);
	}
	
	tab_idx = GET_COOKIE(config.ID+'_tab_idx');
	if(tab_idx == null)
	{
		tab_idx = 0;
		SET_COOKIE(config.ID+'_tab_idx',tab_idx);
	}
	
	

	
		
	var p_div = document.getElementById(config.ID+'_content');
	p_div.innerHTML = "<span style='font-size:24px;font-family:arial;line-height:64px;'>Loading Poll&nbsp;&nbsp;&nbsp;<img src='"+config.BASE_INSTALL_URL+"img/loading.gif'/></span>";
	addDOMLoadEvent(function(){do_build_page(config.ID+'_content')});
}


function do_build_page(host_div)
{
	FB.ensureInit(function()
		{
			var p_div = document.getElementById(host_div);
			p_div.innerHTML = '';
			p_div.appendChild(ANCHOR('the_top'));
			build_awards(p_div);	
		
			var pending_anchor = GET_COOKIE(config.ID+'_pending_anchor');
			if(pending_anchor != null)
			{
				document.location.hash = pending_anchor;
				ERASE_COOKIE(config.ID+'_pending_anchor');
			}
		});	
}


function select_tab(tab_idx)
{
	SET_COOKIE(config.ID+'_tab_idx',day_idx);
	SET_COOKIE(config.ID+'_sub_idx',0);
	RELOAD('the_top');
}


function select_sub(idx)
{
	document.location.hash = 'p'+idx;
	update_award_submenu(GET_COOKIE(config.ID+'_tab_idx'),idx);
}

function submit_poll(ctx)
{
	var tab_idx  = ctx.t;
	var sub_idx  = ctx.s;
	var vote 	 = "n"+ctx.v;
	//alert('vote is '+tab_idx+'-'+sub_idx+'='+vote);	

	vote_transpond(tab_idx,sub_idx,vote,function()
		{
			SET_COOKIE(config.ID+'-'+tab_idx+'-'+sub_idx,vote);		
			handle_facebook_connect_share(tab_idx,sub_idx,vote);
		}
	);

}


function handle_facebook_connect_share(tab,sub,vote)
{	
	if(DISABLE_FB_CONNECT)	
	{
		//var old_poll_container = document.getElementById('p'+tab+'-'+sub);
		//var new_poll_container = generate_poll_container(tab,sub,config.tabs[tab].polls[sub],vote);	
		//old_poll_container.parentNode.replaceChild(new_poll_container,old_poll_container);
		RELOAD('p'+sub);
	}
	else
	{
		FB.Connect.ifUserConnected (
			function(){
				var c = document.getElementById("fb_share_check_"+tab+"_"+sub);
            	if(c !=null && c.checked)
            	{
            	    SET_COOKIE('connect-share',1);
            		var uid = FB.Connect.get_loggedInUser();
            		 publish_to_stream(uid,tab,sub,vote);
            	
            	}
            	else
            	{
					//var old_poll_container = document.getElementById('p'+tab+'-'+sub);
					//var new_poll_container = generate_poll_container(tab,sub,config.tabs[tab].polls[sub],vote);	
					//old_poll_container.parentNode.replaceChild(new_poll_container,old_poll_container);
            		SET_COOKIE('connect-share',0);
            		RELOAD('p'+sub);
            	}
            },
			function()
				{
            		//var old_poll_container = document.getElementById('p'+tab+'-'+sub);
					//var new_poll_container = generate_poll_container(tab,sub,config.tabs[tab].polls[sub],vote);	
					//old_poll_container.parentNode.replaceChild(new_poll_container,old_poll_container);
            		RELOAD('p'+sub);
				}
		);
	}
}

var poll_genre_fb_display_map = {"Comedy":"comedy",
                                 "Drama":"drama",
                                 "Reality & Variety":"reality & variety show",
                                 "Animation":"animated show",
                                 "Miniseries":"miniseries"};
function publish_to_stream(fbuser,tab,sub,vote)
{
	var vote_idx = vote.substring(1);
	var poll = config.tabs[tab].polls[sub];
	var poll_title = poll.title;
	var nominee 	= poll.nominees[vote_idx];
	var nominee_img = nominee.img;
	var title = 'TV.com: Best of 2009';
	var link_url = "http://www.tv.com/bestof2009";
	var msg = "I picked "+nominee.title+" for "+poll_title+" in TV.com's Best of 2009 poll. Which shows, scenes, and characters were your favorites last year? Watch videos and cast your vote here: www.tv.com/bestof2009";
	var attachment = {'name':title,
					  'href':link_url,
					  'caption':'',
					  'description':'',	  
					  'media':[{'type':'image','src':nominee_img,
						        'href':'http://www.tv.com/bestof2009'}
						       ]
					  };
	FB.Connect.streamPublish(msg, attachment,null,null,null,function()
		{
					var old_poll_container = document.getElementById('p'+tab+'-'+sub);
					var new_poll_container = generate_poll_container(tab,sub,config.tabs[tab].polls[sub],vote);	
					old_poll_container.parentNode.replaceChild(new_poll_container,old_poll_container);
		
		});
}


function vote_transpond(tab,sub,vote,f)
{
	//alert('vote_transpond '+tab+' '+sub+' '+vote)
	var asset_id = config.ID+'-'+tab+'-'+sub;
	var poll = new Poll(config.TRANSPOND_WIDGET_ID, config.VOTE_SERVICE_URL);
	poll.sendVote(asset_id+"-"+vote, function(data)
			{
				if(f != null)
					f();
			});
}


var SORTED_COUNTERS;
var RESULTS_MAP = new Object();
function load_transpond(tab,sub,percent_divs,totals_divs)
{
	//alert("wid: "+config.TRANSPOND_WIDGET_ID);
	//alert("cid: "+config.TRANSPOND_COMPONENT_ID);
	var asset_id = config.ID+'-'+tab+'-'+sub;
	
	
	var poll = new Poll(config.TRANSPOND_WIDGET_ID, config.VOTE_SERVICE_URL);
	poll.requestStatsWithPrefix(asset_id+"-", function (t_obj)
	{
		var a = new Array(t_obj.data.counters.length);
		var o = new Object();
		var total = 0;
		for(var i=0; i < t_obj.data.counters.length; i++)
		{
			var name 	= t_obj.data.counters[i].name;
			var index 	= parseInt(name.substring(name.lastIndexOf("-")+2));
			o['v'+index] 	= t_obj.data.counters[i];
			total 	   += parseInt(t_obj.data.counters[i].counter);
		}
		

		for(var i=0; i < percent_divs.length; i++)
		{
			var cc		 = o['v'+i]; 
			var percent;

			if(cc != null)
			{
				percent = parseInt(cc.counter) / total;
				//alert('percent is '+percent+' cc.counter is '+cc.counter+' total is '+total+' cc.counter length is '+cc.counter.length);
			}
			else
				percent = 0;
			
			var t;
			if(cc != null)
				t = add_commas(cc.counter)+' votes'
			else
				t = '0 votes';
		
			percent_divs[i].innerHTML = Math.round(percent*100)+"%";	
			totals_divs[i].innerHTML = t;
		}
		
	});
	
/*
	poll.requestStats(function(t_obj)
		{
		
			SORTED_COUNTERS	 = t_obj.data.counters.sort(
					function(a,b)
					{
						return parseInt(a.counter) < parseInt(b.counter) ? 1 : -1;
					});	
		
			var total_votes = 0;				
			for (var i = 0; i < SORTED_COUNTERS.length; i++)
				total_votes += new Number(SORTED_COUNTERS[i].counter);

			var s = "";
			for (var i = 0; i < SORTED_COUNTERS.length; i++)
			{
				RESULTS_MAP['votes-'+SORTED_COUNTERS[i].name]   = add_commas(SORTED_COUNTERS[i].counter);
				RESULTS_MAP['percent-'+SORTED_COUNTERS[i].name] = Math.floor(new Number(SORTED_COUNTERS[i].counter)/total_votes*100);
				s+=(SORTED_COUNTERS[i].name+' '+RESULTS_MAP['votes-'+SORTED_COUNTERS[i].name]+' '+RESULTS_MAP['percent-'+SORTED_COUNTERS[i].name]+'%\n');
			}
			//alert(s);
		});
*/
}



function add_commas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



function build_awards(parent_div)
{
	var tab_idx = GET_COOKIE(config.ID+'_tab_idx');
	var sub_idx = GET_COOKIE(config.ID+'_sub_idx');
	if(sub_idx == null)
	{
		sub_idx = 0;
		SET_COOKIE(config.ID+'_sub_idx',0);
	}
	
	var content_right = MAKE_DIV('content_right','content_right_id');
	
	var polls = config.tabs[tab_idx].polls;
	for(var i = 0;i < polls.length;i++)
	{
		var poll = polls[i];

		var existing_vote_val =  GET_COOKIE(config.ID+'-'+tab_idx+'-'+i);
		var poll_container = generate_poll_container(tab_idx,i,polls[i],existing_vote_val);
		content_right.appendChild(poll_container);
	}
	
	parent_div.appendChild(content_right);

		//NEED TO GRAB THAT OTHER MPU DIV HERE//
	var sub_menu = generate_award_submenu(tab_idx,sub_idx);
	parent_div.appendChild(sub_menu);
	install_video_overlay(parent_div);
}	

function generate_poll_container(tab,sub,poll,existing_vote_val)
{
	var show_totals=true;
	if(!config.TEST_RESULTS && existing_vote_val == null)
		show_totals=false;
	if(config.SHOW_WINNERS)
		show_totals = true;
	
	var nominees = poll.nominees;
	var poll_id = 'p'+tab+'-'+sub;
	var poll_container = MAKE_DIV('poll_container');
	poll_container.setAttribute('id',poll_id);
	poll_container.id = poll_id;
	var poll_header_container = MAKE_DIV('poll_header_container');
	poll_container.appendChild(ANCHOR('p'+sub));
	var poll_header = MAKE_TEXT_DIV(poll.title,'poll_header');
	
	var poll_share_bar = MAKE_DIV('poll_share_bar');	
	var share_html;
	if(existing_vote_val != null)
	{
		var fb_share_link   	  = "javascript:open_fb_sharer_window("+tab+","+sub+","+existing_vote_val.substring(1)+");";
		var twitter_share_link    = "javascript:open_twitter_sharer_window("+tab+","+sub+","+existing_vote_val.substring(1)+");";
		share_html 				  = "SHARE ON &nbsp;&nbsp;<a href='"+fb_share_link+"'><img src='"+config.BASE_INSTALL_URL+"img/fb-icon.png'/></a> <a href='"+twitter_share_link+"'><img src='"+config.BASE_INSTALL_URL+"img/twitter-icon.png'/></a>";
		set_html_if_fb_connected('',share_html,poll_share_bar);	
	}
	else
	{
		if(GET_COOKIE('connect-share') == 0)
			c_val='';
		else
			c_val="checked='on'";
		
		share_html = "<input type='checkbox' id='fb_share_check_"+tab+"_"+sub+"' "+c_val+"/> Publish to Facebook";
		poll_share_bar.className='poll_share_bar_connect';
		set_html_if_fb_connected(share_html,'',poll_share_bar);	
	}
	
		
	poll_header_container.appendChild(poll_header);
	if(!config.SHOW_WINNERS)
		poll_header_container.appendChild(poll_share_bar);
	poll_header_container.appendChild(CLEAR_BOTH());
	poll_container.appendChild(poll_header_container);
	
	var percent_divs = new Array();
	var totals_divs = new Array();
	for(var i = 0;i < poll.nominees.length;i++)
	{
		var pf =  function(){play_video(this.vid,this.pid,this.title,this.subtitle,this.running_time,this.description);}
	
		
		var nominee 	= poll.nominees[i];
		var HAS_VIDEO   = (nominee.video_pid != null);
		nominee.video_id = 'video-'+tab+'-'+sub+'-'+i;
		nominee.vid      = nominee.video_id;
		var poll_item   = MAKE_DIV('poll_item');
		var poll_item_head1	= MAKE_HREF_DIV(nominee.url,nominee.title,'poll_item_head1',null,'_tv_com_page');
		var poll_item_head2	= MAKE_TEXT_DIV((nominee.subtitle == "")?'&nbsp;':nominee.subtitle,'poll_item_head2');
<!-- START -->
	var poll_item_img_container = MAKE_DIV('poll_item_img_container');
	var poll_item_img = MAKE_IMG_DIV(nominee.img,'poll_item_img');
	poll_item_img.url = nominee.url;
	if(HAS_VIDEO)
	{
		poll_item_img_container.vid 			= nominee.video_id;
		poll_item_img_container.pid 			= nominee.video_pid;
		poll_item_img_container.episode_title 	= nominee.episode_title;
		poll_item_img_container.scene_title 	= nominee.scene_title;
		poll_item_img_container.running_time  	= '';
		poll_item_img_container.title   		= nominee.title;
		poll_item_img_container.subtitle  		= nominee.subtitle;
		poll_item_img_container.description   	= nominee.video_description;
		poll_item_img_container.video_id 	  	= nominee.video_id;
		poll_item_img_container.onmousedown   	= pf;
		poll_item_img_container.onmouseover = function(){playbutton_over(this.video_id)};
		poll_item_img_container.onmouseout  = function(){playbutton_out(this.video_id)};	
	}
	else
	{
		poll_item_img.onclick = function()
		{
			var newWindow = window.open(this.url, '_tv_com_page');
			 newWindow.focus();
		}
	}
	
	var poll_item_play_bar = MAKE_DIV('poll_item_play_bar');
	var poll_item_play_bar_transparency = MAKE_DIV('poll_item_play_bar_transparency');
	var bt_id = "bar-transparency-"+nominee.video_id;
	poll_item_play_bar_transparency.id = bt_id;
	poll_item_play_bar_transparency.setAttribute('id',bt_id);
	var poll_item_play_bar_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/play-button.png' onmouseover=\"playbutton_over('"+nominee.video_id+"')\" onmouseout=\"playbutton_out('"+nominee.video_id+"')\" />",'poll_item_play_bar_button');
	poll_item_play_bar_button.vid 			= nominee.video_id;
	poll_item_play_bar_button.pid 			= nominee.video_pid;
	poll_item_play_bar_button.episode_title = nominee.episode_title;
	poll_item_play_bar_button.scene_title   = nominee.scene_title;
	poll_item_play_bar_button.running_time  = '';
	poll_item_play_bar_button.title   		= nominee.title;
	poll_item_play_bar_button.subtitle  	= nominee.subtitle;
	poll_item_play_bar_button.description   = nominee.video_description;
	

	poll_item_play_bar_button.onmousedown = pf;


	
	var poll_item_play_bar_text   = MAKE_TEXT_DIV('Watch Video','poll_item_play_bar_text');
	bt_id = "bar-text-"+nominee.video_id;
	poll_item_play_bar_text.id = bt_id;
	poll_item_play_bar_text.setAttribute('id',bt_id);
	poll_item_play_bar.appendChild(poll_item_play_bar_transparency);
	poll_item_play_bar.appendChild(poll_item_play_bar_button);
	poll_item_play_bar.appendChild(poll_item_play_bar_text);
	poll_item_play_bar.appendChild(CLEAR_BOTH());
	
	poll_item_img_container.appendChild(poll_item_img);
	if(HAS_VIDEO)
		poll_item_img_container.appendChild(poll_item_play_bar);
	else
		;
	
	<!-- END -->
		
	//var poll_item_img	= MAKE_HREF_IMG_DIV(nominee.url,config.BASE_RESOURCE_URL+nominee.img,'poll_item_img');
		
		//var poll_item_img	= MAKE_HREF_IMG_DIV(nominee.url,"img/test-img.jpg",'poll_item_img');
		var poll_item_bottom_bar = MAKE_DIV('poll_bottom_bar');
		var poll_item_vote_button;
		if(show_totals)
		{
			if(config.SHOW_WINNERS)
			{
				if(poll.winner == i)
					poll_item_vote_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/winner.png'/>",'poll_item_winner');					
				else
					poll_item_vote_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/closed.png'/>",'poll_item_closed');					
			}
			else
				poll_item_vote_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/vote-again-button.png' onmouseover=\"this.src='"+config.BASE_INSTALL_URL+"img/vote-again-button-on.png';\" onmouseout=\"this.src='"+config.BASE_INSTALL_URL+"img/vote-again-button.png';\"/>",'poll_item_vote_again_button');
		}
		else
			poll_item_vote_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/vote-button.png' onmouseover=\"this.src='"+config.BASE_INSTALL_URL+"img/vote-button-on.png';\" onmouseout=\"this.src='"+config.BASE_INSTALL_URL+"img/vote-button.png';\"/>",'poll_item_vote_button');
		
		if(!config.SHOW_WINNERS)
		{
			poll_item_vote_button.t 	= tab;
			poll_item_vote_button.s 	 = sub;
			poll_item_vote_button.v 	 = i;
			poll_item_vote_button.onclick = function(){submit_poll(this);}
		}
		
		var poll_item_totals = MAKE_DIV('poll_item_totals');
		var poll_item_percentage;
		if(show_totals)
		{
			poll_item_percentage = MAKE_IMG_DIV(config.BASE_INSTALL_URL+'img/loading.gif','poll_item_percentage');
			percent_divs.push(poll_item_percentage);
		}
		else
			poll_item_percentage = MAKE_TEXT_DIV('','poll_item_percentage');
		
		var poll_item_total = MAKE_TEXT_DIV('233,000 votes','poll_item_total');
		if(show_totals)
		{
			poll_item_total = MAKE_TEXT_DIV('','poll_item_total');
			totals_divs.push(poll_item_total);
		}
		else
			poll_item_total = MAKE_TEXT_DIV('','poll_item_total');
		
		poll_item_totals.appendChild(poll_item_percentage);
		poll_item_totals.appendChild(poll_item_total);
		poll_item_bottom_bar.appendChild(poll_item_vote_button);
		

		
		poll_item_bottom_bar.appendChild(poll_item_totals);
		poll_item.appendChild(poll_item_head1);
		poll_item.appendChild(poll_item_head2);
		poll_item.appendChild(poll_item_img_container);
		poll_item.appendChild(poll_item_bottom_bar);
		poll_container.appendChild(poll_item);
	}
		if(show_totals)
			load_transpond(tab,sub,percent_divs,totals_divs);
		poll_container.appendChild(CLEAR_BOTH());
		return poll_container;
	}

	function playbutton_over(id)
	{
		var bar = document.getElementById('bar-transparency-'+id);
		var text = document.getElementById('bar-text-'+id);
		bar.style.visibility = "visible"; 
		text.style.visibility = "visible"; 
	}
	
	function playbutton_out(id)
	{
		var bar = document.getElementById('bar-transparency-'+id);
		var text = document.getElementById('bar-text-'+id);
		bar.style.visibility = "hidden"; 
		text.style.visibility = "hidden"; 
	}

	var last_watched;
	function play_video(vid,pid,title,subtitle,time,description)
	{
		open_video_overlay(pid,title,subtitle,time,description);
		if(last_watched != null)
		{
			var last_div = document.getElementById('poll_item_'+last_watched);
			last_div.style.outlineWidth = '0px';
		}
		/*
		var current_div = document.getElementById('poll_item_'+vid);
		current_div.style.outlineWidth = '2px';
		current_div.style.outlineStyle = 'dashed';
		current_div.style.outlineColor = '#222';
		*/
		//alert('current_div '+current_div.style.outline);
		last_watched = vid;
		post_metric('/'+config.ID+'/video_view/'+title);
	}


<!-- MAKE SURE THE IMAGE SIZE IN THE POLL ITEM CSS FOR THE THUMBNAIL MATCHES-->
var BACK_TO_TOP_Y_OFFSET = 330;
function generate_award_submenu(tab_idx,selected_idx)
{
	var polls = config.tabs[tab_idx].polls;
	
	var menu  = MAKE_DIV('sub_nav_container','sub_nav_container_id');
	//var title = MAKE_TEXT_DIV( config.tabs[tab_idx].title,'sub_nav_title');
	//menu.appendChild(title);
	if(selected_idx == null)
		selected_idx = 0;
	
	for(var i = 0;i < polls.length;i++)
	{
		var title = polls[i].title;
		var item;
		if(i == selected_idx)
			item = MAKE_TEXT_DIV(title,'sub_nav_element_selected');	
		else
		{
			var url = 'javascript:select_sub('+i+')';
			var link = '<a href='+url+'>'+title+'</a>';
			item = MAKE_TEXT_DIV(link,'sub_nav_element');	
		}
		menu.appendChild(item);
	}
	
	for(var i = 1;i < polls.length;i++)
	{
		var poll_id = 'p'+tab_idx+'-'+i;
		var poll_container = document.getElementById(poll_id);
		var y = getY(poll_container) - BACK_TO_TOP_Y_OFFSET;
		//alert('poll_conatienr '+poll_container+" y:"+getY(poll_container));
		
		var title = polls[i].title;
		var item;
		var url = 'javascript:select_sub('+0+')';
		var link = "<a href='"+url+"'>Back To Top<img class='arrow-up' src='"+config.BASE_INSTALL_URL+"img/blue-arrow-up.png'/></a>";
		var classname = (i==1)?'back_to_top_first':'back_to_top';
		item = MAKE_TEXT_DIV(link,classname);	
		item.style.top = y+'px';
		menu.appendChild(item);
		
		
	
	}
	return menu;
}

function update_award_submenu(tab_idx,selected_idx)
{
	var polls = config.tabs[tab_idx].polls;
	
	var menu  = document.getElementById('sub_nav_container_id');
	menu.innerHTML = '';
	
	if(selected_idx == null)
		selected_idx = 0;
	
	for(var i = 0;i < polls.length;i++)
	{
		var title = polls[i].title;
		var item;
		if(i == selected_idx)
			item = MAKE_TEXT_DIV(title,'sub_nav_element_selected');	
		else
		{
			var url = 'javascript:select_sub('+i+')';
			var link = '<a href='+url+'>'+title+'</a>';
			item = MAKE_TEXT_DIV(link,'sub_nav_element');	
		}
		menu.appendChild(item);
	}
	
		for(var i = 1;i < polls.length;i++)
	{
		var poll_id = 'p'+tab_idx+'-'+i;
		var poll_container = document.getElementById(poll_id);
		var y = getY(poll_container) - BACK_TO_TOP_Y_OFFSET;
		//alert('poll_conatienr '+poll_container+" y:"+getY(poll_container));
		
		var title = polls[i].title;
		var item;
		var url = 'javascript:select_sub('+0+')';
		var link = "<a href='"+url+"'>Back To Top<img class='arrow-up' src='"+config.BASE_INSTALL_URL+"img/blue-arrow-up.png'/></a>";
		var classname = (i==1)?'back_to_top_first':'back_to_top';
		item = MAKE_TEXT_DIV(link,classname);	
		item.style.top = y+'px';
		menu.appendChild(item);
		
		
	
	}

}

function remove_html_entities(s,c)
{
	if(c == null)
		c = '-';
	var r = s.replace(/\&[a-zA-Z0-9_]*\;/g, c);
	return r;
}

function install_video_overlay(p_div)
{
	var video_overlay 				= MAKE_DIV('video_overlay','video_overlay_id');
	var video_overlay_transparency 	= MAKE_DIV('video_overlay_transparency');
	var video_player_container     	= MAKE_DIV('video_player_container','video_container_id');
	var video_player_container_close_button = MAKE_TEXT_DIV("<img src='"+config.BASE_INSTALL_URL+"img/close-button-video.png' />",'video_player_container_close_button');
	video_player_container_close_button.onmousedown = close_video_overlay;
	var video_player					= MAKE_DIV('video_player','video_player_id');
	var video_player_text_container 	= MAKE_DIV('video_player_text_container');
	var video_player_title_subtitle_container = MAKE_DIV('video_player_title_subtitle_container');
	var video_player_text_title     	= MAKE_TEXT_DIV('','video_player_title','video_player_title_id');
	var video_player_text_subtitle     	= MAKE_TEXT_DIV('','video_player_subtitle','video_player_subtitle_id');
	var video_player_text_description   = MAKE_TEXT_DIV('','video_player_description','video_player_description_id');
	var video_player_text_time     		= MAKE_TEXT_DIV('2:00','video_player_time','video_player_time_id');
	

	video_player_title_subtitle_container.appendChild(video_player_text_title);
	video_player_title_subtitle_container.appendChild(video_player_text_subtitle);
	video_player_text_container.appendChild(video_player_title_subtitle_container);
	video_player_text_container.appendChild(video_player_text_description);
	video_player_text_container.appendChild(video_player_text_time);
	
	video_overlay.appendChild(video_overlay_transparency);
	video_player_container.appendChild(video_player_container_close_button);
	video_player_container.appendChild(video_player);
	video_player_container.appendChild(video_player_text_container);
	video_overlay.appendChild(video_player_container);
	
	document.body.insertBefore(video_overlay, document.body.firstChild)
	//only do this if it is showing// 
	window.onscroll = center_video_player;
	window.onresize = center_video_player;
	set_video();
	center_video_player();
}

function open_video_overlay(clip_id,title,subtitle,time,description)
{
	
	var vo 						= document.getElementById('video_overlay_id');
	var title_div 				= document.getElementById('video_player_title_id');
	title_div.innerHTML 		= title;
	var subtitle_div 			= document.getElementById('video_player_subtitle_id');
	subtitle_div.innerHTML 		= subtitle;
	
	var description_div 		= document.getElementById('video_player_description_id');
	description_div.innerHTML 	= description;
	var time_div 				= document.getElementById('video_player_time_id');
	time_div.innerHTML 			= (time == null)?subtitle:time;
	
	
	set_video(clip_id);
	vo.style.visibility = 'visible';
}

function close_video_overlay()
{
	var vo = document.getElementById('video_overlay_id');
	var vp = document.getElementById('video_player_id');
	vo.style.visibility = 'hidden';
	vp.innerHTML = '';
	window.onscroll = null;
	window.onresize = null;
}


var SF_VIDEO_PLAYER_WIDTH = 640;
var SF_VIDEO_PLAYER_HEIGHT = 360;
function center_video_player()
{
	
	var top 	= (GetHeight()-SF_VIDEO_PLAYER_HEIGHT)/2;
	var left 	= (GetWidth()-SF_VIDEO_PLAYER_WIDTH)/2;		
	var player 	= document.getElementById('video_container_id');
	//alert("sh:"+screen.height+" sw:"+screen.width+" t:"+top+" l:"+left);
	player.style.top  = top+'px';
	player.style.left = left+'px'; 
}

function set_video(clip_id)
{

	//alert('attemting to open pid '+clip_id);
	 var so = new SWFObject("http://www.cbs.com/thunder/player/1_0/partner/can/1_7_0/canplayer.swf", "canPlayer", "640", "360", "9", "#000000");
					                     so.addParam("quality", "high");
					                     so.addParam("scale", "noscale");
					                     so.addParam("menu", "true");
					                     so.addParam("salign", "tl");
					                     so.addParam("allowScriptAccess", "always");
					                     so.addParam("wmode", "transparent");
					                     so.addParam("allowFullScreen", "true");
					                     //so.addVariable("prevImg", "");  //optional variable, the value should be the fully qualified thumbnail url
					                     so.addVariable("autoPlayVid", "true");
					                    // so.addVariable("link", ""); //optional variable, the link on the link tab, requires config setup
					                     //so.addVariable("pid", "X0YiET1ZMZVCylcg3LYUqikg2_N87nuQ"); // fall
					                     so.addVariable("pid",clip_id); // astro
					                     so.addVariable("partner", "tvcom"); //PARTNER code, please make sure correct partner value is passed in
					                     so.addVariable("config", "http://grid.transpond.com/iwidget/TVCOM_BEST_OF_09/tv_com_video_player_config.xml"); //optional variable, location of your config
	so.write('video_player_id');
	
}



function open_fb_sharer_window(tab,sub,vote_idx)
{
	var poll = config.tabs[tab].polls[sub];
	var poll_title = poll.title;
	var nominee 	= poll.nominees[vote_idx];
	var nominee_img = nominee.img;
	var title = 'TV.com: Best of 2009';
	var link_url = "http://www.tv.com/bestof2009";
	var msg = "I picked "+nominee.title+" for "+poll_title+" in TV.com's Best of 2009 poll. Which shows, scenes, and characters were your favorites last year? Watch videos and cast your vote here: www.tv.com/bestof2009";
	msg = remove_html_entities(msg);	
	
	var share_link = "http://www.facebook.com/sharer.php?u="+escape("http://grid.transpond.com/sharetofb.php?title="+title+"&description="+msg+"&image_src="+nominee_img+"&url="+escape(link_url));
	var popup = window.open(share_link,'sharer_win',
							'left=200,top=200,width=640,height=480,toolbar=1,resizable=0');
	popup.focus();


}

function open_twitter_sharer_window(tab,sub,vote_idx)
{
	var poll = config.tabs[tab].polls[sub];
	var poll_title = poll.title;
	var nominee 	= poll.nominees[vote_idx];
	var title 		= 'TV.com: Best of 2009';
	var msg = "I picked "+nominee.title+" for "+poll_title+" in TV.com's Best of 2009 poll. VOTE NOW: www.tv.com/bestof2009";

	msg = encodeURI(remove_html_entities(msg)).split('%20').join('+');
	var share_link = "http://twitter.com/home?status="+msg;
	//alert(share_link);
	var popup = window.open(share_link,'t_sharer_win',
							'left=200,top=200,width=550,height=480,toolbar=1,resizable=1,scrollbars=1');

	popup.focus();
}

function MAKE_DIV(classname,id)
{
	var div = document.createElement('div');
	div.className = classname;
	div.setAttribute('class',classname);
	div.id = id;
	div.setAttribute('id',id);
	return div;
}

function MAKE_TEXT_DIV(text,classname,id)
{
	var div = MAKE_DIV(classname,id);
	div.innerHTML = text;
	return div;
}

function MAKE_HREF_DIV(url,text,classname,id,target)
{
	var div = MAKE_DIV(classname,id);
	var t = (target == null)?"":"target='"+target+"'";
	div.innerHTML = "<a href='"+url+"' "+t+">"+text+"</a>";
	return div;
}

function MAKE_IMG_DIV(src,classname,id)
{
	var div = MAKE_DIV(classname,id);
	div.innerHTML = "<img src='"+src+"'/>";
	return div;
}

function MAKE_HREF_IMG_DIV(url,src,classname,id)
{
	var div = MAKE_DIV(classname,id);
	div.innerHTML = "<a href='"+url+"'><img src='"+src+"'/></a>";
	return div;
}

function MAKE_ID_IMG_DIV(id,src,classname,did)
{
	var div = MAKE_DIV(classname,did);
	div.innerHTML = "<img src='"+src+"' id='"+id+"'/>";
	return div;
}

function CLEAR_BOTH()
{
	var d = MAKE_DIV();
	d.style.clear = 'both';
	return d;
}


/* UTIL */
var aw_cookie = {id:config.ID+'_cookie'};
var __the_cookie__ = aw_cookie;
var cinit = false;
function SET_COOKIE(c_name,value,expiredays)
{
	if(cinit == false)
	{
		GET_COOKIE("");
		cinit = true;
	}
	
	if(value == null)
		delete __the_cookie__[c_name];
	else
		__the_cookie__[c_name] = value;
	
	var flattened_cookie = flatten_cookie();
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie= __the_cookie__.id+ "=" +flattened_cookie+
	"; path=/" ;
}

function flatten_cookie()
{
	var s="";
	for(key in __the_cookie__ )
	{
		s+= key;
		s+= '=';
		s+= __the_cookie__[key];
		s+= '&';
	}
	return escape(s); 
}


function GET_COOKIE(c_name)
{
	var unflattened_cookie = unflatten_cookie();
	var pairs = unflattened_cookie.split('&');
	for(var i = 0;i < pairs.length;i++)
	{
		var ppair = pairs[i].split('=');
		__the_cookie__[ppair[0]]=ppair[1];
	}
	//alert("cookie "+c_name+" is "+__the_cookie__[c_name]);
	return __the_cookie__[c_name];
}

function unflatten_cookie()
{
	if (document.cookie.length>0)
	  {
		  c_start=document.cookie.indexOf(__the_cookie__.id+ "=");
		  if (c_start!=-1)
		    {
			    c_start=c_start + __the_cookie__.id.length+1;
			    c_end=document.cookie.indexOf(";",c_start);
			    if (c_end==-1) c_end=document.cookie.length;
				//alert(document.cookie.substring(c_start,c_end));
			    return unescape(document.cookie.substring(c_start,c_end));
		    }
	  }
	  return "";
}


function ERASE_COOKIE(name) 
{
	SET_COOKIE(name,null,-1);
}

function GET_URL_PARAM( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  else
    return results[1];
}

function RELOAD(anchor)
{
	SET_COOKIE('internal_reload',"yes");
	
	if(anchor != null)
		SET_COOKIE(config.ID+'_pending_anchor',anchor);

	document.location.href = document.location.href.replace(/#.*/, "");	
}

function GOTO_TOP(anchor)
{
	SET_COOKIE('cat_idx',0);
	RELOAD('the_top');
}



function ANCHOR(name)
{
	var a = document.createElement('a');
	a.setAttribute('name',name);
	a.name = name;
	a.id   = name;
	return a;
}

//Get Y position
function getY(element,relative_to)
{
	var y=0;
	
	while(element)
	{
		//alert(element.id);
		y += element.offsetTop;
		if(relative_to != null && relative_to.id == element.id)
		{
			return y;
		}
		element=element.offsetParent;

	}
	return y;
}


      function GetWidth()
      {
          var x = 0;
          if (self.innerHeight)
          {
                  x = self.innerWidth;
          }
          else if (document.documentElement && document.documentElement.clientHeight)
          {
                  x = document.documentElement.clientWidth;
          }
          else if (document.body)
          {
                  x = document.body.clientWidth;
          }
          return x;
      }

       

	function GetHeight()
 	{
          var y = 0;
          if (self.innerHeight)
          {
                  y = self.innerHeight;
          }
          else if (document.documentElement && document.documentElement.clientHeight)
          {
                  y = document.documentElement.clientHeight;
          }
          else if (document.body)
          {
                  y = document.body.clientHeight;
          }
          return y;
      }
	
function post_metric(key)
{
	pageTracker._trackEvent('feature_bestof2009', key);
}
