PRES = 10;//Precision

function PubLnk(link, over, linkTxt)
{
	this.link = link;
	this.over = over;
	this.linkTxt = linkTxt;
}

function PubContent(elems)
{
	this.elem = elems;
}

function PubImgs(pre_imgs, elems)
{
	this.pre_imgs = pre_imgs;
	this.elem = elems;
}

function PubCollection(publnk, pubdt, weights)
{
	this.lnk = publnk;
	this.dt = pubdt;
	this.weights = weights;
}

function get_rnd_idx(arr, w_arr, h, offset, cw)
{
  vsel_col = ((h * PRES) + (Math.random() * cw * PRES)) % (arr.length * PRES);

  if(w_arr == null)
  {
	return (Math.floor(vsel_col / PRES) + offset) % arr.length;
  }
  else
  {
	total_w = 0;
	for(i = 0; i < w_arr.length; i++)
		total_w += w_arr[i];

	sel_w = vsel_col / (PRES * arr.length) * total_w;

	acc_w = w_arr[0];

	for(c_col = 0; acc_w <= sel_w; c_col++)
		acc_w += w_arr[c_col + 1];
 
//	alert('sw: ' + total_w + ' sel_w: ' + sel_w + ' c_col: ' + c_col);
	return (Math.floor(c_col) + offset) % arr.length;
  }
}

function RenderPub(pubcol, nested_col_ws, offset, h_offset, choice_w, h_choice_w, txt, target)
{
	today = new Date();
	hour = today.getHours() + today.getDay();

	if(nested_col_ws != null)
	{
		pubcol = pubcol[get_rnd_idx(pubcol, nested_col_ws, hour, h_offset, h_choice_w)];
	}

	if(choice_w > pubcol.dt.elem.length)
		choice_w = pubcol.dt.elem.length;

	has_links = !(pubcol.lnk == null || pubcol.lnk.link == null);

	if(has_links)
	{
		document.write('<a href="' + pubcol.lnk.link + '" '+(pubcol.lnk.over != null ? 'onmouseover="window.status=\''+pubcol.lnk.over+'\'; return true" onMouseOut="window.status=\'\'; return true;" ' : '')+'target="'+ (target == null ? '_top' : target) + '">');
	}

	if(pubcol.dt != null)
	{
		col = get_rnd_idx(pubcol.dt.elem, pubcol.weights, hour, offset, choice_w);
		if(has_links)
			document.write('<img src="' + pubcol.dt.pre_imgs + pubcol.dt.elem[col]+ '" border=0>');
		else
			document.write(pubcol.dt.elem[col]);
	}

	if(txt != null && has_links)
	{
		document.write(txt + pubcol.lnk.linkTxt);
	}

	if(has_links)
	{
		document.write('</a>');
	}
}