// Combination IDs - these are sequential on the keypad
var combo1 = "#fiv";
var combo2 = "#eig";
var combo3 = "#six";
var combo4 = "#sev";

var displayN = '';

var current = "#home";

$(document).ready(function(){

   //$.preloadCssImages(); // dont really need this
   
   $("#taplink").click(function() {
      $("#tapestry").slideToggle();
      return false;
   });

   $("#ask").hide();
   $(".innerSanctum").hide();
   $("#home").show();
   
   function chgView(c) {
      if(current != c) {
        $(current).fadeOut(200, function(){
          $(c).fadeIn(500);
        });
        current = c;
      }
      return false;
   } // chgView

   $("#hLink").click(function() {
      $("#doorEdge").removeClass('standard').addClass('valview');
      $(current).fadeOut(200, function(){
        $("#home").fadeIn(500);
      });
      current = "#home";
      return false;
   });

   $("#aLink").click(function() {
      $("#doorEdge").removeClass('valview').addClass('standard');
      chgView('#about');
      return false;
   });
   
   $("#cLink").click(function() {
      $("#doorEdge").removeClass('valview').addClass('standard');
      chgView('#contact');
      return false;
   });
   
   $("#fLink").click(function() {
      $("#doorEdge").removeClass('valview').addClass('standard');
      chgView('#follow');
      return false;
   });

   $(".key").click(retfalse);
   
   $("#askUp").click(function() {
  
      $("#ask").fadeIn('slow');
      $("#askPad").html('<p>This is a four digit combination safe. Try the keypad. Mouseover the keys for the activated key. Experiment until you key in the fourth number to open the safe.</p>');
      return false;

   });

   $("#closeAsk").click(function() {
      $("#ask").fadeOut('slow');
      return false;
   });

   function openSafe() {

      $("#theLever").removeClass('static').addClass('animate');

      // Cheesy delay trick
      $("#wrapper").animate({opacity: 1.0}, 1000, function(){
      
         $("#doorEdge").animate( { width:"422px" }, 300, function(){
           $("#hiddenCover").animate( { top:"-64px" }, 300);
         });

         $("#doorMain").animate( { width:"22px" , left:"418px" , height:"352px" , top:"-10px" }, 300, function(){
           $("#doorMain").removeClass('doorClosed').addClass('doorOpen');
           $("#padlock").removeClass('padlockC').addClass('padlockO');
         });

         $("#doorHandle, #theLever").animate( { width:"18px" , left:"440px" }, 300, function(){
           $("#theLever").removeClass('static').addClass('open');
         });

         $("#doorMcontent").fadeOut(150); // in case we want something in this space for future... area with handle

      });

      $("#theLever").unbind('click', openSafe);

      return false;

   } // openSafe

   function retfalse() { return false; }
   
   function activateLvr() {   
      writeOutput(combo4);
      $("#theLever").css('cursor','pointer').click(openSafe);
      unbindC(combo4);
      return false;
   } // activateLvr

   function activateC4() {   
      writeOutput(combo3);
      $(combo4).addClass("on");
      unbindC(combo3); // unbind activateC4 from combo3
      $(combo4).click(activateLvr); // $(combo4).click(openSafe);
      return false;
   } // activateC4
   
   function activateC3() {
      writeOutput(combo2);
      $(combo3).addClass("on");
      $(combo3).click(activateC4);
      unbindC(combo2); // unbind activateC3 from combo2
      return false;
   } // activateC3
   
   function activateC2() {
      writeOutput(combo1);
      $(combo2).addClass("on");
      $(combo2).click(activateC3);
      unbindC(combo1); // unbind activateC2 from combo1
      return false;
   } // activateC2
   
   function unbindC(c) {
     switch(c) {
       case combo1: $(c).unbind('click', activateC2); break;
       case combo2: $(c).unbind('click', activateC3); break;
       case combo3: $(c).unbind('click', activateC4); break;
       case combo4: $(c).unbind('click', activateLvr); break;  //activate lever
       default: break;
     }
      $(c).click(retfalse);
      $(c).removeClass("on");
   } // unbindC
   
   function unmapC(c) {
     switch(c) {
       case '#one': displayN = '1'; break;
       case '#two': displayN = '2'; break;
       case '#thr': displayN = '3'; break;
       case '#fou': displayN = '4'; break;
       case '#fiv': displayN = '5'; break;
       case '#six': displayN = '6'; break;
       case '#sev': displayN = '7'; break;
       case '#eig': displayN = '8'; break;
       case '#nin': displayN = '9'; break;
       case '#sta': displayN = '*'; break;
       case '#zer': displayN = '0'; break;
       case '#has': displayN = '#'; break;
       default: break;
     }
     return displayN;
   } // unmapC
   
   function writeOutput(c) {
     if(c) {
       var thisNum = unmapC(c);
       $("#display").append(thisNum);
     } else {
       $("#display").text('');
     }
   } // writeOutput
   

   // this begins things...
   $(combo1).addClass("on").click(activateC2);

  $("#close").click(function() {
  
      $("#doorEdge").animate( { width:"0px" }, 300);
      $("#hiddenCover").animate( { top:"0px" }, 300);

      $("#doorMain").removeClass('doorOpen').addClass('doorClosed');
      $("#doorMain").animate( { width:"440px" , left:"0px" , height:"332px" , top:"0px" }, 300, function(){
      
         $("#padlock").removeClass('padlockO').addClass('padlockC');

      });

      $("#theLever").removeClass('open').addClass('static');
      $("#doorHandle, #theLever").animate( { width:"240px" , left:"90px" }, 300, function(){});

      $("#doorMcontent").fadeIn(150);

      // unbind openSafe from the lever to reset everything
      $("#theLever").css('cursor','default').unbind('click', openSafe);
      
      // clear the display
      writeOutput();
      
      // reset the keypad
      $(combo1).addClass("on").click(activateC2);
      
      // reset the lever
      $("#theLever").removeClass('animate').addClass('static');

      return false;

  });


});