// Check to see if JavaScript is enabled
function squirrelmail_loginpage_onload() {
 document.forms[0].js_autodetect_results.value = '1';
}

// Open pages in new windows using a standards compliant method
// Simply add a 'rel="external"' attribute to a link and it will execute this code
// This is taken from a SitePoint article featuring this code:
// http://www.sitepoint.com/article/standards-compliant-world
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

function navHoverEffect() {
  var tabs = $$('div#tabs a');
  tabs.each(function(nextTab) {
    if (nextTab.getParent().hasClass('selected-level')) {
      nextTab.setStyle('color', '#fff');
    } else {
      nextTab.setStyle('color', '#d1dae2');
      var tabFx = new Fx.Style(nextTab, 'color', {
        duration: 350,
        wait: false
      });
      nextTab.addEvent('mouseenter', function() {
        tabFx.start('#fff');
      });
      nextTab.addEvent('mouseleave', function() {
        tabFx.start('#d1dae2');
      });
    }
  });

  var subTabs = $$('div#header li a');
  subTabs.each(function(nextTab) {
    if (nextTab.getParent().hasClass('selected-level')) {
      nextTab.setStyle('color', '#3a5063');
    } else {
      nextTab.setStyle('color', '#738ea3');
      var tabFx = new Fx.Style(nextTab, 'color', {
        duration: 350,
        wait: false
      });
      nextTab.addEvent('mouseenter', function() {
        tabFx.start('#3a5063');
      });
      nextTab.addEvent('mouseleave', function() {
        tabFx.start('#738ea3');
      });
    }
  });

  var emailLoginLinks = $$('div#email-login a');
  emailLoginLinks.each(function(nextLink) {
    nextLink.setStyle('color', '#678094');
    nextLink.removeEvents();
  });
}

function customSearch() {
  var closeContainer = new Element('div');
  closeContainer.setProperty('class', 'cse-closeResults');
  closeContainer.setHTML('<a title="Close search results"><span>&times;</span></a>');

  var resultsContainer = new Element('div');
  resultsContainer.setProperty('class', 'cse-resultsContainer');

  var resultsDiv = new Element('div');
  resultsDiv.setProperty('id', 'results_008669607253442300773:mym5qnyslce');
  resultsDiv.setStyle('display', 'none');

  closeContainer.injectInside(resultsDiv);
  resultsContainer.injectInside(resultsDiv);
  resultsDiv.injectAfter('search');

  $('query').setProperty('value', 'Search EduTech');
  $('query').addEvent('focus', function() {
    if ($('query').getProperty('value') == 'Search EduTech')
      $('query').setProperty('value',  '');
  });
  $('query').addEvent('blur', function() {
    if ($('query').getProperty('value') == '')
      $('query').setProperty('value', 'Search EduTech');
  });
  $('query').addEvent('submit', function() {
    return false;
  });
}

function displayPhishingAlert() {
  $('phishing-alert').setStyle('display', 'block');
  var alertSlider = new Fx.Slide('phishing-alert', {
    duration: 1500,
    onStart: function() {
      $('phishing-alert').getFirst().setStyle('background', '#fff');
    },
    onComplete: function() {
      setPhishingAlertColor($('phishing-alert').getFirst());
    }
  });
  alertSlider.hide();
  alertSlider.slideIn();
}

function setPhishingAlertColor(alertBox) {
  var alertFx = new Fx.Style(alertBox, 'background-color', {
    duration: 3000
  });
  alertFx.start('#ffebe8');
}

function eduwebGallery() {
  var allSpans = $$('span.gallery-span');
  for (var i = 0; i < allSpans.length; i++) {
    initGallerySpan(allSpans[i]);
  }
}

function initGallerySpan(nextSpan) {
  var spanArgs = nextSpan.id.split('.');
  var theme = spanArgs[0];
  var color = spanArgs[1];

  nextSpan.setStyle('color', '#36a');
  nextSpan.setStyle('text-decoration', 'underline');
  nextSpan.setStyle('cursor', 'pointer');

  nextSpan.addEvent('click', function() {
    //document.body.setStyle('cursor', 'wait');
    
    var nextImage = new Asset.image('/images/support/eduweb/' + theme + '/' + color + '-master.gif', {
      id: theme + '-img-next',
      onload: function() {
        //document.body.setStyle('cursor', 'auto');

        var imageFx = new Fx.Style(theme + '-img-next', 'opacity', {
          duration: 750,
          onComplete: function() {
            $(theme + '-img').setProperty('src', '/images/support/eduweb/' + theme + '/' + color + '-master.gif');
            if ($(theme + '-img-next'))
              $(theme + '-img-next').remove();
          }
        });
        
        $(theme + '-href').setProperties({
          'href'   : '/support/eduweb/gallery/' + theme + '/preview/' + color + '.html',
          'target' : '_blank'
        });
        
        imageFx.start(1);
      }
    });
    
    nextImage.setStyle('position', 'absolute');
    nextImage.setStyle('opacity', 0);
    nextImage.injectAfter(theme + '-img');
  });
}

window.onload = function() {
  externalLinks();
  navHoverEffect();
  customSearch();
  if (window.location == "http://www.edutech.nodak.edu/") {
    //$('phishing-alert').setStyle('display', 'none');
    //setTimeout('displayPhishingAlert()', 750);
  }
  if (window.location == "http://www.edutech.nodak.edu/support/eduweb/gallery/" ||
      window.location == "http://www.edutech.nodak.edu/support/eduweb/gallery/index.php")
    eduwebGallery();
};
