
$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};
// t: current time, b: begInnIng value, c: change In value, d: duration
$.easing['jswing'] = jQuery.easing['swing'];

$.extend( jQuery.easing,
{
	def: 'easeOutBack',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutBack: function (x, t, b, c, d, s) {
		//if (s == undefined) s = 1.70158;
		if (s == undefined) s = 0.8;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	}
});

/*$.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);  
}*/

$.fn.assignLinktype = function() {

	var thisDomain = window.location.host,
		thisURI = window.location.pathname;
		

	return this.each(function (i) {
		
		var thishref = $(this).attr("href");
	
		if ((thishref.match("http") == "http") && (thishref.match(thisDomain) != thisDomain)) {
			$(this).click(function(){
				this.target = "_blank";
			});
		}

	});
}

$(document).ready(function(){
			
	// Scrolling Header

	var backgroundwidth = 500700,
		offset = '0';
	function scrollbackground() {
		
		// decrease the offset by 1, or if its less than 1 increase it by
		// the background height minus 1
   		 offset = (offset < 1) ? offset + (backgroundwidth - 1) : offset - 1;
   		$('#headerContainer').css("background-position", offset + "px 0px");
   		setTimeout(function() {
			scrollbackground();
			}, 100
		);
   	}
   	
	$("a").assignLinktype();
	
	scrollbackground();
	
	$(".menuNav .menuMiddle").prepend('<div class="menuLeft">').append('<div class="menuRight">');
    
    $("#miniNav ul li:last a").css("background","transparent");
    
	$("#mainNav li").hoverIntent({
		sensitivity: 1, 
      	interval: 50, 
      	over: function() {
        $(this).children("div").slideDown();
		$(this).children("a").addClass("active");
	}, 
      	timeout: 300, 
      	out: function() {
		$(this).children("div").slideUp();
		$(this).children("a").removeClass("active");
	}
	});
 	
 	//$(".sidebar #mailinglist_form input#email").attr("value","Join Our Mailing List");
 
 	$(".searchBttn input, #mailinglist_form input, .podDonations .donate_amount").search();
	
	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) {
		$("#bannerContent").css("width","990px");
	}
	
 	$(".bannerTab").hoverIntent({
 		sensitivity: 1, 
      	interval: 50, 
      	over: function () {
      			$(this).stop(true, true).animate({bottom:"-20px"}, 300, 'easeOutBack');
      			}, 
      	timeout: 300, 
      	out: function () {
      				$(this).stop(true, true).animate({bottom:"-93px"}, 300, 'easeOutBack');
      			}
 	});
 	
 	$('#informationPod ul li').each(function(index) {
    	//$(this).find(".panel").height($(this).find(".panel").outerHeight(true) - 20);
  	});
 	
 	$('#informationPod ul li').eq(0).addClass("first-panel expanded").find('h4').addClass("subnavParentOpen");
 	$('#informationPod ul li:not(.expanded)').find('.panel').hide();
 	$('#informationPod ul li:last').addClass("last-panel");
 
 	$('#informationPod ul h4').click(function() {
 		var accordionLink = $(this);
		$(this).parent().siblings().find("div").stop(true, true).slideUp(300, function(){
			accordionLink.parent().siblings().find("h4").removeClass("subnavParentOpen");
			accordionLink.siblings("div").stop(true, true).slideDown(400);
		}); 
		$(this).addClass("subnavParentOpen");
		return false;
	});

	// Create Pod Sliders
	
	$('.box-pod .actionBttn').after('<div class="podMask"></div><div class="podPanel"></div>');
	
	$('.box-pod .podPanel').css({opacity : '.4'});
		
	$('.nav_categories .box-pod').hover(function(){
		$(this).find('.podPanel').stop(true, true).animate({
			//opacity : '.6',
			marginLeft: '0'
		});
	},
		function(){
		$(this).find('.podPanel').stop(true, true).animate({
			//opacity : '.4',
			marginLeft: '-115px'
		});
	});
	
	$('#bottomContainer .box-pod').hover(function(){
		$(this).find('.podPanel').stop(true, true).animate({
			//opacity : '.6',
			marginLeft: '0'
		});
	},
		function(){
		$(this).find('.podPanel').stop(true, true).animate({
			//opacity : '.4',
			marginLeft: '-140px'
		});
	});
 
 	
 	/* ===== OLD ===== */
 	
 	/*
 	$("#accreditation a").hover(function() {
 		var logoTitle = $(this).attr("rel");
 		var logoTitleId = logoTitle.replace(' ','');
			$(this).parent("li").append("<h3 id="+logoTitleId+">"+logoTitle+"</h3>");
			$("h3#"+logoTitleId).fadeIn("fast");
		}, 
  	function () {
  		var logoTitle = $(this).attr("rel");
 		var logoTitleId = logoTitle.replace(' ','');
    	$("#"+logoTitleId).fadeOut("fast").remove();
	});
	*/
    
    $('.swapImage').hover(function() {
        var $th = $(this);
        var src = $(this).attr('src');
        var newSrc = src.replace(/_off.gif/, '_on.gif');
        $th.attr('src', newSrc)
    },
    function() {
        var $th = $(this);
        var src = $(this).attr('src');
        var newSrc = src.replace(/_on.gif/, '_off.gif');
        $th.attr('src', newSrc)
    });
	
	
    
    $('.infoBttn').hover(function() {
	 	var infoContent = $(this).attr("title");
        $("."+infoContent).fadeIn("fast");
    });
    
    $('.infoBox').mouseleave(function() {
        $(this).fadeOut("fast");
    });
    
    /* Donations Pod "Other" field click selects */
    
    $('.podDonations .donate_amount').prop('disabled', true).addClass('input_disabled');
    
    $('.podDonations input[type="radio"]').change(function(){
    
    	var donationInput = $(this).parents('.podDonations').find('.donate_amount');
    	var thisradi
    	
    	if ($(this).hasClass('donate_3')) {
  
    		donationInput.removeClass('input_disabled').prop('disabled', false).focus().keyup(function () {
    			$(this).parents('.podDonations').find('.donate_3').val(donationInput.val());
    		});
    		
    		
    	} else {
    		donationInput.addClass('input_disabled').prop('disabled', true).val('$');
    		$(this).parents('.podDonations').find('.donate_3').val();
    	}
    });
    
    
    
});


	var speed = 50;
	var pic, numImgs, arrLeft, i, totalWidth, n, myInterval; 

$(window).load(function(){
	pic = $(".scrolling_banner").children("img");
	numImgs = pic.length;
	arrLeft = new Array(numImgs);
	
	for (i=0;i<numImgs;i++){
		
		totalWidth=0;
		for(n=0;n<i;n++){
			totalWidth += $(pic[n]).width();
		}
		
		arrLeft[i] = totalWidth;
		$(pic[i]).css("left",totalWidth);
	}
	
	myInterval = setInterval("flexiScroll()",speed);
	$('#imageloader').hide();
	$(pic).show();	
});

function flexiScroll(){

	for (i=0;i<numImgs;i++){
		arrLeft[i] -= 1;		

		if (arrLeft[i] == -($(pic[i]).width())){	
			totalWidth = 0;	
			for (n=0;n<numImgs;n++){
				if (n!=i){	
					totalWidth += $(pic[n]).width();
				}			
			}	
			arrLeft[i] =  totalWidth;	
		}					
		$(pic[i]).css("left",arrLeft[i]);
	}
}
