// função para adicionar mais de uma função ao carregamento da página
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  } else {
    window.onload = function()
	{
      oldonload();
      func();
    }
  }
}

// função para resetar os campos do formulário
function resetFields(whichform) 
{
  for (var i=0; i<whichform.elements.length; i++)
  {	  
    var element = whichform.elements[i];
	
	// cor original da borda do elemento
	var corBordaOriginal = element.style.borderColor;
	  
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function()
	{
		if (this.value == this.defaultValue) {
			this.value = "";
		}
		this.style.borderColor = "#92ac7d";
    }
    element.onblur = function() 
	{
      if (this.value == "") 
	  {
        this.value = this.defaultValue;
      }
	  this.style.borderColor = corBordaOriginal;
    }
  }
}

// prepara os formulários para serem validados
function prepareForms()
{
  for (var i=0; i<document.forms.length; i++)
  {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function()
	{
      //return validateForm(this);
    }
  }
}

// ============================ galeria de imagens 
// exibe a imagem
function showPic(whichpic)
{
  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href");
  var title = whichpic.getAttribute("title");
  //var alt = whichpic.getAttribute("alt");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  placeholder.setAttribute("title",title);
  placeholder.setAttribute("alt",title);
  return false;
}

// prepara a galeria
function prepareGallery()
{
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("miniaturas")) return false;
  var gallery = document.getElementById("miniaturas");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++)
  {
    links[i].onclick = function()
	{
		var descricao = this.getAttribute("title");
		return showPic(this);
    }
  }
}

// prepara a imagem placeholder
function preparePlaceholder()
{
	if(!document.getElementById("placeholder")) return false;
	var placeholder = document.getElementById("placeholder");
	placeholder.onload = function()
	{
		//onload="MM_effectAppearFade(this, 1000, 0, 100, false)"
		MM_effectAppearFade(this, 1000, 0, 100, false);
	}
}

function MM_effectAppearFade(targetElement, duration, from, to, toggle)
{
	Spry.Effect.DoFade(targetElement, {duration: duration, from: from, to: to, toggle: toggle});
}

//
addLoadEvent(prepareForms);
addLoadEvent(prepareGallery);
addLoadEvent(preparePlaceholder);