
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};

// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());


// place any jQuery/helper plugins in here, instead of separate, slower script files.



//-------------------------------------------------
//		youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//
//		v1.1 - updated to allow fullscreen 
//			 - thanks Ashraf for the request
//-------------------------------------------------

jQuery.fn.ytplaylist = function(options) {
 
  // default settings
  var options = jQuery.extend( {
  holderId: 'tv',
	playerHeight: '190',
	playerWidth: '300',
	addThumbs: false,
	thumbSize: 'small',
	showInline: false,
	autoPlay: false,
	showRelated: false,
	allowFullScreen: true
  },options);
 
  return this.each(function() {
							
   		var selector = $(this);
		
		var autoPlay = "";
		var showRelated = "&rel=0";
		var fullScreen = "";
		if(options.autoPlay) autoPlay = "&autoplay=1"; 
		if(options.showRelated) showRelated = "&rel=1"; 
		if(options.allowFullScreen) fullScreen = "&fs=1"; 
		
		//throw a youtube player in
		function play(id)
		{
		   var html  = '';
	
		   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
		   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
		   html += '<param name="wmode" value="transparent"> </param>';
		   if(options.allowFullScreen) { 
		   		html += '<param name="allowfullscreen" value="true"> </param>'; 
		   }
		   html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'&showinfo=0"';
		   if(options.allowFullScreen) { 
		   		html += ' allowfullscreen="true" '; 
		   	}
		   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
		   html += '</object>';
			
		   return html;
		   
		};
		
		
		//grab a youtube id from a (clean, no querystring) url (thanks to http://jquery-howto.blogspot.com/2009/05/jyoutube-jquery-youtube-thumbnail.html)
		function youtubeid(url) {
			var ytid = url.match("[\\?&]v=([^&#]*)");
			ytid = ytid[1];
			return ytid;
		};
		
		/* if tv is already shown. Some pages intitially hide tv but have tv thumbnails */
		if ($('#tv').is (':visible')) { 		
			//load inital video
			var firstVid = selector.children("li:first-child").addClass("currentvideo").children("a").attr("href");
			$("#"+options.holderId+"").html(play(youtubeid(firstVid)));
	
			// added to populate the video link text box with the link of the firstvideo...
			$('#video-link').val(firstVid);
			// ... and the video description paragraph with its description
			var firstVidTitle = selector.children("li:first-child").addClass("currentvideo").children("a").attr("title");
			$('#video-description').text(firstVidTitle);
						
			$("#video-link").click(function() {
			   this.select();
			 });
			$("#share-this").click(function() {
				$("#video-link").select();
			 });
		};		
		
		/* add a class to stop the last item in thumbs having the right border */
		$("ul#thumbs li:last-child").addClass("last");
		
		//load video on request
		selector.children("li").children("a").click(function() {
			
			if(options.showInline) {
				$("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo").html(play(youtubeid($(this).attr("href"))));
			}
			else {
				$("#"+options.holderId+"").html(play(youtubeid($(this).attr("href"))));
				$(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
				$(this).parent("li").addClass("currentvideo");
				// for version of tv where player initially hidden, show when thumbnail clicked on, give ability to close again
				if ($('#tv').is (':hidden')) { 
					$("#tv").slideDown();
					$("#tv-close").show();
					$("#collapsible").slideUp();
					$("#tv-close").click(function() {
						$("#tv").slideUp();
						$("#tv-close").hide();						
						$("#collapsible").slideDown();
					});
				}
			}

			// added to populate the video link text box with the link of the selected video
 			$('#video-link').val($(this).attr("href"))
			// ... and the video description paragraph with its description
			$('#video-description').text($(this).attr("title"));
			return false;
		});
		
		// do we want thumbs with that?
		if(options.addThumbs) {
			
			selector.children().each(function(i){
											  
				var replacedText = $(this).text();
				
				if(options.thumbSize == 'small') {
					var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/2.jpg";
				}
				else {
					var thumbUrl = "http://img.youtube.com/vi/"+youtubeid($(this).children("a").attr("href"))+"/0.jpg";
				}
				
				
				$(this).children("a").empty().html("<img src='"+thumbUrl+"' alt='"+replacedText+"' />"+replacedText).attr("title", replacedText);
				
			});	
			
		}
			
		
   
  });
 
}; /* JQuery YouTube PLaylist Plugin */


//-------------------------------------------------
//
// 		Better image preloading 
//		@ http://perishablepress.com/press/2009/12/28/3-ways-preload-images-css-javascript-ajax/
//
//-------------------------------------------------

/*
function preloader() {
	if (document.getElementById) {
		document.getElementById("preload-01").style.background = "url(../img/nav-home-s.png) no-repeat -9999px -9999px";
		document.getElementById("preload-02").style.background = "url(../img/nav-share-s.png) no-repeat -9999px -9999px";
		document.getElementById("preload-03").style.background = "url(../img/nav-what-you-can-s.png) no-repeat -9999px -9999px";
		document.getElementById("preload-04").style.background = "url(../img/nav-resources-s.png) no-repeat -9999px -9999px";		
		document.getElementById("preload-05").style.background = "url(../img/nav-stay-in-touch-s.png) no-repeat -9999px -9999px";		
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
addLoadEvent(preloader);
*/


//-------------------------------------------------
//
// 		jtwt - jQuery Twitter Plugin
//		http://buzzrocket.de/labs/jtwt/
//
//-------------------------------------------------
(function($){

	$.fn.extend({ 
	
	//pass the options variable to the function
	jtwt: function(options) {
	
	
	//Set the default values, use comma to separate the settings, example:
	var defaults = {
		username : 'google',
		count : 1,
		image_size: 48,
		convert_links: 1,
		loader_text: 'loading new tweets'
	}
	
	var options =  $.extend(defaults, options);
	
	return this.each(function() {
	var o = options;
	var obj = $(this);  
	
	$(obj).append('<p id="jtwt_loader" style="display:none;">' + o.loader_text + '</p>');	
	$("#jtwt_loader").fadeIn('slow');
	
	
	$.getJSON('http://twitter.com/status/user_timeline/' + o.username + '.json?count=' + o.count + '&callback=?', function(data){ 
	
	$.each(data, function(i, item) {       
	
		jtweet = '<div id="jtwt">';
	
		if (o.image_size != 0) {
			today = new Date();	
			jtweet += '<div id="jtwt_picture">';
			jtweet += '<a href="http://twitter.com/' + item.user['screen_name'] + '">'
			jtweet += '<img width="' + o.image_size +'" height="' + o.image_size + '" src="' + item.user['profile_image_url'] + '" />';
			jtweet += '</a><br />';
			jtweet += '</div>';
			jtweet += '<div id="jtwt_tweet">';
		} 
	
		var tweettext = item.text;
		var tweetdate = item.created_at;
	
		if (o.convert_links != 0) {
			tweettext = tweettext.replace(/(http\:\/\/[A-Za-z0-9\/\.\?\=\-]*)/g,'<a href="$1">$1</a>');
			tweettext = tweettext.replace(/@([A-Za-z0-9\/_]*)/g,'<a href="http://twitter.com/$1">@$1</a>');
			tweettext = tweettext.replace(/#([A-Za-z0-9\/\.]*)/g,'<a href="http://twitter.com/search?q=$1">#$1</a>');		
		}
	
		jtweet += '<div id="jtwt_text">';
		jtweet += tweettext;
		jtweet += '<br />';
		
		
		tweetdate = tweetdate.replace(/201.{1}/, "");
		tweetdate = tweetdate.replace(/\+00.{2}/, "");
		jtweet += '<a href="http://twitter.com/' + item.user['screen_name'] + '/statuses/' + item.id + '" id="jtwt_date">';
		
		jtweet += tweetdate;
		jtweet += '</a></div>';
		jtweet += '</div>';
		
		// also add it to "share our latest tweet" link for them to retweet
		$("#twitter-link").attr("href", "http://twitter.com/home?status=RT @opendoorsyouth "+encodeURIComponent(item.text));
		
		$(obj).append(jtweet);

	});   
	
	
	$("#jtwt_loader").fadeOut('fast');   
	
	});
	
	
	
	});
	}
	});

})(jQuery);
