﻿//globals variables are set in the master view
var g_siteRoot;

jQuery(document).ready(function() {
  $("input[type='checkbox'],input[type='radio']").click(vickerman.ProductSelector.OnFilterChange);
})

if (typeof vickerman == 'undefined')
  vickerman = function() { };

vickerman.ProductSelector = function() { }

vickerman.ProductSelector.ResizeMe = function() {
  if (window.innerHeight) wh = window.innerHeight;
  else wh = (document.documentElement.clientHeight > 0) ? document.documentElement.clientHeight : document.body.clientHeight;
  if (wh < $(".fullwide").height())
    wh = $(".fullwide").height();
  $("body").height(wh);
}

vickerman.ProductSelector.__ParseReturnCode = function(reply) {
  return eval('(' + reply.getResponseHeader("X-ProductSelector-Json-ActionReturnCode") + ')');
}

vickerman.ProductSelector.OnFilterChange = function() {
  vickerman.ProductSelector.GetPage();
}

vickerman.ProductSelector.Sort = function(sort) {
  $("#sort").val(sort);
  vickerman.ProductSelector.GetPage();
}

vickerman.ProductSelector.GetPage = function(pageIndx) {
  $('#waitdialog').dialog('open');
  var params = $("#selectorfrm").serializeArray();
  params = $.merge([{ name: "sort", value: $("#sort").val()}], params);

  if (pageIndx)
    params = $.merge([{ name: "page_indx", value: pageIndx}], params);

  var url = $("#selectorfrm").attr("action");
  $.post(url,
    params,
    function(reply) {
      $('#waitdialog').dialog('close');
      $('#resultspanel').html(reply);
    });
}

vickerman.ProductSelector.ShareAdd = function(item) {
  $('#waitdialog').dialog('open');
  var reply = $.post(g_siteRoot + "/home/shareadd",
    { item_id: item },
    function(html) {
      $('#waitdialog').dialog('close');
      var rc = vickerman.ProductSelector.__ParseReturnCode(reply);
      if (!rc.Success) {
        alert(rc.Err);
        return;
      }
      $('#sharelistcontainer').html(html);
    });
  return false;
}

vickerman.ProductSelector.ShareClear = function(item) {
  $('#waitdialog').dialog('open');
  var reply = $.post(g_siteRoot + "/home/shareclear",
    { item_id: item },
    function(html) {
      $('#waitdialog').dialog('close');
      var rc = vickerman.ProductSelector.__ParseReturnCode(reply);
      if (!rc.Success) {
        alert(rc.Err);
        return;
      }
      $('#sharelistcontainer').html(html);
    });
  return false;
}

vickerman.ProductSelector.ShareByEmailDlg = function(anchor) {
  $('#waitdialog').dialog('open');
  $.get(g_siteRoot + '/home/ShareByEmailDlg',
    function(html) {
      $('#waitdialog').dialog('close');
      var pos = $(anchor).offset();
      $('#dialog').html(html);
      $('#dialog').dialog('option', 'title', 'Email SHARE LIST');
      $('#dialog').dialog('option', 'width', 350);
      $('#dialog').dialog('option', 'height', 320);
      //$('#dialog').dialog('option', 'position', [pos.left - 300, pos.top - 48]);
      $("a", "#dialog").button();
      $('#dialog').dialog('open');
      $('#dialog input[name=recipients]').focus();
    });
  return false;
}

vickerman.ProductSelector.ShareByFaxDlg = function(anchor) {
  $('#waitdialog').dialog('open');
  $.get(g_siteRoot + '/home/ShareByFaxDlg',
    function(html) {
      $('#waitdialog').dialog('close');
      var pos = $(anchor).offset();
      $('#dialog').html(html);
      $('#dialog').dialog('option', 'title', 'Fax SHARE LIST');
      $('#dialog').dialog('option', 'width', 350);
      $('#dialog').dialog('option', 'height', 320);
      //$('#dialog').dialog('option', 'position', [pos.left - 300, pos.top - 48]);
      $("a", "#dialog").button();
      $('#dialog').dialog('open');
      $('#dialog input[name=fax]').focus();
    });
  return false;
}

vickerman.ProductSelector.Share = function(frm) {
  $('#dialog').dialog('close');
  $('#waitdialog').dialog('open');

  var params = frm.serializeArray();
  var url = frm.attr("action");

  var reply = $.post(
    url,
    params,
    function(html) {
      $('#waitdialog').dialog('close');
      var rc = vickerman.ProductSelector.__ParseReturnCode(reply);
      if (!rc.Success) {
        alert(rc.Err);
        $('#dialog').dialog('open');
      }
    });

  return false;
}

vickerman.ProductSelector.ShowItemPic = function(partnum, img) {

  $('#dialog').html("<img src='http://www.vickerman.com/mmVIC1/Images/" + img + "' width='500' height='500'/>");
  $('#dialog').dialog('option', 'title', partnum);
  $('#dialog').dialog('option', 'width', 525);
  $('#dialog').dialog('option', 'height', 550);
  $('#dialog').dialog('open');

  return false;
}

