	jQuery.noConflict();
var gt = {};
(function($) {
	gt.nav = function() {
		$('.nav .selected').parents('.nav li').addClass('selected');
		$('body').addClass($('#nav > .selected, #utilities > .selected').attr('id'));
		$('#utilities #ebay').before('<li class="last">Follow Us:</li>')
	}
	
	gt.search = function() {
	  var search = $('#header #search');
	  $('#query', search).click(function() {
	    this.select();
	    return true;
	  });
	  $('a', search).click(function() {
	    search.submit();
	    return false;
	  });
	}
	
	gt.for_media = function() {
		$("#view-releases").click(function(){
			if($(this).parent().is('.selected')) {
				return false;
			} else {
				$("#latest-stories").hide("fast");
				$("#latest-releases").fadeIn("slow");
				$("#view-releases").parent().addClass("selected");
				$("#view-stories").parent().removeClass("selected");
				
			}
		});
		$("#view-stories").click(function(){
			if($(this).parent().is('.selected')) {
				return false;
			} else {
				$("#latest-releases").hide("fast");
				$("#latest-stories").fadeIn("slow");
				$("#view-releases").parent().removeClass("selected");
				$("#view-stories").parent().addClass("selected");
				
			}
		});		
	}

	gt.num_comments = function() {
		if (gt.threads_data == null) return;
		var threads = gt.threads_data.message;
		$('.num-comments').each(function() {
			var thread_id = $(this).html();
			if (threads[thread_id]) {
				var num_comments = threads[thread_id][1];
				$(this).html(num_comments);
				if (num_comments == 1) {
					var singular = $(this).parent().text().replace(/comments/, 'comment');
					$(this).parent().text(singular);
				}
			} else {
				$(this).html(0);
			}
		}).show();
	}	
	
	$.fn.frame = function(images, options) {
		var defaults = {};
		var options = $.extend(defaults, options);
		return this.each(function() {
			var framed = $(this);
			framed.wrap('<div class="frame"></div>');
			var frame = framed.parent();
			frame.addClass(framed.attr('class')).removeClass('framed');
			framed.attr('class', '').addClass('framed');
			frame.css('background', 'url(' + $(this).attr('src') + ') no-repeat');
			framed.attr('src', '/assets/images/site/blank.gif');
			$('.main .module .framed_center').parent().addClass('centering');
		});
	}
	
	gt.fast_facts = function() {
		if ($('#fast-facts .items').length == 0) return;
		
		$('#fast-facts .items').cycle({
			speed: 'slow',
			timeout: 8000,
			next: '#fast-facts .items',
			pause: true,
			cleartypeNoBg: true
		});
	}
	
	gt.your_thoughts = function() {
		if ($('#your-thoughts').length == 0) return;

		var thoughts = [];
  	var html = '';
		
		if (gt.recent_tweets) {
			var tweets = gt.recent_tweets.results;
		  for (var i = 0; i < tweets.length; i++) {
				var thought = {};
				thought.source = 'twitter';
				thought.date = new Date(tweets[i].created_at);
				thought.author = '<a target="_blank" href="http://www.twitter.com/' + tweets[i].from_user + '">' + tweets[i].from_user + '</a>';
				thought.message = gt.linkify(tweets[i].text);
				thoughts.push(thought);
		  }
		}
	
		if (gt.recent_posts) {
			var posts = gt.recent_posts.message;
			for (var i = 0; i < posts.length; i++) {
				var thought = {};
				thought.source = 'green-team-talks';
				thought.date = new Date(Date.parse(posts[i].created_at.replace(/-/g, '/').replace(/T/g, ' ') + ' GMT'));
				thought.author = '<a target="_blank" href="http://myworld.ebay.com/' + posts[i].anonymous_author.name + '">' + posts[i].anonymous_author.name + '</a>';
				thought.message = posts[i].message;
				thoughts.push(thought);
			}
		}
		
		if (thoughts.length > 0) {
			thoughts.sort(gt.compare_dates);
		
			for (var i = 0; i < thoughts.length; i++) {
				var additional_class = '';
				if (i == 0) additional_class = ' first';
	    	html += '<li class="thought' + additional_class + '"><blockquote class="ugc ' + thoughts[i].source + '">' + thoughts[i].message + '<div class="time">Posted ' + gt.pretty_date(thoughts[i].date) + '</div><cite>by ' + thoughts[i].author + '</cite></blockquote></li>';
			}
		
			var thoughts = $('#your-thoughts .thoughts');
			thoughts.html(html);
			thoughts.cycle({
				fx: 'scrollUp',
				timeout: 6000,
				speed: 'slow',
				pause: true,
				before: function() {
					$(this).find('blockquote').css('top', 0);
				},
				after: function() {
					var thought = $(this);
					var blockquote = thought.find('blockquote');
					if (blockquote.outerHeight() > thought.innerHeight()) {
						blockquote.animate({
							top: -1 * (blockquote.outerHeight() - thought.innerHeight())
						}, 'slow');
					}
				}
			});
		}
	}
	
	gt.compare_dates = function(a, b) {
		return b.date - a.date;
	}
	
	gt.linkify = function(text) {
 		var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
 		return text.replace(exp, '<a target="_blank" href="$1">$1</a>'); 
	}
	
	gt.strip_html = function(text) {
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
    return text.replace(regexp, '');
	}
	
	gt.pretty_date = function(date) {
		var time = date.getTime() / 1000;
		var now = Math.round(new Date().getTime() / 1000);
		var diff = (time < now ? now - time : -1);
		var text = '';
		if (diff < 0) {
			return false;
		} else {
			var seconds = diff % 60;
			diff = (diff - seconds) / 60;
			var minutes = diff % 60;
			diff = (diff - minutes) / 60;
			var hours = diff % 24;
			diff = (diff - hours) / 24;
			var days = diff;
			text += (days > 0 ? days + ' day' + (days == 1 ? '' : 's') + ' ': '');
			if (days == 0) text += (hours > 0 ? hours + ' hour' + (hours == 1 ? '' : 's') + ' ': '');
			if (days == 0 && hours == 0) text += (minutes > 0 ? minutes + ' minute' + (minutes == 1 ? '' : 's') + ' ': '');
			if (days == 0 && hours == 0 && minutes == 0) text += (seconds > 0 ? seconds + ' second' + (seconds == 1 ? '' : 's') + ' ': '');
			text += 'ago';
		}
		return text;
	}
	
	gt.clean_article = function() {
		$('.article a[href^=http], .ugc a[href^=http]').attr('target', '_blank');
	}
	
	gt.videos = function() {
		$('.modules .module').each(function() {
			var module = $(this);
			module.find('a[href*=youtube.com], a[href*=vimeo.com], a[href*=viddler.com]').play_video();
			module.find('a[href*=youtube.com]:first, a[href*=vimeo.com]:first, a[href*=viddler.com]:first').each(function() {
				var link = $(this);
				$.getJSON('/view/page.standard/oembed', { url: link.attr('href'), maxwidth: module.width() }, function(data) {
					if (module.find('.image').length == 0) {
						module.find('.heading').after('<div class="image video-thumbnail"></div>');
						var image = module.find('.image');
						image.append(link.clone());
						image.find('a').append('<img src="' + data.thumbnail_url + '" width="' + image.width() + '" alt="" title=""/><div class="tl"></div><div class="tr"></div><div class="br"></div><div class="bl"></div><div class="play"></div>').play_video();
						var play = image.find('.play');
						image.find('img').load(function() {
							gt.center(play, image);
						});
					} else {
						var image = module.find('.image');
						image.addClass('video-thumbnail');
						image.find('a').append('<div class="play"></div>');
						var play = module.find('.video-thumbnail .play');
						gt.center(play, image);
					}
				});
			});
		});
	}
	
	$.fn.play_video = function() {
		return this.each(function() {
			var link = $(this);
			var modal = $('#modal');
			link.click(function() {
				$.blockUI({ message: modal });
				$.getJSON('/view/page.standard/oembed', { url: link.attr('href'), maxwidth: modal.width() }, function(data) {
					$('#modal #message').html('<h2>' + data.title + '</h2>' + data.html).removeClass('loading');
				});
				return false;
			});
		});
	}
	
	gt.slides = function() {
		$('.modules .module').each(function() {
			var module = $(this);
			module.find('a.source[href*=api.flickr.com]:first').each(function() {
				var href = $(this).attr('href');
				href = gt.set_param(href, 'format', 'json');
				href = gt.set_param(href, 'jsoncallback', '?');
				$.getJSON(href, function(data) {
					module.find('.heading').after('<div class="slideshow"><ul class="items"></ul></div>');
					var slides = module.find('.items');
					$.each(data.items, function(i, item) {
						$('<img alt="' + item.title + '"/>').attr('src', item.media.m).appendTo(slides).wrap('<li class="item"></li>'); 
					});
					module.find('.items img:first').load(function() {
						slides.carousel();
					});
				});
			});
		});
	}
	
	gt.get_param = function(href, name) {
	  var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
	  var results = regex.exec(href);
	  if (results == null) {
	    return null;
		}
	  else {
	    return results[1];
		}
	}
	
	gt.set_param = function(href, name, value) {
	  var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
	  var results = regex.exec(href);
		if (results == null) {
			if (href.indexOf('?') == -1) {
				return href + '?' + name + '=' + value;
			} else {
				return href + '&' + name + '=' + value;
			}
		} else {
			return href.replace('=' + gt.get_param(href, name), '=' + value);
		}
	}
	
	gt.num_carousels = 0;
	$.fn.carousel = function() {
		return this.each(function() {
			var items = $(this);
			items
			.before('<a class="items-prev" id="items-prev-' + gt.num_carousels + '" href="#">Prev</a>')
			.after('<a class="items-next" id="items-next-' + gt.num_carousels + '" href="#">Next</a>')
			.cycle({
				speed: 'fast',
				next: '#items-next-' + gt.num_carousels,
				prev: '#items-prev-' + gt.num_carousels,
				pause: true,
				after: function(current, next) {
					$(next).find('.reflected').reflect({height: 0.4, opacity: 0.4});
				}
			});
			gt.num_carousels++;
			items.find('a').tooltip({
				showBody: '|',
				showURL: false,
				top: 1,
				left: 1,
				track: true
			});
		});
	}

	gt.modals = function() {
		$.blockUI.defaults.css = { position: 'absolute' };
		$.blockUI.defaults.overlayCSS = { backgroundColor: '#fff', opacity: 0.9 };
		$('a.modal').click(function() {
			$.blockUI({ message: $('#modal') });
			$.get($(this).attr('href') + '?fragment=true', function(html) {
				$('#modal #message').html(html).removeClass('loading');
				sifr_replace();
			});
			return false;
		});
		$('#modal #close').click(function() {
			$('#message').html('').addClass('loading');
			$.unblockUI();
			return false;
		});
		if (gt.getParameter('modal')) {
			$.blockUI({ message: $('#modal') });
			$.get(gt.getParameter('modal') + '?fragment=true', function(html) {
				$('#modal #message').html(html).removeClass('loading');
				sifr_replace();
			});
		}
	}
	
	gt.getParameter = function(name) {
  	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return results[1];
	}
	
	gt.center = function(object, container) {
		object.css('left', container.width() / 2 - object.width() / 2).css('top', container.height() / 2 - object.height() / 2).show();
	}
	
	gt.pie_charts = function() {
		$('table.pie-chart').each(function() {
			var table = $(this);
			var data = [];
			var labels = table.find('th');
			var values = table.find('td');
			var total = 0;
			for (var i = 0; i < labels.length; i++) {
				data.push({ label: $(labels[i]).html(), data: [[1, $(values[i]).html()]] });
				total += parseInt($(values[i]).html());
			}
			table.after('<div class="chart"></div><div class="legend"></div>');
			var chart = table.next('.chart');
			var legend = chart.next('.legend');
			$.plot(chart, data, {
				series: {
					pie: {
						show: true
					}
				}, legend: {
					noColumns: 1,
					container: legend,
					labelBoxBorderColor: 'none',
					labelFormatter: function(label, series) {
						return Math.round(series.data[0][1] / total * 100) + '% ' + label;
					}
				}, colors: ['#7dc242', '#e0e31c', '#004e26']
			});
		});
	}
	
	gt.cycle_modules = function() {
		var modules_to_cycle = $('.sidebar .modules #slideshow-begin').nextAll().not('.sidebar .modules #slideshow-end').not($('.sidebar .modules #slideshow-end').nextAll());
		if (modules_to_cycle.length == 0) {
		  return false;
		}
		var modules_before_cycle = $('.sidebar #slideshow-begin').prevAll('.module').reverse();
		var modules_after_cycle = $('.sidebar #slideshow-end').nextAll('.module');
		$('.sidebar .modules').remove();
		if (modules_before_cycle.length > 0) {
		  $('.sidebar').append($('<ul class="modules"></ul>').append(modules_before_cycle));
		}
		$('.sidebar').append($('<ul class="modules cycle"></ul>').append(modules_to_cycle));
		if (modules_after_cycle.length > 0) {
		  $('.sidebar').append($('<ul class="modules"></ul>').append(modules_after_cycle));
		}

		var max_height = 0;
		for (var i = 0; i < modules_to_cycle.length; i++) {
		  module = $(modules_to_cycle[i]);
		  if (module.height() > max_height) {
		    max_height = module.height();
		  }
		}
		modules_to_cycle.addClass('slide').css('height', max_height + 'px');

		$('.sidebar .modules.cycle').cycle({
	    speed:  'slow', 
			timeout: 8000,
			pause: true,
	 		after: function() { 
				sifr_replace(); 
			}
		});
	}
	
	$.fn.reverse = [].reverse;
	
	$(document).ready(function() {
		gt.nav();
		gt.search();
		gt.for_media();
		gt.num_comments();
		$('.framed').frame();
		gt.fast_facts();
		gt.your_thoughts();
		gt.clean_article();
		gt.modals();
		gt.videos();
		gt.slides();
		$('.main .module .product-featured-content .items').carousel();
		gt.pie_charts();
		gt.cycle_modules();
	});
})(jQuery);

var omnes = { src: '/assets/scripts/sifr/omnes.swf' };
var sifr_replace = function() {
	sIFR.replace(omnes, {
		useDomLoaded: false,
		selector: 'ul#nav li.primary.selected .label',
		transparent: true,
		forceSingleLine: true,
		offsetTop: 4,
		css: [
			'a {color: #004f27;, text-decoration: none;}',
			'a:hover {color: #004f27;, text-decoration: none;}'
		],
		ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
	});
	sIFR.replace(omnes, {
		useDomLoaded: false,
		selector: 'ul#nav li.primary .label, ul#user li.first .label',
		transparent: true,
		forceSingleLine: true,
		offsetTop: 4,
		css: [
			'a {color: #d6e03d;, text-decoration: none;}',
			'a:hover {color: #d6e03d;, text-decoration: none;}'
		],
		ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
	});
	sIFR.replace(omnes, {
		useDomLoaded: false,
		selector: '.main h1, .main h2, .main h3, .sidebar h3',
		transparent: true,
		css: [
			'.sIFR-root {color: #004f27; leading: -.5;}',
			'a {color: #7DC243;, text-decoration: none;}',
			'a:hover {color: #7DC243;, text-decoration: none;}'
		],
		ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
	});
	sIFR.replace(omnes, {
		useDomLoaded: false,
		selector: '.sidebar h2',
		transparent: true,
		css: [
			'.sIFR-root {color: #ffffff;}',
			'a {color: #ffffff;, text-decoration: none;}',
			'a:hover {color: #ffffff;, text-decoration: none;}'
		],
		ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
	});
	sIFR.replace(omnes, {
		useDomLoaded: false,
		selector: '.tagline',
		transparent: true,
		css: [
			'.sIFR-root {color: #004f27; leading: -.5;}',
			'a {color: #7DC243;, text-decoration: none;}',
			'a:hover {color: #7DC243;, text-decoration: none;}'
		],
		ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
	});
}
sIFR.activate(omnes);
sifr_replace();