var dp_char=".";

function padLeft(o,p,n) {
	o=""+o;
	while(o.length<n) o=p+o;
	return o;
}
function putClass(target,cn) {if (target.className.indexOf(cn)==-1) target.className+=" "+cn;}
function remClass(target,cn) {target.className=target.className.split(cn).join("");}

function format_ncd(src,ncd) {
	var n=parseFloat(src);
	if (isNaN(n)) n=0;
	src=""+n;
	var pt=src.indexOf(dp_char);//check for decimal point
	//alert(pt+src+1);
	if (pt==-1) {//had no decimal point
		src+=dp_char;
		pt=src.indexOf(dp_char);
	}
	while((pt+ncd+1)>src.length) src+="0";//add zeroes while needed
	src=src.substr(0,pt+ncd+1);//truncate if needed
	return src;
}

function format_euros(src) {return format_ncd(src,2)+" €";}

function format_data(d) {
	return ""+d.getFullYear()+"/"+padLeft(d.getMonth()+1,"0",2)+"/"+padLeft(d.getDate(),"0",2);
}

function getPosition(o) {return getPos(o,[0,0]);}
function getPos(o,p) {
	if (o==element.capImg.offsetParent) return p;
	p[0]+=o.offsetLeft;
	p[1]+=o.offsetTop;
	if (o.offsetParent) {
		//alert((o.offsetParent)+": "+o.offsetParent.tagName);
		p=getPos(o.offsetParent,p);
	}
	return p;
}

function getScrollingPosition() 
{ 
	position = [ 
		document.body.scrollLeft, 
		document.body.scrollTop 
	]; 
	return position; 
}

/*function posItem(foto,p) {
	//alert('posItem');
	//foto.filters[0].apply();
	var s=getScrollingPosition();
	foto.style.left=window.event.x+p[0]+s[0];
	foto.style.top=window.event.y+p[1]+s[1];
	foto.style.display='block';
	foto.style.visibility='visible';
	//foto.filters[0].play();
	//window.status=window.event.y;
}*/
function posItem(foto,p) {
	//alert('posItem');
	//foto.filters[0].apply();
	var s=getScrollingPosition();
	foto.style.left=window.event.x+p[0]+s[0];
	foto.style.top=window.event.y+p[1]+s[1];
	foto.style.display='block';
	foto.style.visibility='visible';
	//foto.filters[0].play();
	//window.status=window.event.y;
}

function scanDown(o) {
	if (o) {

		for(var i=o.firstChild;i;i=i.nextSibling) {
			switch(i.className) {
			case "protoblock":
				info_pane.innerHTML+="<hr/>PROTOTYPE"+i.innerHTML;
				//i.style.display="none";
				break;
			case "ctrlbox":
				info_pane.innerHTML+="<hr/>CTRLBOX"+i.innerHTML;
				//i.style.display="none";
				break;
			}
		}
		if (o.genCfg) {
			try {
				//alert(""+o+": "+o.tagName+" "+o.className);
				info_pane.innerHTML+=o.genCfg();
			} catch(e) {
				alert(e.message);
			}
		}
		scanDown(o.parentNode);
	}
}

function setClass(o,c) {
	o.className=c+(c==""?"":" regular")+" zz_mcbfix";
	if (o.mountBehaviors) o.mountBehaviors();
}

function imgClick(o) {
	//alert("imgClick! ");
	//alert(o.name+"->"+site.getNode(o.name).text);
	//o.document.create
	if (editing&&o.className.indexOf("inpress_img_uponclick")!=-1) {
		tempSelection=document.body.createControlRange();
		tempSelection.add(o);
		tempSelection.select();
		if (piclib_toolbar.style.display=="none") togPicLib();
	} else {
		if (ie)
			fotoView(site.getNode(o.name).text,'Foto');
		else window.open(o.src);
	}
}

function delLibImg(id) {
	if (confirm("Apagar esta imagem da livraria?"))
		navigate("imgDel.asp?img=/pic_lib/img[@id='"+id+"']","_self");
}

function fotoView(i,title) {
	//alert("fotoView "+i);
	if(!title) title="";
	var info=site.imgInfo(i);
	//alert(""+info.xml);
	var x=parseInt(info.selectSingleNode("//x").text);
	var y=parseInt(info.selectSingleNode("//y").text);
	var scr="0";
    if (x>777||y>577) {
        y+=18;
        x+=18;
        scr="1";
    }
	//alert(x+","+y);
	window.open(auxPath+'/site/imgView.asp?title='+title+'&amp;src='+i,'_blank','width='+x+' height='+y+' toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+scr+',resizable=0')
}

function fotoPrint(i,title) {
	//alert("printing "+i);
	if(!title) title="image preview/print";
	var info=site.imgInfo(i);
	//alert(""+info.xml);
	var x=parseInt(info.selectSingleNode("//x").text);
	var y=25+parseInt(info.selectSingleNode("//y").text);
	var scrl=x>600||y>400;
	if (scrl) {x+=20;y+=20;}
	x+=10;y+=4;
	//alert(x+","+y);
	window.open(auxPath+'/site/imgPrint.asp?title='+title+'&amp;src=../'+i,'_blank','width='+x+' height='+y+' toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+(scrl?'1':'0')+',resizable=0')
}

function makeNode(text) {
	var doc;
	if (window.ActiveXObject) {
		doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML(text);
	} else {// code for Mozilla, Firefox, Opera, etc.
		var parser=new DOMParser();
		doc=parser.parseFromString(text,"text/xml");
	}// documentElement always represents the root node
	return doc.documentElement;
}

function togDisplay(element) {if (!ie) element.style.display=element.style.display=='none'?'':'none';}

//isto porque necessitamos de varias funções a executar no 'page onload'
var startups=new Array();
//aqui avaliamos um array de 'onloads' concorrentes
function pageStart() {
    //montras();
    for(s in startups)
        if (typeof(startups[s])=="string") eval(startups[s])
        else startups[s]();
}

function togBody(o) {
    //alert((!ie)&&o&&o.lastChild&&o.lastChild.previousSibling);
    if (!ie&&o&&o.lastChild&&o.lastChild.previousSibling) {
        var t=o.lastChild.previousSibling;
        //alert(t);
        t.style.display=(t.style.display=='none'?'':'none');
    }
}

//scroller helpper functions --------------------------------------------------------------
	document.onmousemove = getMouseXY;
	var mouse_x=0;
	var mouse_y=0;
	
	function getx(e) {
		var left = 0;
		
		if (e.offsetParent) {
			while (e.offsetParent) {
				left += e.offsetLeft
				e = e.offsetParent;
			}
		} else if (e.x)
			left += e.x;
		
		return left;
	}

	function gety(e) {
		var top = 0;
		
		if (e.offsetParent) {
			while (e.offsetParent) {
				top += e.offsetTop
				e = e.offsetParent;
			}
		} else if (e.y)
			top += e.y;
		
		return top;
	}

	function getMouseXY(m){
		//window.status="getMouseXY(m)";

  		if (ie)	{
			 var tmpX = event.clientX;
  			 var tmpY = event.clientY;
			}	
 		else 	{
  			 var tmpX = m.pageX;
			 var tmpY = m.pageY;
  			}  
		if (!document.body.scrollTop)
			{
			 var l = document.documentElement.scrollLeft;	
			 var t = document.documentElement.scrollTop;
			}
		else 	{
			 var l = document.body.scrollLeft;	
			 var t = document.body.scrollTop;	
			}
		mouse_x=tmpX + l;
		mouse_y=tmpY + t;
		return {"x":mouse_x,"y":mouse_y};
  	}


function hscroller(o) {
	o.behavior='scroll';
	window.status='';
	var p=(o.clientWidth/2-(mouse_x-getx(o)))/10;
	//window.status=getx(o);
	if (p>0) {
		o.direction="right";
		o.scrollAmount=p;
	} else {
		o.direction="left";
		o.scrollAmount=-p;
	}
}

