// Spin game file
var SpinImgHeight = 166;  //Hight of one image
var SpinImgNum = 10;  //Number of images in spin.
var SpinReloadY = -1660; //Hight when we should reload spin

var MaxRotSpeedDef = .3;  //Max speed default
var SpinNum = 3;          //Number of spins
var ReelMode = 0;
var LinesNum = 5;

//______________________________________________
//var (declarations)
var RotBrakeAccell = new Array();
var RotSpeedAccell = new Array();
var MaxRotates = new Array();     //Number Of Spin Rotations
var MaxRotSpeed = new Array();    //Max Spin Speed of Rotation
var AccellFlag = new Array();
var CurRotSpeed = new Array();
var CurPosY = new Array();
var SpinRotates = new Array();
var SpinVal = new Array(); //Znacheniye otveta servera
var oSpin = new Array();
var oViewLine = new Array();        //View line objects Array
var WinLines = new Array();
var nTimerID = new Array();        //Timer objects array

//___Program vars  
var GameResult = 0;
var Money = 1;
var CurrentStake = 1;
var StakeStep = 0.1;
var LunchTimeMode = 1;
var CurrentLines = 1;                //The number of lines user play for.
var PlayMode = 0;                    //1 - real money; 2 - virtual money; 3 - virtual guest money; 0 - undefined

//___Program Service Vars
var nTimerID  = 0;
var SpinImgPos = new Array();
var LunchTimePrev = 0;
var NumOfMovingSpins;             //0 if no one of the spin moving.
var TimerInt = 1;
var CurTimer = 0;
var TimerNum = 18;
var CurrentWinLines = 0;             //The number of current win lines.
var FlagEmergencyStop = false;       //Flag ne rezultativnoy ostanovki slota


//_____________________Spin Settings
function ReelModeSet(ReelMode)
{
  if (ReelMode == 0)
  {
    MaxRotates[1] = 0;
    MaxRotates[2] = 0;
    MaxRotates[3] = 0;
    MaxRotSpeed[1] = 10.16;
    MaxRotSpeed[2] = 10.12;
    MaxRotSpeed[3] = 10.14;
    LunchTimeMode = 0;
  }
  if (ReelMode == 1)
  {
    MaxRotates[1] = 1;
    MaxRotates[2] = 16;
    MaxRotates[3] = 25;
    MaxRotSpeed[1] = 16.5;
    MaxRotSpeed[2] = 16.6;
    MaxRotSpeed[3] = 16.4;
    LunchTimeMode = 0;
  }
  if (ReelMode == 2)
  {
    MaxRotates[1] = 2;
    MaxRotates[2] = 2;
    MaxRotates[3] = 2;
    MaxRotSpeed[1] = 2.6;
    MaxRotSpeed[2] = 2.65;
    MaxRotSpeed[3] = 2.5;
    LunchTimeMode = 1;
  }
  if (ReelMode == 3)
  {
    MaxRotates[1] = 0;
    MaxRotates[2] = 2;
    MaxRotates[3] = 3;
    MaxRotSpeed[1] = 1.5;
    MaxRotSpeed[2] = 1.4;
    MaxRotSpeed[3] = 1.3;
    LunchTimeMode = 1;
  }
  if (ReelMode == 4)
  {
    MaxRotates[1] = 0;
    MaxRotates[2] = 0;
    MaxRotates[3] = 0;
    MaxRotSpeed[1] = 0.5;
    MaxRotSpeed[2] = 0.6;
    MaxRotSpeed[3] = 0.45;
    LunchTimeMode = 1;
  }
  
}
  
//______________________Technical info
SpinImgPos[1] = new Array();
SpinImgPos[1][1] = 1;
SpinImgPos[1][2] = 2;
SpinImgPos[1][3] = 3;
SpinImgPos[1][4] = 4;
SpinImgPos[1][5] = 5;
SpinImgPos[1][6] = 6;
SpinImgPos[1][7] = 7;
SpinImgPos[1][8] = 8;
SpinImgPos[1][9] = 9;
SpinImgPos[1][10] = 10;

SpinImgPos[2] = new Array();
SpinImgPos[2][1] = 7;
SpinImgPos[2][2] = 1;
SpinImgPos[2][3] = 4;
SpinImgPos[2][4] = 8;
SpinImgPos[2][5] = 2;
SpinImgPos[2][6] = 5;
SpinImgPos[2][7] = 10;
SpinImgPos[2][8] = 3;
SpinImgPos[2][9] = 6;
SpinImgPos[2][10] = 9;

SpinImgPos[3] = new Array();
SpinImgPos[3][1] = 2;
SpinImgPos[3][2] = 7;
SpinImgPos[3][3] = 9;
SpinImgPos[3][4] = 8;
SpinImgPos[3][5] = 1;
SpinImgPos[3][6] = 6;
SpinImgPos[3][7] = 5;
SpinImgPos[3][8] = 4;
SpinImgPos[3][9] = 3;
SpinImgPos[3][10] = 10;

//____________________________________________________________________________________________________FUCNTIONS
function GetBrakeAccell(Spin)
{
  if (SpinVal[Spin] <= 0)
  { 
    return 0;
  } else
  {
    BrakeDistance = CurPosY[Spin] + (SpinVal[Spin] + SpinImgNum - 2) * SpinImgHeight;
	if (BrakeDistance <= 0) BrakeDistance = CurPosY[Spin] + (SpinVal[Spin] + (SpinImgNum * 2) - 2) * SpinImgHeight;
    BrakeAccell = (CurRotSpeed[Spin] * CurRotSpeed[Spin]) / (2 * BrakeDistance);
  //  alert(SpinVal[Spin] + '   - --- -   ' + BrakeDistance + '   - --- - CurPosY  ' + CurPosY[Spin] + ' SpinImgHeight ' +  (SpinVal[Spin] + (SpinImgNum) - 2));
  //  if (BrakeAccell<0) CheckPrint(CurPosY[Spin], SpinVal[Spin], BrakeDistance);
    return BrakeAccell;
  }
}

//__________________
function SpinInit(Spin)
{

//Init Spin Values

  if (MaxRotSpeed[Spin] == '') MaxRotSpeed[Spin] = MaxRotSpeedDef;
  RotSpeedAccell[Spin] = .1;
    
  oSpin[Spin] = document.getElementById("spin"+Spin.toString());
  AccellFlag[Spin] = 0;
  CurRotSpeed[Spin] = 0;
  CurPosY[Spin] = 0;
  SpinRotates[Spin] = 0;
}


//=====================================Emergency Stop spin
function SpinEmergencyStop()
{
  FlagEmergencyStop = true;
  for (i = 1; i <= SpinNum; i++)
  {
    SpinVal[i] = Math.floor(Math.random() * SpinImgNum) + 1;
  }
}


//========================================================= *  *  *  *  *  *  * SPIN MOVE

//========================================================= *  *  *  *  *  *  * SPIN MOVE

//========================================================= *  *  *  *  *  *  * SPIN MOVE

function SpinMove(Spin, LTime)
{
  if (AccellFlag[Spin]==3)   //tormozim
  {
    if (CurRotSpeed[Spin]>0)
    { 
      CurRotSpeed[Spin] = CurRotSpeed[Spin] - RotBrakeAccell[Spin] * LTime;
    } else 
    { 
      CurRotSpeed[Spin] = 0;
      CurPosY[Spin] = -(SpinVal[Spin] - 2) * SpinImgHeight;
      if (SpinVal[Spin] == 1) CurPosY[Spin] = -(SpinImgNum - 1) * SpinImgHeight;
      AccellFlag[Spin] = 0;
      NumOfMovingSpins--;
      if (NumOfMovingSpins <= 0)
      {
        ProcessGameResult(GameResult, Money);
      }
    }
  }

//razgonyaemsya
  if (AccellFlag[Spin]==1)   
  {
    if (CurRotSpeed[Spin] < MaxRotSpeed[Spin]) 
    {
       CurRotSpeed[Spin] = CurRotSpeed[Spin] + RotSpeedAccell[Spin] * LTime;
    } else
    {  
       CurRotSpeed[Spin] =  MaxRotSpeed[Spin];
       if (MaxRotates[Spin] > 0) 
       {
         AccellFlag[Spin] = 2;
       } else
       {
         RotBrakeAccell[Spin] = GetBrakeAccell(Spin);
         if (RotBrakeAccell[Spin] != 0)
         {
           AccellFlag[Spin] = 3;
         }
       }
    }
  }  

  CurPosY[Spin] = CurPosY[Spin]-(CurRotSpeed[Spin] * LTime);

//_____reload spin, brake activation

  if (CurPosY[Spin] < SpinReloadY) 
  {
    CurPosY[Spin] = CurPosY[Spin] - SpinReloadY;
    if (AccellFlag[Spin] == 2)
    {
      SpinRotates[Spin]++;
      if (SpinRotates[Spin] > MaxRotates[Spin]) 
      { 
        RotBrakeAccell[Spin] = GetBrakeAccell(Spin);
        if (RotBrakeAccell[Spin] != 0)
        {
          AccellFlag[Spin] = 3;
        }
      }
    }
  } 
  
  oSpin[Spin].style.top = CurPosY[Spin].toString() + "px";
}


//========================================================= *  *  *  *  *  *  * /SPIN MOVE
//========================================================= *  *  *  *  *  *  * /SPIN MOVE

//=========================================================

function AllSpinMove()
{
  LunchTime = new Date();
  LunchTimeMS = LunchTime.getTime();
  if ((LunchTimePrev > 0) && (LunchTimeMode == 1))
  {  
    LTime = (LunchTimeMS - LunchTimePrev);
    if (LTime > 200, LTime = 1);
  } else
  {
    LTime = 1;
  }
  
  for (i = 1; i <= SpinNum; i++)
  {
    SpinMove(i, LTime);
  }

  LunchTimePrev = LunchTimeMS;
}

//=====================================================LUNCH SPINS
function LunchSpins()
{
  NumOfMovingSpins = SpinNum;
  FlagEmergencyStop = false;
  WinLines[0] = 0;
  
  for (i = 1; i <= SpinNum; i++)
  {
    SpinVal[i] = 0;
    SpinRotates[i] = 0;
    AccellFlag[i] = 1;
  }
  
  GameResult = 0;
  for (i = 0; i < CurrentLines; i++)
  {
    oViewLine[i + 1].style.visibility = 'hidden';
  }
  ReelModeSet(ReelMode);
}

function SpinTimerSet()
{
  if (CurTimer < TimerNum)
  {
    for (i = 0; i <= TimerNum; i++)
    {
      nTimerID[CurTimer] = window.setInterval(AllSpinMove, 1);  
      CurTimer++;
    }
  }
}

//___________________Slot Init Program Function
function SlotInit()
{
  //Make Init Request to server
  send_new_request('init');
  //alert('BLYA!!!');
  
  for (i = 1; i <= SpinNum; i++)
  {
    SpinInit(i); 
  }
  
  ReelModeSet(ReelMode);
  ChangeRiskActivity(false);
 
  for (i = 1; i <= LinesNum; i++)
  {
    oViewLine[i] = document.getElementById("ViewLine" + i);
  }
  nSpinTimer = window.setTimeout(SpinTimerSet, 1);
  LastRequestTimer = window.setInterval(LastRequestTimeCheck, 3000);  
} 

//___________________Main code here
//Init Spins
  SlotInit();
 
  
/*

  nTimerID1 = window.setInterval(AllSpinMove, 1);
  nTimerID2 = window.setInterval(AllSpinMove, 1);
  nTimerID3 = window.setInterval(AllSpinMove, 1);
  nTimerID4 = window.setInterval(AllSpinMove, 1);
  nTimerID5 = window.setInterval(AllSpinMove, 1);
  nTimerID6 = window.setInterval(AllSpinMove, 1);
  nTimerID7 = window.setInterval(AllSpinMove, 1);
  nTimerID8 = window.setInterval(AllSpinMove, 1);
  nTimerID9 = window.setInterval(AllSpinMove, 1);
  nTimerID10 = window.setInterval(AllSpinMove, 1);
  nTimerID11 = window.setInterval(AllSpinMove, 1);
  nTimerID12 = window.setInterval(AllSpinMove, 1);
  nTimerID13 = window.setInterval(AllSpinMove, 1);
  nTimerID14 = window.setInterval(AllSpinMove, 1);
  nTimerID15 = window.setInterval(AllSpinMove, 1);
  nTimerID16 = window.setInterval(AllSpinMove, 1);
  nTimerID17 = window.setInterval(AllSpinMove, 1);
  nTimerID18 = window.setInterval(AllSpinMove, 1);
*/

