$(document).ready(function(){   
    var closed = true;
    $('#login-tab,#login-close').bind('click', function(){
        var t = closed ? '-0px' : '-199px';
        $('#login-wrapper').animate({top:t});
        closed = !closed;		            
    });   
    setEmailLinks();
});

function setEmailLinks()
{
    var text = $('#footer-email').html();
    $('#footer-email').html('<a href="mailto:'+$('#footer-email').attr('link')+'@sharefile.com" target="_blank">' + text + '</a>');
}

function showLayerPopup(id)
{
    document.getElementById(id).style.visibility = 'visible';
}

function hideLayerPopup(id)
{
    document.getElementById(id).style.visibility = 'hidden';
}

function getEmail(id, base)
{
    document.getElementById(id).innerHTML = '<a href="mailto:' + base + '@sharefile.com" target="_blank">' + base + '@sharefile.com</a>';
}

function doNav(url)
{
    location.href = url;
}

//Code to correctly save Google Analytics sources in first-party cookie
function createCookie (cName, cValue, cTime) {  
      var date = new Date(), expires;
      cName += "=";
      cName += cValue;
      if (cTime) {
            date.setTime(date.getTime()+cTime);
            cName += "; expires="+date.toGMTString();
      }
      cName += "; path=/";
      document.cookie = cName;
}

function readCookie (name) {        
      var nameRegex = RegExp("(?:; |^)" + name + "=([^;]+)"), nameValue; // match name of cookie and store value in $1
      nameValue = nameRegex.exec(document.cookie);                      
      return nameValue && nameValue[1];
}

function readSubCookie (name, vari) {     // function to read subcookie of passed value of __utmz
      var nameRegex = new RegExp("(?:\\||\\.)" + name + "=([^|]+)"), nameValue;
      nameValue = nameRegex.exec(vari);
      return nameValue && nameValue[1];
}

(function () {
      var rh = readCookie("GASources");
      var lastrh = rh && (rh.indexOf(",") > -1 ? rh.substring(rh.lastIndexOf(",")+1) : rh);
      var utmz = readCookie('__utmz');
      var src = readSubCookie('utmcid', utmz);
      var med = readSubCookie('utmcmd', utmz);
      src = src ? src : readSubCookie('utmcsr', utmz);
      var gclid = readSubCookie('utmgclid', utmz);
      if (gclid && src === "google") {
            src = "googlesearch";
      }
      if (med === "cpc" && src === "yahoo") {
            src = "yahoosearch";
      }
      if (src !== lastrh) {
            rh = rh ? rh + "," : "";
            rh += src;
            createCookie("GASources", rh, 15768000000);
      }
})();
