﻿$(window).load(function () {
  // give the live chat script enough time to load the invite html into the DOM before we start messing with it.
  $('#sectionStripe').animate({
    opacity: 1.0
  }, 1000, function () {
    initCustomChat();
  });
});

function initCustomChat(){
// fix that absolutely ridiculously bad way that live person forces the html of their invites to be generated.
//
	$(window).live('scroll', function(){
		$(this).trigger('mousemove');																 
	});
	$(window).live('mousemove', function(){
		$(this).trigger('scroll');																 
	});
	$("body").live('mousemove', function () {
	  // since the id is different every time, we have to resort to this to isolate what we need to edit.
	  chatInvite = $(this).find("div:first");
	  var isChatInvite = $(chatInvite).attr("id");

	  if (isChatInvite != "sectionStripe") {

	    // prevent chat invite flicker on page scroll
	    $(window).bind("scroll", function (event) {
	      var invState = chatInvite.css("left");
	      if (invState != "10px") {
	        $(chatInvite).addClass("showme");
	        //GAevent('live chat','invite','bottom','');
	      }
	      // we only want to do this on the first scroll - not every time.
	      $(this).unbind(event);
	    });

	    // add a variable to the user (session scope) so we can see what else they did after prompted / helped.
	    GAcustomVar(3, 'chat_invite', 'prompted', 2);
	    GAevent('live_chat', 'invite', 'prompt'); // track the event

	    // unbind these events since the invite has already occurred.
	    $("body").die('mousemove');
	    $(window).die('mousemove');
	    $(window).die('scroll');

	    // add a new inner-wrapper
	    var newwrap = "<div id='newwrap' />";
	    $(chatInvite).wrapInner(newwrap);

	    $(chatInvite).addClass("mylayer showme").show(); // oh i dunno, maybe i actually want a unique identifier on this wrapper instead of a randomly generated one!!!

	    // if they click something within the invite...
	    $(chatInvite).find("a").click(function () {
	      var whichButton = $(this).attr("class");
	      if (whichButton == "lpInviteChatHrefAccept") {
	        // they accepted the live chat invitation
	        // update chat_invite variable for the user so we can see what else they did after they accepted.
	        GAcustomVar(3, 'chat_invite', 'accepted', 2);
	        GAevent('live_chat', 'invite', 'accept');

	        $(chatInvite).hide();
	        // we also want to unbind the scroll method so that it doesn't keep reappearing
	        $(window).unbind("scroll");
	      }
	      if (whichButton == "lpInviteChatHrefClose") {
	        // they declined the invitation
	        // update chat_invite variable for the user so we can see what else they did after they declined.
	        GAcustomVar(3, 'chat_invite', 'declined', 2);
	        GAevent('live_chat', 'invite', 'reject');
	        // since they are declining, we want the div to dissapear
	        $(chatInvite).hide();
	        // we also want to unbind the scroll method so that it doesn't keep reappearing
	        $(window).unbind("scroll");
	      }
	    });

	    // make it a bit easier to test this thing
	    var path = location.href;
	    var debug = path.indexOf("debug");
	    try {
	      if (debug != "-1") {
	        //console.info("Fpweb.net developer detected... showing hidden elements now");
	        $(chatInvite).css("visibility", "visible");
	      } else {
	        // console.warn("chat module did not load properly.")
	      } // END if(debug)
	    } catch (err) {
        // do nothing
	    }

	  } // END if(isChatInvite)

	});  // END body.live(mousemove)
	
} // END initCustomChat()

// LIVE CHAT CONSOLE
try {
  var lpMTagConfig = { 'lpServer': "server.iad.liveperson.net", 'lpNumber': "82666039", 'lpProtocol': (document.location.toString().indexOf('https:') == 0) ? 'https' : 'http' };
  function lpAddMonitorTag(src) {
    if (typeof (src) == 'undefined' || typeof (src) == 'object') {
      src = lpMTagConfig.lpMTagSrc ? lpMTagConfig.lpMTagSrc : '/hcp/html/mTag.js';
    } if (src.indexOf('http') != 0) {
      src = lpMTagConfig.lpProtocol + "://" + lpMTagConfig.lpServer + src + '?site=' + lpMTagConfig.lpNumber;
    } else {
      if (src.indexOf('site=') < 0) {
        if (src.indexOf('?') < 0) {
          src = src + '?';
        } else {
          src = src + '&';
          src = src + 'site=' + lpMTagConfig.lpNumber;
        }
      }
    };
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('charset', 'iso-8859-1');
    s.setAttribute('src', src);
    document.getElementsByTagName('head').item(0).appendChild(s);

  }
  if (window.attachEvent) {
    window.attachEvent('onload', lpAddMonitorTag);
  } else {
    window.addEventListener("load", lpAddMonitorTag, false);
  }
} catch (err) {
  //console.warn("error loading chat console script: " + err);
  // TODO: Add Hoptoad Error Here (LiveChat Script Error)
}
// END LIVE CHAT CONSOLE
