var debugMode = true;          /* when set to true, display detailed error messages */  
var LastStatus = 0;            /*  LastStatus - status of current condition */
//temp vars
var CurWinSound = 0;
var CurSoundType = '';
var FlagRiskActivity = false;               //Flag aktivnosti v danniy moment knopki "Risk"
var FlagCheckSwitch = false;              //Flag, vivodim li mi vremennie peremennye dlya monitoringa.
var FlagSound = false;                    //Flag zvuka, vkluchen i viklyuchen
var FlagAutoMode = false;

//PATHES
var LoginWinPath = '/show_form.php?path=sets/spin/';
var Msg2Path = '/show_form.php?path=sets/spin/';
var HelpWinPath = '/show_form.php?path=sets/spin/';
var CashPath = '/add_money.php';

//_________________________________ INTERFACE functions;


//===============================Lunch Button Click
function LunchSpinButton()
{
  if (!FlagSending) //Esli zapros v puti, mi ne mojem najat knopku spin
  {
    var CurrentMoney = RoundNumber(Money - (CurrentStake * CurrentLines), 10);

// PROVERKA KOLICHESTVA DENEG KLIENTOM
    if (CurrentMoney >= 0)
    {
	  ChangeInfoWin('MoneyInfo', CurrentMoney);
      ChangeInfoWin('WinInfo', 'Launching');
      ChangeRiskForm(0); //hidden
      ChangeRiskActivity(false);
      LunchSpins();
  	  make_request('Spin');
    } else 
    {
	  SpinEmergencyStop();
	  if (PlayMode == 1 || PlayMode == 2)
	  {
	    //.......FORM NO MONEY FOR LOGGED IN USER
	    ShowFormInWin(Msg2Path + "msg_no_money_ul.htm");
	  } else
	  {
	    ShowFormInWin(Msg2Path + "msg_no_money.htm");
	  }
    }
  }
}

//===============================Risk Button Click
function RiskButton()
{
  if (!FlagSending) 
  {
	if (FlagRiskActivity) //Esli zapros v puti, mi ne mojem najat knopku risk
    {
      var CurrentMoney = Money - (GameResult);
      ChangeRiskForm(1); //visible
      ChangeInfoWin('RiskWinInfo', "Risking...");
      make_request('Risk');
    }
  }
}

//_______________________Lines Button Click
function LinesButton()
{
  CurrentLines = CurrentLines + 2;
  SetViewLinesPos();

  if (CurrentLines > LinesNum) 
  { 
    CurrentLines = 1; 
  }
  for (i = 1; i <= LinesNum; i++)
  { 
    oViewLine[i].style.visibility = 'hidden';
  }
  for (i = 1; i <= CurrentLines; i++)
  { 
	oViewLine[i].style.visibility = 'visible';
  }
}

//______________________________Stakes Button Click;

function ButtonPlusClick()
{
  if (CurrentStake >= 1) StakeStep = 1;
  if (CurrentStake >= 5) StakeStep = 5;
  //if (CurrentStake >= 30) StakeStep = 10;
  if (CurrentStake < 25)
  {
    PlaySlotSounds('MinusBtnClick');
    CurrentStake = RoundNumber(CurrentStake + StakeStep, 10);
    ChangeInfoWin('StakeInfo', CurrentStake.toString());
  }
}

function ButtonMinusClick()
{
  //if (CurrentStake <= 30) StakeStep = 5;
  if (CurrentStake <= 5) StakeStep = 1;
  if (CurrentStake <= 1) StakeStep = 0.1;
  if (CurrentStake > 0)
  {
    PlaySlotSounds('PlusBtnClick');
    CurrentStake = RoundNumber(CurrentStake - StakeStep, 10);
    ChangeInfoWin('StakeInfo', CurrentStake.toString());
  }
}

function CashButtonClick()
{
  if (PlayMode == 3 || PlayMode == 0) 
  {
    ShowFormInWin(LoginWinPath + "login_win.htm");
  } else
  {
    NewWin = window.open(CashPath, 'CashWindow');
  }
}

function LoginFormClick()
{
  ShowFormInWin(LoginWinPath + "login_win.htm");
}

function WinClose(WinName)
{
  var oWin = document.getElementById(WinName);
  oWin.innerHTML = '';
}

function HelpButton()
{
  var oHelpWin = document.getElementById("oHelpWin");
  oHelpWin.innerHTML = '<iframe id="helpwin" src="' + HelpWinPath + 'spin_help.htm" class="HelpWinObject"></iframe>';
  oHelpWin.style.left = Math.abs((getClientWidth() - 880) / 2);
  oHelpWin.style.top = Math.abs((getClientHeight() - 580) / 2);
}

//_____________________________Change Mode Balls (Just Cosmetic Function)

function BallsChange(OffBall, OnBall)  //Parametri - imena sharov, kotoriy nado vkluchit' i vikluchit sootvetstvenno.
{
  //Old ball (Off Ball)
  var oBall = document.getElementById(OffBall);
  var oBallSrc = document.getElementById("modeballoffsp");
  oBall.src = oBallSrc.src;
  //OnBall
  oBall = document.getElementById(OnBall);  
  oBallSrc = document.getElementById("modeballonsp");
  oBall.src = oBallSrc.src;
}

//______________________________Auto Ball Click;
function AutoBallClick(AutoMode)
{
  if (AutoMode == 1)
  { 
    if (PlayMode == 1) 
    {
      FlagAutoMode = true;
      BallsChange('autoball1', 'autoball0');
    } else
    {
      ShowMessageInWin(Msg2Path + 'msg_win2.htm');
    }
  } else
  { 
    FlagAutoMode = false; 
	BallsChange('autoball0', 'autoball1');
  }
  
}

//================================Change Reel Mode function
function ModeBallClick(NewReelMode)  
{
  BallsChange("modeball" + ReelMode, "modeball" + NewReelMode);
  ReelMode = NewReelMode;
}

//================================Change Status Info window;
function ChangePlayMode(PlayMode)
{
  if (PlayMode == 1) NewPic = "src_playmoney";
  if (PlayMode == 2) NewPic = "src_playvirt";
  if (PlayMode == 3 || PlayMode == 0) 
  {
    NewPic = "src_playguest";
    NewInitPic = 'src_loginpic';
  } else
  {
    NewInitPic = 'src_loginpic1';
  }
  
  //change Game Mode Picture (for money, for fun or guest)
  var oPlayMode = document.getElementById("oPlayMode");
  var oPicSrc = document.getElementById(NewPic);
  oPlayMode.src = oPicSrc.src;
  
  //change login pic
  var oLoginPic = document.getElementById("oLoginPic");
  var oPicSrc = document.getElementById(NewInitPic);
  oLoginPic.src = oPicSrc.src;
  
}

//================================Change Status Info window;
function ChangeInfoWin(StatusWindow, StatusText)
{
  var oWinInfo = document.getElementById(StatusWindow);
  oWinInfo.innerHTML = StatusText;
}

//================================Show Form For user
function ShowFormInWin(MessagePath)
{
  var oLoginWin = document.getElementById("oLoginWin");
  //oLoginWin.innerHTML = '<object data="tempfiles/login_win_spin.htm" class="LoginWinObject" type="text/html"></object>';
  oLoginWin.innerHTML = '<iframe name="loginwin" src="' + MessagePath + '" class="LoginWinObject"></iframe>';
  oLoginWin.style.top = (getClientHeight() - 420) / 2;
  oLoginWin.style.left = (getClientWidth() - 420) / 2;
}

//================================Show Message For user
function ShowMessageInWin(MessagePath)
{
  var oMessageWin = document.getElementById("oMessageWin");
  oMessageWin.innerHTML = '<iframe name="messagewin" src="' + MessagePath + '" class="MessageWinObject"></iframe>';
  oMessageWin.style.top = (getClientHeight() - 420) / 2;
  oMessageWin.style.left = (getClientWidth() - 420) / 2;
}  

//================================Show or hide Risk Form
function ChangeRiskForm(State)  //switch interface form, depending on switch mode
{
  var oRiskForm = document.getElementById('RiskForm'); //oRiskForm;
  if (State == 0)
  {
    oRiskForm.style.visibility = 'hidden';
  }
  if (State == 1)
  {
    oRiskForm.style.visibility = 'visible'; 
    oRiskForm.style.top = (getClientHeight() - 240) / 2;
    oRiskForm.style.left = (getClientWidth() - 300) / 2;
  }
}

//================================Change Reel Mode function
function ChangeRiskActivity(State)  
{
  FlagRiskActivity = State;
  if (State == true)
  {
    var oOldBtn = document.getElementById("src_risk_btn");
    CursorStyle = "pointer";
  } else
  {
    var oOldBtn = document.getElementById("src_risk_btn_off");
    CursorStyle = "default";
  }
  var oNewBtn = document.getElementById("risk_button");
  oNewBtn.src = oOldBtn.src;
  oNewBtn.style.cursor = CursorStyle;
  var oNewBtn = document.getElementById("risk_button1");
  oNewBtn.src = oOldBtn.src;
  oNewBtn.style.cursor = CursorStyle;
}

//================================SOUND PLAY;

function PlaySlotSounds(SoundType)
{
  if (FlagSound)
  {
    switch (SoundType) 
    {
      case "PlusBtnClick":
		func_PlaySound('files/sound/sound1.wav');
		break;
	  case "MinusBtnClick":
		func_PlaySound('files/sound/sound1.wav');
		break;
	  default:
		break;
	}
    
    
    if (CurSoundType == 'Win')
    {
	  sp = Math.random() * 6;
	  sp = Math.floor(sp);
      func_PlaySound('files/sound/coin_'+sp+'.wav');  //
    }
    CurWinSound++;
    
    if (CurWinSound < 6)
    {  
      nSoundTimer = window.setTimeout(PlaySlotSounds, 1);
    }
    
  } //if (FlagSound)
}

//------------------------------------
//Set positions of ViewLines
function SetViewLinesPos()
{
  var oO = document.getElementById("oLinePos");	
  var oT = 0;
  var oL = 0;
  while (oO)
  {
    oT = oT + oO.offsetTop; //71
    oL = oL + oO.offsetLeft;
	oO = oO.offsetParent;
  }
  oViewLine[1].style.top = oT + 248;
  oViewLine[1].style.left = oL;
  oViewLine[2].style.top = oT + 82;
  oViewLine[2].style.left = oL;
  oViewLine[3].style.top = oT + 414;
  oViewLine[3].style.left = oL;

  oViewLine[4].style.top = oT;
  oViewLine[4].style.left = oL;
  oViewLine[5].style.top = oT;
  oViewLine[5].style.left = oL;
}

//_______________________________________
//Display an error message
function displayError(message)
{
  // display error message, with more technical details if debugMode is true
  if (debugMode) 
  {
    alert('Error - ' + message);
  }
  ShowMessageInWin(Msg2Path + 'msg_con_lost.htm');
  SpinEmergencyStop();
}

//===================================== Process Game Result and Change Game Status Wins;

function ProcessGameResult(GameResult, Money)
{
  if (!FlagEmergencyStop)
  { 
    if (GameResult > 0)
    {
      ChangeInfoWin('WinInfo', "You Win $" + GameResult + "!");
      PlaySlotSounds('Win');
      ChangeRiskActivity(true);
    } else
    {
      ChangeInfoWin('WinInfo', '0');
      ChangeRiskActivity(false);
    }
    ChangeInfoWin('MoneyInfo', Money);
    
    //CheckPrint(SpinVal[1], SpinVal[2], SpinVal[3]);
    
    if (CurrentWinLines > 0)
    {
	  if (WinLines[0] == '-1')  //RISK GAME
	  {
	    if (GameResult > 0)
        {
          ChangeInfoWin('RiskWinInfo', "WIN!  $" + GameResult + "!");
          PlaySlotSounds('WinRisk');
        } else
        {
          ChangeInfoWin('RiskWinInfo', '0');
          ChangeRiskActivity(false);
        }
	  } else   //SPIN GAME
	  {
	    for (i = 0; i < CurrentWinLines; i++)
        {
		  oViewLine[WinLines[i]].style.visibility = 'visible';
        }
	    SetViewLinesPos();
	  }
    }
	
	//____AUTO MODE
	if (FlagAutoMode)  
	{
	  if (PlayMode == 1) window.setTimeout('LunchSpinButton()', 800);
	}
    
  } else //if (!FlagEmegrencyStop)
  {
    ChangeInfoWin('WinInfo', "Stopped");
  }
}


//_________________________NOT IMPORTANT FUNCTIONS

function ShowStat()
{
  alert(SpinVal[1] + ' ' + SpinVal[2] + ' ' + SpinVal[3] + ' - - - ' + LastAnswer + ' - - - ' + XHO_send.responseText + ' - - - ' + XHO_send.readyState + ' - - - ' + XHO_send.status);
}

// CHECK BOX CHANGE STATUS
/*

function CheckPrint(check1, check2, check3)
{
  if (FlagCheckSwitch == true)
  {
    chk1 = document.getElementById('checkbox1');
    chk1.innerHTML = check1;
    document.getElementById('checkbox2').innerHTML = check2;
    document.getElementById('checkbox3').innerHTML = check3;
  } 
}
*/

/* handles keydown to detect when enter is pressed */
 
function handleKey(e) 
{
  // get the event
  e = (!e) ? window.event : e;
  // get the code of the character that has been pressed        
  code = (e.charCode) ? e.charCode :
         ((e.keyCode) ? e.keyCode :
         ((e.which) ? e.which : 0));
  // handle the keydown event       
  if (e.type == "keydown") 
  {
    // if enter (code 13) is pressed
    if(code == 13)
    {
      // send the current message  
      make_request('Spin');
    }
  }
}

/* removes leading and trailing spaces from the string */
function trim(s)
{
  return(s);
  // if (s!='') return s.replace(/(^\s+)|(\s+$)/g, "");
}


