(function($){

var width = $('#searchform #s').css('width');
$('#searchform #s').focus(function(){
	$(this).animate({width: '285px'}, 500);
});
$('#searchform #s').focusout(function(){
	if(!$(this).val()){
		$(this).animate({width: width}, 500);
	}
});	

$('.x').click(function(){
	$('#error404').animate({
		opacity: 0,
		height: 0
	}, 500, function(){
		$(this).animate({
			margin: '0px'
		},500, function(){
			$('#error404').remove();
		});
	});
});


//.unique function
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

window.mcFunc = {
	
	loadVideo: function(playerUrl, autoplay, watchUrl, watchTitle) {
		$('#playerContainer').empty().html(function(){
				var ytVideoId = playerUrl.substr(25, 11);
				if (autoplay == true){
					var play = '&amp;autoplay=1';
				} else {
					var play = '';
				}
				 
				var ytVideo = 	$('<iframe></iframe>').attr({
									width: 620,
									height: 379,
									title: 'YouTube Video Player',
									frameborder: 0,
									src: 'http://www.youtube.com/embed/' + ytVideoId + '?rel=0&amp;hd=1' + play
								});
				return ytVideo;
		});
		
		
		//setup sharing icons
		$('#vidFaceShare').attr('href', 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(watchUrl) + '&t=' + encodeURIComponent(watchTitle));
		$('#vidEmailShare').attr('href', 'mailto: ?body=I saw this video on the Eden Network website www.eden-network.org and thought you might like it: ' + encodeURIComponent(watchUrl) + '&subject=Video from the Eden Network: ' + watchTitle);
		$('#vidTwitterShare').attr('href', 'http://twitter.com/share?text=I am loving this video I found on the Eden Network website www.eden-network.org &url=' + encodeURIComponent(watchUrl));
	},
	//get youtube videos by Ajax call then use above loadVideo function to play them
	ytGetVideos: function(){
		//put loading in place
		$('#playerContainer').addClass('loading');
		//start query
		var baseURL = 'http://gdata.youtube.com/feeds/api/users/EdenNetwork/uploads?v=2&alt=jsonc&max-results=24&callback=?';
		$.getJSON(baseURL, function(response){
			$.each(response.data.items, function(i, item){
				//setup variables for each entry
				var title = item.title;
				var thumb = item.thumbnail.sqDefault;
				var playerUrl = item.content['5'];
				var watchUrl = item.player['default'];
				var ytDate = item.uploaded; var date = Date.parse(ytDate);
				//create a for each entry
				$('<a></a>')
				.hide()
				.attr('href','#')
				.addClass('ytVideoThumb')
				.html('<img src="' + thumb +'" /><span class="titlec">' + title.toLowerCase() +'</span><span class="date">' + date.toString('d MMM yyyy') + '</span>')
				.appendTo('#videos')
				.fadeIn()
				.click(function(){
					mcFunc.loadVideo(playerUrl, true, watchUrl, title);
					return false;
				});
			});
			//get first video and play it
			var first = response.data.items[0];
			mcFunc.loadVideo(first.content['5'], false, first.player['default'], first.title);
			$("a.ytVideoThumb:nth-child(8n-7)").each(function(){
					$(this).nextAll("a.ytVideoThumb:lt(7)").andSelf().wrapAll("<div></div>");
			});
		});
		
	},
	/////////////////////////////////////////////////////////////////////////
	// On Teams page filter by region!                                     //
	/////////////////////////////////////////////////////////////////////////
	teamsIsotope: function() {
		var local = "";
		if(document.location.hostname == "192.168.1.47") {local = "/eden";}
		$.getScript("http://" + window.location.host + local + "/wp-content/themes/eden/js/jquery.isotope.min.js", function(){
			$('.team').show();
			$('#teams').isotope({
				itemSelector: '.team',
				layoutMode: 'fitRows',
				animationEngine: 'best-available'
			});
			var hash = location.hash.substring(1);
			if (hash){
				$('#teams').isotope({filter: '.' + hash });
				$('#teamsNav a').parent('.current').removeClass('current');
				$('#teamsNav a[data-filter=".' + hash + '"]').parent().addClass('current');
			}
			$('#teamsNav a').click(function(){
				var selector = $(this).attr('data-filter');
				$('#teams').isotope({filter: selector });
				$('#teamsNav a').parent('.current').removeClass('current');
				$(this).parent().addClass('current');	
				return false;
			});
			
			//type and filter
			var typedString = "";
			$(document).one("keypress", function(event){
				$("<span/>").addClass('typefilter').appendTo('#teamsNav');
				$(document).keydown(function(e){
					if (e.which == 32){e.preventDefault();}				
					if (e.which == 8){
						e.preventDefault();
						typedString = typedString.slice(0, -1);
					}
					updateString();
				});
			});
			$(document).keypress(function(event){
				var key = String.fromCharCode(event.keyCode);
				typedString = typedString + key;
				updateString();
			});
			
			$(document).keyup(function(e){
				if (e.keyCode == 27){typedString = "";}
				updateString();
			});
			
			
			function updateString(){
				$("#teamsNav span").html(typedString);
				if(typedString == ''){
					$('#teams').isotope({filter: '*' });
				}else{
					typedString = typedString[0].toUpperCase() + typedString.substr(1);
					var title = $(".team h3:contains('"+ typedString +"')").parent().parent();
					$('#teams').isotope({filter: title });
				}
			}
		});
	}
		
	
}







})(jQuery);
