activeSocialWindow = false;
twitterChange = false;
function showSocialWindow(socialmediatype) {
  // hide existing content first
  hideExisting();
  // then show social window if there's not currently a window open (if there is, close it instead)
  if (activeSocialWindow == socialmediatype)
  {
    var floatingfooterdiv = document.getElementById('floating_footer');
    floatingfooterdiv.style.display = "none";
    activeSocialWindow = false;
    return;
  }
  activeSocialWindow = socialmediatype;
  var floatingfooter = document.getElementById('floating_footer');
  floatingfooter.style.display = "block";
  adjustFloatingFooterPosition();
  // then load specific content
  switch (socialmediatype) {
    case 'twitter':
      twitterChange = setInterval('changeTwitterTitleText()', 80);
      showTwitterContent();
    break;
    case 'facebook':
      showFacebookContent();
    break;
    case 'linkedin':
      //document.getElementById('floating_footer').style.display = "none"; window.open('http://www.linkedin.com/companies/mcgarrah-jessee');
      showLinkedInContent();
    break;
  }
}

function adjustFloatingFooterPosition() {
  var floatingfooter = document.getElementById('floating_footer');
  if (floatingfooter.offsetTop < 604) {
    // window is small so adjust appearance
    var normalBottomSpacing = 62;
    floatingfooter.style.bottom = (0 - ((604-normalBottomSpacing) - floatingfooter.offsetTop)) + 'px';
  }
}
/*window.onresize = function(event) {
  adjustFloatingFooterPosition();
}*/

function hideExisting() {
  try { clearInterval(twitterChange); } catch(e){};
  // this will hide the existing social media modal window
  // and reset the divs in preparation of loading social medias
  var floatingfooterdiv = document.getElementById('floating_footer');
  var floatingfooterinsidediv = document.getElementById('ff_inside');
  // create new elements
  var newdiv = document.createElement('div');
  var newtwitterdiv = document.createElement('div');
  var newlinkedindiv = document.createElement('div');
  var newfacebookdiv = document.createElement('div');
  var newclosediv = document.createElement('div');
  // remove ff_inside (existing elements)
  floatingfooterdiv.removeChild(floatingfooterinsidediv);
  // add ff_inside again, this time will clean content divs
  floatingfooterdiv.appendChild(newdiv);
  newdiv.setAttribute('id', 'ff_inside');
  newdiv.innerHTML = '<table id="ff_inside_table"><tr><td class="tl"></td><td class="t"></td><td class="tr"></td></tr><tr><td class="l"></td><td class="m"><div id="tablecontentarea"></div></td><td class="r"></td></tr><tr><td class="bl"></td><td class="b"><div id="arrow"></div></td><td class="br"></td></tr></table>';
  var tablecell = document.getElementById('tablecontentarea');
  tablecell.appendChild(newtwitterdiv);
  newtwitterdiv.setAttribute('id', 'twitter-widget');
  tablecell.appendChild(newlinkedindiv);
  newlinkedindiv.setAttribute('id', 'linkedin');
  tablecell.appendChild(newfacebookdiv);
  newfacebookdiv.setAttribute('id', 'facebook');
  // add close button
  tablecell.appendChild(newclosediv);
  newclosediv.setAttribute('id', 'ff_close');
  // give close button an action when clicked
  newclosediv.onclick = function() {
    var floatingfooterdiv = document.getElementById('floating_footer');
    floatingfooterdiv.style.display = "none";
    activeSocialWindow = false;
  };
}

function showTwitterContent() {
  // load a new widget into the "twitter-widget" div
  /*new TWTR.Widget({
    version: 2,
    type: 'search',
    id: 'twitter-widget',
    search: 'from:mcgarrahjessee',
    interval: 4000,
    title: '',
    subject: 'Recent Tweets',
    width: 247,
    height: 250,
    theme: {
      shell: {
        background: '#ffffff',
        color: '#a5a5a7'
      },
      tweets: {
        background: '#ffffff',
        color: '#a5a5a7',
        links: '#0098d4'
      }
    },
    features: {
      scrollbar: false,
      loop: true,
      live: true,
      hashtags: true,
      timestamp: true,
      avatars: true,
      toptweets: true,
      behavior: 'default' // or 'all'
    }
  }).render().start();*/
  new TWTR.Widget({
    version: 2,
    type: 'profile',
    id: 'twitter-widget',
    rpp: 8,
    interval: 6000,
    width: 247,
    height: 250,
    theme: {
      shell: {
        background: '#ffffff',
        color: '#a5a5a7'
      },
      tweets: {
        background: '#ffffff',
        color: '#a5a5a7',
        links: '#0098d4'
      }
    },
    features: {
      scrollbar: true,
      loop: true,
      live: true,
      hashtags: true,
      timestamp: true,
      avatars: true,
      behavior: 'default' // or 'all'
    }
  }).render().setUser('mcgarrahjessee').start();
  // tweak position
  var thetable = document.getElementById('ff_inside_table');
  thetable.className = 'pos_twitter';
}

function showFacebookContent() {
  // facebook widget iframe
  var iFrameContents = '<iframe src="http://www.facebook.com/plugins/likebox.php?id=151014784911092&amp;width=280&amp;connections=0&amp;stream=true&amp;header=false&amp;height=395" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:280px; height:395px; display:block;" allowTransparency="true"></iframe>';
  // add iframe into div
  document.getElementById('facebook').innerHTML = iFrameContents;
  // tweak position
  var thetable = document.getElementById('ff_inside_table');
  thetable.className = 'pos_facebook';
}

function showLinkedInContent() {
  // load a new widget into the "linkedin" div
  new LinkedIn.CompanyInsiderBox("linkedin", "McGarrah Jessee");
  // tweak position
  var thetable = document.getElementById('ff_inside_table');
  thetable.className = 'pos_linkedin';
  // tweak iframe
  try {
    document.getElementById('linkedin').firstChild.setAttribute('scrolling', 'no');
    document.getElementById('linkedin').firstChild.setAttribute('height', '210');
  } catch (e) {};
}

function changeTwitterTitleText() {
  try {
    var headerdiv = getElementsByClassName(document,'div','twtr-hd');
    var h4tag = getElementsByTagNames('h4', headerdiv[0]);
    h4tag[0].innerHTML = 'Recent Tweets';
    h4tag[0].style.marginLeft = '0';
  } catch(e) {};
}

function getElementsByClassName(oElm, strTagName, strClassName){
  var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  strClassName = strClassName.replace(/\-/g, "\\-");
  var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  var oElement;
  for(var i=0; i<arrElements.length; i++){
      oElement = arrElements[i];
      if(oRegExp.test(oElement.getAttribute("class"))){
          arrReturnElements.push(oElement);
      }
  }
  return (arrReturnElements)
};
function getElementsByTagNames(list,obj) {
  if (!obj) var obj = document;
  var tagNames = list.split(',');
  var resultArray = new Array();
  for (var i=0;i<tagNames.length;i++) {
    var tags = obj.getElementsByTagName(tagNames[i]);
    for (var j=0;j<tags.length;j++) {
      resultArray.push(tags[j]);
    }
  }
  var testNode = resultArray[0];
  if (!testNode) return [];
  if (testNode.sourceIndex) {
    resultArray.sort(function (a,b) {
        return a.sourceIndex - b.sourceIndex;
    });
  }
  else if (testNode.compareDocumentPosition) {
    resultArray.sort(function (a,b) {
        return 3 - (a.compareDocumentPosition(b) & 6);
    });
  }
  return resultArray;
};
