var base_url = 'http://' + document.domain;
var swfupload_url = '/swfupload-ya.swf';
var upload_url = '/files/flashupload';
var swfu = new Array();
var isIE = (navigator.appName.indexOf("Microsoft")!=-1)?1:0;
var registeredPolls = new Array();

//function $(eId) { return document.getElementById(eId); }

function registerPoll(pollId)
{
	registeredPolls.push(pollId);
}

function addFriend(username, target)
{
 	jQuery("#" + target).load('/f/contacts/add/subscribe/' + username);
}

function goInvite(poll_id)
{
	top.location = '/polls/invite/' + poll_id
}

function uploadProgressFunction(file, bytesloaded, bytestotal) {
	
	//DVIR - Not tested to work
	var target = swfu[file.callerid].getSetting('target');
	//var progress = $('pg_' + target);
	//Element.show(progress);
	jQuery('#pg_' + target).show()
	var percent = Math.ceil((bytesloaded / bytestotal) * 100);
	//Element.update(target,'loading: ' + percent + '%');	
	target.innerHTML = "loading: " + percent + "%";
}

function uploadFileComplete(file) {
	//DVIR - Not tested to work
	var target = swfu[file.callerid].getSetting('target');
	//Element.hide('pg_' + target);
	jQuery('#pg_' + target).hide();
	//Element.update(target,'<a href="javascript:void(0);" onclick="uploadRemoveFile(\' + id + \');">remove file</a>');
	target.innerHTML = '<a href="javascript:void(0);" onclick="uploadRemoveFile(\' + id + \');">remove file</a>';
}

function switchTab(type, tab, content) {
	var tabs = Element.getElementsByClassName('tabs' + (type ? '_' + type : ''), 'current');
	for (var i=0; i<tabs.length; i++) {selected = tabs[i].id;Element.removeClassName(tabs[i],'current');}
	Element.toggleClassName('tab_' + tab,'current');
	if (content) {
		Element.removeClassName(content,selected);
		Element.addClassName(content,'tab_'+tab);
	}
	lastSelected = tab;
} 

function uploadRemoveFile(id) {swfu[id].loadUI();}

function initSwfupload(id, eid, filesize, filetypes, filedescription) {
	var movieCount = SWFUpload.movieCount;
	swfu[id] = new SWFUpload({
		key: id,
		browse_link_innerhtml: 'upload file...',
		upload_script: upload_url,
		target: eid,
		flash_path: swfupload_url,
		allowed_filesize: filesize,
		allowed_filetypes: filetypes,
		allowed_filetypes_description: filedescription,
		flash_loaded_callback: "swfu['" + id + "'].flashLoaded",
		upload_progress_callback: 'uploadProgressFunction',
		upload_file_complete_callback: 'uploadFileComplete',
		auto_upload: true
	});
}

function initTinyMCE(type) {
//	alert(tinyMCE);
	if (type == 'simple') {
		tinyMCE.init({theme : "bare", mode : 'none', convert_urls : false, width: '450px', height: '20px'});
	}
	else tinyMCE.init({theme : "bare", mode : 'none', convert_urls : false, width: '100%', height: '40px'}); 
}

function hideFlash() {
	
	/*var messages = document.getElementsByClassName('message');
	for (i = 0; i<messages.length; i++) 
		Element.Hide(messages[i]);
	*/
	//DVIR - Not tested to work
	jQuery(".message").hide();
}

function isEmail(src) {
	 src = src.replace(/^\s*|\s*$/g,'');
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
}

function updateBarsChart(eid, url) {
	//XXX - 
	new Ajax.Updater(eid,url + '?eid=' + eid, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', eid]});
}

/* utils functions */
function ballon(type, eid, data, displaytime, position) {
	url = false;
	created = false;
	if (data && data.charAt(0) == '/') url = data;
	if (!$(eid + '_' + type)) {
		var obtn = $(eid + '_btn_' + type);
		//XXX Eran, I couldn't find a matching function in jQuery. Do you know of any? I can write a function like that, but should I?
		pos = Position.cumulativeOffset(obtn);
		var top = pos[1];
		var side = pos[0] + (Element.getWidth(obtn) / 2);
		new Insertion.Top(document.body,'<div id="' + eid + '_' + type +'" class="btn_popup ballon_' + type + (position ? '_' + position : '') + '" style="display:none;"><a href="javascript:Element.hide(\'' + eid + '_' + type + '\');void(0);" class="btn btn_close">&nbsp;</a><div id="' + eid + '_' + type + '_space" class="content">' + (!url ? data : '') + '</div></div>');
		$(eid + '_' + type).style.top = top - (position == 'top' ? -27 : Element.getHeight(eid + '_' + type)) + 'px';
		side -= Element.getWidth(eid + '_' + type); 
		$(eid + '_' + type).style.left = (side + (position == 'left' ? -80 : 80)) + 'px';
		created = true;
	}
	if (!$(eid + '_' + type).visible() && url) {
		//XXX
                new Ajax.Updater(eid + '_' + type + '_space',url + '?eid=' + eid, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', eid + '_' + type + '_space']});
        }
	if (displaytime == undefined || !created) { 
		//Element.toggle(eid + '_' + type);
		//DVIR - Not tested to work
		jQuery('#' + eid + '_' + type).toggle();
	}
	else if (displaytime) {
		//setTimeout("Element.hide('" + eid + "_" + type + "');",displaytime);
		//DVIR - Not tested to work
		setTimeout("jQuery('#" + eid + "_" + type + "').hide();",displaytime);
	}
	
	//Element.show(eid + '_' + type);
	//DVIR - Not tested to work
	jQuery("#" + eid + "_" + type).show();

}
/* end utils functions */
/* login functions */
var unamereq = null;
function validusername(silent) {
	
	//DVIR - TESTED
	var uname = jQuery('#user_username').val();
	if (uname && uname.length >= 3) {
		return true;
	}
	
	if (!silent) {
		jQuery('#checkusername').html('<div class="error_message">Must be at least 3 characters and/or numbers with no spaces.</div>');
		return false;
	}
}

function checkemail(email, err) {

	//DVIR - TESTED
	var msg = isEmail(email) ? '' : '<div class="error_message">E-mail address is invalid.</div>';
	jQuery("#" + err).html(msg);
	
}

function checkpassword(passwd, minlen, err) {
	
	//DVIR - TESTED
	var msg = (passwd.length < minlen) ? '<div class="error_message">Password must be at least 6 characters long.</div>' : '';
    jQuery("#" + err).html(msg);
}

function checkusername(request) {
	if (unamereq) 
		unamereq.abort();
	unamereq = request;
}
/* end login functions */
/* search functions */

//DVIR - Tested
function fieldFocus(field,text) {q = document.getElementById(field); if (q && q.value == text) q.value = ''; q.style.color = '#000';}
//DVIR - Tested
function fieldPrepare(field,text) {q = document.getElementById(field); if (q && q.value == text) q.value = '';}
//DVIR - Tested
function fieldBlur(field,text) {
	q = document.getElementById(field);
	if (q && q.value == "")  {
		q.value = text;
		 q.style.color = '#c0c0c0';
	}
}

/* end search functinos */

/* polls functions */

var global_create_poll = null;

function checkaddpoll() {
	pageTracker._trackPageview('/polls/added');
	//tinyMCE.triggerSave();
	if (jQuery('#create_poll_title').val().length > 0) {
		var othUsername = jQuery.cookie('othAuth');
		if (!othUsername && jQuery("#combo").length > 0) {
			var combo = jQuery("#combo");
			combo.css({position:'absolute', left:(jQuery(window).width()/2)-(combo.width()/2), top:(document.body.scrollTop ||document.documentElement.scrollTop)+20+'px'});
			combo.jqmShow();
			//jQuery('.jqmWindow').css('z-index', '2')
			//jQuery('.jqmOverlay').css('z-index', '1')
			global_create_poll = 1;
			
			if (jQuery('#poll_partner').val().length > 0)
			{
				jQuery(".pop_login").show()
				jQuery(".pop_register").hide()
				jQuery('#pop_login').find('.gigya').hide()
				jQuery('#pop_login').find('.continue').hide()
			}
			
			jQuery('#pop_reg_create_title').show();
			jQuery('.b_skip').hide();
			pageTracker._trackPageview('/register/popup_addpoll_reg_view/');
			return(false);
		}
		else { //user logged in.
			jQuery('#create_poll_form').submit();
			//return(true);
		}
	}
	else alert('Please fill in poll details');
	return(false);
}

var global_eid = null;
var global_special = null;
var global_answer_me = 0;
var numOfGuestVotings = 0;
var global_votenow = 0;


function getPopLogin(){
	alert("Hello");
}


function checkpoll(eid,special) {
	numOfGuestVotings = (parseInt(jQuery.cookie('numOfGuestVotings')))? parseInt(jQuery.cookie('numOfGuestVotings')):0;
	numOfGuestVotings = numOfGuestVotings + 1;
	jQuery.cookie('numOfGuestVotings', numOfGuestVotings, {expires: 0, path: '/'});
	var options = Form.getInputs(eid + '_poll_form','radio','oid');
	//var options = jQuery('#' + eid + '_poll_form');
	//-DVIR - Not Tested
	//-DVIR var options = jQuery("#" + eid + "_poll_form > radio[name='oid']").get();
	//alert(options.length);
	var i = 0;
	while (i < options.length && !options[i].checked) 
	{
		i++;
	}
	if (i < options.length && options[i].checked) {
		//$(eid + '_poll_form').action = '/votes/vote';
		jQuery('#' + eid + '_poll_form').attr('action', '/votes/vote');
		// if (true..
		//if ((jQuery("#combo").length > 0 && (numOfGuestVotings == 2 || (numOfGuestVotings>2&&(numOfGuestVotings-1)%3==0)))){
		var othUsername = jQuery.cookie('othAuth');
		if (!othUsername && (jQuery("#combo").length > 0 && (numOfGuestVotings == 2 || (numOfGuestVotings>1&&(numOfGuestVotings-1)%5==0)))){
			global_eid = eid;
			jQuery("#skip").show();
			global_special = special;
			register_popup();
			pageTracker._trackPageview('/register/popuppageview/');
			return(0);
		}
		else {
			//Dvir - not tested
			jQuery('#' + eid + '_poll_form').submit();
			//new Ajax.Updater(eid + '_poll_vote','/votes/vote/', {asynchronous:true, evalScripts:true, parameters:$(eid + '_poll_form').serialize(), requestHeaders:['X-Update', '' + eid + '_poll_vote'], onComplete: function() {showvote(eid,special);}});
		}
	} else {
		ballon('vote',eid,'',5000);
	}
}

function register_popup() {
	var combo = jQuery("#combo");
	var othUsername = jQuery.cookie('othAuth');
	if (!othUsername && combo)
	{
		combo.css({position:'absolute', left:(jQuery(window).width()/2)-(398/2) +'px', top:(document.body.scrollTop ||document.documentElement.scrollTop)+20+'px', width: '398px'});
		combo.jqmShow();
		//jQuery('.jqmWindow').css('z-index', '2')
		//jQuery('.jqmOverlay').css('z-index', '1')
		//$(".pop_register").show();
		//$(".pop_guest").hide();
		jQuery('#pop_reg_vote_title').show();
		jQuery('#your_vote').show();
		jQuery('.b_skip').show();
		jQuery('.no_thanks').hide();
		//jQuery('#RegisterFormVersion').val(3)
		//jQuery('#passwordField').show()
		//jQuery('#buddyIconField').show()
		jQuery('.extraCss').remove()
		jQuery('#pop_reg_create_gigya').hide()
		var html = '<style>.gigyaField {display:block;}</style>'
		jQuery('head').append(html);
	}
	//if (!jQuery.cookie('othAuth'))
		//gigya.services.socialize.showLoginUI(gigyaConf, gigyaParamsA);
}

/*function guest_popup() {
	var combo = jQuery("#combo");
	combo.css({position:'absolute', left:(jQuery(window).width()/2)-(398/2) +'px', top:(document.body.scrollTop ||document.documentElement.scrollTop)+20+'px', width: '398px'});
	combo.jqmShow();
	$(".pop_register").hide();
	$(".pop_guest").show();
	//jQuery('#combo_guest').show();
	//jQuery('#combo_register').hide();
	jQuery('#pop_reg_vote_title').show();
	jQuery('#your_vote').show();
	jQuery('.b_skip').show();
	jQuery('.no_thanks').hide();
}*/

/* end of polls functions */

var global_cid = null;
var reply_parent_id = null;
function checkcomment(cid) {
	try{ tinyMCE.triggerSave();} catch(e){}
//	alert(cid);
	//if ($(cid + '_text').value.length > 0) {
	
	if (jQuery('#' + cid + '_text').val().length > 0) {
		/*
		if ($(cid + '_captcha').value.length > 0) {
			var ajrq = new Ajax.Request('/comments/captcha/' + $(cid + '_captcha').value,{asynchronous: false});
			if (eval(ajrq.transport.responseText)) return(true);
			else alert('We sure you are human, please verify again the image text or, click on the image to get a new text');
		}
		else alert('Sorry, we must verify you are human, please write the text you see in the image');
		*/
		
		var othUsername = jQuery.cookie('othAuth');
		global_cid = cid;
		if (!othUsername && jQuery("#combo").length > 0) {
			var combo = jQuery("#combo");
			combo.css({position:'absolute', left:(jQuery(window).width()/2)-(combo.width()/2), top:(document.body.scrollTop ||document.documentElement.scrollTop)+20+'px'});
			combo.jqmShow();
			//jQuery('.jqmWindow').css('z-index', '2')
			//jQuery('.jqmOverlay').css('z-index', '1')
			jQuery('#pop_reg_comment_title').show();
			jQuery('.b_skip').hide();
			return(true);
		}
		else {
			//$(cid + '_form').submit();
			var options = {
			      type: 'post',
			      success: function(responseXML) 
			      {
			      	
			      	if(reply_parent_id)
			      	{
			      		jQuery('#'+global_cid).after(responseXML);
			      		var ch = jQuery('#'+global_cid).next();
		//	      		alert(ch);
			      		jQuery(ch).fadeIn();
			      		jQuery('#'+global_cid+'_reply').hide();
			      	}
			      	else
			      	{
			      		jQuery('#commentsdiv').prepend(responseXML);
			      		var ch = jQuery('#commentsdiv').children();
			      		jQuery('#' +ch[0].id).fadeIn();
			      		try
			      		{
			      		
			      			tinyMCE.execInstanceCommand(global_cid+'_text', 'mceSetContent', false, ' ');
			      		}
			      		catch(e)
			      		{
			      			jQuery('#'+global_cid+'_text').val('');
			      		}
			      	}
			      	
		      	/*	if(jQuery('#topicSearchWord'))
		      		{
		      			jQuery('input#srch').val(jQuery('#topicSearchWord').text());
			      		searchSubmit();
			      	}
					else			      	
			      		document.location.reload();*/
			      }
			};
  			jQuery('#' + cid + '_form').ajaxSubmit(options);
			//jQuery('#' + cid + '_form').submit();
		}
	}
	else alert('Please fill in comment');
	return(false);
}

function _comment(eid, comment_id, action, obj_type, obj_id, level) {

	if ($(eid + '_' + action)) {
		//XXX
		Element.remove($(eid + '_' + action));
	} else {
		//XXX
		new Insertion.After($(eid),'<div id="' + eid + '_' + action + '" class="addcomment"></div>');
		new Ajax.Updater('' + eid + '_' + action + '','/comments/' + action + '/' + comment_id + '?obj_type=' + obj_type + '&obj_id=' + obj_id + '&parent_id=' + comment_id + '&eid=' + eid + '&action=' + action + '&level=' + level + '', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', '' + eid + '_' + action + '']});
		reply_parent_id = comment_id;
//		window.location = '#' + eid;
	}
}

function updateChart(o, file) {
	//Dvir - Not Tested
	var FCObject = (navigator.appName.indexOf("Microsoft") !=-1 && !window.opera) ? document.getElementById(o) : document[o];
	FCObject.SetVariable('_root.xml_file', file);
}

function updateAmChartBig(obj, target, data) 
{
	so = new SWFObject('/charts/ampie.swf',obj,253,253,8,'#f4f4f4');
	so.addParam("wmode", "transparent");
	so.addVariable("settings_file", escape('/charts/ampie_big_settings.xml'));		
	so.addVariable("data_file", escape(data));		

	so.write(target);
}

function popUp(URL,width,height) {
	day = new Date();
	id = day.getTime();
	eval("window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + ",left = 0,top = 0');");
}

function grabPoll(eid, pid, width,tag) {
	code = upwidget(eid,['id','pid','width','bg','color','tag'],[eid,pid,width,'#C0CBD9','#000000',tag],false);
        html = '<label for="' + eid + '_code" class="sidebyside">Embed:</label><input id="' + eid + '_code" class="grabcode" onclick="this.select();" /><br />';
        html += '<a href="/polls/grab/' + pid + '">customize widget</a>';
        ballon('grab',eid,html,0);
	var ocode = $(eid + '_code');
	ocode.value = code;
}

var swfparams = new Object();
var widget_width = null;
var widget_height = null;
function upwidget(key,param,value,show) {
	movie = widget(key,param,value,show);
	embed = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="%%width%%" height="%%height%%" id="%%ido%%"><param name="bgcolor" value="%%bg%%" /><param name="movie" value="%%movie%%" /><param name="allowScriptAccess" value="always" /><param name="wmode" value="transparent" /><embed src="%%movie%%" bgcolor="%%bg%%" width="%%width%%" height="%%height%%" id="%%ide%%" type="application/x-shockwave-flash" allowScriptAccess="always" wmode="transparent"></embed></object>';
	embed = embed.replace(/\%\%movie\%\%/g,movie);
	embed = embed.replace(/\%\%bg\%\%/g,swfparams[key]['bg']);
	embed = embed.replace(/\%\%width\%\%/g,swfparams[key]['width']);
	embed = embed.replace(/\%\%height\%\%/g,(swfparams[key]['height'] ? swfparams[key]['height'] : 300));
	embed = embed.replace(/\%\%ido\%\%/g,swfparams[key]['id']+'_obj');
	embed = embed.replace(/\%\%ide\%\%/g,swfparams[key]['id']+'_emb');
	var tag = swfparams[key]['tag'];
	if (tag) embed += '<br /><a href="' +base_url + '/tags/' + tag + '">more polls about ' + tag + '</a><br />';
	else embed += '<br /><a href="' +base_url + '/polls">more polls, boss</a><br />';
	
	//if ($(key + '_embed')) $(key + '_embed').value = embed;
	//Dvir - Not Tested
	jQuery("#" + key + "_embed").val(embed);
	return(embed);
}

function widget(key,param,value,show) {
	swfparams[key] = swfparams[key] ? swfparams[key] : {};
	if (typeof(param) === 'object') {
		for (i=0; i<param.length; i++) swfparams[key][param[i]] = value[i];
	} else swfparams[key][param] = value;
	
	movie = base_url + "/apps/pollsbw?ws=" + base_url + "/api?wsdl";
	for (var param in swfparams[key]) {
		if (param == 'width') movie = movie + '&wdth=' + swfparams[key][param];
		else if (swfparams[key][param]) movie = movie + '&' + param + '=' + (param == 'bg' || param == 'color' ? swfparams[key][param].substring(1) : swfparams[key][param]);
	}
	var width = widget_width ? widget_width : (swfparams[key]['width'] ? swfparams[key]['width'] : 400);
	if (swfparams[key]['pid']) {
		//XXX
		var ajrq = new Ajax.Request('/apps/pollsbwh/' + swfparams[key]['pid'] + '/' + width + (swfparams[key]['font'] ? '/' + swfparams[key]['font'] : '/Arial') + (swfparams[key]['fontsize'] ? '/' + swfparams[key]['fontsize'] : ''),{asynchronous: false});
		swfparams[key]['height'] = ajrq.transport.responseText;
	}
	
	//Dvir - Not Tested
	if (jQuery("#" + key).length  && show) {
		var height = widget_height ? widget_height : (swfparams[key]['height'] ? swfparams[key]['height'] : 300);
		var so = new SWFObject(movie + '&demomode=true',key + (isIE ? '_obj' : '_emb'),width,height,8,swfparams[key]['bg']);
		so.addParam("wmode","transparent");
		so.write(key);
	}
	return(movie);
}

var perline = 9;
var divSet = false;
var curId;
var colorLevels = Array('0', '3', '6', '9', 'C', 'F');
var colorArray = Array();
var ie = false;
var nocolor = 'none';
if (document.all) { ie = true; nocolor = ''; }

function addColor(r,g,b) {
	var red = colorLevels[r];
	var green = colorLevels[g];
	var blue = colorLevels[b];
	addColorValue(red, green, blue);
}

function addColorValue(r,g,b) {colorArray[colorArray.length] = '#' + r + r + g + g + b + b;}

function setColor(color) {
	var link = $(curId);
	var field = $(curId + 'field');
	var picker = $('colorpicker');
	field.value = color;
	if (color == '') {
		link.style.background = nocolor;
		link.style.color = nocolor;
		color = nocolor;
	} else {
		link.style.background = color;
		link.style.color = color;
	}
	picker.style.display = 'none';
	eval(document.getElementById(curId + 'field').title);
}

function setDiv() {
	if (!document.createElement) {return(false);}
	var elemDiv = document.createElement('div');
	if (typeof(elemDiv.innerHTML) != 'string') { return; }
	genColors();
	elemDiv.id = 'colorpicker';
	elemDiv.style.display = 'none';
//        elemDiv.innerHTML = '<div class="title">Pick a color: ' +  '(<a href="javascript:setColor(\'\');">No color</a>)</div>';
	elemDiv.innerHTML += getColorTable();
	document.body.appendChild(elemDiv);
	document.body.insertBefore(elemDiv,document.body.firstChild);
	divSet = true;
	return(true);
}

function pickColor(id) {
	if (!divSet) {setDiv();}
	var picker = $('colorpicker');
	if (id == curId && picker.style.display == 'block') {
		picker.style.display = 'none';
		return;
	}
	curId = id;
	//XXX Eran, again, jQuery doesn't have cumulativeOffset. TBD
	var pos = Position.cumulativeOffset($(id));
	picker.style.top = (pos[1]+20)+'px';
	picker.style.left = pos[0]+'px';
	picker.style.display = 'block';
}

function genColors() {
	addColorValue('0','0','0');
	addColorValue('3','3','3');
	addColorValue('6','6','6');
	addColorValue('8','8','8');
	addColorValue('9','9','9');
	addColorValue('A','A','A');
	addColorValue('C','C','C');
	addColorValue('E','E','E');
	addColorValue('F','F','F');
	for (a = 1; a < colorLevels.length; a++) addColor(0,0,a);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(a,a,5);
	for (a = 1; a < colorLevels.length; a++) addColor(0,a,0);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(a,5,a);
	for (a = 1; a < colorLevels.length; a++) addColor(a,0,0);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(5,a,a);
	for (a = 1; a < colorLevels.length; a++) addColor(a,a,0);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(5,5,a);
	for (a = 1; a < colorLevels.length; a++) addColor(0,a,a);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(a,5,5);
	for (a = 1; a < colorLevels.length; a++) addColor(a,0,a);
	for (a = 1; a < colorLevels.length - 1; a++) addColor(5,a,5);
	return(colorArray);
}

function getColorTable() {
	var colors = colorArray;
	var tableCode = '';
	tableCode += '<table cellpadding="0" cellspacing="0">';
	for (i = 0; i < colors.length; i++) {
	if (i % perline == 0) { tableCode += '<tr>'; }
		tableCode += '<td bgcolor="' + colors[i] + '"><a style="color: '
		+ colors[i] + '; background: ' + colors[i] + ';" title="'
		+ colors[i] + '" href="javascript:setColor(\'' + colors[i] + '\');">&nbsp;</a></td>';
		if (i % perline == perline - 1) { tableCode += '</tr>'; }
	}
	if (i % perline != 0) { tableCode += '</tr>'; }
	tableCode += '</table>';
	return(tableCode);
}
     
function relateColor(id, color) {
	var link = $(id);
	if (color == '') {
		link.style.background = nocolor;
		link.style.color = nocolor;
		color = nocolor;
	} else {
		link.style.background = color;
		link.style.color = color;
	}
	eval(document.getElementById(id + 'field').title);
}

(function($) {
	$(document).ready(function(){
	
		$(".adsense_on_vn").hide().css({top:'521px', left:'106px'})
		$("#adsense_on_vn_cde859").show()
		
		$('.view_results').click(
			function(event){
				alert('We will appreciate your opinion before seeing the results.');
			}
		);
		
		$('.btn_share').click(function(event)
		{
			event.preventDefault();
			event.stopPropagation();
			$('#share_pnl').css('left', $(this).offset().left - 8);
			$('#share_pnl').css('top', $(this).offset().top - 390);

			var str = '%3Cscript src="http://www.pollsb.com/wd/{poll_id}"%3E%3C/script%3E{link_text}';
			str = str.replace(/{poll_id}/gi, $(this).attr('attr_id'));
			str = str.replace(/{poll_title}/gi, decodeURI($(this).attr('attr_title')));
			str = str.replace(/{link_text}/gi, decodeURI($(this).attr('attr_text')));
			$('#share_pnl').find('#widgetCode').val(unescape(str));

			var str= 'http://www.pollsb.com/polls/p{poll_title}';
			str = str.replace(/{poll_id}/gi, $(this).attr('attr_id'));
			str = str.replace(/{poll_title}/gi, decodeURI($(this).attr('attr_title')));
			str = str.replace(/{link_text}/gi, decodeURI($(this).attr('attr_text')));
			$('#share_pnl').find('#pollUrl').val(str);

			var str= '<a href="/widgets/add/{poll_id}">Customize your own widget</a>';
			str = str.replace(/{poll_id}/gi, $(this).attr('attr_id'));
			$('#share_pnl').find('#customizeWidget').html(str);
			
			var str = $('#share_pnl').find('#flashWidgetCodeOrig').val()
			str = str.replace(/{poll_id}/gi, $(this).attr('attr_id'))
			str = str.replace(/{poll_title}/gi, decodeURI($(this).attr('attr_title')))
			str = str.replace(/{link_text}/gi, decodeURI($(this).attr('attr_text')))
			$('#share_pnl').find('#flashWidgetCode').val(unescape(str))

			$('#share_pnl').show();
		});
		
		$('#share_pnl').find('.btn_x').click(function(event)
		{
			event.preventDefault();
			event.stopPropagation();
			$('#share_pnl').hide();
		})
	})
})(jQuery);

function updateHeader()
{
	jQuery.ajaxSetup({cache:false, global:false});
	jQuery.get(
			'/users_handler/header_control_1_6',
			function(data)
			{
				jQuery('#hdrContent').prepend(data);
				var gigya_login_provider = jQuery('#header').find('#col1').find('#is_gigya_loggedin').text()
				if (gigya_login_provider)
				{
					jQuery('.gigya_login_provider').html(gigya_login_provider)
					jQuery('.gigya_loggedin').show() 
				}
			}
		);
}