// (c) Валентин Ханбеков, 2004-2006
function ic4u_onunload() {
ic4u_time = ic4u_max=ic4u_viewtime[0]; ic4u_h=document.body.scrollHeight;
var i; for (i=1; i<=19; i++) {ic4u_time+=ic4u_viewtime[i]; if (ic4u_max<ic4u_viewtime[i]) ic4u_max=ic4u_viewtime[i];}
if (ic4u_max>0) {
 ic4u_pages=ic4u_h/document.body.clientHeight;
 if (ic4u_max<ic4u_viewtime[0]) ic4u_max=ic4u_viewtime[0];
 ic4u_viewpos=parseInt(document.body.scrollTop/(document.body.scrollHeight-document.body.clientHeight)*100);
 for (var i=0; i<=19; i++) ic4u_viewtime[i]=parseInt(ic4u_viewtime[i]*100.0/ic4u_max);
 var ic4u_viewdata='&ic4u_viewdata='+ic4u_viewtime.join(',');
 ic4u_url=document.URL.replace('&','%26');
 while (ic4u_url.indexOf('&')>0) ic4u_url=ic4u_url.replace('&','%26');
 pic.src= 'http://ic4u.ru/add_view.php?ic4u_uid='+ic4u_uid+'&ic4u_prc='+ic4u_viewpos+'&ic4u_url='+ic4u_url+ic4u_viewdata+'&ic4u_time='+ic4u_time+'&ic4u_h='+ic4u_h+'&ic4u_pages='+ic4u_pages+'&r='+Math.random();
 ic4u_now=(new Date).valueOf(); do{ic4u_now2=(new Date).valueOf(); } while((ic4u_now2-ic4u_now)<1000);
}
if (ic4u_n_onunload!=null)
 {document.body.onunload=ic4u_n_onunload;
 ic4u_n_onunload();}
 else document.body.onunload=null;
}

function ic4u_timer() {
setTimeout("ic4u_timer()", 1000);
var ua = window.navigator.userAgent;
if ( ua.indexOf ( "MSIE " ) >= 0 )
 if (document.readyState!='complete' && document.readyState!='interactive') return;
if (document.body.scrollHeight==document.body.clientHeight) return;
if (window.onunload/*==null*/!=ic4u_onunload)
 {ic4u_n_onunload=window.onunload;
 window.onunload=ic4u_onunload;}
ic4u_viewpos=parseInt(document.body.scrollTop/(document.body.scrollHeight-document.body.clientHeight)*100);
ic4u_pos=parseInt(ic4u_viewpos/5);
if (ic4u_pos >= ic4u_viewtime.length) ic4u_pos = ic4u_viewtime.length-1;
if (ic4u_prevpos==ic4u_viewpos) ic4u_stopcounter++;
 else {ic4u_stopcounter=0; ic4u_prevpos=ic4u_viewpos;}
if (ic4u_stopcounter<(ic4u_pos == 0?3:15) ) (ic4u_viewtime[ic4u_pos]++);
}

function ic4u_onresize() {
setTimeout("ic4u_onresize();", 3000);
var ua = window.navigator.userAgent;
if ( ua.indexOf ( "MSIE " ) >= 0 )
 if (document.readyState!='complete' && document.readyState!='interactive') return;

if (document.all["ic4u_bar0"]==null)
 for (ic4u_j=0;ic4u_j<20;ic4u_j++)
 document.body.insertAdjacentHTML('AfterBegin', '<table onclick="ic4u_scroll();" class="" bgcolor="" id="ic4u_bar'+ic4u_j+'" width="100%" style="position:absolute;left:0;z-index:200" background="http://www.ic4u.ru/smallpic.gif"><tr style="background-color: transparent"><td style="background-color: transparent">&nbsp;</td></tr></table>'
 +'<table onclick="ic4u_scroll();" class="" bgcolor="" id="ic4u_bar_x'+ic4u_j+'" width="100%" style="position:absolute;left:0;z-index:2000" background="http://www.ic4u.ru/smallpic_.gif"><tr style="background-color: transparent"><td style="background-color: transparent">&nbsp;</td></tr></table>');

if (document.all["ic4u_bar0"]==null) return;

h=document.body.scrollHeight;
w=document.body.scrollWidth;

if (ic4u_h==h && ic4u_w==w
 && ic4u_oh==document.body.offsetHeight
 && ic4u_ow==document.body.offsetWidth) return;

ic4u_h=h; ic4u_w=w;
ic4u_oh=document.body.offsetHeight;
ic4u_ow=document.body.offsetWidth;

h=h/20;

if (ic4u_max!=0)
 for (ic4u_j=0;ic4u_j<20;ic4u_j++)
 {
 if ((ic4u_o=document.all["ic4u_bar"+ic4u_j])==null) break;
 ic4u_o.height=h; ic4u_o.width=w; ic4u_o.style.top=ic4u_j*h;
 if (ic4u_data[ic4u_j+20]<0) ic4u_o.style.filter="Gray";
 else ic4u_o.style.filter="Alpha(Opacity="+parseInt(50.0*ic4u_data[ic4u_j+20]/ic4u_max)+")";
 }

if (ic4u_max_x!=0)
 for (ic4u_j=0;ic4u_j<20;ic4u_j++)
 {
 if ((ic4u_o=document.all["ic4u_bar_x"+ic4u_j])==null) break;
 ic4u_o.height=h; ic4u_o.width=w; ic4u_o.style.top=ic4u_j*h;
 if (ic4u_data[ic4u_j]<0) ic4u_o.style.filter="Gray";
 else ic4u_o.style.filter="Alpha(Opacity="+parseInt(50.0*ic4u_data[ic4u_j]/ic4u_max_x)+")";
 }
}

function ic4u_scroll() {
if (ic4u_sum_x<=0 || ic4u_sum<=0) {alert("Too few data to simulation"); return;}
ic4u_div=1/10.0;
if (ic4u_scrollpos==null)
 {ic4u_scrollpos=0;
 if (!window.confirm("Averadge viewtime of this page: "+parseInt(ic4u_data[40]/ic4u_sum_x)+" sec.\r\rStart view simulation?\r\rWarning! Don`t click mouse, while simulation done!"))
 return;}
if (ic4u_scrollpos < 20)
{if (ic4u_data[parseInt(ic4u_scrollpos)+20]>=0)
 {setTimeout("ic4u_scroll();", ic4u_data[parseInt(ic4u_scrollpos)+20]*ic4u_tick*ic4u_div);
 document.body.scrollTop=(document.body.scrollHeight-document.body.clientHeight)*(ic4u_scrollpos/20);
 window.status='position '+parseInt(ic4u_scrollpos*5)+'%; wait '+parseInt(ic4u_data[parseInt(ic4u_scrollpos)+20]*ic4u_tick*ic4u_div/1000)+'c';
 ic4u_scrollpos+=ic4u_div;}
 else
 {alert("For free account, simulation show partly.");
 ic4u_scrollpos=null; }}
else
 if (window.confirm("Simulation end. Show again?"))
 {ic4u_scrollpos=0;
 setTimeout("ic4u_scroll();", 500);
 } else ic4u_scrollpos=null;
}

if (document.URL.substring(0, 4)!='http:')
 {
 if (location.search.indexOf("ic4u_data=")>0)
 {ic4u_h = ic4u_w = ic4u_oh = ic4u_ow = 0;
 ic4u_data = location.search.substr(location.search.indexOf("ic4u_data=")+10);
 ic4u_data = ic4u_data.split(',');
 if (ic4u_data.length==41)
 {
 for (ic4u_j=0;ic4u_j<ic4u_data.length;ic4u_j++) ic4u_data[ic4u_j]=parseInt(ic4u_data[ic4u_j]);
 ic4u_sum=ic4u_max=parseInt(ic4u_data[20]);
 for (ic4u_j=21;ic4u_j<40;ic4u_j++)
{if (ic4u_data[ic4u_j]<0) break;
else ic4u_sum+=ic4u_data[ic4u_j];
if (ic4u_data[ic4u_j] > ic4u_max) ic4u_max=ic4u_data[ic4u_j]};
if (ic4u_j<40) ic4u_sum*=2;
ic4u_sum_x=ic4u_max_x=ic4u_data[0]; for (ic4u_j=1;ic4u_j<20;ic4u_j++) {ic4u_sum_x+=ic4u_data[ic4u_j]; if (ic4u_data[ic4u_j] > ic4u_max_x) ic4u_max_x=ic4u_data[ic4u_j];}
if (ic4u_data[40]<3) ic4u_data[40]=3;
if (ic4u_sum_x>0 && ic4u_sum>0) {ic4u_tick=ic4u_data[40]*1000.0/ic4u_sum_x/ic4u_sum; ic4u_scrollpos=null;}
ic4u_onresize();}}
else
{var pic = new Image();
ic4u_viewtime= new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
ic4u_viewpos=ic4u_prevpos=ic4u_stopcounter=0;
ic4u_timer();}
}
