var sound;

function writeCookie(name, value){
   if(arguments.length < 2)return;
   var days = 50;
   var expireDate = new Date();
   expireDate.setTime(expireDate.getTime() + (days * 24 * 60 * 60 * 1000));
   document.cookie = escape(name) + "=" + escape(value) + "; expires=" + expireDate.toGMTString();
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function intro() {
  moveDivs();
  new Fx.Style('logo-intro', 'opacity', {duration: 1000, onComplete: function(){
		new Fx.Style('logo-intro', 'opacity', {duration: 1000, onComplete: function(){
			$('logo-intro').style.display = 'none';
			$('content').style.display = '';
			new Fx.Style('bg01', 'opacity', {duration: 1000, onComplete: function(){
				new Fx.Style('content', 'opacity', {duration: 2000, onComplete: function(){
					new Fx.Style('bg02', 'opacity', {duration: 1000, onComplete: function(){

					  curUrl = document.location.href.split("#");
  					bookmark = curUrl[1];
  					if (bookmark != undefined && bookmark != 'home') {
  						change_page(bookmark);
  					} else {
              homeGallery.prepareTimer();
            }

						new Fx.Style('box1', 'opacity', {duration: 2000, onComplete: function(){
							new Fx.Style('box2', 'opacity', {duration: 3000, onComplete: function(){
								new Fx.Style('box3', 'opacity', {duration: 3000}).start(1);

							}}).start(1);
						}}).start(1);
					}}).start(1);
				}}).start(1);
			}}).start(1);
		}}).start(0);
	}}).start(1);

  sound = getFlashMovie('mp3player');

}

current_page = 'home';

function change_page(page) {
  window[current_page + '_hide']();
  window[page + '_show']();
  current_page = page;
}

function home_show() {
  $('page-home').style.display = 'block';
  new Fx.Style('page-home', 'opacity', {duration: 1000}).start(1);
  homeGallery.prepareTimer();
}

function home_hide() {
  new Fx.Style('page-home', 'opacity', {duration: 250}).start(0);
  $('page-home').style.display = 'none';
  homeGallery.clearTimer();
}

function ristorante_hide() {
  new Fx.Style('page-ristorante', 'opacity', {duration: 250}).start(0);
  $('page-ristorante').style.display = 'none';
  ristoranteGallery.clearTimer();
}

function ristorante_show() {
  load_content('ristorante', function() {
    if (!ristoranteGallery) {
      ristoranteGallery = new gallery($('ristoranteGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-ristorante').style.display = 'block';
    new Fx.Style('page-ristorante', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('ristorante_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('ristorante_text', 'opacity', {duration: 1000}).start(1);
  		  ristoranteGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function cucina_hide() {
  new Fx.Style('page-cucina', 'opacity', {duration: 250}).start(0);
  $('page-cucina').style.display = 'none';
  cucinaGallery.clearTimer();
}

function cucina_show() {
  load_content('cucina', function() {
    if (!cucinaGallery) {
      cucinaGallery = new gallery($('cucinaGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-cucina').style.display = 'block';
    new Fx.Style('page-cucina', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('cucina_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('cucina_text', 'opacity', {duration: 1000}).start(1);
  		  cucinaGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function matrimoni_hide() {
  new Fx.Style('page-matrimoni', 'opacity', {duration: 250}).start(0);
  $('page-matrimoni').style.display = 'none';
  matrimoniGallery.clearTimer();
}

function matrimoni_show() {
  load_content('matrimoni', function() {
    if (!matrimoniGallery) {
      matrimoniGallery = new gallery($('matrimoniGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-matrimoni').style.display = 'block';
    new Fx.Style('page-matrimoni', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('matrimoni_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('matrimoni_text', 'opacity', {duration: 1000}).start(1);
  		  matrimoniGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function cucina_astice_hide() {
  sound.highVolume();
  new Fx.Style('page-cucina_astice', 'opacity', {duration: 250}).start(0);
  $('page-cucina_astice').style.display = 'none';
}

function cucina_astice_show() {
  load_content('cucina_astice', function() {
    sound.lowVolume();
    flowplayer("cucina_astice_video", "media/flowplayer-3.0.3.swf", {
      canvas: {
          backgroundColor: "#ffffff"
      },
      plugins: {
         controls: {
          buttonColor: '#878787',
          timeColor: '#000000',
          durationColor: '#ffffff',
          sliderColor: '#000000',
          backgroundGradient: 'medium',
          bufferGradient: 'none',
          progressGradient: 'medium',
          backgroundColor: '#999999',
          buttonOverColor: '#728B94',
          bufferColor: '#cec0d8',
          borderRadius: '4',
          sliderGradient: 'none',
          progressColor: '#76429a',
          autoHide: '2000',
       	  opacity: 1.0,
       	  height: 20
         }
      }
    });

    $('page-cucina_astice').style.display = 'block';
    new Fx.Style('page-cucina_astice', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('cucina_astice_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('cucina_astice_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function cucina_asparago_hide() {
  sound.highVolume();
  new Fx.Style('page-cucina_asparago', 'opacity', {duration: 250}).start(0);
  $('page-cucina_asparago').style.display = 'none';
}

function cucina_asparago_show() {
  load_content('cucina_asparago', function() {
    sound.lowVolume();
    flowplayer("cucina_asparago_video", "media/flowplayer-3.0.3.swf", {
      canvas: {
          backgroundColor: "#ffffff"
      },
      plugins: {
         controls: {
          buttonColor: '#878787',
          timeColor: '#000000',
          durationColor: '#ffffff',
          sliderColor: '#000000',
          backgroundGradient: 'medium',
          bufferGradient: 'none',
          progressGradient: 'medium',
          backgroundColor: '#999999',
          buttonOverColor: '#728B94',
          bufferColor: '#cec0d8',
          borderRadius: '4',
          sliderGradient: 'none',
          progressColor: '#76429a',
          autoHide: '2000',
       	  opacity: 1.0,
       	  height: 20
         }
      }
    });

    $('page-cucina_asparago').style.display = 'block';
    new Fx.Style('page-cucina_asparago', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('cucina_asparago_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('cucina_asparago_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function cucina_alici_hide() {
  sound.highVolume();
  new Fx.Style('page-cucina_alici', 'opacity', {duration: 250}).start(0);
  $('page-cucina_alici').style.display = 'none';
}

function cucina_alici_show() {
  load_content('cucina_alici', function() {
    sound.lowVolume();
    flowplayer("cucina_alici_video", "media/flowplayer-3.0.3.swf", {
      canvas: {
          backgroundColor: "#ffffff"
      },
      plugins: {
         controls: {
          buttonColor: '#878787',
          timeColor: '#000000',
          durationColor: '#ffffff',
          sliderColor: '#000000',
          backgroundGradient: 'medium',
          bufferGradient: 'none',
          progressGradient: 'medium',
          backgroundColor: '#999999',
          buttonOverColor: '#728B94',
          bufferColor: '#cec0d8',
          borderRadius: '4',
          sliderGradient: 'none',
          progressColor: '#76429a',
          autoHide: '2000',
       	  opacity: 1.0,
       	  height: 20
         }
      }
    });

    $('page-cucina_alici').style.display = 'block';
    new Fx.Style('page-cucina_alici', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('cucina_alici_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('cucina_alici_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function eventi_show() {
  load_content('eventi', function() {
    if (!eventiGallery) {
      eventiGallery = new gallery($('eventiGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-eventi').style.display = 'block';
    new Fx.Style('page-eventi', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('eventi_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('eventi_text', 'opacity', {duration: 1000}).start(1);
  		  eventiGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function eventi_hide() {
  new Fx.Style('page-eventi', 'opacity', {duration: 250}).start(0);
  $('page-eventi').style.display = 'none';
  eventiGallery.clearTimer();
}

function ricette_hide() {
  new Fx.Style('page-ricette', 'opacity', {duration: 250}).start(0);
  $('page-ricette').style.display = 'none';
}

function ricette_show() {
  load_content('ricette', function() {
    $('page-ricette').style.display = 'block';
    new Fx.Style('page-ricette', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('ricette_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('ricette_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function giardino_hide() {
  new Fx.Style('page-giardino', 'opacity', {duration: 250}).start(0);
  $('page-giardino').style.display = 'none';
  giardinoGallery.clearTimer();
}

function giardino_show() {
  load_content('giardino', function() {
    if (!giardinoGallery) {
      giardinoGallery = new gallery($('giardinoGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-giardino').style.display = 'block';
    new Fx.Style('page-giardino', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('giardino_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('giardino_text', 'opacity', {duration: 1000}).start(1);
  		  giardinoGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function cantina_show() {

  load_content('cantina', function() {
    if (!cantinaGallery) {
      cantinaGallery = new gallery($('cantinaGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-cantina').style.display = 'block';
    new Fx.Style('page-cantina', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('cantina_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('cantina_text', 'opacity', {duration: 1000}).start(1);
  		  cantinaGallery.prepareTimer();
  		}}).start(1);
  	}}).start(1);
  });
}

function cantina_hide() {
  new Fx.Style('page-cantina', 'opacity', {duration: 250}).start(0);
  $('page-cantina').style.display = 'none';
  cantinaGallery.clearTimer();
}

function camere_show() {
  load_content('camere', function() {

    if (!camereGallery) {
      camereGallery = new gallery($('camereGallery'), {
        timed: true,
        showArrows: true,
        showCarousel: false,
        embedLinks: false,
        delay: 5000,
        fadeDuration: 1000,
        showInfopane: false
        });
    }

    $('page-camere').style.display = 'block';
    new Fx.Style('page-camere', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('camere_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('camere_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function camere_hide() {
  new Fx.Style('page-camere', 'opacity', {duration: 250}).start(0);
  $('page-camere').style.display = 'none';
  camereGallery.clearTimer();
}

function pasqua_show() {
  load_content('pasqua', function() {
    $('page-pasqua').style.display = 'block';
    new Fx.Style('page-pasqua', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('pasqua_title', 'opacity', {duration: 1000, onComplete: function(){
  		  new Fx.Style('pasqua_text', 'opacity', {duration: 1000}).start(1);
  		}}).start(1);
  	}}).start(1);
  });
}

function pasqua_hide() {
  new Fx.Style('page-pasqua', 'opacity', {duration: 250}).start(0);
  $('page-pasqua').style.display = 'none';
  camereGallery.clearTimer();
}

function contatti_show() {

  load_content('contatti', function() {

    $('page-contatti').style.display = 'block';
    new Fx.Style('page-contatti', 'opacity', {duration: 1000, onComplete: function(){
  		new Fx.Style('contatti_text', 'opacity', {duration: 1000}).start(1);
  	}}).start(1);
  });
}

function contatti_hide() {
  new Fx.Style('page-contatti', 'opacity', {duration: 250}).start(0);
  $('page-contatti').style.display = 'none';
}

function contact_ok(status) {

  if (status) {
    $('contactFormResponse').innerHTML = "Il suo messaggio &egrave; stato inviato, verr&agrave; ricontattato quanto prima.";
  } else {
    $('contactFormResponse').innerHTML = "Si &egrave; verificato un errore, la preghiamo di mandare una email a <a href='info@ilpedrocchino'>info@ilpedrocchino</a> o contattarci telefonicamente allo 0434 70034";
  }
  new Fx.Style('contactForm', 'opacity', {duration: 500, onComplete: function(){
    $('contactForm').style.display = 'none';
    $('contactFormResponse').style.display = 'block';
		new Fx.Style('contactFormResponse', 'opacity', {duration: 1000}).start(1);
	}}).start(0);

}

var WinWidth;
var WinHeight

function getWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    WinWidth = window.innerWidth;
    WinHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    WinWidth = document.documentElement.clientWidth;
    WinHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    WinWidth = document.body.clientWidth;
    WinHeight = document.body.clientHeight;
  }
}

var moving = false;
function moveDivs(){
  if (!moving) {
    getWinSize();
    moving = true;
  	frameHeight = (!document.all && document.getElementById) ? window.innerHeight : document.body.clientHeight;
  	if (WinHeight > 600) {
  	  $('bodyTag').style.overflow = 'hidden';
  	  newTop = (WinHeight - 640) / 2;
    	new Fx.Style('content', 'margin-top', {duration: 1000}).start(newTop);
    } else {
      $('bodyTag').style.overflow = 'auto';
      newTop = 10;
    }
    new Fx.Style('content', 'margin-top', {duration: 1000, onComplete: function(){ moving = false} }).start(newTop);
  }
}

function set_opacity() {
  $$('.transparent').each(function(el) {
    el.setStyle('opacity', 0);
  });
}

function load_content(page, oncompletefunction) {
  if($('page-' + page).innerHTML == '') {
    var ajax = new Ajax(page + '_content.html', {
    			method: 'get',
    			update: $('page-' + page),
    			evalScripts: true,
    			onComplete: oncompletefunction
    		});
    ajax.request();
  } else {
    oncompletefunction();
  }
}

var homeGallery;
var ristoranteGallery;
var cucinaGallery;
var eventiGallery;
var giardinoGallery;
var cantinaGallery;
var camereGallery;
var matrimoniGallery;

var loc = location.href.split('/');

if (loc[3] != '' && loc[3].indexOf('#') != 0) {
  window.location = '/#' + loc[3];
  } else {
  window.addEvent('domready', function() {

    $$('img.mo').each(function(img) {
      var src = img.getProperty('src');
      var extension = src.substring(src.lastIndexOf('.'),src.length)
      img.addEvent('mouseenter', function() { img.setProperty('src',src.replace(extension,'_o' + extension)); });
      img.addEvent('mouseleave', function() { img.setProperty('src',src); });
    });

    $$('a.jsme').each(function(link) {
      var href = link.getProperty('href');
      link.addEvent('click', function() { change_page(href) });
      link.href = '#' + href;
      //link.addEvent('mouseover', function(){ load_content(href) });
    });

    homeGallery = new gallery($('homeGallery'), {
      timed: true,
      showArrows: true,
      showCarousel: false,
      embedLinks: false,
      delay: 5000,
      fadeDuration: 1000,
      showInfopane: false
      });
    setTimeout("homeGallery.clearTimer();", 2000);
    // setTimeout("load_galleries();", 2000);

    intro();

  });

  window.addEvent('resize', moveDivs);
}

