var timeout;

function hoverMenu(whois){
	var menu1 = document.getElementById("menu1");
	var menu2 = document.getElementById("menu2");
	menu1.style.visibility = "hidden";
	menu2.style.visibility = "hidden";
	if (whois == "trova"){
		menu1.style.visibility = "visible";
		document.getElementById("trovaOff").style.display = "none";
		document.getElementById("trovaOn").style.display = "block";
	}else{
		menu2.style.visibility = "visible";
		document.getElementById("areaOff").style.display = "none";
		document.getElementById("areaOn").style.display = "block";
	}
	clearTimeout(timeout);
}
function outMenu(){
	timeout = setTimeout("closeMenu()", 200);
}
function closeMenu(){
	var menu1 = document.getElementById("menu1");
	var menu2 = document.getElementById("menu2");
	document.getElementById("trovaOn").style.display = "none";
	document.getElementById("areaOn").style.display = "none";
	menu1.style.visibility = "hidden";
	document.getElementById("trovaOff").style.display = "block";
	menu2.style.visibility = "hidden";
	document.getElementById("areaOff").style.display = "block";
}

var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* Functions to unobtrusively open links in new windows when className is "new-window" */

/* Create the new window */
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
	    var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}

/*
Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Find all links 
		var links = document.getElementsByTagName('a');
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "non-html"
			if (/\bnew-window\b/.test(link.className)) {
				// append new window function to onclick & onkeypress events (for accessibility!)
				link.onclick = openInNewWindow;
				link.onkeypress = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

/*
CLEAR ON FOCUS
*/
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}



function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
	var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
	var aQueryString = strQueryString.split("&");
	for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	  if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
		var aParam = aQueryString[iParam].split("=");
		strReturn = aParam[1];
		break;
	  }
	}
  }
  return unescape(strReturn);
}

function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false; //Here determine return if no parameter is found
  }
}


function galleryLoad(){
	if (document.getElementById("loadgallery")){
		 setTimeout("closeLoading()", 1000);
	}
}
function closeLoading(){
	document.getElementById("loadgallery").style.display="none";
}

function loadGmap(){
	if (document.getElementById("map_canvas")){
		initializeMap();
	}
}

function checkFlashIntro(){
	if (document.getElementById("flashContainer")){
		document.getElementById("flashContainer").innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="760" height="462" id="casa" align="middle" alt="Ferretti Casa"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="swf/casa2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="swf/casa2.swf" quality="high" bgcolor="#ffffff" width="760" height="462" name="casa" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	}
	initHomeFeedback();
}
var isExtended = 0;
var height = 275;
var width = 240;
var slideDuration = 1000;
var opacityDuration = 800;

function extendContract(){
	if(isExtended == 0){
		document.getElementById("loadFeedback").style.top="0";
		sideBarSlide(0, height, 0, width);
		sideBarOpacity(0, 1);
		isExtended = 1;
	}
	else{
		sideBarSlide(height, 0, width, 0);
		sideBarOpacity(1, 0);
		isExtended = 0;
	}
}
function sideBarSlide(fromHeight, toHeight, fromWidth, toWidth){
		var myEffects = new Fx.Styles('loadFeedback', {duration: slideDuration, transition: Fx.Transitions.linear});
		myEffects.custom({'height': [fromHeight, toHeight],'width': [fromWidth, toWidth]});
}
function sideBarOpacity(from, to){
		var myEffects = new Fx.Styles('loadFeedback', {duration: opacityDuration, transition: Fx.Transitions.linear});
		myEffects.custom({'opacity': [from, to]});
}
function sideBarOpacity1(from, to){
		var myEffects = new Fx.Styles('loadFeedback', {duration:opacityDuration, transition: Fx.Transitions.linear});
		myEffects.custom({'opacity': [from, to]});
}
function sideBarOpacityC(from, to){
		extendContract();
}

function initHomeFeedback(){
	document.getElementById("containerLoadFeedback").innerHTML = '<iframe frameborder="0" id="loadFeedback" src="http://www.ferretticasa.it/Feedback/Feedbackform.html"></iframe>';
	$('feedback').addEvent('click', function(){extendContract()});
	//sideBarOpacity1(1, 0);
}

function checkNewsletter(){
	var email = document.getElementById("emailnewsletter").value;
	if ((email.indexOf('@') < 0) || ((email.charAt(email.length-4) != '.') && (email.charAt(email.length-3) != '.'))){
		window.alert("Per favore inserisci il campo email correttamente");
		return false;
	}
	else{
		return true;
	}
}

function checkInvioStuff(){
	checkInvioContatti();
}

function checkInvioContatti(){
	if (queryString("newsletter") == "true"){
		document.getElementById("containerNewsLetter").innerHTML = "<p style='color:#d7ab00;padding-left:15px;'>Grazie per esserti iscritto!</p>"
	}
	if (queryString("contacted") == "true"){
		document.getElementById("containerForm").innerHTML = "<p style='color:#d7ab00;margin-top:20px;'><strong>Grazie per averci contattato.</strong><br /><br />Il tuo messaggio è stato inoltrato all'Ufficio competente.<br />Sarà nostra premura ricontattarti il più presto possibile.<br /><br />Potete sempre contattarci anche al seguente numero telefonico <strong>035-56.16.33</strong>.</p>"
	}
	if (queryString("lavora") == "true"){
		document.getElementById("containerForm").innerHTML = "<p style='color:#d7ab00;margin-top:20px;'><strong>Grazie per averci contattato.</strong><br /><br />Il tuo messaggio è stato inoltrato all'Ufficio competente.<br />Sarà nostra premura ricontattarti il più presto possibile.<br /><br />Potete sempre contattarci anche al seguente numero telefonico <strong>035-56.16.33</strong>.</p>"
	}
}


addLoadEvent(closeMenu);
addLoadEvent(galleryLoad);
addLoadEvent(getNewWindowLinks);
addLoadEvent(checkFlashIntro);
addLoadEvent(loadGmap);
addLoadEvent(checkInvioStuff);