window.onresize = function() {setBackground()};
window.onload = function() {set_page()};

function set_page() {
    Cookie.set("contact", "off");
    Cookie.set("price", "off");
	setBackground();
	setArtiquario();
}

function lastMinute() {
    var last_minute = $("last_minute");
    var last_minute_img = $("last_minute_img");
    new Effect.Appear("last_minute", {from:0.0, to:0.6 });
    new Effect.Appear("last_minute_img", {from:0.0, to:1 });
    new Ajax.Updater('last_minute_img', 'last_minute.php', {
        method: 'get'}
    );
    var dimension = Element.getDimensions("body");
    var total = dimension.width;
    var image = 500;
    var margin = (total - image) / 2;
    if (margin <= 0) {
        margin = 1;
    }
    margin += "px";
    last_minute_img.style.marginLeft = margin;
    last_minute_img.style.marginRight = margin;
    total += "px";
    last_minute.style.width = total;
}

function exitLastMinute() {
    var last_minute = $("last_minute");
    var last_minute_img = $("last_minute_img");
    new Effect.Fade("last_minute", {from:0.6, to:0.0 });
    new Effect.Fade("last_minute_img");
    //shade.style.visibility = "hidden";
    //white_border.style.visibility = "hidden";
}

function slide(id, table) {
    var shade = $("shade");
    var white_border = $("border");
    new Effect.Appear("shade", {from:0.0, to:0.6 });
	new Effect.Appear("border", {from:0.0, to:1 });
    //shade.style.visibility = "visible";
    //white_border.style.visibility = "visible";
    getPhoto(id, table);
}

function getPhoto(id, table) {
    new Ajax.Updater('border', 'get_photo.php', {
        method: 'get' ,
        parameters: {id: id, table: table}}
    );
}

function contact() {
    Cookie.set("contact", "on");
    new Ajax.Updater('content', 'contatti.php', {
        method: 'get'}
    );
    menu();
}

function price() {
    Cookie.set("price", "on");
    new Ajax.Updater('price', 'price.php', {
        method: 'get'}
    );
    new Ajax.Updater('menu_residence', 'menu_residence.php', {
        method: 'get'}
    );
    menu();
}

function menu() {
    new Ajax.Updater('menu_pannolino', 'menu.php', {
        method: 'get',
        onComplete: setArtiquario}
    );
}

function getNextPhoto(id, id_next, table) {
    new Ajax.Updater('border', 'get_photo.php', {
        method: 'get' ,
        parameters: {id: id_next, table: table},
        insertion: Insertion.Bottom}
    );
    new Effect.Fade(id, {from:0.0, to:1 });
    new Effect.Appear(id_next, {from:0.0, to:1 });
}

function exitViewer() {
	var shade = $("shade");
    var white_border = $("border");
    new Effect.Fade("shade", {from:0.6, to:0.0 });
	new Effect.Fade("border");
	//shade.style.visibility = "hidden";
    //white_border.style.visibility = "hidden";
}

function setArtiquario() {
	var width;
    var lang;
    var padding = "";
    lang = getLang();
	var artiquario = $("artiquario");
    if (lang == "it") {
        if (navigator.userAgent.match(/Safari/) || navigator.userAgent.match(/Macintosh/)) {
            width = "83px";
        } else {
            if (navigator.platform.match(/Win32/)) {
                width = "81px";
            } else {
                width = "92px";
            }
        }
        artiquario.style.width = width;
    }
    if (lang == "en") {
        if (navigator.userAgent.match(/Safari/) || navigator.userAgent.match(/Macintosh/)) {
            width = "105px";
        } else {
            if (navigator.platform.match(/Win32/)) {
                width = "";
            } else {
                width = "114px";
            }
        }
        artiquario.style.width = width;
    }
    if (lang == "de") {
        if (navigator.userAgent.match(/Safari/) || navigator.userAgent.match(/Macintosh/)) {
            width = "77px";
        } else {
            if (navigator.platform.match(/Win32/)) {
                width = "77px";
                padding = "4px";
                artiquario.style.paddingLeft = padding;
                artiquario.style.paddingRight = "0px";
            } else {
                width = "85px";
            }
        }
        artiquario.style.width = width;
    }
}

function setBackground() {
	var image = 660;
	var dimension = Element.getDimensions("body");
	var total = dimension.width;
	var width = (total - image) / 2;
	if (width <= 0) {
		width = 1;
	}
    if ($("shade")) { setHidden(total)};
    setGlobal(total);
	setLeft(width);
	setRight(width);
}

function setHidden(total) {
    var image = 800;
    var margin = (total - image) / 2;
    if (margin <= 0) {
        margin = 1;
    }
    margin += "px";
    var white_border = $("border");
	white_border.style.marginLeft = margin;
    white_border.style.marginRight = margin;
    var shade = $("shade");
    total += "px";
    shade.style.width = total;
}

function setGlobal(total) {
    total += "px";
    var global = $("global");
    global.style.width = total;
}

function setLeft(width) {
	width += "px";
	var top_left = $("top_left");
	top_left.style.width = width;
	var left = $("left");
	left.style.width = width;
	var bottom_left = $("bottom_left");
	bottom_left.style.width = width;
}

function setRight(width) {
	if (navigator.userAgent.match(/Gecko\/(\d{4})/)) {
		width = width - 1;
	}
	width += "px";
	var top_right = $("top_right");
	top_right.style.width = width;
	var right = $("right");
	right.style.width = width;
	var bottom_right = $("bottom_right");
	bottom_right.style.width = width;
}

function setLang(lang) {
    Cookie.set("lang", lang);
    var contactReload = Cookie.get('contact');
    var priceReload = Cookie.get('price');
    if (contactReload == "on") {
        new Ajax.Updater('flag', 'flag.php', {method: 'get'});
        contact();
    } else if (priceReload == "on") {
        new Ajax.Updater('flag', 'flag.php', {method: 'get'});
        price();
    }
    else {
        window.location.reload();
    }
}

function getLang() {
    var lang;
    lang = Cookie.get('lang');
    return lang;
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};








