/*
-------------------------------------------------
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 = true;
var _popupTimeout = 3000;     // Measured in milliseconds
var _screenPosition = "ID";    // "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 = 160;          // 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

var _officeHours = {
  "Monday": new Array("8:00","20:00"),
  "Tuesday": new Array("8:00","20:00"),
  "Wednesday": new Array("8:00","20:00"),
  "Thursday": new Array("8:00","18:00"),
  "Friday": new Array("8:00","16:30")
}

var _isDuringOfficeHours = isDuringOfficeHours(_officeHours);

function isDuringOfficeHours(_officeHours) {
  var _currentTime = new Date();
  var _dow = _currentTime.getDay();
  var _hours = _currentTime.getHours();
  var _minutes = _currentTime.getMinutes();
  
  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 _windowWidth = window.innerWidth;
  if (_windowWidth == null || _windowWidth == 0) {
    _windowWidth = document.body.clientWidth;
  }
  var _windowHeight = window.innerHeight;
  if (_windowHeight == null || _windowHeight == 0) {
    _windowHeight = document.body.clientHeight;
  }
  
  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;
          }
          _popupFrame.style.top = (_positionTop + _screenPositionY) + "px";
          _popupFrame.style.left = (_positionLeft + _screenPositionX) + "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 - 26) + "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";
  }
  else { 
    _popupFrame = parent.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;
}