/* ver. 07.01.10
Draw VML on page & other common functions.
Author Roberto Trinchero.
Thanks to Adrian Jowett (Adult Dental Care - University of Sheffield) and Stewart Harris (CiCs - University of Sheffield) for inspiration and basic code. 
*/

[].indexOf || (Array.prototype.indexOf = function(v){
       for(var i = this.length; i-- && this[i] !== v;);
       return i;
});


function mandaIndietro(z)
{
z.style.left=precedX;
z.style.top=precedY;
aggiornaContatori();
}


function progressione(mini,maxi,regola,nume)
{
var ni=mini+Math.floor(Math.random()*(maxi-mini));
var nt=0;
var arg="<font size=+3>"+ni;
for(var i=1;i<=nume;i++)
{
regola1=regola.replace(/n/g,ni)
nt=eval(regola1);
arg=arg+"&nbsp;&nbsp;&nbsp;"+nt;
ni=nt;
}
return arg+"</font>";
}




function eleCasuale(lista)
{
l=lista.length;
i=Math.floor(Math.random()*l);
return lista[i];
}

function figCasuali(nume,lista)
{
var arg="";
var elem="";
for(var i=1;i<=nume;i++)
{
elem=eleCasuale(lista);
arg=arg+"<img src="+elem+">";
lista.splice(lista.indexOf(elem), 1);
}
return arg;
}

var iniz = "0,0";
var offY = 0;
var offX = 0;
var drawapproved = false;
var precedX = 0;
var precedY = 0;


function showXY()
{
window.status = 'X: ' + window.event.clientX + ' Y: ' + window.event.clientY;
z=event.srcElement;
precedX=z.style.pixelLeft;
precedY=z.style.pixelTop;
}

function drawSN(){
if (event.keyCode==13) return (event.keyCode != 13);
if (event.keyCode==119) {
document.onclick=clikmouse;
document.onmousemove=momouse;
elastoc=document.getElementById("elastoc");
if (!elastoc){
var elastoc = document.createElement("v:line");
elastoc.id="elastoc";
elastoc.style.position="absolute";
elastoc.style.allowoverlap="true";
elastoc.style.zIndex=10;
elastoc.strokecolor="#c0c0c0";
elastoc.strokeweight="3px";
var elastocS = document.createElement("v:stroke");
elastocS.dashstyle="dot";
elastoc.appendChild(elastocS);
document.body.appendChild(elastoc);
}
elastoc.from="0,0";
elastoc.to="0,0";
elastoc.style.visibility="hidden";
offY=elastoc.offsetTop;
offX=elastoc.offsetLeft;

if (drawapproved)
{
document.body.style.cursor="auto";
drawapproved=false;
}
else {
document.body.style.cursor="url('pencil.ico'),crosshair";
drawapproved=true;
}

}
}

function line(xy1,xy2){
if (drawapproved && xy1!="0,0")
{
var arg = document.createElement("v:line");
arg.style.position="absolute";
arg.style.allowoverlap="true";
arg.style.zIndex=10;
arg.strokecolor="navy";
arg.strokeweight="3px";
arg.from=xy2;
arg.to=xy1;
document.body.appendChild(arg);
}
}

function vector(valid,x1,y1,x2,y2){
this.valid=valid;
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}


function getpointer(x,y){
var xoffset=document.body.scrollLeft-4-offX; //-4
var yoffset=document.body.scrollTop+18-offY; //-4
this.x=window.event.clientX+xoffset;
this.y=window.event.clientY+yoffset;
}


// a global vector
redline=new vector(true,0,0,50,50);


function momouse(){
showXY();
if (drawapproved && elastoc.from!="0,0")
{
endpoint=new getpointer();
redline.x1=endpoint.x;
redline.y1=endpoint.y;
elastoc.to=redline.x1+","+redline.y1;
}
}


function clikmouse(){
if (drawapproved)
{
iniz=elastoc.from.value;
clikpoint=new getpointer();
redline.x2=clikpoint.x;
redline.y2=clikpoint.y;
elastoc.from=redline.x2+","+redline.y2; // as the mouse is clicked restart where it's drawn from
if (elastoc.style.visibility=="visible") line(iniz,elastoc.to);
else {
elastoc.to=redline.x2+","+redline.y2;
elastoc.style.visibility="visible";
}

}
}


/* Procedures for calculating weigth sum */

function vn(arg){
rege=/.*\/n([^_]*).*/;
var arg1=arg.replace(rege,"$1");
arg1=parseFloat(arg1);
if (isNaN(arg1)) arg1=0;
return arg1;
}

function vnco(arg){
rege=/counter([^_]*).*/;
var arg1=arg.replace(rege,"$1");
arg1=parseFloat(arg1);
if (isNaN(arg1)) arg1=1;
return arg1;
}


function dd(arg){
var arg1=parseFloat(arg);
arg1=Math.round(arg1*1000)/1000;
if (isNaN(arg1)) arg1=0;
return arg1;
}


function equiv(fn1,fn2)
{
fn1=fn1.replace(/\_.*\./,".");
fn2=fn2.replace(/\_.*\./,".");
return fn1==fn2;
}


function getOffsetTop(elm) {

  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return parseInt(mOffsetTop);
}

function getOffsetLeft(elm) {

  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return parseInt(mOffsetLeft);
}


//stabilisce se l'oggetto si trova nell'area peso
function ovl(ogg1,ogg2){
var xc1=getOffsetLeft(ogg1)+parseInt(ogg1.clientWidth)/2;
var yc1=getOffsetTop(ogg1)+parseInt(ogg1.clientHeight)/2;
var xc2=getOffsetLeft(ogg2)+parseInt(ogg2.clientWidth)/2;
var yc2=getOffsetTop(ogg2)+parseInt(ogg2.clientHeight)/2;
dista=Math.sqrt(Math.pow(xc1-xc2,2)+Math.pow(yc1-yc2,2));
sovrap=(dista<(parseInt(ogg2.clientWidth)/2));
return sovrap;
}

function estraiInfo(arg){
arg1="";
if (arg.indexOf("(")>-1 && arg.indexOf(")")>-1)
{
rege=/.*\(([^_]*)\).*/;
arg1=arg.replace(rege,"$1");
}
return arg1;
}


function aggiornaContatori(){
var ele=document.getElementById("liv2");
if (ele.style.visibility=="hidden")
{
/* conta il tocco
var j=0;
while(j<document.all.length)
{
self.status="Calcola ..."+j;
if (document.all(j).className=="counter")
{
if (!document.all(j).totale) document.all(j).totale=0;
if (ovl(z,document.all(j))) document.all(j).totale=parseInt(document.all(j).totale)+1;
}
if (document.all(j).id=="contatore") document.body.removeChild(document.all(j))
j++;
}
*/

/*
for(j=0;j<document.all.length;j++)
{
if (document.all(j).className=="counter") document.all(j).totale=0;
if (document.all(j).className=="ok") document.all(j).ok="";
}
*/


for(j=0;j<document.all.length;j++)
{

if (document.all(j).className.indexOf("counter")==0 && document.all(j).className!="counter")
{

for(k=0;k<document.all.length;k++)
 if (document.all(k).className=="drag" && ovl(document.all(k),document.all(j)))
  {
  nome=document.all(k).src;
  if (document.all(k).id=="") document.all(k).id=nome;
  valo=vn(nome);
  valoc=vnco(document.all(j).className);
  valot=parseFloat(document.all(j).totale);
   if (Math.round((Math.ceil(valot)-valot)*10)==1 && valo*valoc==0.1) 
   {
      for(l=0;l<document.all.length;l++)
	   if (document.all(l).src==nome && ovl(document.all(l),document.all(j)))
	   {
	   document.body.removeChild(document.all(l));
	   l--;
	   }
 	  
   nvalo=valo*10;
   nome1=nome.replace(valo,nvalo);
   var arg = document.createElement("img");
   arg.style.zIndex=3;
   arg.src=nome1;
   arg.className="drag";
   document.body.appendChild(arg);
   arg.style.position="absolute";
   arg.style.top=getOffsetTop(document.all(j))+18+"px";
   arg.style.left=getOffsetLeft(document.all(j))+30+30/valo+"px";

   }

  nvalo=valo/10;
  nome=nome.replace(valo,nvalo);
  if (valo*valoc>10)
      mandaIndietro(document.all(k));
   else if (nvalo>=1/valoc)
   {
  document.body.removeChild(document.all(k));
  for(l=0;l<10;l++)
    {
   var arg = document.createElement("img");
   arg.style.zIndex=3;
   arg.src=nome;
   arg.id=nome;
   arg.className="drag";
   document.body.appendChild(arg);
   arg.style.position="absolute";
   arg.style.top=getOffsetTop(document.all(j))+28+(l*10)+"px";
   arg.style.left=getOffsetLeft(document.all(j))+20+(l*2)+"px";
    } 
   }
  }
}


if (document.all(j).className.indexOf("counter")==0)
{
 
document.all(j).totale=0;
for(k=0;k<document.all.length;k++)
 if (document.all(k).className=="drag" && ovl(document.all(k),document.all(j)))
{
  if ((document.all(j).id!="") && parseFloat(document.all(j).id)<=parseFloat(document.all(j).totale)) mandaIndietro(document.all(k));
   else document.all(j).totale=parseFloat(document.all(j).totale)+vn(document.all(k).src)*vnco(document.all(j).className); 
}
// document.all(j).totale=parseFloat(document.all(j).totale)+vn(document.all(k).src)*vnco(document.all(j).className);

}

 if (document.all(j).className=="ok")
{
document.all(j).ok="";
for(k=0;k<document.all.length;k++)
 if (document.all(k).className=="drag" && ovl(document.all(k),document.all(j)) && equiv(document.all(k).src,document.all(j).src))
  document.all(j).ok="OK";
}



if (document.all(j) && (document.all(j).id=="cn" || document.all(j).id=="cf")) document.body.removeChild(document.all(j))

}

for(j=0;j<document.all.length;j++)
 if (document.all(j) && (document.all(j).id=="cn" || document.all(j).id=="cf")) {
 document.body.removeChild(document.all(j))
 j--;
 }


// visualizza i risultati
var j=0;
while(j<document.all.length)
{

if (document.all(j).className.indexOf("counter")==0 /*&& document.all(j).totale>0*/)
{
hdl = document.createElement("div"); 
document.body.appendChild(hdl);
hdl.id="cn";
hdl.style.background="none";
hdl.style.position="absolute";
hdl.style.border="outset #eeeeee 0px";
hdl.style.zIndex=10;
hdl.style.top=getOffsetTop(document.all(j))-40+"px";
hdl.style.left=getOffsetLeft(document.all(j))+"px";
hdl.style.fontSize="50px";
hdl.style.color="#8c0000";
//hdl.style.filter="Glow(Color=#3DF500, strength=9)";
hdl.innerHTML=dd(document.all(j).totale);
}


if (document.all(j).className=="ok" && document.all(j).ok=="OK")
{
hdl1 = document.createElement("div"); 
document.body.appendChild(hdl1);
hdl1.id="cf";
hdl1.style.background="none";
hdl1.style.position="absolute";
hdl1.style.border="outset #eeeeee 0px";
hdl1.style.zIndex=10;
hdl1.style.top=(getOffsetTop(document.all(j))-3)+"px";
hdl1.style.left=(getOffsetLeft(document.all(j))+3)+"px";
hdl1.style.fontSize="20px";
hdl1.style.color="#8c0000";
//hdl1.style.filter="Glow(Color=#3DF500, strength=9)";
hdl1.innerHTML=document.all(j).ok;

var arg=estraiInfo(z.src);

if (arg!="")
{
hdl2 = document.createElement("div"); 
document.body.appendChild(hdl2);
hdl2.id="cf";
hdl2.style.background="none";
hdl2.style.position="absolute";
hdl2.style.border="outset #eeeeee 0px";
hdl2.style.zIndex=10;
hdl2.style.top=(getOffsetTop(document.all(j))-15)+"px";
hdl2.style.left=(getOffsetLeft(document.all(j))+document.all(j).clientWidth-30)+"px";
hdl2.style.fontSize="40px";
hdl2.style.color="#008c00";
hdl2.innerHTML=arg;
}
}

j++;
}

}

}


/* Drag Class - Credit JavaScript Kit www.javascriptkit.com*/
var dragapproved=false
var z,x,y
function move(){
if (event.button==1&&dragapproved){
//z.style.pixelLeft=temp1+event.clientX-x;
//z.style.pixelTop=temp2+event.clientY-y;
z.style.left=temp1+event.clientX-x;
z.style.top=temp2+event.clientY-y;
return false
}
}

function drags(){
if (!document.all)
return
if (event.srcElement.className=="drag"){
dragapproved=true
z=event.srcElement
temp1=z.style.pixelLeft
temp2=z.style.pixelTop
x=event.clientX
y=event.clientY
document.onmousemove=move

if(event.button==2) ruota();

}
}


function endDrags(){
aggiornaContatori();
dragapproved=false;
document.onmousemove=momouse;
}

document.onmousedown=drags;
document.onmouseup=endDrags;




function specchia(){

if (event.srcElement.className=="drag"){

z=event.srcElement;
filtro=z.filters.item("DXImageTransform.Microsoft.BasicImage").Mirror;

switch (filtro) { 
case 0: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Mirror=1;
break; 

case 1: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Mirror=0;
break; 

default: 
z.style.filter='progid:DXImageTransform.Microsoft.BasicImage(mirror=0)';

}



}
}

function ruota(){
//Ruota oggetti di 90 gradi, ma sposta tutti gli altri

z=event.srcElement;
filtro=z.filters.item("DXImageTransform.Microsoft.BasicImage").Rotation;

switch (filtro) { 
case 0: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Rotation=1;
break; 

case 1: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Rotation=2;
break; 

case 2: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Rotation=3;
break; 

case 3: 
z.filters.item("DXImageTransform.Microsoft.BasicImage").Rotation=0;
break; 

default: 
z.style.filter='progid:DXImageTransform.Microsoft.BasicImage(rotation=1)';

}
}


function convertiOggDinamici(){
for(j=0;j<document.all.length;j++)
{

// per poter rimuovere gli oggetti nei cambi
if (document.all(j).className=="cube")
   {
   var arg = document.createElement("img");
   arg.style.zIndex=3;
   arg.src=document.all(j).src;
   arg.className="drag";
   document.body.appendChild(arg);
   arg.style.position="absolute";
   arg.style.top=getOffsetTop(document.all(j))+"px";
   arg.style.left=getOffsetLeft(document.all(j))+"px";
   document.all(j).style.visibility="hidden";
   }

if (document.all(j).className=="deck")
   {
   if (precedX!=0 && precedY!=0)
   {
   document.all(j).className="drag";
   document.all(j).style.position="absolute";
   document.all(j).style.top=precedY+"px";
   document.all(j).style.left=precedX+"px";
   }
   else
   {
   precedY=getOffsetTop(document.all(j));
   precedX=getOffsetLeft(document.all(j));
   document.all(j).className="drag";
   document.all(j).style.position="absolute";
   document.all(j).style.top=precedY+"px";
   document.all(j).style.left=precedX+"px";
   }
   }

// mette una quantità di default sui piatti contatori   
if (document.all(j).className.indexOf("counter")==0 && document.all(j).id)
   {
   num=parseInt(document.all(j).id);
   for (k=0;k<4;k++)
   {
   valo=Math.pow(10,k);
   quant=num % 10;
   num=(num-quant)/10;
   for (l=0;l<quant;l++)
   {
   nome="n"+valo+"_bar1.png";
   var arg = document.createElement("img");
   arg.style.zIndex=3;
   arg.src=nome;
   arg.className="drag";
   document.body.appendChild(arg);
   arg.style.position="absolute";
   arg.style.top=getOffsetTop(document.all(j))+18+(l*10)+"px";
   arg.style.left=getOffsetLeft(document.all(j))+30+30/valo+(l*2)+"px";
   }
   }
   aggiornaContatori();
   }

}
}

document.ondblclick=specchia;
