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) {
	//alert(""+o+": "+o.tagName+" "+o.className);
	if (o) {
		try {
			if (o.genCfg)
				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("click on img");
    //alert(o);
	//alert(tempSelection.item);
	if (editing) {
	    tempSelection=document.body.createControlRange();
	    tempSelection.add(o);
	    tempSelection.select();
	    //upload(o);
	    if (piclib_toolbar.style.display=="none") togPicLib();
	    //pickImg(o);
	} else fotoView(site.getNode(o.name).text,'Foto');//navigate("?at="+o.name);
}

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("clicked "+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>400) {
        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]();
}

