function getObject(objectName)
{	
  var objItem;
  
  if (document.all)
    objItem = document.all[objectName];
  else
  {
    objItem = document.getElementById(objectName);
    if (objItem==null)
      objItem= document.getElementsByName(objectName)[0];
    if (document.getElementsByName(objectName).length>1)
      objItem=document.getElementsByName(objectName);	
  }
  if(objItem == null) return false;
  return objItem;
}

function getName (object)
{
  return (object.name) ? object.name : object.id;
}

function launch(newURL, newName, newFeatures, orgName) {
  var remote = open(newURL, newName, newFeatures);
  if (remote.opener == null) // if something went wrong
    remote.opener = window;
  remote.opener.name = orgName;
  return remote;
}


// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


var prev_ft = new Object;
prev_ft.myTop = 0;

function set_footnote(ref, prev_ft)
{
	//alert(ref + ": top = " + prev_ft.myTop + ", height = " + prev_ft.myHeight);
	
	fn_objName = "fn_" + ref;
	ft_objName = "ft_" + ref;
	
	fn_obj = getObject(fn_objName);
	ft_obj = getObject(ft_objName);
	
	ft_obj.style.position = "absolute";
	ft_obj.style.right = "0px";
	
	if(fn_obj.offsetParent.nodeName == "BODY")
	{
		ft_obj.style.top = fn_obj.offsetTop + "px";
	}
	else
	{
		theParent = fn_obj.offsetParent;
		theOffset = fn_obj.offsetTop;
		
		while(theParent.nodeName != "BODY")
		{
			//alert("theParent: " + theParent.nodeName + ", theOffset: " + theOffset + "")
			theOffset += theParent.offsetTop;
			theParent = theParent.offsetParent;
		}
		
		theOffset += theParent.offsetTop;
		
		ft_obj.style.top = theOffset; + "px";
	}
	
	
	if((prev_ft.myHeight + 10) > (ft_obj.offsetTop))
	{
		//alert("Adjusting " + ref + ":\nprev_ft.myHeight + 10 = " + (prev_ft.myHeight + 10) + "\nft_obj.offsetTop + ft_obj.offsetHeight = " + (ft_obj.offsetTop) + "\n\nft_obj.style.top set to " + (prev_ft.myHeight + 10) + "px")
		ft_obj.style.top = prev_ft.myHeight + 10 + "px";
	}
	
	//var prev_ft = fn_obj.offsetTop;
	//var prev_ft = new Object;
	prev_ft.myTop = ft_obj.offsetTop;
	prev_ft.myHeight = ft_obj.offsetTop + ft_obj.offsetHeight;
	
	//alert(ref + ": " + prev_ft.myTop);
}

function dec_font()
{
	change_font("dec");
}

function inc_font()
{
	change_font("inc");
}

function change_font(inc_or_dec)
{
	article_content_obj = getObject("article_content");
	revista_home_table_obj = getObject("revista_home_table");
	
	//if(!article_content_obj) alert("article_content_obj not found!")
	fontsize_float = parseFloat(article_content_obj.style.fontSize);
	
	//alert(article_content_obj.style.fontSize);
	
	if(inc_or_dec == "inc")
	{
		new_float = fontsize_float + 0.2;
		font_size = new_float + "em";
		//alert(font_size)
		article_content_obj.style.fontSize = font_size;
		//if(revista_home_table_obj) revista_home_table_obj.style.fontSize = font_size;
	}
	if(inc_or_dec == "dec")
	{
		if(fontsize_float <= 0.2) return;
		
		new_float = fontsize_float - 0.2;
		font_size = new_float + "em";
		article_content_obj.style.fontSize = font_size;
		//if(revista_home_table_obj) revista_home_table_obj.style.fontSize = font_size;
	}
	
	set_font_size_cookie(font_size);
	
	setup_screen_h();
	//set_all_footnotes();
}

function set_font_size_cookie(font_size)
{
	setCookie("font_size_c", font_size);
	
	return;
}

function recover_font_size_cookie()
{
	font_size = getCookie("font_size_c");
	
	if(font_size != null) return font_size;
	
	return null;
}

function setMenus()
{
	menu_arr = new Array("revista", "enlaces", "jung", "acerca", "donaciones"); //"noticias",
	
	for(i=0; i<menu_arr.length; i++)
	{
		link_obj = getObject("l_" + menu_arr[i]);
		menu_obj = getObject(menu_arr[i]);
		
		if(!link_obj || !menu_obj) return;
		
		menu_obj.style.left = link_obj.offsetLeft;
	}
	
	return;
}


function onMenu(menu_id)
{
	if(menu_id == null) return;
	menu_obj = getObject(menu_id);
	
	if(menu_obj.my_timer)
	{
	    clearTimeout (menu_obj.my_timer);
	    menu_obj.my_timer = null;
	}
	menu_obj.style.visibility = "visible";
	return;
}

function hide_menu(menu_id)
{
	if(menu_id == null) return;
	menu_obj = getObject(menu_id);

	menu_obj.style.visibility = "hidden";
	return;
}

function offMenu(menu_id)
{
	if(menu_id == null) return;
	menu_obj = getObject(menu_id);
	
	menu_obj.my_timer = setTimeout("hide_menu('" + menu_id + "')", 200);
}

function keep_menu(menu_id)
{
	if(menu_id == null) return;
	menu_obj = getObject(menu_id);
	
	if(menu_obj.my_timer)
	{
	    clearTimeout (menu_obj.my_timer);
	    menu_obj.my_timer = null;
	}
	return;
}

function get_fullscreen()
{
	var fs = new Object;
	
	fullscreen_obj = getObject("fullscreen");
	content_obj = getObject("content");
	
	content_h = content_obj.offsetTop + content_obj.offsetHeight;
	
	fs.screen_h = fullscreen_obj.offsetHeight;
	fs.full_h = Math.max(fullscreen_obj.offsetHeight, content_h);
	fs.content_h = content_h;
	
	fs.w = fullscreen_obj.offsetWidth;
	
	//alert(fs.full_h);
	
	return fs;
}

function setup_screen_h()
{
	article_content_obj = getObject("article_content");
	revista_home_table_obj = getObject("revista_home_table");
	
	font_size = recover_font_size_cookie();
	
	if(font_size != null) article_content_obj.style.fontSize = font_size;
	//if((font_size != null) && revista_home_table_obj) revista_home_table_obj.style.fontSize = font_size;
	
	fs = get_fullscreen();
	
	leftbar_container_obj = getObject("leftbar_container");
	credit_obj = getObject("credit");
	
	leftbar_container_obj.style.height = fs.full_h + 100;
	credit_obj.style.top = fs.full_h + 65;
	
	return;
}

function set_leftbar_img()
{
	leftbar_img_obj = getObject("leftbar_img");
	local_links_obj = getObject("local_links");
	
	//alert(local_links_obj.offsetHeight);
	leftbar_img_obj.style.top = local_links_obj.offsetHeight + local_links_obj.offsetTop + 20;
}

function setup()
{
	setup_screen_h();
	setMenus();
	set_leftbar_img();
}


function home_setup()
{
	fullscreen_obj = getObject("fullscreen");
	hometable_obj = getObject("hometable");
	
	hometable_obj.style.height = Math.max(fullscreen_obj.offsetHeight, hometable_obj.offsetHeight);
}

function email_article(uri, title, description)
{
	//alert("Enviar este artículo:\nEste dispositivo está en fase de desarrollo.");
	//return;
	destination = "email_article.php?uri=" + escape(uri) + "&title=" + escape(title) + "&description=" + escape(description);
	launch(destination, "popup", "height=400,width=550,screenX=50,left=50,screenY=50,top=50,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0", "myLauncher");
}

function paid_article()
{
	//alert("Enviar este artículo:\nEste dispositivo está en fase de desarrollo.");
	//return;
	destination = "articulo_de_pago.php";
	launch(destination, "popup", "height=400,width=550,screenX=50,left=50,screenY=50,top=50,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0", "myLauncher");
}

function print_format(url)
{
	if (location.search.length == 0)
	{
		url += "?print=1";
	}
	else
	{
		url += "&print=1";
	}
	myRemote = launch(url, "myRemote", "height=400,width=750,screenX=50,left=50,screenY=50,top=50,channelmode=0,dependent=0,directories=0,fullscreen=0,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1", "myWindow");
}


function set_radio(form_name, radio_name, value) {

	if(value == "") return;
	
	var radio = eval("document." + form_name + "." + radio_name);
	var num_buttons = radio.length;
	
	for (i=0; i<num_buttons; i++) {
	
		button_val = radio[i].value;
		
		if (button_val == value) radio[i].checked = true;
	
	}
	
	return;	

}


function buy_article(article_id, price)
{
	alert("Comprar artículo id " + article_id + ".\nPrecio: " + price);
}

function download(article_id)
{
	//alert("Descargar artículo id " + article_id + ".");
	
	document.location.href = "pdf_download.php?id=" + article_id;
}
