var custom_price_per_inch = 0.9;
var wtsWLQT = "qt_price-0.90";

var wtsNodeId = 0;

var wtsNodes = new Array();
wtsNodes['font'] = "input[name='option_6']";
wtsNodes['width'] = "input[name='option_1']";
wtsNodes['text'] = "input[name='option_2']";
wtsNodes['text2'] = "input[name='option_3']";
wtsNodes['text3'] = "input[name='option_4']";
wtsNodes['text4'] = "input[name='option_5']";
wtsNodes['fontRow'] = 3;

var wtsColorMap = new Array();
wtsColorMap['Beige'] = 'ffffcc,000000';
wtsColorMap['Black'] = '000000,ffffff';
wtsColorMap['Blue (Dark)'] = '1e3366,ffffff';
wtsColorMap['Blue (Genetian)'] = '0066cc,ffffff';
wtsColorMap['Blue (Ice)'] = '66ccff,000000';
wtsColorMap['Blue (Light)'] = '3399ff,ffffff';
wtsColorMap['Brown'] = '663300,ffffff';
wtsColorMap['Brown (Light)'] = 'cc9933,ffffff';
wtsColorMap['Brown (Nut)'] = 'cc6600,ffffff';
wtsColorMap['Copper (Metallic)'] = '996600,ffffff';
wtsColorMap['Gold (Metallic)'] = 'e3ad0b,ffffff';
wtsColorMap['Grey (Dark)'] = '666666,ffffff';
wtsColorMap['Green'] = '009933,ffffff';
wtsColorMap['Green (Dark)'] = '003300,ffffff';
wtsColorMap['Green (Lime Tree)'] = '99cc00,ffffff';
wtsColorMap['Lavender'] = '9966cc,ffffff';
wtsColorMap['Mint'] = '33ffcc,000000';
wtsColorMap['Orange (Light)'] = 'ff6600,ffffff';
wtsColorMap['Pink (Soft)'] = 'ff6699,ffffff';
wtsColorMap['Red'] = 'e41a1a,ffffff';
wtsColorMap['Red (Dark)'] = '990000,ffffff';
wtsColorMap['Silver (Metallic)'] = 'cccccc,ffffff';
wtsColorMap['Turquoise'] = '009999,ffffff';
wtsColorMap['Violet'] = '663399,ffffff';
wtsColorMap['Yellow (Brimstone)'] = 'ffff00,000000';
wtsColorMap['Yellow (Light)'] = 'ffe400,000000';
wtsColorMap['Yellow (Signal)'] = 'ffcc33,000000';
wtsColorMap['White'] = 'ffffff,000000';
wtsColorMap['Stealth Jet Black'] = '222222,ffffff';
wtsColorMap['Breakfast Blend Brown'] = '663300,ffffff';
wtsColorMap['Gobi Desert Tan'] = 'd8be5e,000000';
wtsColorMap['Boston Fern Green'] = '869343,000000';
wtsColorMap['Awning Red'] = 'ae0606,ffffff';
wtsColorMap['Popped Corn White'] = 'ffffff,000000';
wtsColorMap['Parchment Paper Cream'] = 'ffffc5,000000';
wtsColorMap['Soft Heather Purple'] = 'be68fe,000000';
wtsColorMap['Cornsilk Yellow'] = 'ffff66,000000';
wtsColorMap['Rhythmic Blue'] = '88cedc,000000';
wtsColorMap['My Fair Lady Pink'] = 'ffb7b7,000000';
wtsColorMap['Forest Ridge Green'] = '2c3a00,ffffff';
wtsColorMap['Twilight Chimes Blue'] = '0c1b2e,ffffff';
wtsColorMap['Chianti Burgundy'] = '6e0a02,ffffff';
wtsColorMap['White Porcelain Tile'] = 'ffffff,000000';
wtsColorMap['Cream Tile'] = 'f2eee0,000000';
wtsColorMap['Beige Tile'] = 'e3d0b7,000000';
wtsColorMap['Light Brown Tile'] = 'dbb489,000000';
wtsColorMap['Light Brown Marble Tile'] = '916538,000000';



function selectQuoteGroup(groupId){
  $("#quoteListIntro, .quoteGroup").hide();
  $("#quoteGroup-"+groupId).show();
}

function wtsColorSelect_select(nodeId,e){
  if($(e).data("wtsColor")){
    $("#wts-color-select-"+nodeId).data("wtsColor", $(e).data("wtsColor"));
    $("#wts-color-select-"+nodeId).data("wtsBgColor", $(e).data("wtsBgColor"));
  }
  $("#wts-color-select-"+nodeId+" a").removeClass("selected");
  $(e).addClass("selected");
  $("#wts-color-select-"+nodeId+" div").html($(e).html());
  $("#wts-color-select-"+nodeId).parent().find("select").val($(e).data("selectVal"));
  
  if($(e).data("wtsColor")) {
    $("#wts-color-select-"+nodeId).parent().css("background", "#"+$(e).data("wtsColor"));
    updateImagePreview();
  }
  qtCalcUpdate();
}

function wtsColorSelect_hide(){
  $("#wts-color-select-sheild").hide();
  $(".wts-color-select").removeClass("wts-color-select-active");
}

function updateImagePreview() {
  if($("#prod_rn_344").length) {
    color = $("#wts-color-select-0").data("wtsColor");
    bgcolor = $("#wts-color-select-0").data("wtsBgColor");
    text = $(wtsNodes['text']).val();
    text2 = $(wtsNodes['text2']).val();
    text3 = $(wtsNodes['text3']).val();
    text4 = $(wtsNodes['text4']).val();
    width = $(wtsNodes['width']).val();
    font = $(wtsNodes['font']).val();
    
    $(".wl-preview-img")
      .attr("src", basedir+"/includes/letters.php?t1="+text+"&t2="+text2+"&t3="+text3+"&t4="+text4+"&c="+color+"&b="+bgcolor+"&w="+width+"&f="+font)
      .load(function(){
        $.get(basedir+"/includes/letters-info.php", function(data) {
          data = data.split("&");
          wl = new Array();
          for(i=0; i<data.length; i++) {
            temp = data[i].split("=");
            wl[temp[0]] = temp[1];
          }
          if(wl['w']) {
            $(wtsNodes['width']).val(wl['w']);
            $(wtsNodes['width']).removeClass(wtsWLQT);
            wtsWLQT = "qt_price-"+(wl['w']*custom_price_per_inch);
            $(wtsNodes['width']).addClass(wtsWLQT);
            qtCalcUpdate();
          }
        });
      });
  }  
  qtCalcUpdate();
}

function selectFont(fid) {
  if($("#prod_rn_344").length) {
    font = $("#fid_"+fid).attr("alt");
    fontGroup = $("#fid_"+fid).parent().parent().parent().parent().find("h3").attr("title");
    $(wtsNodes['font']).val(fontGroup+"/"+font);
    
    $("#wl-fonts img").removeClass("selected");
    $("#fid_"+fid).addClass("selected");
    $("#wl-font-selected-name").html($("#fid_"+fid).attr("title"));
    
    updateImagePreview();
  }
  qtCalcUpdate();
}

function wlSplitLines(number){
  wlJoinLines();
  lineText = $(wtsNodes['text']).val();
  words = lineText.split(" ");
  line = new Array();
  
  if((words.length < 10 && !number) || number==1){
    line[1] = words.join(" ");
    line[2] = line[3] = line[4] = "";
  }else if((words.length < 20 && !number) || number==2){
    perline = Math.round(words.length/2);
    line[1] = words.slice(0, perline).join(" ");
    line[2] = words.slice(perline).join(" ");
    line[3] = line[4] = "";
  }else if((words.length < 30 && !number) || number==3){
    perline = Math.round(words.length/3);
    line[1] = words.slice(0, perline).join(" ");
    line[2] = words.slice(perline, perline*2).join(" ");
    line[3] = words.slice(perline*2).join(" ");
    line[4] = "";
  }else{
    perline = Math.round(words.length/4);
    line[1] = words.slice(0, perline).join(" ");
    line[2] = words.slice(perline, perline*2).join(" ");
    line[3] = words.slice(perline*2, perline*3).join(" ");
    line[4] = words.slice(perline*3).join(" ");
  }
  
  $(wtsNodes['text']).val($.trim(line[1]));
  for(i=2; i<=4; i++){
    $(wtsNodes['text'+i]).val($.trim(line[i]));
  }
  updateImagePreview();
}

function wlJoinLines(){
  lineText = $(wtsNodes['text']).val();
  for(i=2; i<=4; i++){
    lineText += ' '+$(wtsNodes['text'+i]).val();
  }
  wlClearLines();
  $(wtsNodes['text']).val($.trim(lineText));
}

function wlClearLines(){
  $(wtsNodes['text']).val("");
  $(wtsNodes['text2']).val("");
  $(wtsNodes['text3']).val("");
  $(wtsNodes['text4']).val("");
}

function wtsSelectArtCategory(node) {
  $("#wall-art-radios").scrollTo($("#"+$(node).val()), 500);
}

function copyBillingToShipping() {
  $(".bill_address input, .bill_address select").each(function(){
    val = $(this).val();
    id = $(this).attr('id');
    id = id.split("Bill_").join("Ship_");
    $("#"+id).val(val);
  });
  $(".bill_address textarea").each(function(){
    html = $(this).html();
    id = $(this).attr('id');
    id = id.split("Bill_").join("Ship_");
    $("#"+id).val(html);
  });
}



$(document).ready(function(){

  //color menu dropdowns
  if($(".prod_options select:not(.skip)").length){
    $(".prod_options select:not(.skip)").each(function(){
      $(this).before("<div id='wts-color-select-"+wtsNodeId+"' class='wts-color-select'><div>&nbsp;</div></div><div class='wts-color-spacer'>&nbsp;</div>");
      $(this).find("option").each(function(){
        temp = $(this).html();
        temp = temp.split(":");
        if(wtsColorMap[temp[0]]) {
          temp = wtsColorMap[temp[0]];
          temp = temp.split(",");
          wtsColor = temp[0];
          wtsBgColor = temp[1];
          swatch = "<span style='background-color:#"+wtsColor+"'>&nbsp;</span>";
        } else {
          swatch = wtsColor = "";
        }
        $("#wts-color-select-"+wtsNodeId).append("<a href='javascript:;' onclick='wtsColorSelect_select("+wtsNodeId+",this)'>"+swatch+$(this).html()+"</a>");
        if(wtsColor) {
          $("#wts-color-select-"+wtsNodeId+" a:last").data("wtsColor", wtsColor);
          $("#wts-color-select-"+wtsNodeId+" a:last").data("wtsBgColor", wtsBgColor);
        }
        $("#wts-color-select-"+wtsNodeId+" a:last").attr("title", $(this).val());
        $("#wts-color-select-"+wtsNodeId+" a:last").data("selectVal", $(this).val());
      });
      wtsColorSelect_select(wtsNodeId, $("#wts-color-select-"+wtsNodeId+" a:first"));
      $(this).css({ visibility: "hidden" });
      wtsNodeId++;
    })
    .after("<a id='wts-color-select-sheild' href='javascript:wtsColorSelect_hide();'>&nbsp;</a>");
    
    $(".wts-color-select")
      .click(function(){
        $(this).toggleClass("wts-color-select-active");
        if($(this).hasClass("wts-color-select-active")){
          $(".wts-color-select").not(this).removeClass("wts-color-select-active");
          $("#wts-color-select-sheild").show();
          
        } else {
          wtsColorSelect_hide();
        }
      })
      .keydown(function(event){
        if(event.keyCode==27) {
          wtsColorSelect_hide();
        }
      });
  } else {
    //if there is no dropdowns, this is not a vinyl product
    $("#vinyl-disclaimers").hide();
  }
  
  //form enhancements
  $("input, textarea")
    .focus(function(){ $(this).addClass("focus"); })
    .blur(function(){ $(this).removeClass("focus"); });
  $(".qty_field").bind("keydown keyup change mousedown mouseup", function(){
    thisval = $(this).val();
    thisval = thisval.replace(/[^0-9]/g, '') * 1;
    if(thisval<=0||!thisval) thisval = 1;
    $(this).val(thisval);
  });
  
  //wall art page
  if($("#prod_rn_350").length) {
    //$(".options .instruction:last").css("display", "");
    //$(".options .instruction:first").next().attr("colspan", 2).prev().remove();
  
    $("#wall-art-radios label").click(function(){
      $("#wall-art-radios label").removeClass("selected");
      $(this).addClass("selected");
      image = $(this).find("img").attr("src").replace("_image.", "_large_image.");
      $(".product_image img").stop().fadeTo(300, 0, function(){ $(".product_image img").attr("src", image); });
    });
    $(".product_image img").load(function(){ $(this).fadeTo(800, 1); });
    $("#wall-art-radios label:first").click();
  }
  
  //custom lettering page
  if($("#prod_rn_344").length) {
    
    //add stuff after the length field
    $(wtsNodes['width'])
      .attr('maxlength', 3)
      .after("&nbsp; Max 540 inches (15 yards)")
      .bind("keypress change", function(e){
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
          return false;
        } else {
          qtCalcUpdate();
          return true;
        }
      })
      .bind("blur", function(){
        thisVal = $(this).val();
        thisVal = thisVal.replace("/[0-9]/i", "");
        thisVal = $(this).val(thisVal);
        updateImagePreview();
      });
    
    //put text fields into one row
    $(wtsNodes['text']).parent().prev().parent().nextAll(":lt(3)").find("input").css("marginTop", "5px").clone(true).insertAfter(wtsNodes['text']);
    $(wtsNodes['text']).parent().parent().nextAll(":lt(3)").remove();
    
    //controls for line splitting
    $(wtsNodes['text']).parent().prev()
      .append(
        '<hr />'+
        '<p>'+
        '<br /><b>Split Lines</b><br />'+
        '<span class="split-buttons">'+
        '&nbsp;<button type="button" onclick="wlSplitLines(1);">1</button>'+
        '&nbsp;<button type="button" onclick="wlSplitLines(2);">2</button>'+
        '&nbsp;<button type="button" onclick="wlSplitLines(3);">3</button>'+
        '&nbsp;<button type="button" onclick="wlSplitLines(4);">4</button>'+
        '</span>'+
        '</p>'
      );
    
    //default text
    $(wtsNodes['text'])
      .data("defaultText", "Your text here")
      .focus(function() {
        if($(this).val()==$(this).data("defaultText")) {
          $(this).val("");
        }
      })
      .blur(function() {
        if(!$(this).val()) {
          $(this).val($(this).data("defaultText"));
        }
      });
    if($(wtsNodes['text']).val()=="") {
      $(wtsNodes['text']).val($(wtsNodes['text']).data("defaultText"));
    }
    $(wtsNodes['text']).nextAll(":last").after('<button type="button" onclick="updateImagePreview();">Update Preview Image</button><p>Need some ideas? Try our <a href="'+basedir+'/quotes">Quotes Library</a>');
    
    //font browser
    $(".prod_options tr:eq("+wtsNodes['fontRow']+") td:last *").hide();
    $(".prod_options tr:eq("+wtsNodes['fontRow']+")").after("<tr><td colspan='2'></td></tr>");
    $("#wl-fonts").clone(true).appendTo(".prod_options tr:eq("+(wtsNodes['fontRow']+1)+") td:last").attr("id","wl-temp");
    $("#wl-fonts").remove();
    $("#wl-temp").attr("id","wl-fonts");
    $("#wl-font-category-select").change(function(){
      selection = $(this).val().split(" ").join("");
      if(selection!="*") selection = "div.fgroup_"+selection;
      $("#wl-fonts div.browser div").hide();
      $("#wl-fonts div.browser "+selection).show();
      $("#wl-fonts div.browser").scrollTop(0);
    });
    $(wtsNodes['font']).parent().prev().append('<div id="wl-font-selected-name"></div>');
    
    //replace the req images with numbered images
    instructionImgs = $("td.opt_instruction img");
    for(i=0; i<instructionImgs.length; i++) {
      $(instructionImgs[i])
        .attr("src", themedir+"/images/num"+(i+1)+".gif")
        .addClass("step-num");
    }
    
    updateImagePreview();
    
  }
});