 // JavaScript Document
//window.imgLoading = "/pcv/custom/themes/default/img/imgLoading.gif";
window.textsize = 100;

function fly_openPage(link){
	IFly.util.openPage(link);
}

function fly_openPageOnConfirm(link, msg){
	IFly.util.openPageOnConfirm(link, msg);
}

function fly_submitForm(formId, flyAction, formAction){
	IFly.util.submitForm(formId, flyAction, formAction);
}

function fly_xSubmitForm(formId, flyAction, formAction){
	IFly.util.xSubmitForm(formId, flyAction, formAction);
}

/*
function resizeCpBody(){
	if(navigator.appName == "Netscape"){
		var winSize = window.size();
		var winH = winSize.height;
		var h = winH - 108;
		var cpBody = document.getElementsByClassName('cp_ComponentBody').find(function(s) {return true;});
		if(typeof(cpBody) == "undefined" || cpBody==null){
			cpBody = $('cp_body');	
			h = winH - 45;
		}
		if(typeof(cpBody) != "undefined" && cpBody!=null){
			cpBody.style.overflow = "auto";
			cpBody.style.height = h + "px";
		}
	}
}

if(navigator.appName != "Microsoft Internet Explorer"){
	Event.observe(window, 'load', resizeCpBody);
	Event.observe(window, 'resize', resizeCpBody);
}*/



function switchIFlyMenu(menuId){
	resetIFlyMenu();
	var mItem = $(menuId);
	mItem.addClassName('selected');
}

function resetIFlyMenu(){
	var menu = $('menu');
	var items = menu.getElementsByClassName('menuLink');
	items.each(function(el){
		el.removeClassName('selected');
	});
}

/* IMG MANAGER */
var FieldGroupManager = Class.create();

FieldGroupManager.prototype = {
 initialize: function(containerEl, beginActive, passiveMode) {
   this.container = $(containerEl);
   this.mode = passiveMode ? 'passive' : 'active';
   this.controllerCheckbox = this.container.getElementsByClassName('controller').find(function(s) {return true;});
   this.inputs = this.container.getElementsBySelector('input', 'select', 'textarea');
   if(beginActive){
   	this.enable();
   }else{
   	this.disable();
   }
   Event.observe(this.controllerCheckbox, 'change', function(){
    this.el.toggleEnablement();
   }.bind({el: this}));
 },
 toggleEnablement: function(){
  if(this.mode=='active'){
	  if(this.controllerCheckbox.checked == false){
	   this.enable();
	  }else{
	   this.disable();
	  }
	}else{
	  if(this.controllerCheckbox.checked == true){
	   this.enable();
	  }else{
	   this.disable();
	  }
	}
 },
 enable: function(){
  this.inputs.each(function(el){if(!el.hasClassName('controller')){el.disabled = null;}});
 },
 disable: function(){
  this.inputs.each(function(el){if(!el.hasClassName('controller')){el.disabled = 'disabled';}});
 }
}



/* SCROLL MANAGER */
var IflyScrollManager = Class.create();

IflyScrollManager.prototype = {
	initialize: function(containerEl) {
		this.el = $(containerEl);
		this.movementStep = 4;
		this.movementTime = 0.00001;
		this.timeout;
		this.moving = false;
	},
	scrollBy: function(distance, direction,speed){
		if(!this.moving){
		}
	},
	scrollTo: function(x,y,speed){},
	___scrollTo: function(x,y,speed){
		if(!this.moving){
			var currX = this.getCurrentX();
			var currY = this.getCurrentY();
			var xDistance = x-currX;
			var yDistance = y-currY;
			if(Math.sqrt(xDistance*xDistance)>this.movementStep){
				this._moveBy(xDistance, this.movementTime, this.movementStep, 'x');
			}
			if(Math.sqrt(yDistance*yDistance)>this.movementStep){
				this._moveBy(yDistance, this.movementTime, this.movementStep, 'y');
			}
		}
	},
	getCurrentX: function(){
		return this.el.scrollLeft;
	},
	getCurrentY: function(){
		return this.el.scrollTop;
	},
	_moveBy: function(distance, time, step, axis){
			var direction="";
			if(distance>0){direction='+'}else{direction='-'}
			this._step(step,direction,axis);
			distance = Math.sqrt(distance*distance)-step;
			this.distance = distance;
			this.travelledDistance = 0;
			new PeriodicalExecuter(function(pe) {
				this.msh.moving = true;
				if(this.msh.travelledDistance>=this.distance){
					this.msh.moving = false;
					pe.stop();
				}
			  this.msh._step(step,direction,axis);
			}.bind({msh: this, distance: distance, time: time, step: step, axis: axis}), time);
	},
	_step: function(size, direction, axis){
		this.travelledDistance = this.travelledDistance+size;
		if(axis=='x'){
			if(direction=='+'){
				this.el.scrollLeft = this.el.scrollLeft + size ;
			}else{
				this.el.scrollLeft = this.el.scrollLeft - size ;
			}
		}else{
			if(direction=='+'){
				this.el.scrollTop = this.el.scrollTop + size ;
			}else{
				this.el.scrollTop = this.el.scrollTop - size ;
			}
		}
	}
}


/* ALTRO */
function alcedo_increaseTextSize(step, maxSize){
	var step = 10;
	var maxSize = 150;
	var newSize = window.textsize + step;
	if(newSize>maxSize){
		newSize = 100;
	}
	//alert('increaseTextSize: ' + newSize);
	var bodyContainer = $('af_body_center');
	window.textsize = newSize;
	var pars = bodyContainer.childElements();
	pars.each(function(el){
		//console.log(el.tagName);
		if(el.tagName=='P' || el.tagName=='p' || el.tagName=='UL' ){
			el.style.fontSize = this.size+"%";
		}
	}.bind({size: newSize}));
	//textContainer.style.fontSize = newSize+"%";
}



function alcedo_decreaseTextSize(step, minSize){
	var step = 10;
	var minSize = 70;
	var newSize = window.textsize - step;
	if(newSize<minSize){
		newSize = 100;
	}
	//alert('increaseTextSize: ' + newSize);
	var bodyContainer = $('af_body_center');
	window.textsize = newSize;
	var pars = bodyContainer.childElements();
	pars.each(function(el){
		if(el.tagName=='P' || el.tagName=='p'){
			el.style.fontSize = this.size+"%";
		}
	}.bind({size: newSize}));
	//textContainer.style.fontSize = newSize+"%";
}


function prepareMouseOverImages(){
	var images = $$('.mouseOverImage');	
	images.each(function(el){
		el.largesrc = el.largesrc;
		Event.observe(el, 'mouseover', function(e){showLargeImage(e);});
		Event.observe(el, 'mouseout', function(e){hideLargeImage(e);});
		Event.observe(el, 'mousemove', function(e){hideLargeImage(e);showLargeImage(e);});
	}
	);
}

function showLargeImage(evt){
	var openerEl = evt.element();
	var showEl = document.createElement('div');
	var showImg = document.createElement('img');
	var mouseX = evt.pointerX(), mouseY = evt.pointerY();
	showImg.src = window.imgLoading;
	showEl.className = "mouseOverImageContainer";
	showEl.style.top = mouseY+20+"px";
	showEl.style.left = mouseX+10+"px";
	showEl.appendChild(showImg);
	openerEl.insert({after: $(showEl)});
	//document.body.appendChild(showEl);
	var image = new Image();
	image.src = openerEl.getAttribute('largesrc');
	image = $(image);
	if(!image.complete){
		Event.observe(image, "load", function(){
			//alert(image.src);
			this.showImg.src = image.src;
			this.showImg.style.width = "auto";
			this.showImg.style.height = "auto";
		}.bind({showImg: showImg}));
	}else{
		showImg.src = image.src;
		showImg.style.width = "auto";
		showImg.style.height = "auto";
	}
	Event.stop(evt); 
}

function hideLargeImage(evt){
	var el = evt.element();
	var imgs = el.up().getElementsBySelector('.mouseOverImageContainer');
	imgs.invoke('remove');
}

function alcedo_hideImg(container){
	var imgs = $(container).getElementsBySelector('.imgContainer');
	imgs.invoke('remove');
}

function activateLoader(containerEl){
  $(containerEl).update('<span><img src="../loading.gif" /></span>'); //sulla root
}

function deactivateLoader(containerEl, errorMessage){
  $(containerEl).update('<span>'+errorMessage+'</span>');
}


function ifly_loadImage(id, path) { 
 	var img = $(id);
	img.src = "/"+window.ctxRoot+"custom/themes/misteroVenezia/loading.gif";
	var image = new Image();
	image.src = path;
	var o = new Object();
	o.id = id;
	o.path = path;
	if(!image.complete){
		Event.observe(image, "load", function(id, path){
			swapImage(this.id, this.path);
			}.bind(o));
	}else{
		swapImage(id, path);
	}
}

function swapImage(id, path){
 	var img = $(id)
	img.src = path;
}



/* ELIMINARE */
function loadImage_old(id, path, titolo, desc) { 
 	var img = $(id);
	img.src = "loading.gif";
    var image = new Image();
	image.src = path;
	var o = new Object();
	o.id = id;
	o.path = path;
	o.titolo = titolo;
	o.desc = desc;
	Event.observe(image, "load", function(id, path){swapImage(this.id, this.path,this.titolo, this.desc);}.bind(o));
	$('titolo').innerHTML = "";
	var div = $('desc_image');
  div.innerHTML = "Caricamento in corso...";
  
}

function hideAllCards(){
	var cards = $('equipeCardContainer').getElementsBySelector('.equipeCard');
	cards.invoke('hide');	
}

function showCard(id, openerLink){
	hideAllCards();
	$(id).show();
	openerLink = $(openerLink);
	openerLink.up(2).getElementsBySelector('li').invoke('removeClassName','active');
	openerLink.up().addClassName('active');
}


function showTab(id){
	hideAllCards();
	var tabToOpen = $(id+"_tab");
	var tabBodyToOpen = $(id+"_body");
	tabBodyToOpen.getElementsBySelector('li').invoke('removeClassName','active');
	tabToOpen.up('div',0).getElementsBySelector('li').invoke('removeClassName','tabSlipActive');
	tabToOpen.addClassName('tabSlipActive');
	tabToOpen.up().up().getElementsBySelector('.tabBody').invoke('hide');
	tabBodyToOpen.show();
	/*var at = tabBodyToOpen.down('li', 0).down(0);
	eval(at.onclick());*/
}

function removeHtmlFromText(htmlstring){
	return htmlstring.replace(/(<([^>]+)>)/ig,"");
}

function removeHtmlFromHtmlEditor(id){
	var el = Ext.getCmp(id);
	var v = el.getValue();
	v = v.replace(/\n/,'');
	el.setValue(removeHtmlFromText(v));
	//alert(el.getValue());
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE");
   var version = parseFloat(arVersion[1]);
   if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
	  for(var i=0; i<document.images.length; i++){
         var img = document.images[i];
         var imgName = img.src.toUpperCase();
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
            var imgID = (img.id) ? "id='" + img.id + "' " : "";
            var imgClass = (img.className) ? "class='" + img.className + "' " : "";
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
            var imgStyle = "display:inline-block;" + img.style.cssText; 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle;
            if (img.align == "right") imgStyle = "float:right;" + imgStyle;
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"; 
            img.outerHTML = strNewHTML;
            i = i-1;
         }
      }
   }    
}

//window.addEvent("load", correctPNG);


window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}






/**
* Not used. 
* For debugging purposes only
*/
function dump(Obj, ObjName) {
	var result = "";
	for (var i in Obj){
		result += ObjName + "." + i + " = " + Obj[i] + "\n";
	}
	return(result);
}

