/////////////////////////////////////////////////////////
// imageMapping.js
//
// imageMapping object
//
// This file includes the coordinate transformation between brower inputs and actual scales.
// Brower inputs are (x,y) coordinates passed to hidden.html by the browser
// Actural scales are (x,y) coordinates that can be looked up in kangdata
//////////////////////////////////////////////////////////


//constructor
function imageMapping() {

    //public properties - changeable
    this.whichimage = 'null';

    //The states
    this.mni = 0;
    this.talairach = 0;
    this.boundary = 1;

    //content and view
    this.content = 'curvature';
    this.view = 'auditory system (temporal lobe)';

    //kang's (x,y)
    this.flatxK = -5000;
    this.flatyK = -5000;
    this.spherexK = -5000;
    this.sphereyK = -5000;
    this.DxK = -5000;
    this.DyK = -5000;
    this.DzK = -5000;

    //Browser's (x,y)
    this.flatxB = -5000;
    this.flatyB = -5000;
    this.spherexB = -5000;
    this.sphereyB = -5000;
    this.DxB = -5000;
    this.DyB = -5000;
    this.DzB = -5000;

    //Talairach (Tx,Ty)
    this.Tx = -5000;
    this.Ty = -5000;

    //side
    this.side = 'initial';

    //distance to cortex
    this.distance = 0;

    //initial state
    this.initial = 1;

    //absolute (x,y)
    leftPosition = -5000;
    topPositon = -5000;

    //offset constants
    this.offsetxLhflat=60;
    this.offsetyLhflat=150;
    this.offsetxLhsphere=285;
    this.offsetyLhsphere=150;
    this.offsetxLh3DY=750;
    this.offsetyLh3DY=100;
    this.offsetxLh3DX=970;
    this.offsetyLh3DX=100;
    this.offsetxLh3DZ=750;
    this.offsetyLh3DZ=320;
    
    this.offsetxRhflat=60;
    this.offsetyRhflat=550;
    this.offsetxRhsphere=285;
    this.offsetyRhsphere=550;

    //browser related data for (x,y) conversion
    this.lhflatOriginX = 93;
    this.lhflatOriginY = 59;
    this.rhflatOriginX = 65;
    this.rhflatOriginY = 59;
    this.flatMyWidth = 160; 
    this.flatMyHeight = 200;
    this.flatWidth = 85; 
    this.flatHeight = 126;

    this.lhsphereOriginX = 199;
    this.lhsphereOriginY = 98;
    this.rhsphereOriginX = 199; 
    this.rhsphereOriginY = 98;
    this.sphereMyWidth = 400;
    this.sphereMyHeight = 200;
    this.sphereWidth = 618;
    this.sphereHeight = 314;

    //for 3D images, the size is 157X189X136. MySize equals to the original size.
    //this.DMyWidth = 200;
    //this.DMyHeight = 200;
    //this.DWidth = 256;
    //this.DHeight = 256;

    this.DyHeight = 136;
    this.DyWidth = 157;
    this.DxHeight = 136;
    this.DxWidth = 189;
    this.DzHeight = 189;
    this.DzWidth = 157;

    //public functions
    this.restore = restore;
    this.FreshDraw = FreshDraw;
    this.lhflatBtoK = lhflatBtoK;
    this.lhflatBtoA = lhflatBtoA;
    this.lhflatKtoA = lhflatKtoA;
    this.rhflatBtoK = rhflatBtoK;
    this.rhflatBtoA = rhflatBtoA;
    this.rhflatKtoA = rhflatKtoA;
    this.lhsphereKtoA = lhsphereKtoA;
    this.lhsphereBtoK = lhsphereBtoK;
    this.lhsphereBtoA = lhsphereBtoA;
    this.rhsphereKtoA = rhsphereKtoA;
    this.rhsphereBtoK = rhsphereBtoK;
    this.rhsphereBtoA = rhsphereBtoA;
    this.DYKtoA = DYKtoA;
    this.DYBtoK = DYBtoK;
    this.DYBtoA = DYBtoA;
    this.DXKtoA = DXKtoA;
    this.DXBtoK = DXBtoK;
    this.DXBtoA = DXBtoA;
    this.DZKtoA = DZKtoA;
    this.DZBtoK = DZBtoK;
    this.DZBtoA = DZBtoA;
    this.resetInput = resetInput;
}

function restore()
{
    //kang's (x,y,z)
    this.flatxK = -5000;
    this.flatyK = -5000;
    this.spherexK = -5000;
    this.sphereyK = -5000;
    this.DxK = -5000;
    this.DyK = -5000
    this.DzK = -5000;

    //Browser's (x,y)
    this.flatxB = -5000;
    this.flatyB = -5000;
    this.spherexB = -5000;
    this.sphereyB = -5000;
    this.DxB = -5000;
    this.DyB = -5000;
    this.DzB = -5000;

    //talairach (Tx,Ty)
    this.Tx = -5000;
    this.Ty = -5000;

    //side
    //this.side = 'initial';

    //distance
    this.distance = 0;
    
    //initial state
    //this.initial = 1;

    //alert(this.content+',restore');
    //content and view
    //this.content = 'Curvature';
    //this.view = 'view1';
}

//refresh the current image in cell and draw focus on (leftPosition,topPosition)
//type: LHFLAT,RHFLAT,LHSPHERE,RHSPHERE,LH3D1,LH3D2,LH3D3,RH3D1,RH3D2,RH3D3; 
//cell: refresh and draw in that cell; name: image filename; id: image id name;
//(leftPosition, topPosition): absolute position to draw the focus
function FreshDraw(type,cell,para,name,height,width,id,freshOnly)
{
   //feshOnly=0: fresh the image and draw focus
   //freshOnly=1: fresh without drawing focus
   var imageName,drawFocus;
   var inThere1 = type.match(/FLAT/i);
   var inThere2 = type.match(/SPHERE/i);
   var inThere3 = type.match(/3D/i);
   var leftPosition = this.leftPosition;
   var topPosition = this.topPosition;
   var leftPosition1,topPosition1,leftPosition2,topPosition2;

   if ((inThere1) || (inThere2)){
      leftPosition1 = leftPosition - 2;
      topPosition1 = topPosition - 2;
      imageName = 'http://www.ebire.org/hcnlab/cortical-mapping/kangdata/webDisp/fmap.figs/' + name;
      if (freshOnly == 0) {
         drawFocus = '<style type="text/css">';
         drawFocus += '#' + type +'1'+ '{position: absolute; left:' + leftPosition1 + 'px' + '; top:' + topPosition1 + 'px' + '; width:5; top-margin:0px; left-margin:0px}';
         drawFocus += '</style>';
         drawFocus += '<div id=' + type +'1'+ '>';
         drawFocus += '<img src="http://www.ebire.org/hcnlab/cortical-mapping/square.jpg" width=5 height=5 border=0>';
         drawFocus += '</div>';
       }
   }
   if (inThere3) {
      if (this.boundary==1) //original 3D images
         imageName = 'http://www.ebire.org/hcnlab/cortical-mapping/kangdata/webDisp/3D.figs/' + name;
      else
         imageName = 'http://www.ebire.org/hcnlab/cortical-mapping/kangdata/webDisp/3DNL.figs/' + name;

      if (freshOnly == 0) { 
         //draw horizontal line
         leftPosition1 = leftPosition - 3;
         topPosition1 = topPosition;
         drawFocus = '<style type="text/css">';
         drawFocus += '#' + type + '1'  + '{position: absolute; left:' + leftPosition1 + 'px' + '; top:' + topPosition1 +'px' + '; width:7; top-margin:0px; left-margin:0px}';
         drawFocus += '</style>';
         drawFocus += '<div id=' + type + '1' + '>'; 
         drawFocus += '<img src="http://www.ebire.org/hcnlab/cortical-mapping/redline.jpg" width=7 height=1 border=0>';
         drawFocus += '</div>';
 
         //draw vertical line
         leftPosition2 = leftPosition;
         topPosition2 = topPosition - 3;
         drawFocus += '<style type="text/css">';
         drawFocus += '#' + type + '2' + '{position: absolute; left:' + leftPosition2 + 'px' + '; top:' + topPosition2 + 'px' + '; width:1; top-margin:0px; left-margin:0px}';
         drawFocus += '</style>';
         drawFocus += '<div id=' + type + '2' + '>';
         drawFocus += '<img src="http://www.ebire.org/hcnlab/cortical-mapping/redline.jpg" width=1 height=7 border=0>';
         drawFocus += '</div>';
      }
   }
               
   tempstring = '<a href="http://www.ebire.org/hcnlab/cortical-mapping/hidden1.html?' + para + '" target="hiddenframe"><img src="' + imageName +'" height=' + height + ' width=' + width + ' border=1 id="' + id + '" ismap></a>';
   if (freshOnly == 0)
      tempstring += drawFocus;
   cell.innerHTML = tempstring;
}

//reset flat map, mollweide projection, 3D images, coordinates, input, error

function resetInput(input,error){
   if ((mniOriginal==1)||(mniCortex==1))
      input.innerHTML = '<input type="text" name="mnixyz" size=14 maxlength=14 value=",,">';
   if ((talairachOriginal==1)||(talairachCortex==1))
      input.innerHTML = '<input type="text" name="talairachxyz" size=14 maxlength=14 value=",,">';
   if (error)
      error.innerHTML = '';
}

//function resetLhFlat(image,coords){
//}

//function resetLhMollweide(image,coords){
//}

//function resetRhFlat(image,coords){
//}

//function resetLhMollweide(image,coords){
//}

//convert lhflat Browser (x,y) to Kang's (x,y)
function lhflatBtoK(flatx,flaty){
   var x = Math.round((flatx-this.lhflatOriginX)*this.flatWidth/this.flatMyWidth);
   var y = Math.round((this.lhflatOriginY-flaty)*this.flatHeight/this.flatMyHeight);
   //alert(x+';'+y);
   this.flatxK = x;
   this.flatyK = y;
}

//convert lhflat Browser(x,y) to Absolute (x,y)
function lhflatBtoA(flatx,flaty){
   this.leftPosition = Math.round(flatx);
   this.topPosition = Math.round(flaty);
}

//convert lhflat Kang's (x,y) to Absolute (x,y)
function lhflatKtoA(flatx,flaty) {
   var flatx_my = Math.round(flatx*this.flatMyWidth/this.flatWidth+this.lhflatOriginX);
   var flaty_my = Math.round(this.lhflatOriginY-flaty*this.flatMyHeight/this.flatHeight);
   var leftPosition = Math.round(flatx_my);
   var topPosition = Math.round(flaty_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert rhflat Browser(x,y) to Kang's (x,y)
function rhflatBtoK(flatx,flaty){
   var x = Math.round((flatx-this.rhflatOriginX)*this.flatWidth/this.flatMyWidth);
   var y = Math.round((this.rhflatOriginY-flaty)*this.flatHeight/this.flatMyHeight);
   this.flatxK = x;
   this.flatyK = y;
}

//convert rhflat Browser(x,y) to Absolute (x,y)
function rhflatBtoA(flatx,flaty){
   var leftPosition = Math.round(flatx);
   var topPosition = Math.round(flaty);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert rhflat Kang's (x,y) to Absolute (x,y)
function rhflatKtoA(flatx,flaty){
   var flatx_my = Math.round(flatx*this.flatMyWidth/this.flatWidth+this.rhflatOriginX);
   var flaty_my = Math.round(this.rhflatOriginY-flaty*this.flatMyHeight/this.flatHeight);
   var leftPosition = Math.round(flatx_my);
   var topPosition = Math.round(flaty_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert lhsphere Kang's (x,y) to Absolute (x,y)
function lhsphereKtoA(spherex,spherey){
   var spherex_my = Math.round(spherex*this.sphereMyWidth/this.sphereWidth+this.lhsphereOriginX);
   var spherey_my = Math.round(this.lhsphereOriginY-spherey*this.sphereMyHeight/this.sphereHeight);
   var leftPosition = Math.round(spherex_my);
   var topPosition = Math.round(spherey_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert lhsphere Browser's (x,y) to Kang's (x,y)
function lhsphereBtoK(spherex,spherey) {
   var x = Math.round((spherex-this.lhsphereOriginX)*this.sphereWidth/this.sphereMyWidth);
   var y = Math.round((this.lhsphereOriginY-spherey)*this.sphereHeight/this.sphereMyHeight);
   this.spherexK = x;
   this.sphereyK = y;
}

//convert lhsphere Browser's (x,y) to Absolute (x,y)
function lhsphereBtoA(spherex,spherey) {
   var leftPosition = Math.round(spherex);
   var topPosition = Math.round(spherey);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert rhsphere kang's (x,y) to Absolute (x,y)
function rhsphereKtoA(spherex,spherey){
   var spherex_my = Math.round(spherex*this.sphereMyWidth/this.sphereWidth+this.rhsphereOriginX);
   var spherey_my = Math.round(this.rhsphereOriginY-spherey*this.sphereMyHeight/this.sphereHeight);
   var leftPosition = Math.round(spherex_my);
   var topPosition = Math.round(spherey_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert rhsphere browser's (x,y) to Kang's (x,y)
function rhsphereBtoK(spherex,spherey) {
   var x = Math.round((spherex-this.rhsphereOriginX)*this.sphereWidth/this.sphereMyWidth);
   var y = Math.round((this.rhsphereOriginY-spherey)*this.sphereHeight/this.sphereMyHeight);
   this.spherexK = x;
   this.sphereyK = y;
}

//convert rhsphere browser's (x,y) to Absolute (x,y)
function rhsphereBtoA(spherex,spherey) {
   var leftPosition = Math.round(spherex);
   var topPosition = Math.round(spherey);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert DY kang's (x,z) to Absolute (x,z)
function DYKtoA(Dx,Dz) {
   //var DYx_my = Math.round(Dx*this.DMyWidth/this.DWidth);
   //var DYy_my = Math.round(Dz*this.DMyWidth/this.DWidth);
   var DYx_my = Math.round(Dx);
   var DYy_my = Math.round(this.DyHeight-Dz+1);
   var leftPosition = Math.round(DYx_my);
   var topPosition = Math.round(DYy_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert DY Browser's (x,y,z) to Kang's (x,z)
function DYBtoK(number1,number2,number3) {
   //var x = Math.round(number1*this.DWidth/this.DMyWidth);
   //var z = Math.round(number2*this.DHeight/this.DMyHeight);
   var x = Math.round(number1);
   var z = Math.round(this.DyHeight-number2+1);
   var y = Math.round(number3);
   this.DxK = x;
   this.DyK = y;
   this.DzK = z;
}

//convert DY Browser's Browser's(number1,number2) to Absolute (left,top)
function DYBtoA(number1,number2){
   var leftPosition = Math.round(number1);
   var topPosition = Math.round(number2);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert DX kang's (y,z) to Absolute (y,z)
function DXKtoA(Dy,Dz) {
   //var DXy_my = Math.round(Dy*this.DMyWidth/this.DWidth);
   //var DXz_my = Math.round(Dz*this.DMyWidth/this.DWidth);
   var DXy_my = Math.round(this.DxWidth-Dy+1);
   var DXz_my = Math.round(this.DxHeight-Dz+1);
   var leftPosition = Math.round(DXy_my);
   var topPosition = Math.round(DXz_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
} 

//convert DX Browser's (x,y,z) to kang's (x,y,z)
function DXBtoK(number1,number2,number3) {
   //var y = Math.round(number1*this.DWidth/this.DMyWidth);
   //var z = Math.round(number2*this.DHeight/this.DMyHeight);
   var y = Math.round(this.DxWidth-number1+1);
   var z = Math.round(this.DxHeight-number2+1);
   var x = Math.round(number3);
   this.DxK = x;
   this.DyK = y;
   this.DzK = z;
}

//convert DX browser's (number1,number2) to absolute(left,top)
function DXBtoA(number1,number2) {
   var leftPosition = Math.round(number1);
   var topPosition = Math.round(number2);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert DZ kang's (x,y) to Absolute (x,y)
function DZKtoA(Dx,Dy) {
   //var DZx_my = Math.round(Dx*this.DMyWidth/this.DWidth);
   //var DZy_my = Math.round(Dy*this.DMyWidth/this.DWidth);
   var DZx_my = Math.round(Dx);
   var DZy_my = Math.round(this.DzHeight-Dy+1);
   var leftPosition = Math.round(DZx_my);
   var topPosition = Math.round(DZy_my);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}

//convert DZ Browser's (x,y,z) to Kang's (x,y,z)
function DZBtoK(number1,number2,number3){
   //var x = Math.round(number1*this.DWidth/this.DMyWidth);
   //var y = Math.round(number2*this.DHeight/this.DMyHeight);
   var x = Math.round(number1);
   var y = Math.round(this.DzHeight-number2+1);
   var z = Math.round(number3);
   this.DxK = x;
   this.DyK = y;
   this.DzK = z;
}

//convert DZ Browser's (number1,number2) to Absolute (left,top)
function DZBtoA(number1,number2){
   var leftPosition = Math.round(number1);
   var topPosition = Math.round(number2);
   this.leftPosition = leftPosition;
   this.topPosition = topPosition;
}


