function admin_lastminute_image_change (img) {
	$('image-show').href="/lib/thumbnailer/phpThumb.php?src=/data/img/" + img + ".jpg&w=880&h=550&zc=1";
	$('image-show-img').src="/lib/thumbnailer/phpThumb.php?src=/data/img/" + img + ".jpg&w=445&h=100&zc=1";
}
function admin_lastminute_del(id) {
if (confirm("Sei sicuro di voler eliminare questa offerta ?")) {
	new Ajax.Updater('admin-lastminute-gallery', '/site/ajax.php', {asynchronous:true,parameters: 'method=admin&action=lastminute_del&id='+encodeURIComponent(id), onComplete:admin_edit_refresh_success()});
}
}
function admin_lastminute_change (id) {
	if (id != admin_lastminute_current) {
		Element.addClassName('lastminute-'+id,"selected"); 
		Element.removeClassName('lastminute-'+admin_lastminute_current,"selected"); 
		admin_lastminute_current=id;
		$('cmd-lastminute-mod').href = "/admin-lastminute-mod-"+id+".html";
	}
}

function admin_edit_refresh_success() {
	alert('operazione completata');
	if ($('admin_lastminute_current')) admin_lastminute_current=$('admin_lastminute_current').value;
	if ($('admin_img_current')) admin_img_current=$('admin_img_current').value;
}
function admin_img_mod(id) {
	var ret = prompt('inserisci il nuovo identificativo per ' + id);
	new Ajax.Updater('admin-image-gallery', '/site/ajax.php', {asynchronous:true,parameters: 'method=admin&action=img_mod&id='+encodeURIComponent(id) + '&data=' + encodeURIComponent(ret), onComplete:admin_edit_refresh_success()});
}
function admin_img_del(id) {
if (confirm("Sei sicuro di voler eliminare " + id + " ?")) {
	new Ajax.Updater('admin-image-gallery', '/site/ajax.php', {asynchronous:true,parameters: 'method=admin&action=img_del&id='+encodeURIComponent(id), onComplete:admin_edit_refresh_success()});
}
}
function admin_img_change (id) {
	if (id != admin_img_current) {
		Element.addClassName('img-'+id,"selected"); 
		Element.removeClassName('img-'+admin_img_current,"selected"); 
		admin_img_current=id;
	}
}
function buttonStateHandler(button, enabled) {
 if (button == "prev-arrow") 
   $('prev-arrow').src = enabled ? "/lib/lightbox2/images/prev.gif" : "/lib/lightbox2/images/blank.gif";
 else 
   $('next-arrow').src = enabled ? "/lib/lightbox2/images/next.gif" : "/lib/lightbox2/images/blank.gif"
}

function animHandler(carouselID, status, direction) {
  var region = $(carouselID).down(".carousel-clip-region")
  if (status == "before") {
    Effect.Fade(region, {to: 0.8, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
  if (status == "after") {
    Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
}
var carousel;
var carousel_orientation=1;
var carousel_timeout;
function carousel_scroll() {
	if (carousel.currentIndex == (carousel.options.size-3)) carousel_orientation = -1;
	if (carousel.currentIndex == 0) carousel_orientation = 1;
	if (carousel_orientation==1) carousel.nextScroll(); else carousel.prevScroll();
//	console.log(carousel.currentIndex + "|" + (carousel.options.size-3));
	carousel_timeout = setTimeout('carousel_scroll();',10000);
}
function initCarousel() {      
	carousel = new Carousel("html-carousel",  {numVisible:3,scrollInc:1,animHandler: animHandler,buttonStateHandler: buttonStateHandler,animParameters: {duration: 0.5}});
	carousel_timeout = setTimeout('carousel_scroll();',10000);
};
function fckeditor_updater()
{
        this.UpdateEditorFormValue = function()
        {
                for ( i = 0; i < parent.frames.length; ++i )
                        if ( parent.frames[i].FCK )
                                parent.frames[i].FCK.UpdateLinkedField();
        }
}

function string_trim(s) {
while (s.substring(0,1) == ' ') {
	s = s.substring(1, sString.length);
}
while (s.substring(s.length-1, s.length) == ' ') {
	s= s.substring(0,s.length-1);
}
return s;
}
function ajax_generic_failure() {
	alert('ci dispiace,si e\' verificato un errore. chiudi la finestra del navigatore e riapri il sito.');
	window.refresh();
}
function spinner_reposition() {
	if (waiting==true) {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
/*	var spinnerTop = arrayPageScroll[1] + (arrayPageSize[3] / 2);
	var spinnerLeft = arrayPageScroll[0] + (arrayPageSize[2] / 2);*/
	var spinnerTop = arrayPageScroll[1] + arrayPageSize[3] -50;
	var spinnerLeft = arrayPageScroll[0] + arrayPageSize[2] -100;
	new Effect.Move('spinner',{x:(spinnerLeft),y:(spinnerTop), mode: 'absolute',duration:0});
	}
}
function sifr_setup() {
	sIFR.replaceElement(named({sSelector:"h1.title", sFlashSrc:"/lib/sifr2/univers_lt_std_47_cn_lt.swf", sColor:"#000000", sLinkColor:"#bbbdc0", sBgColor:"#ffffff", sHoverColor:"#9b9da0", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=left&offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:"h1.offertitle", sFlashSrc:"/lib/sifr2/helvetica_neue_lt_std_thin_bull_2.swf", sColor:(document.getElementsByTagName('body')[0].className=='yellow'?"#000000":"#ffffff"), sLinkColor:"#ffffff", sBgColor:"#ffffff", sHoverColor:"#9b9da0", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=left&offsetTop=0"})); 
//	sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"/lib/sifr2/univers_lt_std_47_cn_lt.swf", sColor:"#9c1f2e", sLinkColor:"#bbbdc0", sBgColor:"#ffffff", sHoverColor:"#9b9da0", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=left&offsetTop=0"}));
}
function slideshow_refreshtext() {
	Element.removeClassName($('showcase-txt'),'sIFR-replaced');
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"#showcase-txt", sFlashSrc:"/lib/sifr2/helvetica_neue_lt_std_thin_bull_2.swf", sColor:(document.getElementsByTagName('body')[0].className=='yellow'?"#000000":"#ffffff"), sLinkColor:"#ffffff", sBgColor:"#ffffff", sHoverColor:"#9b9da0", nPaddingTop:0, nPaddingBottom:0, sWmode: "transparent", sFlashVars:"textalign=left&offsetTop=0"})); 
	};
}
function setCookie(sNome, sValore, iGiorni) {
  var dtOggi = new Date()
  var dtExpires = new Date()
  dtExpires.setTime
    (dtOggi.getTime() + 24 * iGiorni * 3600000)
  document.cookie = sNome + "=" + escape(sValore) +
    "; expires=" + dtExpires.toGMTString();
}
function getCookie(sNome) {
  var asCookies = document.cookie.split("; ");
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) { 
      return (unescape(asCookie[1]));
    }
  }
  return("");
}
function delCookie(sNome) {
  setCookie(sNome, "");
}	   
function ajax_cart_failure(request) {
	ajax_generic_failure();
}
function getY( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
function slideshow_preload() {
	slideshow_preload_pos++;
//	slideshow_preload_img.src = "/img/header/" + slideshow[slideshow_preload_pos][0];
slideshow_preload_img.src = "/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[slideshow_preload_pos][0] + ".jpg&w=650&h=315&zc=1";
	if ((slideshow.length-1) > slideshow_preload_pos) {
		slideshow_preload_img.onload = function() {
			setTimeout('slideshow_preload_img.onload=null;slideshow_preload();',10);
		}
	}
}
function slideshow_appear() {
	slideshow_pos++;
//	console.log (slideshow_pos + ":",slideshow_pos_old);
	hd_l_a[slideshow_pos].setAttribute('rel','lightbox[shownheader]');	
	hd_l_a[slideshow_pos_old].setAttribute('rel','lightbox[header]');	
	// hd_img_two.src="/img/header/" + slideshow[slideshow_pos][0];
	hd_img_two.src = "/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[slideshow_pos][0] + ".jpg&w=650&h=315&zc=1";
	new Effect.Parallel([
		new Effect.Fade($('showcase-bg-lower'),{sync:true}),
		new Effect.Move($('showcase-bg-lower'),{y:45,sync:true}),
		new Effect.Move($('showcase-txt'),{y:45,sync:true})
	], {transition: Effect.Transitions.linear,
			afterFinish: function() {
				$('showcase-txt').innerHTML = (slideshow[slideshow_pos][1]) + " ";
				slideshow_refreshtext();
				new Effect.Parallel([
					new Effect.Appear(hd_img_two,{sync:true})
				],{transition: Effect.Transitions.linear,
					afterFinish: function() {
						// hd_img.src="/img/header/" + slideshow[slideshow_pos][0];
						hd_img.src = "/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[slideshow_pos][0] + ".jpg&w=650&h=315&zc=1";
						hd_a.href = "/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[slideshow_pos][0] + ".jpg&w=880&h=550";
						hd_a.setAttribute('desc',slideshow[slideshow_pos][1]);
						Element.show(hd_img);
						Element.hide(hd_img_two);
						new Effect.Parallel([
							new Effect.Appear($('showcase-bg-lower'),{sync:true,to:0.8}),
							new Effect.Move($('showcase-bg-lower'),{y:-45,sync:true}),
							new Effect.Move($('showcase-txt'),{y:-45,sync:true})
						], {transition: Effect.Transitions.linear});
						slideshow_pos_old = slideshow_pos;
						if ((slideshow.length-1) == slideshow_pos) slideshow_pos = -1;
						setTimeout('slideshow_appear();',slideshow_time*1000);
					}
				});
			}
	});
}
function page_booking_setup() {
	obj_create('div',{parent:'showcase-container',id:'showcase-bg-lower',classname:'chamaleon'});
	obj_create('div',{parent:'showcase-container',id:'showcase-txt'});
	$('showcase-txt').innerHTML = "richiesta di prenotazione";
	slideshow_refreshtext();
}
function slideshow_setup() {
	obj_create('div',{parent:'showcase-container',id:'showcase-bg-lower',classname:'chamaleon'});
	var hd_l = obj_create('div',{parent:'showcase-container',id:'showcase-links'});
	hd_a = obj_create('a',{parent:'showcase-container',id:'showcase-a',rel:'lightbox[header]',href:("/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[0][0] + ".jpg&w=880&h=550"),title:'zicarmela.com',alt:'zicarmela.com',desc:slideshow[0][1]});
	hd_img = obj_create('img',{parent:'showcase-a',id:'showcase-img'});
	hd_img_two = obj_create('img',{parent:'showcase-a',id:'showcase-img-two',src:'/img/nulpix.gif'});
	new Effect.Fade(hd_img_two);
	new Effect.Appear(hd_img,{duration:0});
	if (slideshow.length != 1) {
		for (i=0;i<slideshow.length;i++) {
			hd_l_a[i] = obj_create('a',{parent:'showcase-links',id:('showcase-a'+i),rel:'lightbox[header]',href:("/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[i][0] + ".jpg&w=880&h=550"),title:'zicarmela.com',alt:'zicarmela.com',desc:(slideshow[i][1])});
		}
		hd_l_a[0].setAttribute('rel','lightbox[shownheader]');	
		hd_img.onload = function() {
			setTimeout('hd_img.onload=null;slideshow_preload();',10);
			setTimeout('slideshow_appear();',slideshow_time*1000);
		}
	}
	hd_img.src = "/lib/thumbnailer/phpThumb.php?src=/data/img/" + slideshow[0][0] + ".jpg&w=650&h=315&zc=1";
	hd_a.onclick = function () { myLightbox.start(this); return false; };
	obj_create('div',{parent:'showcase-container',id:'showcase-txt'});
	$('showcase-txt').innerHTML = (slideshow[0][1]);
	slideshow_refreshtext();
}
function getX( oElement ) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
}
Position.Center = function(element, parent) {
        var w, h, pw, ph;
        var d = Element.getDimensions(element);
        w = d.width;
        h = d.height;
        Position.prepare();
        if (!parent) {
                var ws = Position.GetWindowSize();
                pw = ws[0];
                ph = ws[1];
        } else {
                pw = parent.offsetWidth;
                ph = parent.offsetHeight;
        }
        element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
        element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
}
function obj_create(objtype,objparams) {
		if (objtype=="textnode") {
			o = document.createTextNode(objparams["value"]);
		} else {
			o = document.createElement(objtype);
		}
		for (objparam in objparams) {
			if (objparam == "classname") {
				var cobjparams = objparams[objparam].split(",");
				for (i=0;i<cobjparams.length;i++) {
					Element.addClassName(o,cobjparams[i]); 
				};
			} else if (objparam=="value") {
				//o.appendChild(document.createTextNode(objparams["value"]));
			} else if (objparam=="onclick") {
				o.onclick=objparams[objparam];
			} else if (objparam=="onmouseover") {
				o.onmouseover=objparams[objparam];
			} else if (objparam=="onmouseout") {
				o.onmouseout=objparams[objparam];
			} else if (objparam=="style") {
				var cobjparams = objparams[objparam].split(",");
				for (i=0;i<cobjparams.length;i++) {
					var cobjparams_a=cobjparams[i].split(":");
					eval("o.style." + cobjparams_a[0] + "=cobjparams_a[1]");
				};
			} else if (objparam=="parent") {
				if (typeof objparams[objparam]=="string" && objparams[objparam] == "body") {
					objparams[objparam] = document.getElementsByTagName('body')[0];
				}
				if (typeof objparams[objparam]=="string") {
					objparams[objparam]= $(objparams[objparam]);
				};
				objparams[objparam].appendChild(o);
			} else {
				o.setAttribute(objparam,objparams[objparam]);
			};
		};
	return o;
};
function stopPropagation(e) 
{ 
    e = e||event;
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; 
};
function hideFocus(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		if (elem) {
			elem = (elem.tagName != 'A') ? elem.parentNode : elem;
			if (elem.tagName == 'A'){
				elem.blur();
			}
		}
	}
}
document.onclick = hideFocus;
var ajax_action;
var hd_l_a = new Array();
var menu_shown;
var action_drag;
var price;
var hd_a;
var hd_img;
var hd_img_two;
var hd_txt;
var slideshow_time = 6;
var slideshow_pos = 0;
var slideshow_pos_old = 0;
var slideshow_preload_pos = 0;
var slideshow_preload_img = new Image();
var	spinner;
var waiting = false;
if ($("description") != undefined) var fckedit;
// var obj_focus;
var ajax_responder = {
	onCreate: function(){
		waiting=true;
		spinner_reposition();
		new Effect.Appear(spinner,{duration:0.5,queue: {position: 'end', scope: 'spinner'}});
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
//			new Effect.Fade(spinner,{queue:'end'});
			new Effect.Fade(spinner,{duration:0.5,queue: {position: 'end', scope: 'spinner'}});
			waiting=false;
		}
	}
};
function animations_activate() {
	spinner = obj_create('div',{parent:'body',id:'spinner',style:'display:none'});
 	waiting=true;
	spinner_reposition();
	waiting=false;
	var menu_rules = {
		'#menu-right li ul': function(el) {
			Element.hide(el);
		},
		'#menu-right li': function(el) {
			Element.cleanWhitespace(el);
			if ((el.parentNode.id != "menu-right") && (el.childNodes[1] == null)) {
				if (el.childNodes[0].href == window.location.href) {
					Element.show(el.parentNode); 
					menu_shown = el.parentNode.parentNode;
				}
			}
			if (el.childNodes[0].href == window.location.href) {
				Element.addClassName(el.childNodes[0],'menu-shown-el');
			}
			el.onclick = function(evt) {
				stopPropagation(evt);
				if ((el.parentNode.id != "menu-right") || (el.childNodes[1] == null)) {
				return true;
				} else {
					if (menu_shown != null  && el != menu_shown) {
						new Effect.Parallel([
							Effect.BlindUp(menu_shown.childNodes[1], {sync:true}),
							Effect.BlindDown(el.childNodes[1], {sync:true})
						],{duration:2,queue:{position:'end',scope:'menu'},transition:Effect.Transitions.Bounce});
						menu_shown = el;
					} else if (menu_shown != null  && el == menu_shown) {
						Effect.BlindUp(el.childNodes[1],{duration:2,queue:{position:'end',scope:'menu'},transition:Effect.Transitions.Bounce});
						menu_shown = null;
					} else {
						Effect.BlindDown(el.childNodes[1],{duration:2,queue:{position:'end',scope:'menu'},transition:Effect.Transitions.BouncePast});
						menu_shown = el;
					};
					return false;
				};
			}
		}
	}
	Behaviour.register(menu_rules);
	Behaviour.apply();
	Ajax.Responders.register(ajax_responder);
	Element.removeClassName($('menu-right'),'hidden');
	if (!$('page-booking')) slideshow_setup(); else page_booking_setup();
	if ($('page-admin') && $('html')) {
		fckedit = new FCKeditor( 'html' ) ;
		fckedit.BasePath = "/lib/fckeditor/" ;
		fckedit.SkinPath = 'editor/skins/office2003';

		fckedit.ToolbarSet = 'Custom';
		fckedit.ReplaceTextarea();
//		setTimeout('buildeditor()',250);
	}
	sifr_setup();
	changecss('#menu-right li ul','display','block');
//	$('sidebar1').setAttribute('min-height',$('container').clientHeight + "px");
// console.log (Element.getHeight($('container')));
// Element.setHeight($('sidebar1'),Element.getHeight($('container'))-20);
// Element.setHeight($('sidebar1'),1000);
//	if (typeof tinyMCE != "undefined") mce_setup(tinyMCE);
//	if (typeof $("registration-form")) registration_form_setup();
}
function init() {
	animations_activate();
	initCarousel();
};
document.observe("dom:loaded", init);

//window.observe("scroll",spinner_reposition);
//Event.observe(window, "load", initCarousel);
//Event.observe(window, 'load', animations_activate, false);
//Event.observe(window, 'scroll', spinner_reposition, false);

