var asset_dir = 'http://static.safehaven.com/images/';
var cache = new Array();

$(document).ready(function() {

	$("div.IAB_fullbanner > a").addClass('no-external');
	
	$('a[rel!=feed][type!=linked][class!=no-external]').filter(function() {
		return this.hostname && this.hostname !== location.hostname && this.hostname !== 'www.addthis.com' && this.hostname !== 'static.safehaven.com' && this.hostname !== 'prova.safehaven.com' && this.hostname !== 'www.amazon.com';
	}).append('<span class="icon external-link"></span>').click(function(){
			window.open(this.href);
			_gaq.push(['_trackEvent', 'external_link', 'click', this.href]);
			return false;
	});
	 
	$(".text_input").focus(function() {
  		if (this.value == this.defaultValue) {
  			this.value = '';
  			this.style.color = '#000';
  		}
  	}).blur(function() {
  		if (this.value == '') {
  			this.value = (this.defaultValue ? this.defaultValue : '');
  			this.style.color = '#999';
  		}
	}).css("color", '#999');
	
	$('a[rel=print]').live('click', function() {
  		window.open(this.href, 'Print Article', 'scrollbars=1,width=780,height=600');
  		_gaq.push(['_trackEvent', 'print', 'click']);
  		return false;
	});
	
	$('a.toggle').each(function() {
		var type = $(this).attr('type');
		$(this).toggleClass(type + '-plus').toggleClass(type);
	});
		
	function toggleIcon(article, type){
		$('a.toggle[rel="' + article + '"]').toggleClass(type + '-plus').toggleClass(type + '-minus');	
	}
	
	$('a.toggle').click(function () {
		_gaq.push(['_trackEvent', 'abstract', 'click', $(this).attr('rel')]);
		toggleAbstract($(this).attr('rel'));
		toggleIcon($(this).attr('rel'), $(this).attr('type'));
		return false;
	});
			
	function toggleAbstract(article){
		if(undefined === cache[article]) {
			var abstract_html = $('div.abstract[title=Article ' + article + ']').html();	
			$.ajax({
			   type: 'GET',
			   url: 'http://' + location.hostname + '/api/get_abstract/' + article,
			   dataType: 'html',
			   success: function(response){
			   	cache[article] = response;
		         $('div.abstract[title=Article ' + article + ']').html(response + abstract_html).toggle('slow');
		      }
			 });
		} else {
			$('div.abstract[title=Article ' + article + ']').toggle('slow');
		}
	}
	
	$('table[bgcolor], tr[bgcolor], td[bgcolor]').each(function(){
		var bgcolor = $(this).attr('bgcolor');
		$(this).css('background-color', bgcolor).removeAttr('bgcolor');
	});
	
});
