$(document).ready(function() 
{

$(document).pngFix();

lightbox_links = $('a.lightbox');
for(var i=0; i < lightbox_links.length; i++) {
   var pof = lightbox_links[i];
   var code = pof['href'];
   var title = pof['title'] + '&nbsp;';
   $(pof).qtip({
      content: {
        title: {
text: title,
            button: '<img onclick="return false;" src="/images/modal-header-close.gif" style="padding-top: 5px;" alt="Close" />'
         },
text: '<img src="' + code + '" />'
},

      position: {
         target: $(document.body),
         corner: 'center'
      },
      show: {
when: 'click',
         solo: true,
effect: { type: 'fade', length: 0}
      },
      hide: false,
      style: { 
   border: {
      width: 8,
      radius: 6,
      color: '#729317'
   },
   background: '#fffdf6',
   color: '#000',
padding: 0,
width: 'auto'
},
api: {
         beforeShow: function()
         {
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         }
      }
   });

}

    $('a.lightbox').click(function() {
        return false;
    });

$('a.lightbox').removeAttr('title');




modal_links = $('a.modal');
for(var i=0; i < modal_links.length; i++) {
   var pof = modal_links[i];
   var code = pof['href'];

   if (pof['title'].length > 0) {
	   var title = pof['title'];
   } else {
   	var title = '<img src="/images/modal-header-logo.png" />';
   }

   $(pof).qtip({
      content: {
        title: {
	    text: title,
            button: '<img onclick="return false;" src="/images/modal-header-close.gif" style="padding-top: 5px;" alt="Close" />'
         },
url: code
},

      position: {
         target: $(document.body),
         corner: 'center'
      },
      show: {
when: 'click',
         solo: true,
effect: { type: 'fade', length: 0}
      },
      hide: false,
      style: { 
   border: {
      width: 8,
      radius: 6,
      color: '#729317'
   },
   background: '#fffdf6',
   color: '#000',
padding: 0,
width: 800
},
api: {
         beforeShow: function()
         {
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         }
      }
   });

}

    $('a.modal').click(function() {
        return false;
    });

$('a.modal').removeAttr('title');







$('a#contribute-cep').qtip({
      content: {
        title: {
text: '&nbsp;',
            button: '<img onclick="return false;" src="/images/modal-header-close.gif" style="padding-top: 5px;" alt="Close" />'
         },
url: '/index.php?page=make-a-contribution'
},
      position: {
         corner: {
         target: 'topRight',
         tooltip: 'bottomLeft'
},
adjust: {
x: 10
}
      },
      show: {
when: 'click',
         solo: true,
effect: { type: 'fade', length: 0}
      },
      hide: false,
      style: { 
   border: {
      width: 8,
      radius: 6,
      color: '#e88a2e'
   },
   background: '#fffdf6',
   color: '#000',
padding: 10,
width: 250
}
   });


$('a#contribute-cep').click(function() {
        this.blur();
        return false;
});




$('a#utils-share').qtip({
      content: {
url: '/tools/share.php?url=' + urlencode(window.location.href) + '&title=' + urlencode(document.title),
prerender: true
},
      position: {
         corner: {
         target: 'bottomRight',
         tooltip: 'topRight'
}
      },
      show: {
when: 'click',
         solo: true,
effect: { type: 'fade', length: 0}
      },
      hide: {
      	delay: 100,
      	fixed: true,
      	when: {
      		event: 'mouseout'
      	}
      },
      style: { 
   border: {
      width: 1,
      radius: 1,
      color: '#cccccc'
   },
   background: '#fff',
   color: '#000',
padding: 5,
width: 175
}
   });


$('a#utils-share').click(function() {
        this.blur();
        return false;
});






$('a#utils-email').qtip({
      content: {
        title: {
text: 'Email a Link',
            button: 'X'
         },
url: '/tools/email.php?url=' + urlencode(window.location.href) + '&title=' + urlencode(document.title) + '&rand=' + new Date().getTime()
},

      position: {
         target: $(document.body),
         corner: 'center',
         adjust: {
         	y: -30
         }
      },
      show: {
when: 'click',
         solo: true,
effect: { type: 'fade', length: 0}
      },
      hide: false,
      style: { 
   border: {
      width: 8,
      radius: 6,
      color: '#729317'
   },
   background: '#fffdf6',
   color: '#000',
padding: 10,
width: 500
},
api: {
         beforeShow: function()
         {
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         },
	 onHide: function(){
		$('a#utils-email').qtip("api").loadContent('/tools/email.php?url=' + urlencode(window.location.href) + '&title=' + urlencode(document.title) + '&rand=' + new Date().getTime());
	 }
        
      }
   });


    $('a#utils-email').click(function() {
        return false;
    });







   $('#maincontent a[href][title]').not('div.blog a[href][title]').qtip({
      content: {
         text: false
      },
position: {
   target: 'mouse',
   adjust: { mouse: false }
},
      hide: {
      	delay: 100,
      	fixed: true,
      	when: {
      		event: 'mouseout'
      	}
      },
      style: { 
   border: {
      width: 7,
      radius: 6,
      color: '#e88a2e'
   },
   background: '#fffdf6',
   color: '#000',
padding: '15px'

}
   });





$('tr.parent')
	.css("cursor","pointer")
	.attr("title","Click to expand/collapse")
	.click(function(){
		$(this).siblings('.child-'+this.id).each(function(i, elem) {
			$(elem).toggle($(elem).css('display') == 'none');
     		});
	});

$('tr[class^=child-]').hide().children('td');




$("#page-content a[href='#']").click(function() {
        this.blur();
        return false;
});






	$("#page-content a[href^='http://']").not("a[href^='http://new.effectivephilanthropy']").not("a[href^='http://www.effectivephilanthropy']").not("a[href^='http://effectivephilanthropy']").not("a[href^='http://staging.effectivephilanthropy']").click(function(){
		window.open(this.href,'external','width=900,height=500,location=1,status=1,scrollbars=1,menubar=1,resizable=1,toolbar=1');
		return false;
	}); 


$("#page-content a[href$='.pdf']").not('#page-content a.sidebar').not('#page-content li.dl-pdf a').append(' (.pdf)').attr("target", "_blank");


function publistchange() {
	var chal = document.getElementById('pubtypelist').value;
	var pubtype = document.getElementById('typelist').value;

	$("table.pubtable tr.no-message").hide();
	
	if (chal == '0' && pubtype == '0') {
		$("table.pubtable tr").not("table.pubtable tr.no-message").show();
	} else {
		$("table.pubtable tr.pub").hide();

		if (chal == '0') {
			$("table.pubtable tr.type-" + pubtype).show();
		} else if (pubtype == '0') {
			$("table.pubtable tr.pubtype-" + chal).show();
		} else {
			$("table.pubtable tr.pubtype-" + chal).filter("table.pubtable tr.type-" + pubtype).show();
		}
		
		var numOfVisibleRows = $('table.pubtable tr.pub:visible').length;
		if (numOfVisibleRows == 0) {
			$("table.pubtable tr.no-message").show();
		}
	}
}

$("#typelist").bind('change', publistchange);
$("#pubtypelist").bind('change', publistchange);


$("#yearlist").change(function(){
	if (this.value == '0') {
		$("table.cep-ylist tr").show();
	} else {
		$("table.cep-ylist tr.ylist").hide();
		$("table.cep-ylist tr.ylist-" + this.value).show();
	}
});



$('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: 0,
         left: 0,
         height: $(document).height(),
         width: '100%',
         opacity: 0.7,
         backgroundColor: 'black',
         zIndex: 5000
      })
      .appendTo(document.body)
      .hide();




CEP.rollover.init();

});

CEP = {};

CEP.rollover =
{
   init: function()
   {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', CEP.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', CEP.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', CEP.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_over' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_over\./, '.');
   }
};


function urlencode(str) {
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g, function (s) {
		switch (s) {
			case "*": s = "%2A"; break;
			case "+": s = "%2B"; break;
			case "/": s = "%2F"; break;
			case "@": s = "%40"; break;
			case "%20": s = "+"; break;
		}
		return s;
	}
	);
}
