var host = 'http://www.criticalmasscatania.it/';
//var host = 'http://127.0.0.1/cmct/';	

function getData()
{
 var oggi = new Date();
 if(document.getElementById('data').title.indexOf('ita') > -1) var mese = new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
 if(document.getElementById('data').title.indexOf('eng') > -1) var mese = new Array('January','February','March','April','May','June','July','August','September','October/','November','December');
 document.getElementById('data').innerHTML = '<b> Aggiornato '+mese[oggi.getMonth()]+' '+oggi.getFullYear()+'</b>';
 initscrll();
 }

  
///// AJAX 

function Ajaxinc(prg,cmd,prm)
{
	var ajxres = false;
	try{ajxres = new ActiveXObject("Msxml2.XMLHTTP");}
	catch(e){try{ajxres = new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e){ajxres = false;}}
	if(!ajxres && typeof XMLHttpRequest != 'undefined'){ajxres = new XMLHttpRequest();}
	if(ajxres)
	{
		qry = host+prg+"?prm="+escape(prm)+"&cmd="+escape(cmd)+"&rnd="+escape(Math.random());
		ajxres.onreadystatechange=function()
		{
			if(ajxres.readyState==4){document.getElementById(cmd).innerHTML = ajxres.responseText;}
		}
		ajxres.open("GET",qry,true);
		ajxres.send(null);
	}
}


function Ajaxresp(prg,cmd,msg)
{
	var ajxres = false;
	try{ajxres = new ActiveXObject("Msxml2.XMLHTTP");}
	catch(e){try{ajxres = new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e){ajxres = false;}}
	if(!ajxres && typeof XMLHttpRequest != 'undefined'){ajxres = new XMLHttpRequest();}
	if(ajxres)
	{
		ajxres.open('POST',host+prg);
		ajxres.setRequestHeader('content-type','application/x-www-form-urlencoded');
		ajxres.onreadystatechange = function() 
		{
			if(ajxres.readyState == 4 && ajxres.status == 200) 
			{
				//document.getElementById(cmd).innerHTML = ajxres.responseText;	//encodeURIComponent			
				document.getElementById(cmd).innerHTML = ajxres.responseText;
			}
		}
		ajxres.send(msg);
	}
	else
	{
		//document.getElementById(cmd).style.background = '#000000';
		document.getElementById(cmd).innerHTML = '';
	}
}



///// PARSE

function displayCheck()
{
	var rec = '';	
	
	/*
	var tipObj = frm.tip;
	var tip = '';	
	for(var i=0; i<tipObj.length; i++)
	  if(tipObj[i].checked) 
		tip = tipObj[i].value;
	*/	
	
	window.parent.document.title = frm.chkctr4.value;

	//if(frm.chkctr4)
		if(frm.chkctr4.checked)
		{
			//rec = frm.chkctr4.value;	
			document.getElementById('divctr4').style.display = 'block';	
		}
		else {document.getElementById('divctr4').style.display = 'none';}
}



function parseFormContact()
{
	document.getElementById('res').innerHTML = 'invio in corso...';

	var msg = '';
	msg += '&cmd='+frm.cmd.value;
	msg += '&ct1='+escape(replaceAccent(frm.ct1.value));
	msg += '&ct3='+escape(replaceAccent(frm.ct3.value));	
	//msg += '&ct3='+escape(replaceAccent(frm.ct3.value));
	//msg += '&ct4='+escape(replaceAccent(frm.ct4.value));
	msg += '&ct5='+escape(replaceAccent(frm.ct5.value));
						
	Ajaxresp('form/smail.php','res',msg);
}



function replaceAccent(msg)
{
	var tmsg = msg;
	tmsg = tmsg.replace("à", "a'");
	tmsg = tmsg.replace("è", "e'");
	tmsg = tmsg.replace("é", "e'");
	tmsg = tmsg.replace("ì", "i'");		
	tmsg = tmsg.replace("ò", "o'");
	tmsg = tmsg.replace("ù", "u'");
	return tmsg;
}




function visForm()
{
	document.getElementById('divfrm').style.display = 'block';
	document.getElementById('divfrm').style.top = document.body.scrollTop+140;
	document.getElementById('divfrm').style.left = (document.body.clientWidth/2)-220;
}

function hidForm()
{
	document.getElementById('divfrm').style.display = 'none';
}



////// DRAG

var drag = 
{
	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.ondragStart	= new Function();
		o.root.ondragEnd	= new Function();
		o.root.ondrag		= new Function();
	},

	start : function(e)
	{
		var o = drag.obj = this;
		
		e = drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.ondragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= drag.drag;
		document.onmouseup		= drag.end;

		return false;
	},

	drag : function(e)
	{
		e = drag.fixE(e);
		var o = drag.obj;
		
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		drag.obj.lastMouseX	= ex;
		drag.obj.lastMouseY	= ey;

		drag.obj.root.ondrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		drag.obj.root.ondragEnd(	parseInt(drag.obj.root.style[drag.obj.hmode ? "left" : "right"]), 
									parseInt(drag.obj.root.style[drag.obj.vmode ? "top" : "bottom"]));
		
		drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


////// MENU SCROLL

var movy = 0, ydiff = 0, ystart = 0,  myYOffs = 0;
function initscrll()
{
    y = "document.body.scrollTop";
    ystart = eval(y)+myYOffs;
    YScrll(); 
}


function YScrll()
{
   yy = eval(y) + myYOffs;
   ydiff = ystart - yy;
   if((ydiff<(-1)) || (ydiff>(1))) movy = Math.round(ydiff/10), ystart -= movy;
    if(ystart >= myYOffs)
	{	
        document.getElementById('nav3m').style.top = ystart;
        document.getElementById('lnksn').style.top = ystart * .92;		
	}	
    else 
	{
         document.getElementById('nav3m').style.top = myYOffs;
         document.getElementById('lnksn').style.top = myYOffs;		 
	}	 
    setTimeout("YScrll()",10);
}




/////// SLIDESHOW

var Nr=1;
function SlideShow()
{
   shiftOpacity('sldu', 600);
   shiftOpacity('sldd', 600);
}

function NextFoto(id)
{
   Nr +=1;
   if (Nr>3){Nr=1;}
   document.getElementById(id).src = "img/"+(Nr)+".jpg";
}


function changeOpac(opacity,id)
{
var object = document.getElementById(id).style; 
object.opacity = (opacity/100);
object.MozOpacity = (opacity/100);
object.KhtmlOpacity = (opacity/100);
object.filter = "alpha(opacity="+opacity+")";
}


function shiftOpacity(id,millisec)
{
  if(document.getElementById(id).style.opacity == 0){opacity(id,0,100,millisec);}
  else{opacity(id,100,0,millisec);}
}



function opacity(id,opacStart,opacEnd,millisec)
{
  var speed = Math.round(millisec/100);
  var timer = 0;
  if(opacStart>opacEnd) 
  {
    for(i=opacStart; i>=opacEnd; i--)
    {
      setTimeout("changeOpac("+i+",'"+id+"')",(timer * speed));
      timer++;
    }
  }
  else
  if(opacStart<opacEnd)
  {
     NextFoto(id);
     for(i=opacStart; i<=opacEnd; i++)
     {
       setTimeout("changeOpac("+i+",'"+id+"')",(timer * speed));
       timer++;
     }
  }
}

// FOTO
var mflg = '';

function showFoto(hit,flg)
{
	//window.parent.document.title = hit.src; //document.images.length;
	ht = hit.src;
	displayFoto(ht, hit.title, flg);
	if(ht.indexOf('jpg') > -1)  	displayFoto(ht.replace('-.jpg', '.jpg'), hit.title, flg);	
	if(ht.indexOf('g.jpg') > -1) 	displayFoto(ht.replace('-g.jpg', '.gif'), hit.title, flg);	
	if(ht.indexOf('gif') > -1) 		displayFoto(ht.replace('-.gif', '.gif'), hit.title, flg);	
}


function displayFoto(src,tit,flg)
{
	document.getElementById('foto').style.display = 'block';
	document.getElementById('foto').src = src;	
	document.getElementById('foto').title = tit;
	document.getElementById('foto').style.borderWidth = '3px';
	document.getElementById('foto').style.top = document.body.scrollTop+40;	
	document.getElementById(flg).style.color='#aa0000';
try{	
	document.getElementById(mflg).style.color='#287e50';	
}catch(e){mflg = flg;} 
finally{mflg = flg;}	
	
	//document.getElementById('preld').innerHTML = 'Loading';
	// /timpf = setInterval("preloadFoto();",100);
}

/*
function preloadFoto()
{
   if(document.images['foto'].readyState == "complete")
   { 
	//document.getElementById('preld').innerHTML = '';
    if(timpf != null){clearInterval(timpf); timpf = null;}
	pngfix();	
   }
   //else{document.getElementById('preld').innerHTML += ' . ';}
}
*/

function closeFoto()
{
	document.getElementById('foto').src = 'ima/ldn.gif';	
	document.getElementById('foto').style.display = 'none';
}

function backTop()
{
	document.body.scrollTop = 0;
}