// A couple of settings for AddThis.com
var addthis_pub="newsinfo";
var addthis_options = 'email, favorites, digg, delicious, google, facebook, twitter, stumbleupon, live, more';

$(document).ready(function(){ 
	if($('#mailerform').length > 0) {
	    data = $(":input[@name^='data']").map(function(){ return $(this).attr("name").replace(/data\[/, '').replace(/\]/, ''); });
	    var d = new Array();
	    jQuery.map(data, function(n) {if(jQuery.inArray(n, d) == -1) {d.push(n);}});
	    h = document.createElement("input");
	    h.name = 'ordered_input_names';
	    h.type = 'hidden';
	    h.value = d;
	    $('#mailerform').append(h);

	    $('#mailerform').submit(function() {
	        $('#submit').val('submitting...');
	        $('#submit').attr("disabled","disabled");
	    })
	}

	(function($) {
	  $.fn.toggler = function(options) {
	    var opts = $.extend({}, $.fn.toggler.defaults, options);
    
	    return this.each(function() {
	      this.togglee = $($(this).attr('href')).hide();
	      $(this).click(onClick);
	    });

	    function onClick() {
	      this.togglee[opts.animate ? 'slideToggle' : 'toggle']();
	      return false;
	    }
	  }
  
	  $.fn.toggler.defaults = {animate: false}
  
	  $(document).ready(function() {
	    $('a.toggler').toggler({animate:true});
	  });
	})(jQuery);
	
	showDateTime();
	fillSearch();

	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {  
		window.scrollTo(0,1);
	}
	
	$('a.print').each(function(){$(this).click(function(){window.print();return false;});}); // add window.print to a.print links
	
	$(function() {
		$('a.lightbox').lightBox(); // Select all links with lightbox class
	});
	
	if($('#fb_form').length > 0) { $('#fb_form').attr('target','_blank'); } // opens the feedburner forms in a new window... evil, i know
	
	shareThis(); /* AddThis.com */
});

function shareThis() {
		$('#share').bind('mouseover', function(){return addthis_open(this, '', '[URL]', '[TITLE]');})
		$('#share').bind('mouseout', function(){addthis_close();})
		$('#share').bind('click', function(){return addthis_sendto();})
}

function showDateTime() {
	var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	
	var currentTime = new Date();
        offset = currentTime.getTimezoneOffset() / 60;
	var hours = currentTime.getUTCHours() - offset;
	if(hours < 0) {hours += 24;}
	var minutes = currentTime.getUTCMinutes();
	var time = '';
	if (minutes < 10){minutes = "0" + minutes;}
	if(hours >= 12){ampm = 'p.m.';}
	else {ampm = 'a.m.';}
	if(hours >= 13){hours -= 12;}
	time = hours + ":" + minutes + ' ' + ampm + ' ';
	
	var month = currentTime.getMonth();
	var weekday = currentTime.getDay();
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var date = days[weekday] + ', ' + months[month] + ' ' + day + ", " + year;

	$('#date').html(date);
	$('#time').html(time + ' EDT - ');
	
	setTimeout('showDateTime()',1000);
}

function fillSearch() {
	if($('#search').length > 0) {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			if(hash[0] == 'topicid') $('#search').val('TopicID: ' + hash[1]);
		}
	}
}

function loadExternalNews() {
	if($('.rss-box-chronicle').length > 0) {
		//$('#feed_chronicle').html($('.rss-box-chronicle').html());
		$('#feed_nytimes').html($('.rss-box-nytimes').html());
		$('#feed_wsj').html($('.rss-box-wsjournal').html());
	}
}

// Homepage "ND Experts" tabs
(function($) {
  $.fn.experts = function() {
    return this.each(function() {
      this.togglee = $($(this).attr('href')).hide();
			this.togglee = $($(this).attr('href'));
      $(this).click(onClick);
    });

    function onClick() {
			$('#expert_info div').hide();
			$('#expert_list ul li a').fadeTo(1, 0.5);
			$(this).fadeTo(1, 1);
			this.togglee.show();
			$(this).parent().addClass('active');
			return false;
    }
  }

  $(document).ready(function() {
		$('#expert_list a').experts();
		$('#expert_list ul li a').fadeTo(1, 0.5);
		$('#expert_list li:first a').fadeTo(1, 1);
		$('#expert1').show();
  });
})(jQuery);

// Homepage "In the Headlines" tabs
(function($) {
	$.fn.headlines = function() {
		$('#headlines div').hide();
		return this.each(function() {
			// this.togglee = $($(this).attr('href')).hide();
			this.togglee = $($(this).attr('href'));
			$(this).click(onClick);
		});

		function onClick() {
			$('#headlines div').hide();
			$('#headline_tabs li').removeClass('active');
			$(this).parent().addClass('active');
			this.togglee.show();
			return false;
		}
	}

  $(document).ready(function() {
		$('#headline_tabs a').headlines();
		$('#headline_tabs li:first').addClass('active');
		//$('#feed_chronicle').show();
		$('#feed_nytimes').show();
  });
})(jQuery);

$(document).ready(function(){
    $.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=Oq7hrKf83RGzvc8Xpwt1Yg&_render=json&_callback=?",
			function(data) {
	      $.each(data.value.items, function(i,item){
					img = '/stylesheets/images/' + item['yweather:condition']['code'] + '.gif';
					text = item['yweather:condition']['text'];
					temp = item['yweather:condition']['temp'];
					$('#weather_icon').html('<img src="' + img + '" alt="' + text + '" />');
					$('#weather').html(text + ', ' + temp + '° F');
	      });

			}
		);
});