function $(v){return document.getElementById(v);}
function $t(v){return document.getElementsByTagName(v);}
var timeOut=10;var allErrors=new Array('Не засчитана (отсутствует подключение)','Не засчитана (ошибка сервера)','Не засчитана (выключены куки)');var i,soids,values,estimate,error,ids,value,flag;var arEstimateIds=new Array();var arEstimateVal=new Array();var backScript=(typeof(root_dir)=='undefined'?'/':root_dir)+'scripts/vote1.php';var backScriptChoose=(typeof(root_dir)=='undefined'?'/':root_dir)+'scripts/choose.php';var btName=/ids\[(-?\d+)\]/;var req=new JsHttpRequest();req.onreadystatechange=function(){if(req.readyState==4){if(req.responseText.indexOf('^')!=-1){data=req.responseText.split('^');getEstimate(data[0]);getError(data[1]);}}}
var reqChoose=new JsHttpRequest();reqChoose.onreadystatechange=function(){if(reqChoose.readyState==4&&reqChoose.responseText!=0){$('msg_td_'+reqChoose.responseText).innerHTML='<div style="margin-top:2px;font-weight:normal;font-size:12px;color:#1d8f0d;white-space:nowrap">Добавлено в избранное</div>';}}
function getRate(){if(arEstimateIds.length>0){soids='';values='';for(i=0;i<arEstimateIds.length;i++){soids+=arEstimateIds[i];values+=arEstimateVal[i];if(i<arEstimateIds.length-1){soids+='.';values+='.';}}
arEstimateIds=new Array();arEstimateVal=new Array();req.open(null,'form.POST '+backScript,true);req.send({'soids':soids,'values':values,'uid':getCookie('uid')});}
setTimeout('getRate()',timeOut*1000);}
function getEstimate(_data){if(_data.indexOf('|')==-1)
return;estimate=_data.split('|');for(i=0;i<estimate.length;i++){if(estimate[i].indexOf('=')!=-1){_estimate=estimate[i].split('=');$('msg_td_'+_estimate[0]).innerHTML='<div style="margin-top:2px;font-weight:normal;font-size:12px;color:#1d8f0d;white-space:nowrap">Ваша оценка&nbsp; <span style="font-weight:bold">'+
+(_estimate[1]>0?'+'+_estimate[1]:_estimate[1])+'</span></div>';}}}
function getError(_data){if(_data.indexOf('|')==-1)
return;error=_data.split('|');for(i=0;i<error.length;i++){if(error[i].indexOf('=')!=-1){_error=error[i].split('=');$('msg_td_'+_error[0]).innerHTML='<div style="margin-top:2px;font-weight:normal;font-size:12px;color:#8f1d0d;white-space:nowrap">'+allErrors[_error[1]]+'</div>';}}}
function _radioClick(e){if(!e)e=window.event;if(e.target!=null){ids=btName.exec(e.target.name)[1];val=e.target.value;}else{ids=btName.exec(e.srcElement.name)[1];val=e.srcElement.value;}
flag=false;for(i=0;i<arEstimateIds.length;i++){if(arEstimateIds[i]==ids){arEstimateVal[i]=val;flag=true;break;}}
if(!flag){arEstimateIds[arEstimateIds.length]=ids;arEstimateVal[arEstimateVal.length]=val;}
$('msg_td_'+ids).innerHTML='<div style="margin-top:-8px"><img src="/skin/ajax-loader.gif" width="32" height="32" title="загрузка..." alt="загрузка..." /></div>';return true;}
function getCookie(name){var prefix=name+"=";var cookieStartIndex=document.cookie.indexOf(prefix);if(cookieStartIndex==-1)return null;var cookieEndIndex=document.cookie.indexOf(";",cookieStartIndex+prefix.length);if(cookieEndIndex==-1)cookieEndIndex=document.cookie.length;return unescape(document.cookie.substring(cookieStartIndex+prefix.length,cookieEndIndex));}
function addFavorite(id){id=parseInt(id,10);reqChoose.open(null,'form.POST '+backScriptChoose,true);reqChoose.send({'anekdot_id':id,'action':'add'});}
function initRate(){$t_list=$t('input');for(i=0;i<$t_list.length;i++){if($t_list[i]['type']=='radio')
$t_list[i].onclick=_radioClick;}
$t_list=$t('a');for(i=0;i<$t_list.length;i++){if($t_list[i]['id'].indexOf('img_link_')==0){$t_list[i]['title']='Постоянная ссылка и обсуждения в социальных сетях';}else if($t_list[i]['id'].indexOf('img_email_')==0){$t_list[i]['title']='Послать другу';}else if($t_list[i]['id'].indexOf('img_grade_')==0){$t_list[i]['title']='Оценки';}else if($t_list[i]['id'].indexOf('img_discm_')==0){$t_list[i]['title']='Модерируемое обсуждение';}else if($t_list[i]['id'].indexOf('img_disco_')==0){$t_list[i]['title']='Обсуждение';}}
if(typeof(export_text)!='undefined')
{for(var i in export_text)
{v=$('msg_post_'+export_text[i].id);if(v!=null)
{url=encodeURIComponent('http://www.anekdot.ru/id/'+export_text[i].id);text=export_text[i].text;v.innerHTML='<a class="button_tw" href="http://twitter.com/intent/tweet?text='+text+'&url='+url+'" title="Отправить в Twitter" target="_blank"></a>'+'<a class="button_fb" href="http://www.facebook.com/share.php?u='+url+'&t='+text+'" title="Отправить в Facebook" target="_blank"></a>'+'<a class="button_lj" href="http://www.livejournal.com/update.bml?subject='+text+'&event=%3Ca+href%3D%22'+url+'%22%3E'+text+'%3C%2Fa%3E%0A%0A'+'" title="Отправить в LiveJournal" target="_blank"></a>'+'<a class="button_vk" href="http://vkontakte.ru/share.php?url='+url+'" title="Отправить в В Контакте" target="_blank"></a>'+v.innerHTML;}}}}
