jQuery(function() {
	jQuery('li label').autoWidth();

	// Colored ordered lists
	jQuery('ol').addClass('orderedList').children('li').wrapInner('<span></span>'); 
	
	// Automatically call out linked PDF documents so we don't surprise users
	jQuery('a').each(function(){
		var href = jQuery(this).attr('href');
		if(href.indexOf('.pdf') != -1){
			jQuery(this).addClass('pdf');
			jQuery(this).append(' (pdf)');
		}
	});
	
	if ( ($.browser.msie && $.browser.version > 6) || !$.browser.msie ) {
		jQuery('.btn').prepend('<span class="tl"></span><span class="tr"></span>');
		jQuery('.btn.narrow, .btn.donate, .btn.books').prepend('<span class="bl"></span><span class="br"></span>');
		jQuery('.btn.donate, .btn.books').append('<span class="arrow"></span>').bigClick();
    }
	
	jQuery('#contentSub blockquote p').smartQuotes();
	
	// Donate to a project
	jQuery("#chooseAProject a").click(function() {
		jQuery('div#countryContent').load(this.href + " #countryContent > *",
				{},
				function(responseText, textStatus, xmlHttpRequest) {
					setup();
				}
			);
		donateToProject.scrollTo( jQuery('div#countryContent') );
		return false;
	});
	
	// ---------------------------------------------------------------------
	//Homepage Image Fader 
	jQuery('#homepageImg').cycle({
		timeout: 5000,
		pause: 1
	});
	
	//Jquery Slider
	jQuery('form#donateAmount .inputText').val("75");
	jQuery('img#donate-value-2').show();
	jQuery('#slider').slider({
		startValue: 84, 
		min: 0, 
		max: 100,
		slide: function(e, ui){
	
			function currentValue( maxval, minval, currentImg, dollarValue ){
				if(ui.value < maxval && ui.value > minval){
					currentImg.fadeIn('fast');
					var altText = currentImg.attr('title');
					jQuery('form#donateAmountDescription p').empty().append('= ' + altText);
					jQuery('form#donateAmount .inputText').val(dollarValue);
				}
				
				else {
					currentImg.fadeOut('fast');
				}
			}
			
			currentValue( 97, 87, jQuery('img#donate-value-1'), "50" );
			currentValue( 87, 76, jQuery('img#donate-value-2'), "75" );
			currentValue( 76, 66, jQuery('img#donate-value-3'), "100" );
			currentValue( 64, 57, jQuery('img#donate-value-4'), "210" );
			currentValue( 54, 44, jQuery('img#donate-value-5'), "500" );
			currentValue( 43, 33, jQuery('img#donate-value-6'), "1000" );
			currentValue( 32, 22, jQuery('img#donate-value-7'), "2500" );
			currentValue( 21, 11, jQuery('img#donate-value-8'), "4750" );
			currentValue( 11, 0, jQuery('img#donate-value-9'), "9500" );
		}
				
		
	});
	
	// Alternating li colors
	jQuery("ul.honorAFriendGifts li:odd").addClass("odd");
});
jQuery(window).load(function() {
	setup();
});
function setup() {
	// ---------------------------------------------------------------------
	// Homepage Story Hover JS
	
	jQuery('.story:first').before('<div id="storySummary"><ul></ul></div>')
	
	var stories = jQuery('.story');
	
	stories.each(function(i){
		var li = document.createElement("li");
		jQuery('#storySummary ul').append(li);
		jQuery(li).append( jQuery("h3 a", jQuery(this)).clone() );
		jQuery("a", jQuery(li)).hover(
				function() {
					switchCurrentStory(i);
				}, function() {
				}
		);
	});
	
	var successAnchor = jQuery("a.archive").clone();
	jQuery("a.archive").remove();
	jQuery('#storySummary li:last').after(successAnchor);
	
	// Cycle through all stories to determine the tallest one
	var maxHeight = 0;
	for ( var i=0; i < stories.size(); i++ ) {
		switchCurrentStory(i);
		var currHeight = jQuery("#storySummary").height();
		if ( currHeight > maxHeight ) {
			maxHeight = currHeight;
		}
	}
	jQuery("#storySummary").height(maxHeight);
	
	// Hide stories. We can't do stories.hide() because the images then say they are 0x0
	stories.css({position: 'absolute', left: '-10000px'});
	
	// Display the first story
	switchCurrentStory(0);

	// ---------------------------------------------------------------------
	// Image captions
	jQuery('#contentMain img').imageCaptions();
}


function switchCurrentStory(i) {
	var story = jQuery('.story').get(i);

	var img = jQuery("img", story);
	if ( jQuery('#storySummary img').size() > 0 ) {
		jQuery('#storySummary img').replaceWith( img.clone().css({width: img.width(), height: img.height()}) );
	} else {
		jQuery('#storySummary ul').before( img.clone().css({width: img.width(), height: img.height()}) );
	}
	if ( jQuery('#storySummary .storySummary').size() > 0 ) {
		jQuery('#storySummary .storySummary').replaceWith( jQuery(".storySummary", story).clone() );
	} else {
		jQuery('#storySummary ul').after( jQuery(".storySummary", story).clone() );
	}
	if ( jQuery('#storySummary h3').size() > 0 ) {
		jQuery('#storySummary h3').replaceWith( jQuery("h3", story).clone() );
	} else {
		jQuery('#storySummary ul').after( jQuery("h3", story).clone() );
	}
	if ( jQuery('#storySummary h3').size() > 0 ) {
		jQuery('#storySummary h3').replaceWith( jQuery("h3", story).clone() );
	} else {
		jQuery('#storySummary ul').after( jQuery("h3", story).clone() );
	}
}

jQuery.fn.imageCaptions = function() {
	
	this.each(function(){
		var text = jQuery(this).attr('alt');
		var cssClass = jQuery(this).attr('class');
		var alignAtt = jQuery(this).attr('align'); // Okay so "align" attribute may not be preferable but it is what the CMS sets.

	    if( !text ) return;
	    
    	var parent = jQuery(this).parents('a')[0];
        
    	jQuery(this).attr({hspace: "", align: ""}); // alt: ""
    	
    	var outerCSS = new Array();
    	outerCSS["width"] = jQuery(this).width();
    	if( alignAtt != undefined || alignAtt != "" ) outerCSS["float"] = alignAtt;
    	
    	var stuffToWrap = (parent)? jQuery(this).parent() : jQuery(this);
    	stuffToWrap.wrap('<div class="imageCaption '+ cssClass +'"></div>')
			.parent()
			.css( outerCSS )
			.append('<p>'+ text +'</p>');
	});
	
}



jQuery.fn.smartQuotes = function(options)
{
	var settings = {
		cssOpen: 'quoteOpen',
		cssClose: 'quoteClose'
	}
		
    if(options) {
        jQuery.extend(settings, options);
    };
    
    var length = 0;
    var text = '';
    var replaceFirst = false;
    var replaceLast = false;
    
    this.each(function(){
    	
    	quote = jQuery(this).text();    	
    	length = quote.length;
    	
    	firstChar = quote.substr(0, 1);
    	lastChar = quote.substr(-1);
    	
    	// see if the first and/or last characters are quotes, if they are, strip them from the text
    	if(firstChar == '"' || firstChar == 'â€œ') { 
    		quote = quote.substr(1, length);
    	}
    	if(lastChar == '"' || lastChar == 'â€�') { 
    		quote = quote.substr(0, length-2);
    	}
    	
    	// wrap the quote with proper quotes
    	jQuery(this)
    		.text(quote)
    		.prepend('<span class="'+ settings.cssOpen +'">&#8220;</span>')
    		.append('<span class="'+ settings.cssClose +'">&#8221;</span>');
	
    });
}

jQuery.fn.autoWidth = function(options) 
{
    var settings = {
    	minWidth	 	: false,
        limitWidth  	: false,
        ignore	: ''
    }
	
    if(options) {
        jQuery.extend(settings, options);
    };
    
    var maxWidth = 0;
    
	this.not(settings.ignore).each(function(){
        if (jQuery(this).width() > maxWidth){
        	if(settings.limitWidth && maxWidth >= settings.limitWidth) {
        		maxWidth = settings.limitWidth;
        	} 
        	else if(settings.minWidth && maxWidth <= settings.limitWidth)
        	{
        		maxWidth = settings.minWidth;
        	} 
        	else 
        	{
        		maxWidth = jQuery(this).width();
        	}
        }
	});	 
	
	this.not(settings.ignore).width(maxWidth);
}

jQuery.fn.bigClick = function(options)
{
	var settings = {
		cssClass : 'clickable'
    }
	
	if(options) {
        jQuery.extend(settings, options);
    };
	
	this.each(function(){
		var anchor = jQuery(this).find('a')[0];
		
		if(!anchor)
			return;			
		
		var href = jQuery(anchor).attr('href');
		var title = jQuery(anchor).attr('title');
		
		jQuery(this).attr('title', title);
		
		jQuery(this).mouseover(function(){
			jQuery(this).css('cursor', 'pointer');
		});
		
		jQuery(this).addClass(settings.cssClass).click(function(){
			window.location = href;
		});
	});
}

