var viewportwidth;
var viewportheight;

function show_sponsors()
{
	update_view_port();
	s = gebi('sponsors');
	if(viewportwidth>=1200)
	{
		html = '';

		lang = get_browser_lang();
		if(lang=='es' || lang=='es-es' || lang=='ca' || lang=='eu' || lang=='ast' || lang=='an')
		{
			//Netfilia advertisements (spanish, catalan, euskera, asturian, aragones languages)
			html += "<iframe name=afi250x250 src=\"http://www.netfilia.com/contenidos/insert.html?cmf=102396&con_capa=1&contenido=250x250&web=40543&p=641247293&id=hisTISmP3yxls&url=http://www.cocoteca.com\" width=250 height=250 frameBorder=no border=0 marginWidth=0 marginHeight=0 scrolling=no style=\"border-style:none;\" ></iframe><br/><br/>";

			html += "<iframe name=afi250x250 src=\"http://www.netfilia.com/contenidos/insert.html?cmf=100189&con_capa=1&contenido=250x250&web=40543&p=123337992&id=hisTISmP3yxls&url=http://www.cocoteca.com\" width=250 height=250 frameBorder=no border=0 marginWidth=0 marginHeight=0 scrolling=no style=\"border-style:none;\" ></iframe><br/><br/>";

			html += "<iframe name=afi250x250 src=\"http://www.netfilia.com/contenidos/insert.html?cmf=102279&con_capa=1&contenido=250x250&web=40543&p=1315651706&id=hisTISmP3yxls&url=http://www.cocoteca.com\" width=250 height=250 frameBorder=no border=0 marginWidth=0 marginHeight=0 scrolling=no style=\"border-style:none;\" ></iframe><br/><br/>";

			html += "<iframe name=afi200x200' src=\"http://www.netfilia.com/contenidos/insert.html?cmf=102582&con_capa=1&contenido=200x200&web=40543&p=1316312944&id=hijOUenSTf/Dg&url=http://www.cocoteca.com\" width=200 height=200 frameBorder=no border=0 marginWidth=0 marginHeight=0 scrolling=no style=\"border-style:none;\" ></iframe><br/><br/>";
		}

		s.innerHTML = html+s.innerHTML;
	}
	s.style.display = 'block';
}

function get_browser_lang()
{
	if (navigator.userAgent.indexOf("Opera")!=-1) (language=navigator.language)
	else { if (navigator.appName == "Netscape") (language=navigator.language)
	else language=navigator.browserLanguage };

	if (language){language=language.substring(0,2)} 
	else language='zz';

	return language;
}

function update_view_port()
{
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }

	 // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }

	 // older versions of IE
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
}

function do_search()
{
	qt = document.forms.panel.qt;
	s = qt.options[qt.selectedIndex].value;
	if(s==2) document.forms.panel.action='/search';
}

function set_view(v)
{
	document.panel.v.value=v;
	document.panel.submit();
}

function set_lang(l,p,t)
{
        idiom = l.options[l.selectedIndex].value;
        document.location = '/sources/'+idiom+'/'+t+'/'+p;
}

function set_country(c,p,t)
{
        country = l.options[c.selectedIndex].value;
        document.location = '/sources/'+idiom+'/'+t+'/'+p;
}

function set_topic(s,p,l)
{
	topic = s.options[s.selectedIndex].value;
	document.location = '/sources/'+l+'/'+topic+'/'+p;
}

function gebi(id) { return document.getElementById(id); }

function vup(id) { vote_source(1, id); } // Vote up

function veq(id) { vote_source(2, id); } // Vote ni fu, ni fa...

function vdw(id) { vote_source(3, id); } // Vote down

function vsup(id) { vote_story(1, id); } // Vote up

function vsdw(id) { vote_story(2, id); } // Vote down

function mv(id, v)
{
	e = gebi('v'+id);
	e.innerHTML = "<b>"+v+"</b>";
}

function vote_story(type, id)
{
        e = gebi('av_'+id);
        e.innerHTML = '<DIV class="shintl">Loading...</DIV>';
        svote = MochiKit.Async.loadJSONDoc("/vote_story/"+id+"/"+type);
        svote.addCallback(vote_story_callback);
}

function vote_source(type, id)
{
	e = gebi('panel_'+id);
	e.innerHTML = '<table border=0 height="200px" align="center"><tr><td valign="middle" align="center"><img src=\"/img/wait.gif\"></td></tr></table>';
	call = MochiKit.Async.loadJSONDoc("/vote/"+id+"/"+type);
	call.addCallback(vote_callback);
}

function vote_story_callback(res)
{
        id = res[0].id; //Source list id
        code = res[0].code; //Response code {0 => ERROR, 1 => NOT_LOGGED_IN, 3 => OK}
        total_up = res[0].votes_up; //positive votes after vote
        total_down = res[0].votes_down; //negative votes after vote

        e = gebi('av_'+id);

	$html = "<div class=\"vote\" style=\"width:160px;margin-left:5px;\">(<b><font style=\"color: green;\">"+total_up+"</font></b> / <b><font style=\"color: red;\">"+total_down+"</font></b>) <font id=\"r105\">voted, <b>thank you!</b></font></div>";

        e.innerHTML = $html;
}

function vote_callback(res)
{
	id = res[0].id; //Source list id
	code = res[0].code; //Response code {0 => ERROR, 1 => NOT_LOGGED_IN, 3 => OK}
	cocos = res[0].cocos; //Cocos after vote
	op = res[0].op; //Operation did

	e = gebi('panel_'+id);
/*	$html = '<ul class="voptions" style="top:0px">';
	$html += '<li><table border="0" style="border-collapse:collapse; margin-left:7px;" width="100%"><tr><td><b>'+cocos+'</b></td>';
	$html += '<tr><td>cocos</td></td></table></li>';
	$html += '<li class="thanks"><img src="/img/voted.gif"></li>';
	$html += '</ul>';*/

	$html = '<table width="100%" border="0" id="vtable">';
	$html+= '<tr><td style="font-size:16px;"><b>'+cocos+'</b></td></tr>';
	$html+= '<tr><td style="font-size:9px;line-height:1.2em;">cocos<br/>last 24h</td></tr>';
	$html+= '</table><table border=0 align="center" height="155px"><tr><td valign="middle" align="center"><img src="/img/voted.gif"></td></tr></table>';

	e.innerHTML = $html;



/*
  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;

  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];
          if(value==0) value = '';

          var single_bar = TD({'class': 's2'}, value, BR(), IMG({'src': IMG_PATH+'pipe'+e+'v.png', 'height': data[0].height[i][e], 'width': '10'}));
          row_bars.appendChild(single_bar);
        }
  }
*/

}
