﻿// namespace: HONU
var HONU = {}

// go back to the new question form
HONU.newq = function () {
  HONU.clear_focus = true;
  $('#question').attr('disabled', false);
  $('#question').val('type your question for the Oracle here...');
  $('#output').fadeOut('medium', function(){
    $('#input').fadeIn('medium');
  });
}

// clears default text on first entry, then disconnects itself from handler
HONU.clear_focus = true;
HONU.focus = function () {
  if (HONU.clear_focus) {
    $('#question').val('');
    HONU.clear_focus = false;
  }
}

// the lines have been generated, display the results
HONU.done = function () {
  // figure out our present and future lower and upper trigrams
  var present_upper_id = 'tri';
  var present_lower_id = 'tri';
  var future_upper_id = 'tri';
  var future_lower_id = 'tri';
  var has_future = false;
  for (i = 0; i < HONU.lines.length; i++) {
    var present_line = 0;
    var future_line = 0;
    switch (HONU.lines[i]) {
    case 6:
      present_line = '0';
      future_line = '1';
      has_future = true;
      break;
    case 7:
      present_line = '1';
      future_line = '1';
      break;
    case 8:
      present_line = '0';
      future_line = '0';
      break;
    case 9:
      present_line = '1';
      future_line = '0';
      has_future = true;
      break;
    default:
      break;
    }
    if (i < 3) {
      present_lower_id = present_lower_id + present_line;
      future_lower_id = future_lower_id + future_line;
    }
    else {
      present_upper_id = present_upper_id + present_line;
      future_upper_id = future_upper_id + future_line;
    }
  }
  
  var present_hexagram = HONU.hexagrams[present_upper_id][present_lower_id];
  var future_hexagram = HONU.hexagrams[future_upper_id][future_lower_id];
  
  var html_build = new Array();
  html_build.push('<div style="text-align: center;">');
  html_build.push('<div style="padding: 4px; width: 350px; border: 1px solid #000000; font-size: 10pt; text-align: center; margin: 0px auto 0px auto; background: #FFFFFF; color: #000000;">I asked the I Ching Oracle:<br />');
  html_build.push('<span style="font-weight: bold;">' + $('#question').val() + '</span><br />');
  html_build.push('<img src="' + HONU.s3_path + present_hexagram['flavour_src'] + '" alt="" title="" style="display: block; margin: 4px auto 0px auto; padding: 5px 0px 5px 0px; border: none; width: ' + present_hexagram['flavour_width'] + 'px; height: ' + present_hexagram['flavour_height'] + 'px;" />');
  html_build.push('<div style="text-align: left; margin: 0px; padding: 0px;">');
  html_build.push('<div style="float: left; width: 44px; height: 50px; padding: 3px 3px 0px 0px;">');
  html_build.push('<img src="' + HONU.s3_path + present_hexagram['upper_src'] + '" alt="" title="" style="width: 44px; height: 20px; display: block; border: none; margin: 0px; padding: 0px;" />');
  html_build.push('<img src="' + HONU.s3_path + present_hexagram['lower_src'] + '" alt="" title="" style="width: 44px; height: 20px; display: block; border: none; margin: 4px 0px 0px 0px; padding: 0px;" />');
  html_build.push('</div>');
  if (has_future) {
    html_build.push('<span style="font-weight: bold;">(present) </span>');
  }
  html_build.push('<span style="font-weight: bold;">' + present_hexagram['num'] + '. ' + present_hexagram['name'] + ' - ' + present_hexagram['title'] + '</span><br />');
  html_build.push('<span>' + present_hexagram['desc'] + '</span><br style="clear: both;" />');
  html_build.push('<br />');
  if (has_future) {  
    html_build.push('<div>');
    html_build.push('<div style="float: left; width: 44px; height: 50px; padding: 3px 3px 0px 0px;">');
    html_build.push('<img src="' + HONU.s3_path + future_hexagram['upper_src'] + '" alt="" title="" style="width: 44px; height: 20px; display: block; border: none; margin: 0px; padding: 0px;">');
    html_build.push('<img src="' + HONU.s3_path + future_hexagram['lower_src'] + '" alt="" title="" style="width: 44px; height: 20px; display: block; border: none; margin: 4px 0px 0px 0px; padding: 0px;">');
    html_build.push('</div>');
    html_build.push('<span style="font-weight: bold;">(future) </span><span style="font-weight: bold;">' + future_hexagram['num'] + '. ' + future_hexagram['name'] + ' - ' + future_hexagram['title'] + '</span><br />');
    html_build.push('<span>' + future_hexagram['desc'] + '</span><br style="clear: both;" />');
    html_build.push('<br />');
    html_build.push('</div>');
  }
  html_build.push('</div>');
  html_build.push('<span style="font-style: italic;">What does the Oracle hold for you?</span><br />');
  html_build.push('<a href="http://luckyhonu.com/" style="color: #0000FF;">http://luckyhonu.com/</a>');
  html_build.push('</div></div>');  
  
  var bbcode_build = new Array();
  var pres_str = '';
  if (has_future) {
    pres_str = 'Present: ';
  }
  bbcode_build.push('---- I asked the I Ching Oracle: ----');
  bbcode_build.push('[b]' + $('#question').val() + '[/b]');
  bbcode_build.push('');
  bbcode_build.push('[img]' + HONU.s3_path + present_hexagram['flavour_src'] + '[/img]');
  bbcode_build.push('');
  bbcode_build.push(pres_str + '[b]' + present_hexagram['num'] + '. ' + present_hexagram['name'] + ' - ' + present_hexagram['title'] + '[/b]');
  bbcode_build.push(present_hexagram['desc']);
  if (has_future) {
    bbcode_build.push('');
    bbcode_build.push('Future: [b]' + future_hexagram['num'] + '. ' + future_hexagram['name'] + ' - ' + future_hexagram['title'] + '[/b]');
    bbcode_build.push(future_hexagram['desc']);
  }
  bbcode_build.push('');
  bbcode_build.push('What does the Oracle hold for you?');
  bbcode_build.push('[url]http://luckyhonu.com/[/url]');

  var text_build = new Array();
  text_build.push('---- I asked the I Ching Oracle: ----');
  text_build.push($('#question').val());
  text_build.push('');
  text_build.push(pres_str + '**** ' + present_hexagram['num'] + '. ' + present_hexagram['name'] + ' - ' + present_hexagram['title']);
  text_build.push(present_hexagram['desc']);
  if (has_future) {
    text_build.push('');
    text_build.push('Future: **** ' + future_hexagram['num'] + '. ' + future_hexagram['name'] + ' - ' + future_hexagram['title']);
    text_build.push(future_hexagram['desc']);
  }
  text_build.push('');
  text_build.push('What does the Oracle hold for you?');
  text_build.push('http://luckyhonu.com/'); 
  
  var html_out = html_build.join('');
  $('#result-html').html(html_out);
  $('#result-html-code').text(html_out);
  $('#result-bb-code').html(bbcode_build.join('<br>')); 
  $('#result-text-code').html(text_build.join('<br>'));

  $('#output').fadeIn('medium');
  
  // reset stuff (coins already got reset at end of last toss round)
  $('#line1').attr('src', HONU.preload['line0'].src);
  $('#line2').attr('src', HONU.preload['line0'].src);
  $('#line3').attr('src', HONU.preload['line0'].src);
  $('#line4').attr('src', HONU.preload['line0'].src);
  $('#line5').attr('src', HONU.preload['line0'].src);
  $('#line6').attr('src', HONU.preload['line0'].src);
  HONU.lines = null;
  $('#focus').css('display', 'none');
  $('#consult').css('display', 'inline');
}

// called every interval to toss another coin and parse line results
HONU.tick = function () {
  if (HONU.coins && HONU.coins.length < 4) {
    // still need to toss more coins for this round
    var toss = Math.floor(Math.random() * 2);
    HONU.coins.push(toss);
    if (toss == 1) {
      $('#coin' + HONU.coins.length).attr('src', HONU.preload['coin_head'].src);
    }
    else {
      $('#coin' + HONU.coins.length).attr('src', HONU.preload['coin_tail'].src);
    }
  }
  else if (HONU.coins && HONU.coins.length >= 4) {
    // this coin toss round is done, figure out what line we generated
    var sum = 0;
    for (i = 0; i < HONU.coins.length; i++) {
      if (HONU.coins[i] == 1) {
        sum = sum + Math.pow(2, 3 - i);
      }
    }
    if (sum < 1) {
      HONU.lines.push(6);
      $('#line' + HONU.lines.length).attr('src', HONU.preload['line6'].src);
    }
    else if (sum < 4) {
      HONU.lines.push(9);
      $('#line' + HONU.lines.length).attr('src', HONU.preload['line9'].src);
    }
    else if (sum < 9) {
      HONU.lines.push(7);
      $('#line' + HONU.lines.length).attr('src', HONU.preload['line7'].src);
    }
    else {
      HONU.lines.push(8);
      $('#line' + HONU.lines.length).attr('src', HONU.preload['line8'].src);
    }
    
    // reset the coin state to begin another round
    $('#coin1').attr('src', HONU.preload['coin_none'].src);
    $('#coin2').attr('src', HONU.preload['coin_none'].src);
    $('#coin3').attr('src', HONU.preload['coin_none'].src);
    $('#coin4').attr('src', HONU.preload['coin_none'].src);
    HONU.coins = null;
    HONU.coins = new Array();
  }
  if (HONU.lines && HONU.lines.length >= 6) {
    // we're done
    window.clearInterval(HONU.interval_id);
    $('#input').fadeOut('medium');
    $('#midput').fadeOut('medium', HONU.done);
  }
  if (!HONU.lines || !HONU.coins) {
    // bad state
    window.clearInterval(HONU.interval_id);
  }
}

// handle clicking of "Consult the Oracle" button
HONU.click = function () {
  // set initial state
  HONU.lines = null;
  HONU.coins = null;
  HONU.lines = new Array();
  HONU.coins = new Array();

  // hide old things, show new things
  $('#question').attr('disabled', true);
  $('#consult').fadeOut('fast', function(){
    $('#focus').css('display', 'block');
    $('#midput').fadeIn('medium', function(){
      // start the interval call for handling coin tosses
      HONU.interval_id = window.setInterval(HONU.tick, 250);
    });
  });
}

// onload handler
$(document).ready(function(){
  $('#question').val('type your question for the Oracle here...');
  $('#question').focus(HONU.focus);
  $('#consult').click(HONU.click);
  $('#newq').click(HONU.newq);
});
