
/*
 * Determines the timezone offset that a user is currently located in
 */
getTimeZone = function(){
   var currTime = new Date();
   var date1 = new Date(currTime.getFullYear(), 0, 1, 0, 0, 0, 0);
   var date2 = new Date(currTime.getFullYear(), 6, 1, 0, 0, 0, 0);
   var temp = date1.toGMTString();
   var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   var temp = date2.toGMTString();
   var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   
   return ((date1 - date3) / (1000 * 60 * 60));
}

/*
 * Given an encoded url param, unencode it into an array
 */
function parseParams(url) {
   var params=url;
   if (url.match(/\?(.+)$/)) {
       // in case it is a full query string with ?, only take everything after the ?
       params = RegExp.$1;
   }
  // split the params 
  var pArray = params.split("&");
  // hash to store result
  var pHash = {};
  // parse each param in the array and put it in the hash
  for(var i=0;i<pArray.length;i++) {
    var temp = pArray[i].split("=");
    pHash[temp[0]] = unescape(temp[1]);
  }
  return pHash;
}

/*
 * Generic utilty to read a cookie of a given name. Note name should be in all uppercase.
 */
function readCookie(name) {
  // Coldfusion cookies are all uppercase
  name = name.toUpperCase();
  
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

$(document).ready(function() {
  /* Document List Tree */
  $("#document-list").treeview({
  	collapsed: true,
  	animated: "fast"
  });
  
  $('#newsletter_textfield').focus(function(){
    $(this).val('');
  })
  
  /* Entering Site Popup
  
  var COOKIE_NAME = 'isa-popup-cookie';
	var go = $.cookie(COOKIE_NAME);
	if (go == null) {
		$.cookie(COOKIE_NAME, 1, { path: '/', expires: 1 });
	}
	else {
	  var value = $.cookie(COOKIE_NAME);
	  value++;
	  $.cookie(COOKIE_NAME, value);
	  if (value == 2) {
	    window.open('http://www.surveymonkey.com/jsPopInvite.aspx?sm=30yRixuxxf6hXvy6YdD76A%3d%3d', 'win', 'height=160,width=350,scrollbars=yes,resizable=yes,status=yes');
	  };
	}
 */

  /* Leaving Site Pop Up
  var noUnload = false;
  
  checkLink = function(url) {
    if (url.indexOf('http:') > -1 ) {
      if (url.indexOf('itaniumsolutions') > -1 ) {
        
      } 
      else {
		window.open('http://www.surveymonkey.com/jsPopInvite.aspx?sm=30yRixuxxf6hXvy6YdD76A%3d%3d', 'win', 'height=160,width=350,scrollbars=yes,resizable=yes,status=yes')
      };
    } 
    else {

    };
  }
  
  $('a').click(function() {
    noUnload = true;
    var url = $(this).attr('href');
    checkLink(url);
  });
  
  $(window).unload(function() {
    if (noUnload == false) {
      window.open('http://www.surveymonkey.com/jsPopInvite.aspx?sm=30yRixuxxf6hXvy6YdD76A%3d%3d', 'win', 'height=160,width=350,scrollbars=yes,resizable=yes,status=yes')
    };
  }); */

  
  $('#third_nav #browse_open_btn').click(function () { 
      $('#resource_widget_container').show('slow');
    return false;
    });
  $('#resource_widget_container #browse_close_btn').click(function () { 
      $('#resource_widget_container').hide('slow');
    return false;
    });
  $('#contact input#name').focus(function () { 
      if(this.value == 'Name'){
      this.value = '';
    }
    });
  $('#contact input#name').blur(function () { 
      if(this.value == ''){
      this.value = 'Name';
    }
    });
  $('#contact input#email').focus(function () { 
      if(this.value == 'Email Address'){
      this.value = '';
    }
    });
  $('#contact input#email').blur(function () { 
      if(this.value == ''){
      this.value = 'Email Address';
    }
    });
  $('#contact input#phone').focus(function () { 
      if(this.value == 'Phone'){
      this.value = '';
    }
    });
  $('#contact input#phone').blur(function () { 
      if(this.value == ''){
      this.value = 'Phone';
    }
    });
  $('div#ctnt_main a.email').each(function(i){
	  if(this.firstChild){
		address_to_replace=this.firstChild;
		real_address=address_to_replace.nodeValue.replace("(at)", "@");
		address_to_replace.nodeValue=real_address;
		address_to_replace.parentNode.setAttribute("href", "mailto:"+real_address);
	  }
  });
  /* Attach Google Analytics to all FILE links */
  $('a[@href$=.pdf]').click(function () {
    var linkName = $(this).text();
    urchinTracker('/resources/pdf/'+linkName+'');
  });
  $('a[@href$=.mp3]').click(function () {
    var linkName = $(this).text();
    urchinTracker('/resources/mp3/'+linkName+'');
  });
});

_uacct = "UA-3037308-1";
if(window.location.toString().indexOf('empirestaging')<0){
	urchinTracker();
}
function removeFile(src, itemID, type){
        if(confirm("Are you sure you want to delete this " + type + "?")){
          src.parentNode.removeChild(src);
          
          // Update the image source if present
          document.getElementById(itemID).value = 0;
            
        }
        return false;
      }
      
openWindow = function(src){
	window.open('/files/' + src);
	return false;
}