﻿var readyToChatDomain = "";
var currentReadyToChatWindowHeight = 365;
var readyToChatWindowWidth = "";
var readyToChatWindowHeight = "";
var readyToChatProxyFrameUrl = "http://www.readytochat.net/External/ChatWindowProxyFrame.htm#";
var readyToChatFrameUrl = "http://www.readytochat.net/External/ChatWindow.aspx?webID=";

function resizeChatWindow(newSize) {
  if (currentReadyToChatWindowHeight == 435) {
    sendMessageToFrame("elementSize=" + newSize);
  }
  else sendMessageToFrame("elementSizeNoBanner=" + newSize);
}

function closeChatWindow(newSize) {
  sendMessageToFrame("closeChat=" + newSize);
}

function sendMessageToFrame(message) {
  var elem = document.getElementById('innerFrameProxy');
  readyToChatProxyFrameUrl = "http://" + readyToChatDomain + "/External/ChatWindowProxyFrame.htm#";
  elem.contentWindow.location = readyToChatProxyFrameUrl + message;
  elem.width = elem.width > 50 ? 50 : 100;
}

$(document).ready(function() {
  $("#divReadyToChat").jqDrag(".jqDrag").jqResize(".jqResize");
});

//drag and drop chat iframe
(function($) {
  $.fn.jqDrag = function(h) { return i(this, h, 'd'); };
  $.fn.jqResize = function(h) { return i(this, h, 'r'); };
  $.jqDnR = { dnr: {}, e: 0,
    drag: function(v) {
      if (M.k == 'd') {
        if ($("#divReadyToChat").css("left") == "50%") {
          M.X = $(window).width() / 2;
        }
        E.css({ left: M.X + v.pageX - M.pX, top: M.Y + v.pageY - M.pY });
        $("#iframeChat").css({ display: 'none' });
        E.css('opacity', 0.6);
      }
      else {
        $("#iframeChat").css({ display: 'none' });
        readyToChatWindowWidth = v.pageX - M.pX + M.W;
        readyToChatWindowHeight = v.pageY - M.pY + M.H;
        E.css({ width: Math.max(readyToChatWindowWidth, 0), height: Math.max(readyToChatWindowHeight, 0) });
        E.css('opacity', 0.6);
        $("#divReadyToChatIFrame").css({ height: Math.max(readyToChatWindowHeight - 30, 0) });
        $("#iframeChat").css({ height: Math.max(readyToChatWindowHeight - 30, 0) });
      }
      return false;
    },
    stop: function() {
      E.css('opacity', 1);
      $('#divReadyToChat').show(function() {
        if ($.browser.msie) { this.style.removeAttribute('filter'); }
      });
      $().unbind('mousemove', J.drag).unbind('mouseup', J.stop);
      $("#iframeChat").css({ display: 'block' });
      resizeChatWindow(readyToChatWindowHeight);
    }
  };
  var J = $.jqDnR, M = J.dnr, E = J.e,
  i = function(e, h, k) {
    return e.each(function() {
      h = (h) ? $(h, e) : e;
      h.bind('mousedown', { e: e, k: k }, function(v) {
        var d = v.data, p = {}; E = d.e;
        // attempt utilization of dimensions plugin to fix IE issues
        if (E.css('position') != 'relative') { try { E.position(p); } catch (e) { } }
        M = { X: p.left || f('left') || 0, Y: p.top || f('top') || 0, W: f('width') || E[0].scrollWidth || 0, H: f('height') || E[0].scrollHeight || 0, pX: v.pageX, pY: v.pageY, k: d.k, o: E.css('opacityy') };
        E.css({ opacityy: 0.9 }); $().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
        return false;
      });
    });
  },
f = function(k) { return parseInt(E.css(k)) || false; };
})(jQuery);
//end drag&drop

// show chat iframe

var readyToChatWebsiteID;
var readyToChatCustomerID;
var readyToChatLanguageCode;
var readyToChatCustomerName;
var readyToChatAdditionalInformation;

function showChatDiv(websiteID, customerID, customerName, languageCode, additionalInformation) {
  // change window height for customers with banners
  readyToChatWebsiteID = websiteID;
  readyToChatCustomerID = customerID;
  readyToChatLanguageCode = languageCode;
  readyToChatCustomerName = customerName;
  readyToChatAdditionalInformation = additionalInformation;
  cleanReadyToChatHtmlCode();
  showChatDivInternal();
}

function cleanReadyToChatHtmlCode() {
  var myFrames = document.getElementsByName("iframeChat");
  var myFramesCount = myFrames.length;
  for (var i = myFramesCount - 1; i > 0; i--) {
    myFrames[i].parentNode.removeChild(myFrames[i]);
  }
}

function showChatDivInternal() {
  if (!readyToChatAdditionalInformation) readyToChatAdditionalInformation = "";
  if (document.getElementById("divReadyToChat").style.display == 'none') {
    readyToChatFrameUrl = "http://" + readyToChatDomain + "/external/ChatWindow.aspx?webID=";
    frames['innerFrameProxy'].location.href = "http://" + readyToChatDomain + "/External/ChatWindowProxyFrame.htm";
    frames['iframeChat'].location.href = readyToChatFrameUrl + readyToChatWebsiteID + "&custID=" + readyToChatCustomerID + "&info=" + readyToChatAdditionalInformation + "&languageCode=" + readyToChatLanguageCode;
    $("#divReadyToChat").css({ width: '420px', height: '360px' });
    $("#divReadyToChat").css("min-height", currentReadyToChatWindowHeight.toString() + "px");
    $("#iframeChat").css("min-height", (currentReadyToChatWindowHeight - 40).toString() + "px");
    $("#divReadyToChatIFrame").css("min-height", (currentReadyToChatWindowHeight - 40).toString() + "px");
    $("#divReadyToChatIFrame").css("display", "block");
    $("#maximizeReadyToChat").css("display", "none");
    $("#divReadyToChatIFrame").css({ height: Math.max(360 - 30, 0) });
    $("#iframeChat").css({ height: Math.max(360 - 30, 0) });
    switch (languageCode) {
      case "fr":
        document.getElementById("chatTitleBar").innerHTML = "En discussion avec: " + readyToChatCustomerName;
        break;
      case "nl":
        document.getElementById("chatTitleBar").innerHTML = "Talking with: " + readyToChatCustomerName;
        break;
      default:
        document.getElementById("chatTitleBar").innerHTML = "Talking with: " + readyToChatCustomerName;
        break;
    }
    $('#divReadyToChat').toggle("slow", function() {
      if ($.browser.msie) { this.style.removeAttribute('filter'); }
    });
  }
  else {
    alert('Ready To Chat is already running!');
  }
}

function hideChatDiv() {
  closeChatWindow("rtc");
  readyToChatWindowHeight = "330";
  $('#divReadyToChat').toggle("slow", function() {
    if ($.browser.msie) { this.style.removeAttribute('filter'); }
  });
}

function minimizeChatDiv() {
  $('#divReadyToChatIFrame').toggle("slow", function() {
    if ($.browser.msie) { this.style.removeAttribute('filter'); }
  });
  $("#jqResize").css("display", "none");
  $("#divReadyToChat").css("height", "28px");
  $("#divReadyToChat").css("min-height", "28px");
  $("#maximizeReadyToChat").css("display", "block");
  $("#minimizeReadyToChat").css("display", "none");
}

function maximizeChatDiv() {
  $('#divReadyToChatIFrame').toggle("slow", function() {
    if ($.browser.msie) { this.style.removeAttribute('filter'); }
  });
  $("#jqResize").css("display", "block");
  $("#divReadyToChat").css("min-height", currentReadyToChatWindowHeight.toString() + "px");
  $("#divReadyToChat").css("height", readyToChatWindowHeight);
  $("#minimizeReadyToChat").css("display", "block");
  $("#maximizeReadyToChat").css("display", "none");
}