var idx = 0;
var parent_id = 0;
var selected_ids = new Array(0, 0, 0, 0, 0, 0, 0);
var selected_pids = new Array(0, 0, 0, 0, 0, 0, 0);
var img_w = new Array(220, 140, 140, 140, 140, 140, 140);
var img_h = new Array(165, 105, 105, 105, 105, 105, 105);
var prod_type = "windows";
var loading = false;
var steps = 7;

function pf_init()
{
  pf_initControls();
  pf_reset();
}

function pf_reset()
{
  idx = 0;
  parent_id = 0;
  selected_ids = new Array(0, 0, 0, 0, 0, 0, 0, 0);
  selected_pids = new Array(0, 0, 0, 0, 0, 0, 0, 0);
  prod_type = "windows";
  loading = false;
  steps = 7;
  
  pf_refreshControls();
  pf_refreshContent();
  
  pf_query("TOP", 1);
}

function pf_initControls()
{
  jQuery('#product_finder .pf_btn_back').click(function()
  {
    pf_back();
  });
  jQuery('#product_finder .pf_btn_next').click(function()
  {
    pf_next();
  });
  jQuery('#product_finder .pf_btn_complete').click(function()
  {
    pf_complete();
  });
  jQuery('#product_finder .pf_btn_complete_reset').click(function()
  {
    pf_complete_reset();
  });
  jQuery('#product_finder .pf_btn_reset').click(function()
  {
    pf_reset();
  });
  
  
  jQuery("#pf_loading").hide();
  jQuery("#product_finder .pf_btn_back").hide();
  jQuery("#product_finder .pf_btn_next").hide();
  jQuery("#product_finder .pf_btn_complete").hide();
  jQuery("#product_finder .pf_btn_complete_reset").hide();
  jQuery("#product_finder .pf_btn_reset").hide();
  
  
}

function pf_back()
{
  /*
  if (prod_type != "windows")
  {
    if (idx > 0)
    {
      idx--;
      pf_query("BACK", idx);
    }
  }
  else
  {
    if (idx > 0)
    {
      pf_query(selected_ids[idx-2], idx-1);
      idx--;
    }
  }
  */
  
  if (idx > 0)
  {
    idx--;
    pf_query("BACK", idx);
  }
  
  
  
  pf_refreshControls();
  pf_refreshContent();
}

function pf_next()
{
  if (idx < steps)
  {
    pf_query(selected_ids[idx], idx+1);
    ++idx;
  }
  else
  {
    pf_complete(); // submit
  }
  
  pf_refreshControls();
  pf_refreshContent();
}

function pf_complete()
{
  // add to quote
  /*
  if (prod_type != "windows")
  {
    url = "/quote/add/"+selected_pids.join('|');
    url_redirect = "/quote";
  }
  else
  {
    url = "/quote/add/"+selected_pids[steps];
    url_redirect = "/quote/redirect/" + selected_pids[steps];
  }
  */
  
  //url = "/quote/add/"+selected_pids.join('|');
  //url_redirect = "/quote";
  
  /*
  jQuery.ajax({ url:url, context: document.body, success: function() {
    // redirect
    window.location.href = url_redirect;
  }});
  */
  
  url = "/quote/add/"+selected_pids.join('|');
  window.location.href = url;
  
}


function pf_complete_reset()
{
  // add to quote
  /*
  if (prod_type != "windows")
    url = "/quote/add/"+selected_pids.join('|');
  else
    url = "/quote/add/"+selected_pids[steps];
  */
  url = "/quote/add/"+selected_pids.join('|');
  
  jQuery.ajax({ url:url, context: document.body, success: function() {
    // reset
    pf_reset();
  }});
}

function pf_refreshControls()
{
  // btn
  if (idx > 0 && !loading)
    jQuery("#product_finder .pf_btn_back").show();
  else
    jQuery("#product_finder .pf_btn_back").hide();
  
  // btn
  if (idx < steps && selected_ids[idx] != 0 && !loading)
    jQuery("#product_finder .pf_btn_next").show();
  else
    jQuery("#product_finder .pf_btn_next").hide();
  
  // loader
  if (!loading)
    jQuery("#pf_loading").hide();
  else
    jQuery("#pf_loading").show();
  
  if (!loading && idx > 0 )
    jQuery("#product_finder .pf_btn_reset").show();
  else
    jQuery("#product_finder .pf_btn_reset").hide();
  
  
  // complete
  if (idx == steps && selected_ids[idx] != 0 && !loading)
  {
    jQuery("#product_finder .pf_btn_complete").show();
    jQuery("#product_finder .pf_btn_complete_reset").show();
  }
  else
  {
    jQuery("#product_finder .pf_btn_complete").hide();
    jQuery("#product_finder .pf_btn_complete_reset").hide();
  }
  
  
}

function pf_refreshContent()
{
  
  // handle descriptions
  jQuery("#product_finder .windows").hide();
  jQuery("#product_finder .doors").hide();
  jQuery("#product_finder .patio").hide();
  
  
  jQuery("#product_finder ."+prod_type).show();
  
  // handle steps
  for (i=0; i<=steps; ++i)
    jQuery("#product_finder .step"+i).hide();
  
  if (!loading)
    jQuery("#product_finder .step"+idx).show();
  
  
  // handle nav
  for (i=0; i<=steps; ++i)
  {
    jQuery("#product_finder .nav .nav"+i).removeClass("complete active incomplete");
    
    if (i < idx)
      jQuery("#product_finder .nav .nav"+i).addClass("complete");
    else if (i == idx)
      jQuery("#product_finder .nav .nav"+i).addClass("active");
    else if (i > idx)
      jQuery("#product_finder .nav .nav"+i).addClass("incomplete");
  }
  
  if (selected_ids[idx] != 0)
    pf_selectItem(selected_ids[idx]);
}

function pf_populate(result)
{
  // tweak classes on the fly
  jQuery('#pf_results').removeClass('windows doors step0 step1 step2 step3 step4 step5 step6');
  
  if (prod_type != "windows")
    jQuery('#pf_results').addClass('doors step'+idx);
  else
    jQuery('#pf_results').addClass('windows step'+idx);
  
  
  jQuery('#pf_results').empty();
  var first_id = "";
  var first_pid = "";
  
  jQuery.each(result, function(i,item){
    
    if (item.id != "HIDDEN" && item.id != "EXTRA")
    {
      var id = "pf_item_popup_" + item.id;
      
      
      var r = "";
      r += "<div class='pf_item' id='pf_item_"+item.id+"'>";
      r += "  <div class='thumb'>";
      r += "    <a href='javascript:void(0);' onclick='pf_userSelectItem(\""+item.id+"\", \""+item.pid+"\");'>" + item.img_hero + "</a>";
      r += "  </div>";
      r += "  <div style='clear:both;'></div>";
      // http://swip.codylindley.com/DOMWindowDemo.html
      
      if (item.pid > 0)
        r += "  <div class='info'><a href='javascript:void(0);' onclick='jQuery.openDOMWindow({width:770, height:570, windowSourceID:\"#"+id+"\"});' ><img src='/images/icons_gif/help.gif' /></a></div>";
      r += "  <div class='title'>"+item.title+"</div>";
      r += "  <div class='blurb'>"+item.blurb+"</div>";
      
      if (item.pid > 0)
      {
        r += "  <div id='"+id+"' class='pf_overlay' style='display:none;'>";
        r += "    <div class='pf_overlay_content'>";
        r += "      <a class='quote_close' href='javascript:void(0);' onclick='jQuery.closeDOMWindow({windowSourceID:\"#"+id+"\"});'>Close X</a>";
        r += "      <div class='thumb'>" + item.img_full + "</div>";
        r += "      <div class='copy'>";
        r += "        <div class='title'>"+item.title+"</div>";
        r += "        <div class='desc'>" + item.content_body + "</div>";
        r += "        <a class='quote_link' href='"+item.prod_url+"'>Read More</a>";
        r += "        <div style='clear:both;'></div>";
        r += "      </div>";
        r += "      <div style='clear:both;'></div>";
        r += "    </div>";
      }
      
      r += "  </div>";
      
      
      
      jQuery(r).appendTo('#pf_results');
      
      if (first_id == "")
        first_id = item.id;
      if (first_pid == "")
        first_pid = item.pid;
    }
    
  });
  
  pf_refreshControls();
  pf_refreshContent();
  
  pf_selectItem(first_id, first_pid); // default to 1st item
}

function pf_highlightItem(id)
{
  jQuery("#pf_results .thumb").removeClass('active');
  jQuery("#"+id+" .thumb").addClass('active');
}


function pf_userSelectItem(id, pid)
{
  pf_selectItem(id, pid);
  pf_next();
}

function pf_selectItem(id, pid)
{
  selected_ids[idx] = id;
  
  if (pid != null)
    selected_pids[idx] = pid;
  
  
  pf_highlightItem("pf_item_"+id);
  pf_refreshControls();
}

var idx_next = 0;
var idx_img_size = 0;

var window_flow = new Array("WINDOW_TYPES", "", "WINDOW_GLASS", "WINDOW_GRILLE_TYPE", "WINDOW_GRILLE_PATTERN", "WINDOW_FRAME_COLOR");
var door_flow_entrance = new Array("DOOR_TYPE", "SLAB", "FRAME_BRICKMOULD", "DOOR_STYLE", "LITE_KIT", "");
var door_flow_garden = new Array("DOOR_TYPE", "GARDEN_PATIO_STYLE");
var prod_flow = window_flow;

function pf_query(pid, next)
{
  parent_id = pid;
  idx_next = next;
  
  
  // product flow
  //
  if (pid == "WINDOWS")
  {
    prod_type = "windows";
    prod_flow = window_flow;
  }
  if (pid == "DOORS" || pid == "MAIN_ENTRANCE")
  {
    prod_type = "doors";
    prod_flow = door_flow_entrance;
  }
  if (pid == "PATIO_GARDEN")
  {
    prod_type = "patio";
    prod_flow = door_flow_garden;
  }
  
  steps = prod_flow.length;
  
  
  
  /*
  // allow the blank flow to use the selected items children
  if (pid == "BACK")
  {
    if (prod_flow[idx-1] == "")
      parent_id = selected_ids[idx];
    else
      parent_id = prod_flow[idx-1];
  }
  else
  {
    parent_id = prod_flow[idx];
  }
  */
  
  // allow the blank flow to use the selected items children
  if (pid == "BACK")
  {
    parent_id = prod_flow[idx-1];
    if (parent_id == "")
      parent_id = selected_ids[idx-1];
  }
  else
  {
    parent_id = prod_flow[idx];
  }
  
  if (idx == 0 && pid == "TOP")
    parent_id = "TOP";
  
  if (parent_id == "")
    parent_id = selected_ids[idx];
  
  if (parent_id == null)
    parent_id = "TOP";
  
  
  if (idx_next == 0)
    idx_next = 1;
  
  
  idx_img_size = idx_next;
  if (parent_id == "TOP")
    idx_img_size = 0;
  
  
  // DOORS: is steel slab is selected, skip the 'door style' choice
  if (in_array(3842, selected_pids) && idx_next == 4)
  {
    parent_id = "LITE_KIT";
    ++idx_next;
    ++idx;
  }
  
  
  
  
  /*
  alert("selected_pids: " + selected_pids);
  alert("parent_id: " + parent_id);
  alert("idx: "+idx);
  alert("pid: "+pid);
  alert("idx_next: "+idx_next);
  */
  
  pf_getProds();
  
  pf_refreshControls();
  pf_refreshContent();
}


function in_array(v, arr)
{
  for (i=0;i<arr.length;i++)
  {
    if (arr[i]==v)
      return true;
  }
  return false;
}

function pf_getProds()
{
  
  loading = true;
  
  var url = '/?pf='+parent_id+'&img_w='+img_w[idx_img_size]+'&img_h='+img_h[idx_img_size]+'&selected_pids='+selected_pids.join(",")+'&idx_next='+idx_next;
  jQuery.getJSON(url, function(result)
    {
      loading = false;
      pf_populate(result);
    }
  );
}
  

