// Terissa ring puzzle engine
// Author: Arvy Budiarto
// Date: 10/29/07
// (c) Light Painter Photography and Web Developer 2007
//ring puzzle engine is written based on arrays,
//each moves redraws the entire table based on the array.

//ring puzzle limited to three columns and three rings

/*
*/

//declare ring_engine class
//this class takes small medium and large document ids to grab the content 
//(which should be an image or characters representing a ring at the cell)
function ring_engine(small_a, med_a, large_a,small_b, med_b, large_b,small_c, med_c, large_c)
{

   var clicks = 0;
   //define the divs ids for later, moving the rings around
   var div_array = new Array();
   div_array[0] = new Array(small_a, med_a, large_a);
   div_array[1] = new Array(small_b, med_b, large_b);
   div_array[2] = new Array(small_c, med_c, large_c);
   
   //generate the ring array
   var large_ring = new Array();
   var medium_ring = new Array();
   var small_ring = new Array();
   
   //fill variables of large medium and small array, this represents the 
   //ring model at each respective positions
   large_ring["model"] = document.getElementById(large_a).innerHTML;
   medium_ring["model"] = document.getElementById(med_a).innerHTML;
   small_ring["model"] = document.getElementById(small_a).innerHTML;
   
   //fill weight of each ring, this is to determine the rule 
   //that there should be no heavier ring on top of lighter ring
   large_ring["weight"] = 3;
   medium_ring["weight"] = 2;
   small_ring["weight"] = 1;
   
   //fill positioning array with the ring
   var pos_array = new Array();
   pos_array[0] = new Array(small_ring, medium_ring, large_ring);
   pos_array[1] = new Array(-1,-1,-1); //-1 means empty
   pos_array[2] = new Array(-1,-1,-1);
   
   //alert('initialized');
   //alert(large_ring["model"] +' ' + medium_ring["model"] + ' ' + small_ring["model"]);
   
   
   //define the functions
   //this functions serves the purpose of drawing the table based on the positioning array
   this.draw_models = function()
   {
      var c_element; //current element
      var c_model; //current model
      var c_div_array = div_array;
      var c_pos_array = pos_array;
      for(var x = 0; x < 3; x++)
      {
         for(var y = 0; y < 3; y++)
         {
            c_element = document.getElementById(c_div_array[x][y]);
            c_model = c_pos_array[x][y];
            
            //check if there's an object inside
            if(c_model != -1)
            {
               c_element.innerHTML = c_model["model"]; //replace current cell content with the model
            }
            else
            {
               c_element.innerHTML = ""; //replace current cell content with clear
            }
         }
      }
      
      //now check for winning
      var win = 0;
      for(var z = 0; z < 3; z++)
      {
         if(pos_array[2][z] != -1)
         {
            win++;
         }
      }
      
      if(win==3)
      {
         return true;
      }
      else
      {
         return false;
      }
   }
   
   this.move_left = function(ring_col)
   {
      //increase clicks
      
      clicks++;
      
      if(ring_col < 0) //prevent illegal moves
      {
         return this.nogo("Can't move that way");
      }
      
      //get the current ring model, top most of current col
      var c_model_found = false;//determine if the model is found
      var c_model;
      var c_col; //define the position of current cell and column
      var c_cell;
      for(var y = 0; y < 3; y++) //remember 0 is the top most supposedly
      {
         c_model = pos_array[ring_col][y];
         
         if(c_model!=-1)
         {
            //model found
            c_model_found = true;
            c_col = ring_col;
            c_cell  = y;
            
            break;//stop loop
         }
      }
      //check if there's model otherwise nogo
      if(c_model_found == false)
      {
         
         return this.nogo("No ring found");
      }
      
      //at this point c_model is found
      //alert('model found');
      //alert(c_model["weight"] + "---" + c_model["model"]);
      //now check target location
      var target_col = ring_col - 1;
      var target_cell;
      var bottom_cell;
      var target_weight;
      var bottom_weight;
      var moved = false;
      
      //check the slot see if its empty, start from bottom
      for(var x= 2; x>=0; x--)
      {
         //alert("x = " + x);
         //reset
         target_cell = false;
         bottom_cell = false;
         if(x == 2) //bottom most
         {
            target_cell = pos_array[target_col][x]; //aim for this cell
            bottom_cell = -1;
            //alert("target content: " + target_cell);
         }
         else
         {
            target_cell = pos_array[target_col][x];
            bottom_cell = pos_array[target_col][x+1];
            
            //alert("target: " + target_cell + " bottom: " + bottom_cell + " target col: " + target_col);
         }
         
         
         //check if target cell empty and there's bottom cell
         if(target_cell == -1 & bottom_cell != -1)
         {
            //alert("preparing to move to target cell...");
            //get weight
            bottom_weight = bottom_cell["weight"];
            target_weight = c_model["weight"];
            
            
            if(target_weight >= bottom_weight)
            {
               return this.nogo("This ring is too heavy");
            }
            else
            {
               //alert("legal move" + " target content: "+pos_array[target_col][x]);
               pos_array[target_col][x] = c_model;
               pos_array[c_col][c_cell] = -1; //clear origin cell
               //alert("current col and cell: " + c_col + " : " + c_cell); 
               return this.draw_models();
               //return;
            }
         }
         else if(target_cell == -1) //no bottom cell
         {
            //alert('here');
            pos_array[target_col][x] = c_model;
            //alert(pos_array[target_col][x]["model"]);
            pos_array[c_col][c_cell] = -1; //clear origin cell
            //alert("current col: " + c_col + " current cell " + c_cell + " target col " + target_col + " target cell " + x);
            //alert(c_model["model"]);
            return this.draw_models();
            //return;
         }
      }
   }
   
   this.move_right = function(ring_col)
   {
      //increase clicks
      
      clicks++;
      
      if(ring_col > 2) //prevent illegal moves
      {
         return this.nogo("Can't move that way");
      }
      
      //get the current ring model, top most of current col
      var c_model_found = false;//determine if the model is found
      var c_model;
      var c_col; //define the position of current cell and column
      var c_cell;
      for(var y = 0; y < 3; y++) //remember 0 is the top most supposedly
      {
         c_model = pos_array[ring_col][y];
         
         if(c_model!=-1)
         {
            //model found
            c_model_found = true;
            c_col = ring_col;
            c_cell  = y;
            
            break;//stop loop
         }
      }
      //check if there's model otherwise nogo
      if(c_model_found == false)
      {
         
         return this.nogo("No ring found");
      }
      
      //at this point c_model is found
      //alert('model found');
      //alert(c_model["weight"] + "---" + c_model["model"]);
      //now check target location
      var target_col = ring_col + 1;
      var target_cell;
      var bottom_cell;
      var target_weight;
      var bottom_weight;
      var moved = false;
      
      //check the slot see if its empty, start from bottom
      for(var x= 2; x>=0; x--)
      {
         //alert("x = " + x);
         //reset
         target_cell = false;
         bottom_cell = false;
         if(x == 2) //bottom most
         {
            target_cell = pos_array[target_col][x]; //aim for this cell
            bottom_cell = -1;
            //alert("target content: " + target_cell);
         }
         else
         {
            target_cell = pos_array[target_col][x];
            bottom_cell = pos_array[target_col][x+1];
            
            //alert("target: " + target_cell + " bottom: " + bottom_cell + " target col: " + target_col+ " target cell: " + x);
         }
         
         //check if target cell empty and there's bottom cell
         if(target_cell == -1 & bottom_cell != -1)
         {
            //alert("preparing to move to target cell...");
            //get weight
            bottom_weight = bottom_cell["weight"];
            target_weight = c_model["weight"];
            
            if(target_weight >= bottom_weight)
            {
               return this.nogo("This ring is too heavy");
            }
            else
            {
               pos_array[target_col][x] = c_model;
               pos_array[c_col][c_cell] = -1; //clear origin cell
               return this.draw_models();
               //return;
            }
         }
         else if(target_cell == -1) //no bottom cell
         {
            //alert('here');
            pos_array[target_col][x] = c_model;
            //alert(pos_array[target_col][x]["model"]);
            pos_array[c_col][c_cell] = -1; //clear origin cell
            //alert("current col: " + c_col + " current cell " + c_cell + " target col " + target_col + " target cell " + x);
            //alert(c_model["model"]);
            return this.draw_models();
            //return;
         }
      }
   }
   
   this.nogo = function(msg)
   {

      //set the message
      document.getElementById('err_div').innerHTML = msg;
      new Effect.Appear('err_div',{delay:0});
      new Effect.Pulsate('err_div');
      
      //delete message after 3 seconds
      setTimeout("new Effect.Fade('err_div')",3000);
      
      //bring attention to the game area
      
   }
}







//separate from the class this function initalize the class itself
var engine;

function init(small_a, med_a, large_a,small_b, med_b, large_b,small_c, med_c, large_c)
{
   engine = new ring_engine(small_a, med_a, large_a,small_b, med_b, large_b,small_c, med_c, large_c);
   engine.draw_models();
}

function hide(id)
{
    new Effect.SlideUp(id);
}

function show(id)
{
   new Effect.SlideDown(id);
}

function validate_radio(radio_element) //true if satisfied
{
   var radio_array = radio_element;
    //alert(radio_array.length);
      if(radio_array!=null)
      {
         for(x = 0; x < radio_array.length; x++)
         {
           
            if(radio_array[x].checked)
            {
               return true;
            }
         }
      }

   return false;
}

function validate_text(id)
{
   if(document.getElementById(id).value!='')
   {
      //alert(document.getElementById(id).value);
      return true;
   }
   return false;
}

function complete_section(radio_element, text_id, area_id)
{
   if(validate_radio(radio_element) & validate_text(text_id))
   {
      hide(area_id);
      show('game');
      engine.nogo('Game Start');
   }
   else
   {
      engine.nogo('Complete all sections');
   }
}

function complete_terissa(radio_element, text_id)
{
   if(validate_radio(radio_element) & validate_text(text_id))
   {
      document.eval.submit();
   }
   else
   {
      engine.nogo('Complete all sections');
   }
}