/*
-------------------------------------------------
Click To Call Proactive Popup 
Copyright 2008-2009 GetStarts, Inc.
All Rights Reserved
-------------------------------------------------
_popupTimeout: set in milliseconds
_screenPosition: general screen position
("NONE", "ID", "TL", "TR", "BL", "BR")
_screenPositionX: specific x-coordinate for popup
(Will only be used if _screenPosition is set to "NONE" or "ID")
_screenPositionY: specific y-coordinate for popup
(Will only be used if _screenPosition is set to "NONE" or "ID")
_officeHours contains days of the week with their respective start and end times
-------------------------------------------------
*/

var _isPopped = false;
var _popupTimeout = 5000;     // Measured in milliseconds
var _screenPosition = "CENTER";    // "NONE", "ID", "CENTER", "TL", "TR", "BL", "BR"
var _referenceId = "ctcButton" // The ID of the DOM element over which to position the popup
var _screenPositionX = 0;      // Measured in pixels from left of screen
var _screenPositionY = 0;      // Measured in pixels from top of screen
var _ctcWidth = 130;           // Used to calculate exact positioning relative to the screen
var _ctcHeight = 140;          // Used to calculate exact positioning relative to the screen
var _titleBarHeight = 32;      // Height of the title bar and close [X] icon
var _margin = 2;               // Positional margin from edge of screen

/*
Time Zones (based on GMT):
5: Eastern
6: Central
7: Mountain
8: Pacific
*/
var _timeZone = 7;

var _officeHours = {
  "Monday": new Array("9:00","19:00"),
  "Tuesday": new Array("9:00","19:00"),
  "Wednesday": new Array("9:00","19:00"),
  "Thursday": new Array("9:00","19:00"),
  "Friday": new Array("9:00","16:00"),
  "Saturday": new Array("9:00","13:00")
}

var _isDuringOfficeHours = isDuringOfficeHours(_officeHours);
//_isDuringOfficeHours = true;

// Enable the hours of operation to be 24/7 for testing
// var _isDuringOfficeHours = true;

// --------------------------------------------------------------
// Functions
// --------------------------------------------------------------

function isDuringOfficeHours(_officeHours) {
  var _currentTime = new Date();
  var _dow = _currentTime.getDay();
  var _hours = _currentTime.getHours();
  var _minutes = _currentTime.getMinutes();
  var _timeZoneOffset = -_currentTime.getTimezoneOffset()/60;
  var _timeDifference = _timeZoneOffset + _timeZone;
  _hours = _hours.valueOf() - _timeDifference.valueOf();
    
  var _isDuringOfficeHours = false;
  var _dayOfWeek = getDowAsString(_dow);
  if (_dayOfWeek == null) {
    return false;
  }
  
  var _dayLookup = _officeHours[_dayOfWeek];
  
  if (_dayLookup == null) {
    return false;
  }

  _startBoundary = _dayLookup[0];
  _endBoundary = _dayLookup[1];
  
  var _startArray = _startBoundary.split(":");
  var _startHours = _startArray[0];
  var _startMinutes = _startArray[1];
  
  var _endArray = _endBoundary.split(":");
  var _endHours = _endArray[0];
  var _endMinutes = _endArray[1];

  if ((_hours >= _startHours) && (_hours <= _endHours)) {
    _isDuringOfficeHours = true;
    
    if ((_hours == _startHours) && (_minutes < _startMinutes)) {
      _isDuringOfficeHours = false;
    }
    else if ((_hours == _endHours) && (_minutes >= _endMinutes)) {
      _isDuringOfficeHours = false;
    }
  }
  
  return _isDuringOfficeHours;
}

function getDowAsString (_dow) {
  _dowString = "";
  switch (_dow) {
    case 0: _dowString = "Sunday"; break;
    case 1: _dowString = "Monday"; break;
    case 2: _dowString = "Tuesday"; break;
    case 3: _dowString = "Wednesday"; break;
    case 4: _dowString = "Thursday"; break;
    case 5: _dowString = "Friday"; break;
    case 6: _dowString = "Saturday"; break;
  }
  
  return _dowString;
}

function setPopup() {
  setTimeout("firePopup()",_popupTimeout);
}

function firePopup() {
  if ((_isPopped == false) && (_isDuringOfficeHours == true)) {
    showPopup();
  }
}

function showPopup() {
  var _popupFrame = document.getElementById("ctcFrame");
  var _popupContents = document.getElementById("ctcContents");

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  var _windowWidth = myWidth;
  var _windowHeight = myHeight;
  
  
  if (_popupFrame != null) {
    if (_popupFrame.style.display == "none") {
      switch (_screenPosition) {
        case "NONE":
          _popupFrame.style.top = _screenPositionY + "px";
          _popupFrame.style.left = _screenPositionX + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "ID":
          var _positionTop = 0;
          var _positionLeft = 0;
          var _referenceIdObject = document.getElementById(_referenceId);
          if (_referenceIdObject != null) {
            
            var _positionArray = findPos(_referenceIdObject);
            _positionLeft = _positionArray[0];
            _positionTop = _positionArray[1];
            
            if (isNaN(_positionTop)) _positionTop = 0;
            if (isNaN(_positionLeft)) _positionLeft = 0;
          }
          
          var positionHeight = _positionTop + _screenPositionY + _ctcHeight;
          var positionWidth = _positionLeft + _screenPositionX + _ctcWidth;
          //alert ("POS:" + positionHeight + ", WH: " + _windowHeight);
          
          if (positionHeight > (_windowHeight - _titleBarHeight)) {
            _popupFrame.style.top = (_windowHeight -_ctcHeight - _titleBarHeight -14) + "px";
          }
          else {
            _popupFrame.style.top = (_positionTop + _screenPositionY - _margin) + "px";
          }
          
          if (positionWidth > _windowWidth) {
            _popupFrame.style.left = (_windowWidth - _ctcWidth - 28) + "px";
          }
          else {
            _popupFrame.style.left = (_positionLeft + _screenPositionX + _margin) + "px";
          }
          
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "CENTER":
          _popupFrame.style.top = (Math.round((_windowHeight - _ctcHeight)/2)) + "px";
          _popupFrame.style.left = (Math.round((_windowWidth - _ctcWidth)/2)) + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "TL":
          _popupFrame.style.top = _margin + "px";
          _popupFrame.style.left = _margin + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "TR":
          _popupFrame.style.top = _margin + "px";
          _popupFrame.style.left = (_windowWidth - _ctcWidth - 12) + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "BL":
          _popupFrame.style.top = (_windowHeight -_ctcHeight - _titleBarHeight) + "px";
          _popupFrame.style.left = _margin + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;
        case "BR":
          _popupFrame.style.top = (_windowHeight - _ctcHeight - _titleBarHeight) + "px";
          _popupFrame.style.left = (_windowWidth - _ctcWidth - 28) + "px";
          _popupContents.style.width = _ctcWidth + "px";
          _popupContents.style.height = _ctcHeight + "px";
          _popupFrame.style.display = "block";
          break;  
      }
    }
    _isPopped = true;
  }
}

function hidePopup() {
  var _popupFrame = document.getElementById("ctcFrame");
  if (_popupFrame != null) {
    _popupFrame.style.display = "none";
  }
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } 
    while (obj = obj.offsetParent);
	  return [curleft,curtop];
  }
}

function getParameter(paramName) {
  paramName = paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+paramName+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function setIframeSRC() {
  var ctcValue = getParameter("ctc");
  var iframeObject = document.getElementById("statusFrame");
  var iframeSRC = "http://getcalls.getstarts.com/euinc/ctc/status?ctcid=" + ctcValue;
  iframeObject.src = iframeSRC;
}