// -------------------------------------------------------------------------------------------------
// For dropdown menus
sfHover = function() {
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onfocus=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// -------------------------------------------------------------------------------------------------

function findField(frm, field){
	 for (var i=0;i<frm.length;i++){
	 	if (document.forms[frm].elements[i].name == field) return i;
	 }
	 return -1;
}

// -------------------------------------------------------------------------------------------------

function searchFocus(frm, field, emptyTxt){
	var fieldIdx = findField(frm, field);
	if (fieldIdx==-1) return;
	if (document.forms[frm].elements[fieldIdx].value == emptyTxt){
		document.forms[frm].elements[fieldIdx].value = '';
	}
}

// -------------------------------------------------------------------------------------------------

function searchBlur(frm, field, emptyTxt){
	var fieldIdx = findField(frm, field);
	if (fieldIdx==-1) return;

	if (document.forms[frm].elements[fieldIdx].value == ''){
		document.forms[frm].elements[fieldIdx].value = emptyTxt;
	}
	
	return false;
}

// -------------------------------------------------------------------------------------------------

// Globals for flash detection
// Major version of Flash required
var requiredMajorVersion = 7;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;

// -------------------------------------------------------------------------------------------------
// MOOTOOLS Functions.  http://www.mootools.net
// -------------------------------------------------------------------------------------------------

// 
//window.addEvent('domready', function(){
   
   
   
  // Magazine article page tooltips
  // -----------------------------------------------------------------------------------------------
  if ($('magarticle-side') || $('magarticle-main')) {
    // Dynamically apply the tooltip class to content images.
    var iTitle = '';
    if ($('magarticle-side')) {
      var sideImages = $('magarticle-side').getElements('img');
      $each(sideImages, function(el) {
        iTitle = el.getProperty('title')
        if (iTitle != '' && iTitle != null) {
          el.addClass('tooltip');
        }
      });
    }  
    if ($('magarticle-main')) {
      var mainImages = $('magarticle-main').getElements('img');
      $each(mainImages, function(el) {
        iTitle = el.getProperty('title')
        if (iTitle != '' && iTitle != null) {
          el.addClass('tooltip');
        }
      });
    }  
    // Generates the image tooltips.
    var Tips1 = new Tips($$('.tooltip'));
  }
  
  
  
  
  // Ambient navigation control
  // -----------------------------------------------------------------------------------------------
  if ($('ticker')) {
    setupTicker();
  }
  
   
   
   
  // Video display page
  // -----------------------------------------------------------------------------------------------
  if ($('morpher')) {
    Fx.Morph = Fx.Styles.extend({
      start: function(className){
        var to = {};
        $each(document.styleSheets, function(style){
          var rules = style.rules || style.cssRules;
          $each(rules, function(rule){
            if (!rule.selectorText.test('\.' + className + '$')) return;
            Fx.CSS.Styles.each(function(style){
              if (!rule.style || !rule.style[style]) return;
              var ruleStyle = rule.style[style];
              to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
            });
          });
        });
        return this.parent(to);
      }
    });
    Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
    Fx.CSS.Styles.extend(Element.Styles.padding);
    Fx.CSS.Styles.extend(Element.Styles.margin);
    Element.Styles.border.each(function(border){
      ['Width', 'Color'].each(function(property){
        Fx.CSS.Styles.push(border + property);
      });
    });
    var myMorph = new Fx.Morph('morpher', {duration:600, wait: false});
    // Morph bigger
    $('morph2').addEvent('click', function(e) {
      e = new Event(e);
      myMorph.start('morph2').addEvent('onComplete', function() {
      });
      e.stop();
      $('hidea').addClass('clearer');
      $('hd1').addClass('colspan-five');
      $('main_text').addClass('fullwidth');
    })
    // Morph smaller
    $('morph1').addEvent('click', function(e) {
      e = new Event(e);
      myMorph.start('morph1').addEvent('onComplete', function() {
      });
      e.stop();
      e = new Event(e);
      $('hidea').removeClass('clearer');
      $('hd1').removeClass('colspan-five');
      $('main_text').removeClass('fullwidth');
      e.stop();
    })
    
    // These are here for the flash call.  They do the same as above.
    larger = function() {
      myMorph.start('morph2').addEvent('onComplete', function() {
      });
      $('hidea').addClass('clearer');
      $('hd1').addClass('colspan-five');
      $('main_text').addClass('fullwidth');
    }
    smaller = function() {
      myMorph.start('morph1').addEvent('onComplete', function() {
      });
      $('hidea').removeClass('clearer');
      $('hd1').removeClass('colspan-five');
      $('main_text').removeClass('fullwidth');
    }
  }

// Functions to be called from within flash.  This is a workaround to call the mootools functions above.
function large() {
  larger();
}
function small() {
  smaller();
}




var tickerCount = 0;
var tickerOver = false;
var tickerInterval = 60000;

function setupTicker() {
  if (Fx) {
    theTicker = $("ticker");
    theTickerScroller = $("tickerScroller");
    theTicker.className += " enabled";
    theTicker.onmouseover = function() {
      tickerOver = true;
    }
    theTicker.onmouseout = function() {
      tickerOver = false;
    }
    theLIs = theTickerScroller.getElementsByTagName("li");
    if (theLIs.length > 0) {
      tEffect = new Fx.Style(theTickerScroller, "opacity", {duration:1500, onComplete:function() {
        var step = 35;
        newTop = "-" + (step * tickerCount);
        if (this.to == 0) {
          theTickerScroller.style.top = newTop + "px"
          tEffect.custom(0,1);
          tickerCount++;
        }
      }});
      tickerCount = $random(1,theLIs.length);
      setTimeout(tick, 5000);  // Make the first one appear quickly.
      setInterval(tick, tickerInterval);  // Then call subsequent ticks after the interval.
//    } else {
//      alert('There are no LIs.');
    }
  }
}
 
function tick() {
  if (!tickerOver) {
    if (tickerCount < theLIs.length) {
      tEffect.custom(1,0);
    } else {
      tickerCount = 1;
      tick();
    }
  }
}

// -------------------------------------------------------------------------------------------------

function openWin(wURL) {
  wName = 'monoclewin';
  w = '600'
  h = '600'
  features = 'scrollbars=1'
  if(screen.width){
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
  } else { 
    winl = 0;
    wint =0;
  }
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(wURL,wName,settings);
  win.window.focus();
}

// -------------------------------------------------------------------------------------------------

function get_rss_feed(sUrl) {
  //clear the content in the div for the next feed.
  $j("#feedContent").empty();
 
  //use the $j get to grab the URL from the selected item, put the results in to an argument for parsing in the inline function called when the feed retrieval is complete
  $j.get('rss_proxy.php?url='+sUrl, function(d) {
 
    iCnt = 0;
 
    //find each 'item' in the file and parse it
    $j(d).find('item').each(function() {
 
      iCnt++; 
      if (iCnt == 1) {
 
        //name the current found item this for this particular loop run
        var $item = $j(this);
        // grab the post title
        var title = $item.find('title').text();
        // grab the post's URL
        var link = $item.find('link').text();
        // next, the description
        var description = $item.find('description').text();
        //don't forget the pubdate
        var pubDate = new Date($item.find('pubDate').text());
        
        aMonths = new Array('January','Februrary','March','April','May','June','July','August','September','October','November','December');
        
        month = aMonths[pubDate.getUTCMonth()];
        day = pubDate.getUTCDate();
        year = pubDate.getUTCFullYear();
        
        theDate = month + ' ' + day + ', ' + year;
        
        var writer = $item.find('writer').text();
        var category = $item.find('category').text();
 
        var html = '';
        
        // now create a var 'html' to store the markup we're using to output the feed to the browser window
        //html += "<div class=\"entry\"><h2 class=\"postTitle\">" + title + "<\/h2>";
        //html += "<em class=\"date\">" + pubDate + "</em>";
        //html += "<p class=\"description\">" + description + "</p>";
        //html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";
        
        html += '<div class="post_meta">';
        html += '  <p><a href="' + link + '">' + title + '</a></p>';
        html += '  <div class="clear"></div>';
        html += '</div>';
        html += '<p>';
        html += '  ' + theDate + ' &mdash;' + category + ' <br />Writer: ' + writer;
        html += '</p>';
        html += '<div class="clear"></div>';
        html += '<p>' + description + '</p>';
        html += '<p><a href="' + link + '">Read more...</a></p>';
        html += '<div class="clear"></div>';
        
 
        //put that feed content on the screen!
        $j('#feedContent').append($j(html));  
        
      }
        
    });
  });
 
};    

  


