var IMG_PATH = '/img/';

//*********************************//
//* StoryComment functions        *//
//*********************************//

function StoryComment(node) {
  bindMethods(this);
  this.div = node;
  this.timer = null;
}

StoryComment.prototype.getUrlAction = function() {
  this.url_action = getNodeAttribute(this.div, 'href');
}

StoryComment.prototype.invoqueJSONdataRequest = function() {
  this.getUrlAction();
  var d  = loadJSONDoc(this.url_action);
  d.addCallback(this.renderComment);
}

StoryComment.prototype.renderComment = function(data) {

  var isMSIE = /*@cc_on!@*/false;

  if(data[0].has_stats=='yes') stats_button = A({'href':'javascript:vss('+data[0].id_source+','+data[0].id_story+',\'stats\');'}, 'stats');
  else stats_button = null;

  taula_comment = TABLE({'class': 'table_comment', 'width': '80%', 'align': 'center', 'cellspacing': '0', 'cellpadding': '0'},
				TR({},
					TD({'colspan':'2'},
						'Subject',
						BR(),
						INPUT({'name': 'subject', 'class': 'input_comment', 'type': 'text', 'value': '', 'style': 'border: 1px solid rgb(181, 195, 206); width: 99%; font-size:11px; color:#AAAAAA; font-family:Tahoma,Arial,georgia; background-color:#F0F0F0;'})
					)
				),
				TR({},
					TD({},
						'Message',
						BR(),
						TEXTAREA({'class': 'input_comment', 'name': 'comment', 'style': 'border: 1px solid rgb(181, 195, 206); width: 99%; height: 90px; font-size:11px; color:#AAAAAA; font-family:Tahoma,Arial,georgia; background-color:#F0F0F0;', 'maxlength': '150', 'rows': '10', 'cols': '16'})
					)
				),
				TR({},
					TD({'align': 'right'},
						'Submit'
					)
				)
			);

link_info = DIV();
link_info.innerHTML = '<DIV class="info" title="Story" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'info\');" >';

link_stats = DIV();
if(data[0].has_stats=='yes') link_stats.innerHTML = '<DIV class="stats" title="Stats" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'stats\');" >';
else link_stats = null;

link_comment = DIV();
link_comment.innerHTML = '<DIV class="comment" title="Discuss it!" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';

//link_rss = DIV();
//link_rss.innerHTML = '<DIV class="rss" title="Subscribe" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';

link_share = DIV();
link_share.innerHTML = '<DIV class="share" onclick="return addthis_sendto()" onmouseover="return addthis_open(this,\'\',\''+data[0].url+'\',\''+escape(data[0].title)+'\')">';

if(isMSIE) mt = '-1';
else mt = '0';

  story = DIV({}, TABLE({'class': 'compactstats2', 'cellspace': 0, 'cellpadding': 0, 'style': 'height:200px;'},
                                        TR({}, TD({'height':'21px', 'background':'/img/bbbg.png', 'vertical-align': 'middle'},
                                                DIV({'class':'pagination', 'style':'padding:0px; margin:0px; margin-top: '+mt+'; width:340px;'},
                                                        SPAN({'class':'disabled', 'style':'border: 0px; float: left; text-align: left; color: rgb(170, 170, 170); padding:0px;'},A({'href': '/stories/viewtopic.php?t='+data[0].url_discuss, 'style': 'border:0px'}, STRONG('discuss it!'), ' (0)'), /*STRONG(' published '),*/ data[0].pub_date+" ago"),
							link_info,
                                                        link_stats,
							link_comment,
							link_share
                                                ))
                                        ),

                                        TR({}, TD({}, taula_comment)) ));

  this.div.innerHTML = story.innerHTML;

}

StoryInfo.prototype.cancelTimer = function () {
        if (this.timer) this.timer.cancel();
        this.timer = null;
}

StoryInfo.prototype.restartTimer = function () {
        this.cancelTimer();
        if(this.time_refresh != null) this.timer = callLater(this.time_refresh, this.invoqueJSONdataRequest);
}


//*********************************//
//* StoryInfo functions           *//
//*********************************//

function StoryInfo(node) {
  bindMethods(this);
  this.div = node;
  this.timer = null;
}

StoryInfo.prototype.getUrlAction = function() {
  this.url_action = getNodeAttribute(this.div, 'href');
}

StoryInfo.prototype.invoqueJSONdataRequest = function() {
  this.getUrlAction();
  var d  = loadJSONDoc(this.url_action);
  d.addCallback(this.renderText);
}

StoryInfo.prototype.renderText = function(data) {

link_vote_up = DIV();
link_vote_up.innerHTML = '<DIV class="plus" title="Vote up!" onMouseOver="h(\'r' + data[0].id_source + '\',\'rate <b>up!</b>\')" onMouseOut="h(\'r' + data[0].id_source + '\',\'rate it!\')" onclick="vsup('+data[0].id_story+');" >'; 

link_vote_down = DIV();
link_vote_down.innerHTML = '<DIV class="minus" title="Vote down!" onMouseOver="h(\'r' + data[0].id_source + '\',\'rate <b>down!</b></font>\')" onMouseOut="h(\'r' + data[0].id_source + '\',\'rate it!\')" onclick="vsdw('+data[0].id_story+');" />';

votes_up = data[0].votes_up;
votes_down = data[0].votes_down;

area_vote = DIV();
area_vote.innerHTML = '<DIV class="vote">(<b><font style="color:green">'+votes_up+'</font></b> / <b><font style="color:red">'+votes_down+'</font></b>) <font id="r'+(data[0].id_source)+'">rate it!</font></DIV>';

area_hint = DIV();
area_hint.innerHTML = '<DIV class="shint" id="h' + data[0].id_source + '"></DIV>';

link_info = DIV();
link_info.innerHTML = '<DIV class="info" title="Story" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'info\');" onMouseOver="h(\'h' + data[0].id_source + '\',\'View story abstract\')" onMouseOut="h(\'h' + data[0].id_source + '\',\'\')"></DIV>';

link_stats = DIV();
if(data[0].has_stats=='yes') link_stats.innerHTML = '<DIV class="stats" title="Stats" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'stats\');" onMouseOver="h(\'h' + data[0].id_source + '\',\'Latest statistics\')" onMouseOut="h(\'h' + data[0].id_source + '\',\'\')"></DIV>';
else link_stats = null;

link_comment = DIV();
//link_comment.innerHTML = '<DIV class="comment" title="Comment" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';
link_comment.innerHTML = '<DIV class="comment" title="Discuss it!" onclick="goto(\'/stories/viewtopic.php?t='+data[0].url_discuss+'\');",\'comment\');" onMouseOver="h(\'h' + data[0].id_source + '\',\'Discuss this story\')" onMouseOut="h(\'h' + data[0].id_source + '\',\'\')"></DIV>';


//link_rss = DIV();
//link_rss.innerHTML = '<DIV class="rss" title="Subscribe" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';

embeded = DIV();
var cs = '5px'; //cellspacing
if((data[0].enclosure_url!='')&&(data[0].enclosure_url!=null)) embeded.innerHTML = '<div style="padding:1px; border:1px solid #AAAAAA;"><object data="'+data[0].enclosure_url+'" type="'+data[0].enclosure_type+'" width="120" height="90" align="center" autostart="false" loop="false"><img src="/img/wait.gif"></object></div>';
else
{
	embeded.innerHTML = '';
	cs = '0px';
}

div_vote = DIV({'id':'av_'+data[0].id_story},link_vote_up, link_vote_down, area_vote, area_hint);

link_share = DIV();
link_share.innerHTML = '<DIV class="share" onclick="return addthis_sendto()" onmouseover="h(\'h' + data[0].id_source + '\',\'Bookmark and share\'); return addthis_open(this,\'\',\''+data[0].url+'\',\''+escape(data[0].title)+'\')" onMouseOut="h(\'h' + data[0].id_source + '\',\'\')"></DIV>';

$quotes_css = "background:transparent url(/img/quotes.gif) no-repeat scroll left top; ";

if(data[0].url!='') $story_menu = TR({}, TD({'height':'16px', 'background':'/img/bbbg.png'},
                                                DIV({'class':'pagination', 'style':'padding:0px; margin:0px; width:340px; background-img:url(\'/img/bbbg.png\')'},
							div_vote,
							DIV({'class':'dpub'}, /*STRONG(' published '),*/ data[0].pub_date+" ago"),
/*
                                                        SPAN({'class':'disabled', 'style':'margin-left:70px; border: 0px; float: left; text-align: left; color: rgb(170, 170, 170); padding:0px;'},A({'href': '/stories/viewtopic.php?t='+data[0].url_discuss, 'style': 'border:0px'}, STRONG('discuss it!'), ' (0)'), STRONG(' published '), data[0].pub_date+" ago"),*/
                                                        link_info,
                                                        link_stats,
                                                        link_comment,
                                                        link_share
                                                ))
                                        );
else
{
	$story_menu = TR();
	$quotes_css = "";
}


  story = DIV({}, TABLE({'class': 'compactstats2', 'cellspace': 0, 'cellpadding': 0, 'style': 'height:200px;'}, $story_menu,

                                        TR({}, TD({'class': 'title', 'align': 'left', 'valign': 'middle', 'style': $quotes_css+'padding-left:35px; height: 40px; text-align:justify;'}, A({'target':'_blank', 'href': data[0].url, 'style': 'color:#173FB2; font-size:15px; font-weight:bold; font-family:Arial, Helvetica, Verdana; text-decoration:none;'}, data[0].title, SPAN({'class': 'pub_date'}) ))),

                                        TR({}, TD({'class': 'caption', 'align': 'left', 'valign': 'middle', 'style': 'font-family:Arial,Helvetica,Geneva,sans-serif; font-size:11px; color:#333333; line-height:180%; height:100px; text-align:justify;padding-left:15px;'}, TABLE({'cellspacing':cs}, TR({}, TD({}, data[0].desc), TD({}, embeded)  ) )  ))));

  this.div.innerHTML = story.innerHTML;

}

StoryInfo.prototype.cancelTimer = function () {
        if (this.timer) this.timer.cancel();
        this.timer = null;
}

StoryInfo.prototype.restartTimer = function () {
        this.cancelTimer();
        if(this.time_refresh != null) this.timer = callLater(this.time_refresh, this.invoqueJSONdataRequest);
}



//*********************************//
//* SimpleGroupBarChart functions *//
//*********************************//

function SimpleGroupBarChart(node) { 
  bindMethods(this);
  this.div = node;
  this.timer = null;
}

SimpleGroupBarChart.prototype.getUrlAction = function() {
  this.url_action = getNodeAttribute(this.div, 'href');
}

SimpleGroupBarChart.prototype.invoqueJSONdataRequest = function() {
  this.getUrlAction();
  var d  = loadJSONDoc(this.url_action);
  d.addCallback(this.renderChart);
}

SimpleGroupBarChart.prototype.renderChart = function(data) {
  var row_bars = TR({'class': 's1', 'valign': 'bottom'});
  var row_text = TR({'class': 's3'});
  var row_legend = TR();
  var num_grups = data[0].text.length;
  var num_tipus = data[0].legend.length;

  var total = 0;
  for (var i=0; i<data[0].values.length; i++)
  {
	for (var e=0; e<data[0].values[i].length; e++)
	{
	  value = data[0].values[i][e];
	  total = total + parseInt(value);
	  if(value==0) value = '';

	  var single_bar = TD({'class': 's2','height':'90px'}, value, BR(), IMG({'src': IMG_PATH+'pipe'+e+'v.png', 'height': data[0].height[i][e], 'width': '10'}));
	  row_bars.appendChild(single_bar);
	}
  }

  if(!total)
  {	
	col_no_activity = TD({'class': 'title', 'colspan': i*(num_grups+1), 'align': 'center', 'valign': 'middle', 'style': 'height: 45px;'},'No statistics available');
	row_no_activity = TR({'class': 'no_available', 'valign': 'bottom'}, col_no_activity);
  }else	row_no_activity = null;

  for (var e=0; e<num_grups; e++)
  {
      var single_tex = TD({'class': 'bottomrow'+e%2+' tablemaintext v2','colspan': num_tipus}, data[0].text[e]);
      row_text.appendChild(single_tex);
  }


link_info = DIV();
link_info.innerHTML = '<DIV class="info" title="Story" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'info\');" >';

link_stats = DIV();
link_stats.innerHTML = '<DIV class="stats" title="Stats" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'stats\');" >';

link_comment = DIV();
link_comment.innerHTML = '<DIV class="comment" title="Comment" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';

//link_rss = DIV();
//link_rss.innerHTML = '<DIV class="rss" title="Subscribe" onclick="vss(' + data[0].id_source + ',' + data[0].id_story + ',\'comment\');" >';

link_share = DIV();
link_share.innerHTML = '<DIV class="share" onclick="return addthis_sendto()" onmouseover="return addthis_open(this,\'\',\''+data[0].url+'\',\''+escape(data[0].title)+'\')">';

  stats = DIV({}, TABLE({'class': 'compactstats2', 'cellspace': 0, 'cellpadding': 0, 'style': 'height:181px;'},

                                        TR({}, TD({'height':'21px', 'background':'/img/bbbg.png', 'colspan': i*(num_grups+1)},
                                                DIV({'class':'pagination', 'style':'padding:0px; margin:0px; margin-top:0px; width:340px;'},
                                                        SPAN({'class':'disabled', 'style':'border: 0px; float: left; text-align: left; color: rgb(170, 170, 170); padding:0px;'},A({'href': '/stories/viewtopic.php?t='+data[0].url_discuss, 'style': 'border:0px'}, STRONG('discuss it!'), ' (0)'), STRONG(' published '), data[0].pub_date+" ago"),
                                                        link_info,
							link_stats,
							link_comment,
							link_share
                                                ))
                                        ),

					TR({}, TD({'class': 'title', 'colspan': i*(num_grups+1), 'align': 'left', 'valign': 'middle', 'style': 'height: 52px; text-align:justify;'}, A({'target':'_blank', 'href': data[0].url, 'style': 'color:#173FB2; font-size:15px; font-weight:bold; font-family:Arial, Helvetica, Verdana; text-decoration:none; text-align:justify;'}, data[0].title ))),

					row_no_activity,
					row_bars,
					row_text,
					TR({}, TD({'class': 'caption', 'colspan': i*num_grups, 'align': 'center'}, ''))));

  for (var j=0; j<num_tipus; j++)
  {
      row_legend.appendChild(TD({'class': 's2'}, IMG({'src': IMG_PATH+'pipe'+j+'.png', 'align': '', 'height': '10', 'width': '20'}), ' '+data[0].legend[j]));
      row_legend.appendChild(TD({'class': 'tinv', 'width': '5'}));
  }

  row_legend.appendChild(TD({'class': 'tinv'}, A({'href': data[0].source_url, 'target': '_blank'}, data[0].source)));

  legend = DIV({}, (TABLE({}, row_legend)));
  this.div.innerHTML = stats.innerHTML + legend.innerHTML;

  this.restartTimer();
}

SimpleGroupBarChart.prototype.cancelTimer = function () {
	if (this.timer) this.timer.cancel();
	this.timer = null;
}

SimpleGroupBarChart.prototype.restartTimer = function () {
	this.cancelTimer();
	if(this.time_refresh != null) this.timer = callLater(this.time_refresh, this.invoqueJSONdataRequest);
}

function updateStoriesArea() {
  var stories = getElementsByTagAndClassName(null, 'story');
  for (var i=0; i<stories.length; i++)
  {
	value = getNodeAttribute(stories[i], 'value');
	if(value == 'stats')
	{
		barChart = new SimpleGroupBarChart(stories[i]);
		barChart.invoqueJSONdataRequest();
	}
	else if(value == 'info')
	{
		info = new StoryInfo(stories[i]);
		info.invoqueJSONdataRequest();
	}
	else if(value == 'comment')
	{
//                comment = new StoryComment(stories[i]);
//                comment.invoqueJSONdataRequest();
	}
  }
}

function goto(url) { document.location.href=url; }

function h(id, text)
{
	d=gebi(id);
	d.innerHTML = text;
}

//addLoadEvent(initSimpleGroupBarCharts);
