jQuery.noConflict();
var $ = jQuery;

jQuery.prototype.cycle = function(_delay, _onFn, _afterFn)
{
	var _elements = this;
	

	Cycle = function()
	{
		this.elements = _elements;
		this.delay = _delay;
		this.on = _onFn;
		this.after = _afterFn;
		this.index = 0;
		this.paused = false;		
	};
	

	Cycle.prototype.execute = function()
	{
		var self = this;
		if(!this.paused) this.on.call(this.elements[this.index], this.index, this.elements[this.index]);
		
		window.setTimeout(function() 
				{ 
					if(!self.paused) self.after.call(self.elements[self.index], self.index, self.elements[self.index]);
					self.index++;
					
					if(self.index>=self.elements.length)
					{
						self.index = 0;
					}
					
					window.setTimeout(function() { self.execute.call(self); }, this.delay);
					
				}, this.delay);	
	};
	
	Cycle.prototype.pause = function()
	{
		if(this.paused) return;
		this.paused = true;
		this.after.call(this.elements[this.index], this.index, this.elements[this.index]);
		
	};
	
	Cycle.prototype.resume = function()
	{
		var self = this;
		window.setTimeout(function() { self.paused = false; }, this.delay);
	};
	
	var cycleImpl = new Cycle();
	cycleImpl.execute(cycleImpl);
	
	return cycleImpl;
	
};

function launchPodcast(podcastId)
{
	powerpress_pinw(podcastId + '-podcast');
}

function playVideo(targetElem, vimeoId)
{
	var popup = $("#video_popup");
	
	if(!popup)
	{
		var popup = document.createElement("DIV");
		popup.id = "video_popup";
		document.appendChild(popup);
		popup = $("#video_popup");
		popup.className = "apple_overlay";
	}
	
	popup.html('<object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoId + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + vimeoId + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object><p><a href="http://vimeo.com/' + vimeoId + '">Love the Neighborhood</a> from <a href="http://vimeo.com/slfc">SLFC VIDEOS</a> on <a href="http://vimeo.com">Vimeo</a>.</p>');
	
	targetElem = $(targetElem);
	targetElem.overlay({effect: 'apple'}); 
	
}

$(document).ready(function () 
{
	initTestimonies();
	
	//initBanners();
	
	initContactForm();
	
	initGrid();
	
	initOverlay();
	
	initLogos();
	
	initFields();
	
	initAds();
	

});

function initAds()
{


	/*
	 
	/var adcycle = $(".ads li img").cycle(4000, 
			function(index) { $(this).fadeTo(750, .01); }, 
			function(index) { $(this).fadeTo(750, 1); });
			
	*/
	
	$(".ads li img").hover(function() 
				{
					//adcycle.pause();
					$(this).fadeTo(350, .01); 
				}, function() 
				{
					$(this).fadeTo(350, 1);
					//adcycle.resume();
				});

	
	
	
/*
	var flashAds = function()
	{
		var ads = $(".ads li img");
		var adIndex = 0;
		
		var flash = function()
		{
			$(ads[adIndex]).fadeTo(50, .01, function() 
					{
						$(this).fadeTo(500, 1, function()
							{
								adIndex++;
								
								if(adIndex >= ads.length)
								{
									// reset timeout									
									window.setTimeout(flashAds, 5000);
								}
								else
								{
									flash();
								}
								
							});
					});
		};
		
		flash();
		
		
		
	};
	
	window.setTimeout(flashAds, 5000);
	*/
	
}


function initLogos()
{
	var on = .4;//1;
	var off = 1;//.4;
	var logos = [];
	var logoIndex = -1;
	var logoInterval;
	
	logos = $(".trust-logos li");

	//logos.fadeTo(0, off); 
	
	logos.hover(function() 
				{
					$(this).fadeTo(250, on); 
				}, function() 
				{
					$(this).fadeTo(250, off); 
				});


	
	var animateLogo = function()
	{
		// fade out old
		if(logoIndex!=-1)
		{
			logos.eq(logoIndex).fadeTo(250, off);
		}
		
		//logoIndex = Math.floor(Math.random()*logos.size());
		logoIndex++;

		if(logoIndex >= logos.size())
		{
			logoIndex = 0;
		}

		// fade in new
		logos.eq(logoIndex).fadeTo(250, on);
	}
	

	window.setTimeout(function() 
		{ 
			logos.fadeTo(1000, off);
			
		}, 2000);
	
	window.setTimeout(function() 
		{ 
			animateLogo();
			logInterval = window.setInterval(animateLogo, 2000);
			
		}, 3000);
	
	
}


function initTestimonies()
{
	var opacity = .75;
	
	
	
	var over = function()
	{
		$(this).animate({
		    opacity: 1
		  }, 500, function() {
		    // Animation complete.
		  });

	};
	

	var out = function()
	{
		$(this).animate({
		    opacity: opacity
		  }, 500, function() {
		    // Animation complete.
		  });

	};
	
	$("#header #testimonies").hover(over, out);
	
	
	
	initRotate({
		query: 	"#header #testimonies li",
		delay: 3000,
		interval: 3000,
		duration: 1000,
		pauseOnHover: false
	});
	
}

function initGrid()
{
	$(".grid").hover(
			function()
			{
				$(this).addClass("hover");
			},
			function()
			{
				$(this).removeClass("hover");
			});
}

function initContactForm()
{
	var onfocus = function()
	{
		var reveal = $("#contactform .reveal");
		reveal.slideDown();
		
	};	
	
	$("#contactFormEmail").focus(onfocus);
	
	
	
	
	var placeholders = $(".field-with-placeholder");
	var ph;
	var lbl;
	var input;

	var fnchange = function(evt)
	{
		var lbl = $(evt.data[0]);
		lbl.hide();		
	};

	var fnblur = function(evt)
	{
		var lbl = $(evt.data[0]);
		if(this.value != "")
		{
			lbl.hide();
		}
		else
		{
			lbl.show();
		}
		
	};

	var fnfocus = function(evt)
	{
		var lbl = $(evt.data[0]);	
	};
	
	var fnclick = function(evt)
	{
		// hack to get IE to work
		var input = evt.data[0][0];
		
		var fnlater = function()
		{
			input.focus();
		};
		window.setTimeout(fnlater, 10);
		
	};
	
	for(var i=0; i<placeholders.length; i++)
	{
		ph = $(placeholders[i]);
		
		
		lbl = $(ph.children("LABEL")[0]);
		input = $(ph.children("SPAN")).children("INPUT")[0];
		
		if(input.value != '')
		{
			lbl.hide();
		}

		input = $(input);
		input.bind("keydown", [lbl], fnchange);
		input.bind("blur", [lbl], fnblur);
		input.bind("focus", [lbl], fnfocus);
		
		lbl.bind("click", [input], fnclick);
		
	}
	
	
	
	
}


function initOverlay() 
{
    $(function() 
    { 
        $("img[rel]").overlay({effect: 'apple'}); 
    });

};


function initRotate(opts)
{
	/*
		var rotateOpts = {
		query: 	"#testimonies li",
		delay: 1000,
		interval: 3000,
		duration: 500,
		pauseOnHover: false
	};
	 */
	
	// setup defaults
	opts.duration = opts.duration ? opts.duration : 500;
	
	
	var rotator = {
		opts: opts,
		execute:  function()
			{
				this.items = $(this.opts.query);
		
				this.index = this.index ? this.index : 0;
								
				
				var self = this;
				
				var callback = function()
				{
					window.setTimeout(function()
							{
								fadeIn.call(self);
							}, self.opts.interval);
				};
				
				var fadeIn = function()
				{
					// increase or reset index
					self.index = self.index + 1 >= self.items.length ? 0 : self.index + 1;
					$(self.items[self.index]).fadeIn(self.opts.duration, function()
							{
								window.setTimeout(function()
										{
											self.execute.call(self);
										}, self.opts.interval);
							});
				};
				
				$(this.items[this.index]).fadeOut(this.opts.duration, callback);
			}
	}
	
	if(opts.delay)
	{
		window.setTimeout(function()
			{
				rotator.execute.call(rotator);
			}, opts.delay);
	}
	else
	{
		rotator.execute.call(rotator);
	}
}


function initFields()
{
	
	$("SPAN[class*=field-with-placeholder] INPUT").each(
		function(index, element)
		{
			if(this.value != "") {
				$(this).prev().hide();
			}
		}
	);
	
	
	$("SPAN[class*=field-with-placeholder] INPUT").keydown(function()
			{
				$(this).prev().hide();
			});
	
	$("SPAN[class*=field-with-placeholder] INPUT").focus(function()
			{
				/*$(this).prev().animate({
				    opacity: .5
				  }, 500, function() {
				    // Animation complete.
				  });*/
				$(this).prev().css('color', function() { return '#999'; });
			});
	
	$("SPAN[class*=field-with-placeholder] INPUT").blur(function()
			{
	
				if(this.value != "")
				{
					$(this).prev().hide();
				}
				else
				{

					$(this).prev().show();
					/*$(this).prev().animate({
					    opacity: 1
					  }, 500, function() {
					    // Animation complete.
					  });*/
					
					$(this).prev().css('color', function() { return '#666'; });
					
				}		
			});	
	
}









/*
function initBanners()
{
	// do banners exist?
	var banners = $("#banners li");
	if(banners.length==0)
	{
		return;
	}
	
	// create banner rotation function
	var bannerIndex = 0;
	var bannerRotateInterval;
	var bannerRotateTimer = 7500;
	var nav = $("#banner-nav");
	var prog = $("#banner-progress");
	var progBar = $("#banner-progress-bar");
	
	var animateBannerProgress = function()
	{
		if(!progBar)
		{
			prog.append("<div id='banner-progress-bar'></div>")
			progBar = $("#banner-progress-bar");			
		}
		var w = progBar.css("width").replace("%", "");
		w = parseInt(w, 10) + 1;
		
		progBar.css("width", (w > 99 ? 100 : w) + "%");
	}
	var hideBannerProgress = function()
	{
		prog.hide();
		resetBannerProgress();
	}
	var showBannerProgress = function()
	{
		prog.show();
		resetBannerProgress();
	}
	var resetBannerProgress = function()
	{
		progBar.css("width", "0%");
	}
	
	var rotateBanner = function(nextIndex, explicit)
	{
		var banners = $("#banners li");

		var cur = bannerIndex;
		var next = explicit ? nextIndex : cur + 1;

		if(explicit)
		{
			// stop rotation interval
			window.clearInterval(bannerRotateInterval);
			bannerRotateInterval = null;
			$("#banner-nav .navPause").addClass("active");
			hideBannerProgress();
		}
		
		if(next >= banners.length)
		{
			next = 0;
		} 

		var callback = function()
		{
			$(banners[next]).fadeIn(1000);
			if(progBar)
			{
				resetBannerProgress();
			}
		};
		
		$(banners[cur]).fadeOut(500, callback);
		

		$("#banner-nav .nav" + (cur + 1)).removeClass("active");
		$("#banner-nav .nav" + (next + 1)).addClass("active");

		bannerIndex = next;
	}
	
	// overlay banner images
	if(jQuery.browser.msie && jQuery.browser.version.charAt(0)=="6")
	{
		//IE 6
	}
	else
	{	
		var imgs = $("ul#banners li img");
		for(var i=0; i<imgs.length; i++)
		{
			var img = $(imgs[i]);
			img.css("background-image", "url(" + img.attr("src") + ")");
			img.attr("src", "/images/banner_overlay.png");
		}
	}
	

	// create banner navigation
	if(nav)
	{
		var clickEventHandler = function(index)
		{
			return function()
			{
				rotateBanner(index, true);
				hideBannerProgress();
			};
		}
		
		for(var i=0; i<banners.length; i++)
		{
			var b = banners[0];
			var li = document.createElement("LI");
			
			li.className = "nav" + (i+1);
			if(i==0)
			{
				li.className += " active";
			}
			nav.append(li);
			li = $(li);
			
			li.click(clickEventHandler(i));
		}
		var li = document.createElement("LI");
		li.className = "navPause";
		nav.append(li);
	
		// create function to play/pause rotation
		var playPauseBanner = function()
		{
			if(bannerRotateInterval==null)
			{
				rotateBanner();
				bannerRotateInterval = window.setInterval(rotateBanner, bannerRotateTimer);
				$("#banner-nav .navPause").removeClass("active");
				showBannerProgress();
			}
			else
			{
				window.clearInterval(bannerRotateInterval);
				bannerRotateInterval = null;
				$("#banner-nav .navPause").addClass("active");
				hideBannerProgress();
			}
		}
	
		$("#banner-nav .navPause").click(playPauseBanner);
	}
	
	// init banner rotation	
	bannerRotateInterval = window.setInterval(rotateBanner, bannerRotateTimer);
	if(prog)
	{
		window.setInterval(animateBannerProgress, bannerRotateTimer / 100);
	}
}
*/


