var strCurrProductCat            =  "2";
var strCurrDesignCat             = "89";
var g_lastSelectedPLDiv          = null;
var g_lastSelectedDLDiv          = null;
var g_strCurrProductCode         = "";
var g_productHoverTimeout        = null;
var g_designHoverTimeout         = null;

function embedBuildStage(strItemChangeType, strItemChangeData)
{
  var params    = { wmode: "transparent", quality: "best", scale: "exactfit", base: "/" };
  var flashvars = { fv_render_type: "LAYOUT", fv_render_width: 200, fv_render_height: 200, fv_item_change_type: strItemChangeType, fv_item_change_data: strItemChangeData };
  swfobject.embedSWF("/TSDFlashStage.swf", "build_stage_container", "100%", "340", "8.0.0", "", flashvars, params);
}

function GetBuildStage()
{
  return $('#build_stage_container').get(0);
}

function SetDesignListTitle(strText)
{
  if (strText.length > 0)
  {
    $('#design_list_title_container').html(unescape(strText.replace(/\+/g, " ")));
    $('#design_list_title').fadeIn("slow");
  }
  else
    $('#design_list_title').fadeOut("slow");
}

function SelectProductByCode_SideList(strCode)
{
  list.SelectItemByUID("pl_" + strCode, true);
}

function SelectDesignCategoryByID_SideList(strID)
{
  if (g_lastSelectedDLDiv)
    $(g_lastSelectedDLDiv).attr('class', '');
  if (!design_cat_list.SelectItemByUID("dl_" + strID, true))
  {
    //Item is not in a collapsible list, so select from the main list
    design_cat_list.DeselectAllItems();
    g_lastSelectedDLDiv = $('#dclnp_' + strID).addClass("selected");
  }
}

function GetCurrentProductCode()
{
  return (g_strCurrProductCode.length > 0) ? g_strCurrProductCode : null;
}

function callback_LoadStageSide(strProductCode)
{
  $('#build_stage_side_frame').attr('src', '/build_stage_side.aspx?m=1&productCode=' + escape(strProductCode));
}

function callback_RefreshSummary()
{
  var iframe = GetIFrameWindow("build_stage_summary_frame");
  if (iframe)
    iframe.location.reload();
}

function callback_ProductChange(strCode, strSizeCode, strPriColour, strSecColour)
{
  g_strCurrProductCode = strCode;
  setTimeout("_timeout_ProductChange('" + strCode + "', '" + strSizeCode + "', '" + strPriColour + "', '" + strSecColour + "');", 2000);
}

function _timeout_ProductChange(strCode, strSizeCode, strPriColour, strSecColour)
{
  var iframe = null;
  iframe = GetIFrameWindow("build_stage_side_frame");
  if (iframe)
  {
    iframe.highlightSizeByCode(strSizeCode);
    iframe.highlightColourByNames(strPriColour, strSecColour);
  }
  iframe = GetIFrameWindow("product_list");
  if (iframe)
    iframe.highlightProductByCode(strCode);
  SelectProductByCode_SideList(strCode);
}

function callback_FlashAlert(strMessage)
{
  if (strMessage != null && strMessage.length > 0 && strMessage.toUpperCase() != "NULL")
    alert(strMessage);
}

function ChangeProductCategory(strCategoryID)
{
  $('#product_list').attr('src', '/product_list.aspx?m=c&catID=' + escape(strCategoryID));
  strCurrProductCat = strCategoryID;
}

function ChangeDesignCategory(strCategoryID)
{
  $('#design_list').attr('src', '/design_list.aspx?m=STANDARD_C&catID=' + escape(strCategoryID));
  strCurrDesignCat = strCategoryID;
}

function DesignList_DesignMode()
{
  $('#design_list').attr('src', '/design_list.aspx?m=STANDARD_C');
}

function DesignList_TextMode()
{
  $('#design_list').attr('src', '/design_list_text.aspx');
}

function DesignList_UploadMode()
{
  $('#design_list').attr('src', '/design_list_upload.aspx');
}

function AddToBasket()
{
  var iQty = $('#basket_qty').val();
  var fValid = true;
  
  if (isNaN(iQty))
    fValid = false;
  else if (iQty < 1)
    fValid = false;
  if (fValid)
  {
    var bs = GetBuildStage();
    if (bs)
      fValid = bs.IsProductValid();
    if (fValid)
    {
      if (confirm("Please check the garment size before adding it to the basket.\n\nIf the size is correct, click OK to add it to the basket otherwise click Cancel and change the size above."))
        document.location.href = "/basket.aspx?bm=ADD&qty=" + escape(iQty);
    }
    else
      alert("Please select a product before adding it to the basket");
  }
  else
    alert("Please enter a number greater than zero in the box to the left");
}

function ViewBasket()
{
  document.location.href = "/basket.aspx?bm=SHW";
}

function FinishEditing()
{
  var fValid = true;
  var bs = GetBuildStage();
  if (bs)
    fValid = bs.IsProductValid();
  if (fValid)
    document.location.href = "/basket.aspx?bm=FIN";
  else
    alert("Please select a product before finishing");
}

function Product_ShowHoverInfo(strCode)
{
    $('#hover_product')
      .stop(true, true)
      .delay(300)
      .fadeIn("fast")
      .load('/hover_product.aspx?code=' + escape(strCode));
}

function Product_HideHoverInfo()
{
  ClearContainer($('#hover_product').stop(true, true).fadeOut("fast").get(0));
}

function Design_ShowHoverInfo(strCode)
{
  $('#hover_design')
    .stop(true, true)
    .delay(300)
    .fadeIn("fast")
    .load('/hover_design.aspx?code=' + escape(strCode));
}

function Design_HideHoverInfo()
{
  ClearContainer($('#hover_design').stop(true, true).fadeOut("fast").get(0));
}

function DesignToolbar_SelectByMode(strMode)
{
  var strSuffix = "null";
  switch (strMode.toUpperCase())
  {
    case "DESIGNS":
      strSuffix = "1";
      break;
    case "TEXT":
      strSuffix = "2";
      break;
    case "UPLOAD":
      strSuffix = "3";
      break;
  }
  $('#design_toolbar_img').attr('src', '/i/design_toolbar_' + strSuffix + '.jpg');
}

