var j = jQuery.noConflict();

j(document).ready(function() {
  var initExtLinks = function() {
    var userExtLinks = j.cookie('userExtLinkOpt');
    var userExtLinksId = j.cookie('userExtLinkId');
    if(userExtLinks == "true") {
      swapLinks(true);
      j("#extLinks").val(["userLinkControl"]);
    } else {
      swapLinks(false);
    }
    if(userExtLinksId == "false") {
      idExtLinks(false);
    } else {
      idExtLinks(true);
      j("#extLinkId").val(["userLinkIdControl"]);
    }
  }
  var idExtLinks = function(idOn) {
    j(".mainCol a[href^=http]").each(function() {
      if(this.href.indexOf(location.hostname) == -1) {
        if(idOn == false) {
          j(this).removeClass('psuExternalLink');
        } else {
          j(this).not(':has(img)').addClass('psuExternalLink')
        }
      }
    });
    restoreDefaults();  
  }
  var swapLinks = function(extOn) {
    j("a[href^=http]").each(function(i){
      if(this.href.indexOf(location.hostname) == -1) {
        if(extOn == true) {
          j(this).attr('target', '_blank');
        } else {
          j(this).removeAttr('target');
        }
      }
    });
    restoreDefaults();
  }
  var restoreDefaults = function() {
    if(j("#restored").hasClass("hidden") == false) {
      j("#restored").fadeOut("slow", function(){ j("#defaults").fadeIn("slow", function(){ j("#restored").addClass("hidden");}); });
    }
    return false;
  }
  initExtLinks();

  j("#extLinks").click(function() {
    j.cookie('userExtLinkOpt', this.checked, { expires: 90, path: '/' });
    swapLinks(this.checked);
  });
  j("#extLinkId").click(function() {
    j.cookie('userExtLinkId', this.checked, { expires: 90, path: '/' });
    idExtLinks(this.checked);
  });
  j("#restoreDefault").click(function() {
    j.cookie('userExtLinkOpt', null);
    j.cookie('userExtLinkId', null);
    swapLinks(false);
    idExtLinks(true);
    j("#extLinkId").val(["userLinkIdControl"]);
    j("#defaults").fadeOut("slow", function(){ j("#restored").fadeIn("slow", function(){ j("#restored").removeClass("hidden");}); });
    return false;
  });
  j("#pageOptions").click(function() {
    function modalOpen (dialog) {
      dialog.overlay.fadeIn('slow', function () {
        dialog.container.fadeIn('slow');
        dialog.data.fadeIn('slow');
      });
    }
    j('#siteOptionsModal').modal({persist: true,position: ["30%",],onOpen: modalOpen});
    return false;
  });

  j('#pageOptions').show();
  j('#loginBlock').hide();
  j('#loginPop').mouseover(function() {
    j('#loginBlock').slideDown('fast');
  });
  j('#loginBlock').hover(function(){ },function() {
    j('#loginBlock').fadeOut('slow');
  });     

  j('.shareLinks').click(function() {
    j('#socialBookmarks').toggle();
  });

  j("li div.faqAnswer").hide();
  j("#faqArea li a.faqQuestion").click(
    function() {
      j(this).next(".faqAnswer").slideToggle(500);
      return false;
    }
  );
  j(".faqArea li a.faqQuestion").click(
    function() {
      j(this).next(".faqAnswer").slideToggle(500);
      return false;
    }
  );
});

j(function () {
  j('.bubbleInfo').each(function () {
    var distance = 10;
    var time = 250;
    var hideDelay = 200;
    var hideDelayTimer = null;
    var beingShown = false;
    var shown = false;
    var trigger = jQuery('.trigger', this);
    var info = jQuery('.popup', this).css('opacity', 0);

    j(trigger.get(0)).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      if (beingShown || shown) {
        // don't trigger the animation again
        return;
      } else {
        // reset position of info box
        beingShown = true;
        info.css({
        top: -97,
        left: -117,
        display: 'block'
      }).animate({
        top: '-=' + distance + 'px',
        opacity: .85
      }, time, 'swing', function() {
        beingShown = false;
        shown = true;
      });
    }
    return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {

                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

      }, hideDelay);
      return false;
    });
  });
});