//General object defining and manipulating functions

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);	
  }

  return objItem;
}

function getName (object)
{
  return (object.name) ? object.name : object.id;
}



//Rollovers
function rollon(obj)
{
	obj_name = getName(obj);
	
	if(obj_name == thisdoc) return;
	
	my_obj = getObject(obj_name);
	my_obj.style.backgroundColor = "#FFB514";
	return;
}

function rolloff(obj, ind)
{
	obj_name = getName(obj);
	
	if(obj_name == thisdoc) return;
	
	my_obj = getObject(obj_name);
	
	if(ind == "index")
	{
		use_color = "#B29900";
	}
	else
	{
		use_color = "#E0AA0F";
	}
	
	
	my_obj.style.backgroundColor = use_color;
	return;
}


function pic_rollon(obj)
{
	//obj_name = getName(obj);
	
	my_obj = getObject(obj);

	my_obj.style.border = "4px solid #ff0000";
	
	dot_obj = getObject(obj + "_dot");
	
	dot_obj.style.backgroundColor = "#ff0000";
	
	return;
}

function pic_rolloff(obj)
{
	//obj_name = getName(obj);
	
	my_obj = getObject(obj);

	my_obj.style.border = "4px solid #B29900";
	
	dot_obj = getObject(obj + "_dot");
	
	dot_obj.style.backgroundColor = "#B29900";
	
	return;
}


function home_pic_rollon(obj)
{
	//obj_name = getName(obj);
	
	my_obj = getObject(obj);

	my_obj.style.border = "4px solid #ff0000";
	
/*
	dot_obj = getObject(obj + "_dot");
	
	dot_obj.style.backgroundColor = "#ff0000";
*/
	
	return;
}

function home_pic_rolloff(obj)
{
	//obj_name = getName(obj);
	
	my_obj = getObject(obj);

	my_obj.style.border = "4px solid #B29900";
	
/*
	dot_obj = getObject(obj + "_dot");
	
	dot_obj.style.backgroundColor = "#B29900";
*/
	
	return;
}


//Site navigation
function nav(obj)
{
	obj_name =getName(obj);
	
	//if(obj_name == thisdoc) return;
	
	document.location.href = obj_name + ".html";
	
	return;
}


function fotos()
{
	document.location.href = thisdoc + "_album.html";
}

function content()
{
	document.location.href = thisdoc + ".html";
}


//Placeholder used for development
function not_yet()
{
	alert("Este dispositivo está en desarrollo.");
}


//Email this page
function mail()
{

	//Section 1: Get information about the page to be sent
	//Get URL of this page
	page_uri = location.href;
	
	//Get title of this page
	page_title = document.title;
	
	lang = get_lang();
	//replace 'www' or nothing with 'es'
	lang = ((lang == "www") || (lang == "") || (lang == "localhost")) ? "es" : lang;
	
	//Define subject message
	var langtexts = new Array();
	langtexts["ca"] = "Pàgina enviada des de Tot-verd";
	langtexts["es"] = "Página enviada desde Tot-verd";
	
	//Get correct subject
	var subject = langtexts[lang];
	
	//Set message body
	var body = page_title + ": " + page_uri;
	
	//set link destination
	var uri_string = "mailto:?subject=" + subject + "&body=" + body;
	
	//Redirect to email client
	document.location.href = uri_string;
	//document.location.href = page_uri;
	return;
}


//Used for determining current language
function get_lang()
{
	loc = document.location.href;
	
	result = loc.match(/http:\/\/([a-z]{1,10})+/);
	
	new_uri = "No Match";
	
	if(result) new_uri = result[1];
		
	return new_uri;
}



//Change language version
function lang(newlang)
{
/*
	newlang = (newlang == "es") ? "www" : newlang;
	
	new_location = "http://" + newlang + ".tot-verd.com/" + thisdoc + ".html";
	
	alert("Redireccionar: " + new_location);
*/
	alert("Aquesta pàgina estarà disponible en català próximament.");
}



//Enlarge photographs
function launch(url, caption) {

  self.name = "opener";
  

  
  remote = open("_popup.html?" + url + "&cap=" + caption, "remote", "resizable,width=200,height=200,left=50,top=50");
  
}


function enlarge(level, pic_id, caption)
{
	prefix = (level == 1) ? "" : "";
	
	pic_url = prefix + "l_pix/" + pic_id + ".jpg";

	launch(pic_url, caption);
}

function enlarge_idx(level, pic_url, caption)
{
	prefix = (level == 1) ? "../" : "";
	
	launch(pic_url, caption);
}

//Set all page elements according to window size
// var pagetype can be "content" or "album"
function setup(level, pagetype)
{
	var sc = new Object;
	
	sc.min_h = 600;
	sc.min_w = 750;
	
	body_obj = getObject("body_container");
	
	sc.avail_h = body_obj.offsetHeight;
	sc.avail_w = body_obj.offsetWidth;
	
	sc.use_h = Math.max(sc.min_h, sc.avail_h);
	sc.use_w = Math.max(sc.min_w, sc.avail_w);
	
/*
	alert("sc.min_h: " + sc.min_h + "\n" + 
	"sc.min_w: " + sc.min_w + "\n" + 
	"sc.avail_h: " + sc.avail_h + "\n" + 
	"sc.avail_w: " + sc.avail_w + "\n" + 
	"sc.use_h: " + sc.use_h + "\n" + 
	"sc.use_w: " + sc.use_w + "\n");
*/
	
	switch(pagetype)
	{
		case "content" :
			set_this_page_link();
			content_resize(level, sc);
			leftbar_resize(sc);
			break;
		
		case "album" :
			set_this_page_link();
			album_resize(sc);
			leftbar_resize(sc);
			break;
		
		case "content_nopic" :
			set_this_page_link();
			album_resize(sc);
			leftbar_resize(sc);
			break;

		case "content_nolink" :
			album_resize(sc);
			leftbar_resize(sc);
			break;
			
		case "creditos" :
			album_resize(sc);
			leftbar_resize(sc);
			credit_resize(sc);
			break;
			
		case "index" :
			album_resize(sc);
			break;
	}

	
	
	document.body.style.visibility = "visible";

	
	return;
	
}


//Set individual elements on the screen
function set_this_page_link()
{
	my_obj = getObject(thisdoc);
	my_obj.style.backgroundColor = "#ffff00";
	//my_obj.style.cursor = "default";
	return;
}

function leftbar_resize(sc)
{
	leftbar_obj = getObject("left_bar");
	leftbar_obj.style.height = sc.use_h - leftbar_obj.offsetTop;
	
	credit_obj = getObject("credit_container");
	credit_obj.style.top = sc.use_h - credit_obj.offsetHeight - 10;
	return;
}

function content_resize(level, sc)
{
	prefix = (level == 1) ? "../" : "";
	
	content_box_obj = getObject("content_box");
	big_pic_obj = getObject("big_pic");
	content_container_obj = getObject("content_container");
	main_title_obj = getObject("main_title");
	pic_container_obj = getObject("pic_container");
	pie_foto_container_obj = getObject("pie_foto_container");
	
	//alert(big_pic_obj.offsetWidth);
	
	if(sc.use_w < 801)
	{
		big_pic_obj.src = prefix + "m_pix/" + thisdoc + ".jpg";
		big_pic_obj.style.width = 200;
		big_pic_obj.style.height = 200;
		content_box_obj.style.height = 208;
		main_title_obj.style.fontSize = "24pt";
		pie_foto_container_obj.style.width = 191;
		//pie_foto_container.width = 191;
	}
	else
	{
		big_pic_obj.src = prefix + "pix/" + thisdoc + ".jpg";
		big_pic_obj.style.width = 300;
		big_pic_obj.style.height = 300;
		content_box_obj.style.height = 308;
		main_title_obj.style.fontSize = "36pt";
		pie_foto_container_obj.style.width = 291;
		//pie_foto_container.width = 291;
	}
	
	//alert(pic_container_obj.offsetTop);
	
	content_box_obj.style.top = pic_container_obj.offsetTop;
	
	content_box_obj.style.left = big_pic_obj.offsetWidth + 30;
	
	content_box_obj.style.width = sc.use_w - content_container_obj.offsetLeft - big_pic_obj.offsetWidth - 100;
	
	sc.use_h = Math.max((content_container_obj.offsetTop + content_container_obj.offsetHeight), sc.use_h);
	
}

function album_resize(sc)
{
	content_container_obj = getObject("content_container");
	main_title_obj = getObject("main_title");
	
	//alert(big_pic_obj.src);
	
	if(sc.use_w < 801)
	{
		main_title_obj.style.fontSize = "24pt";
	}
	else
	{
		main_title_obj.style.fontSize = "36pt";
	}
	
	sc.use_h = Math.max((content_container_obj.offsetTop + content_container_obj.offsetHeight), sc.use_h);
	
}

function credit_resize(sc)
{


}






