/* 
 * Global
*/
$j = jQuery.noConflict();

/* 
 * Navigation fix
*/
$j(function() {
	var w;
	var width = 150;	
	$j('.nav>li').hover(function() {
		eid = $j(this).attr('id');
		w = $j('#' + eid).outerWidth(true);
		offset = 0;
		offset = parseInt((width - w)/2);
		$j(this).children('ul').css('left','-' + offset + 'px');
	},function() {
		$j(this).children('ul').css('left','-999em');
	});
	
});

/*
 * Search Box
*/
	$j(function() {
		$j('.search input').bind('focus',function() {
			$j(this).val('');
		});
		
		$j('.search input').bind('blur',function() {
			if($j(this).val() == "") {
				$j(this).val('Search…');
			}
		});
	
	});
	
/*
 * Fund Button
*/
	$j(function() {
		$j('#feature #action .ctas .fund').bind('mouseenter',function() {
			$j(this).addClass('active');
			$j('#feature #action #donation-graphic').fadeIn('slow');
		});
		
		$j('#feature #action .ctas .fund').bind('mouseleave',function() {
			$j(this).removeClass('active');
			$j('#feature #action #donation-graphic').fadeOut('slow');
		});
	});
	
/*
 * Home Page Slider
*/
	var ss;
	var isStopped = false;
	$j(function() {
		if($j('#viewer').length) {
			$j('#viewer').imagesLoaded( function( $images ) {
				$j('#viewer #viewer-nav').animate({
					opacity: '1.0'
				});
				$j('#viewer .loading').fadeOut(function() {
					$j('#viewer .active').fadeIn();
				});
				ss = window.setTimeout('playSlideShow()',5000);
			});
	
			$j('#viewer-nav li').bind('click',function() {
				id = $j(this).attr('id').substr(4);
				if(!$j('#slides #slide-' + id).hasClass('active')) {
					$j('#slides .active').fadeOut('slow');
					$j('#slides #slide-' + id).fadeIn('slow');
					$j('#slides .active').removeClass('active');
					$j('#slides #slide-' + id).addClass('active');
					$j('#viewer-nav .active').removeClass('active');
					$j(this).addClass('active');
					window.clearTimeout(ss);
					isStopped = true;
				}	
			});
			
			$j('#viewer-nav .next').bind('click',function() {
					var max = $j('#slides .slide').length;
					if(max == $j('#viewer-nav li.active').attr('id').substr(4)) {
						id = 1;
					} else {
						id = $j('#viewer-nav li.active').next().attr('id').substr(4);
					}
					$j('#slides .active').fadeOut('slow');
					$j('#slides #slide-' + id).fadeIn('slow');
					$j('#slides .active').removeClass('active');
					$j('#slides #slide-' + id).addClass('active');
					$j('#viewer-nav .active').removeClass('active');
					$j('#viewer-nav li#nav-' + id).addClass('active');
					window.clearTimeout(ss);
					isStopped = true;
			});
			
			$j('#viewer-nav .prev').bind('click',function() {
					var max = $j('#slides .slide').length;
					if(1 == $j('#viewer-nav li.active').attr('id').substr(4)) {
						id = max;
					} else {
						id = $j('#viewer-nav li.active').prev().attr('id').substr(4);
					}
					$j('#slides .active').fadeOut('slow');
					$j('#slides #slide-' + id).fadeIn('slow');
					$j('#slides .active').removeClass('active');
					$j('#slides #slide-' + id).addClass('active');
					$j('#viewer-nav .active').removeClass('active');
					$j('#viewer-nav li#nav-' + id).addClass('active');
					window.clearTimeout(ss);
					isStopped = true;
			});
	
		
	
	
		
			$j('#viewer').bind('mouseenter',function() {
				if(!isStopped) {
					pauseSlideShow();
				}
			});
			
			$j('#viewer').bind('mouseleave',function() {
				window.clearTimeout(ss);
				if(!isStopped) {
					ss = window.setTimeout('playSlideShow()',5000);
				}
			});
			
			$j(window).blur(function() {
				stopSlideShow();
			}).focus(function() {
			    resumeSlideShow();
			});
		}
	});
	
	function playSlideShow() {
		var d = new Date();
		$j('#viewer-nav .next').click();
		window.clearTimeout(ss);
		ss = window.setTimeout('playSlideShow()',5000);
	}

	
	function pauseSlideShow() {
		window.clearTimeout(ss);
	}
	
	function stopSlideShow() {
		isStopped = true;
		window.clearTimeout(ss);
	}
	
	function resumeSlideShow() {
		isStopped = false;
		window.clearTimeout(ss);
		ss = window.setTimeout('playSlideShow()',5000);
	}
	
/* 
 * Share Story field toggling
*/
	$j(function() {
		$j('#input_1_3').bind('change',function() {
			$j('#field_1_4').show();
		});
		$j('#input_1_4').bind('change',function() {
			$j('#field_1_5').show();
		});
		$j('#input_1_5').bind('change',function() {
			console.log('here');
			$j('#field_1_6').show();
		});
		$j('#input_1_6').bind('change',function() {
			console.log('here');
			$j('#field_1_7').show();
		});
		
		if($j('#field_1_3 .ginput_preview').length) {
			$j('#field_1_4').show();
		}
		
		if($j('#field_1_4 .ginput_preview').length) {
			$j('#field_1_5').show();
		}
		
		if($j('#field_1_5 .ginput_preview').length) {
			$j('#field_1_6').show();
		}
		
		if($j('#field_1_6 .ginput_preview').length) {
			$j('#field_1_7').show();
		}
		
		$j('#gform_1').submit(function(){
			console.log('here');
    		$j('input[type=submit]', this).attr('disabled', 'disabled');
		});
	});

				
			

