if (!('console' in window) || !('firebug' in console)) {
	var names = ['log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'group', 'groupEnd', 'time', 'timeEnd', 'count', 'trace', 'profile', 'profileEnd'];
	window.console = {};
	for (var i = 0, len = names.length; i < len; ++i) {
		window.console[names[i]] = function() {};
	}
}

jQuery.noConflict();

eb = {};

(function($) {

	eb.setupNav = function() {
		$('#nav .selected').parents('#nav li').addClass('selected');
		$('#nav > ul > li').mouseover(function() { 
			$(this).addClass('hover');
		});
		$('#nav > ul > li').mouseout(function() {
			$(this).removeClass('hover');
		});
	}
	
	eb.setupFooter = function() {
		$('#twitter a, #facebook a').attr('target', '_blank');
	}
	
	eb.setupSlideshow = function() {
		if ($('#slides').length == 0) return;
	  $('#slides')
		.wrap('<div id="slideshow_stage"></div>')
		.after('<ul id="slideshow_nav">') 
		.cycle({ 
	    fx: 'fade',
			timeout: 5000,
			speed: 'slow',
	    pager: '#slideshow_nav',
			pagerAnchorBuilder: function(i, el) {
				if ($(el).find('.color').length > 0 && $(el).find('.description').length > 0) {
					return '<li><a class="' + $(el).find('.color').html() + '" href="#"><span class="tail"></span><span class="contents">' + $(el).find('.description').html() + '</span></a></li>';
				}
			}
		});
		$('#slideshow_nav').css('margin-top', ($('#slideshow_nav').parent().height() - $('#slideshow_nav').height()) / 2);
		$('#slideshow_nav li:first').addClass('first');
		$('.slide .featured-video').bind('mousedown', function() {
		  $('#slides').cycle('pause');
			return true;
		});
	}
	
	eb.setupTooltips = function() {
		$('a:not(.share_facebook,.share_twitter)[title], span[title]').livequery(function() {
			$(this).tooltip({
				showURL: false,
				bodyHandler: function(title) {
					return ('<div id="tooltip_top"></div><div id="tooltip_middle">' + this.tooltipText + '</div><div id="tooltip_bottom"></div>');
				}
			});
		});
	}
	
	eb.setupReflections = function() {
		$('img.logo, img.reflect').livequery(function() {
			eb.reflect(this);
		});
	}
	
	eb.reflect = function(img) {
		$(img).reflect({height: 0.40, opacity: 0.25});		
	}
	
	eb.setupToggles = function() {
		if ($('.toggle').length == 0) return;
		$('.toggle').click(function() {
			$(this).next().slideToggle('fast');
			if ($(this).html().indexOf('Close') != -1) {
				$(this).html($(this).html().replace('Close', 'Read'));
			} else {
				$(this).html($(this).html().replace('Read', 'Close'));
			}
			$(this).toggleClass('open_toggle');
			return false;
		}).next().hide();
		$('.toggle').eq(0).click();
	}
	
	eb.setupFilterTables = function() {
		if ($('#filters').length == 0) return;
		$('#filters .filter :checkbox[id!=all]').each(function() {
			if ($('.filter_table .' + $(this).val()).closest('tr, li').length == 0) {
				$(this).attr('disabled', 'disabled');
				$(this).next('label').addClass('disabled');
			}
		});
		$('#filters .filter :checkbox').checked = 'checked';	
		$('#filters .filter :checkbox').click(eb.displayFilterTableRows);
	}
	
	eb.displayFilterTableRows= function(e) {
		if (this.id == 'all' && this.checked) {
			$('#filters .filter :checkbox').each(
				function() {
					this.checked = 'checked';
				}
			);
		} else if (this.id != 'all' && $('#filters .filter :checkbox[id != all]:not(:checked)').length > 0) {
			$('#filters .filter #all').get(0).checked = false;
		}
		if ($('#filters .filter :checkbox[id = all]:checked').length > 0) {
			$('.filter_table tr, .filter_table li').show();
		} else {
			$('#filters .filter :checkbox[id! = all]:not(:checked)').each(function() {
				$('.filter_table .' + $(this).val()).closest('tr, li').hide();
			});
			$('#filters .filter :checkbox[id! = all]:checked').each(function() {
				$('.filter_table .' + $(this).val()).closest('tr, li').show();
			});
		}
		$('.filter_table tr, .filter_table li').removeClass('first');
		$('.filter_table tr:first:visible, .filter_table li:first:visible').addClass('first');
		eb.filterTableRowsCallback();
	}
	
	eb.filterTableRowsCallback = function() {
		return true;
	}

	eb.setupTabs = function() {
		if ($('.tabs').length == 0) return;
		$('.tabs').each(function() {
			$(this).find('li a').click(function() {
				$selected = $(this);
				$selected.closest('ul').find('li a').each(function() {
					if ($(this).get(0) != $selected.get(0)) {
						$(this).closest('li').removeClass('selected');
						$('#' + eb.getHash(this.href)).hide();
					}
				});
				$(this).closest('li').addClass('selected');
				$('#' + eb.getHash(this.href)).show();
				eb.tabShownCallback();
				return false;
			});
		});
	}
	
	eb.tabShownCallback = function() {
		return true;
	}
	
	eb.getHash = function(href) {
		if (href.indexOf('#') == -1) {
			return false;
		} else {
			return href.substring(href.indexOf('#') + 1, href.length);
		}
	}
	
	eb.setupModals = function() {
		if ($('.modal').length == 0) return;
		$('#modal').jqm({ modal:true, ajaxText:'<div class="spinner">Loading...</div>', target:'#modal_content', overlay:70 });
		$('#modal').jqmAddClose('#close_modal');
		$('.modal, #rss a').livequery('click', function() {
			var href = $(this).attr('href');
			$('#modal:visible').jqmHide();
			if (href.indexOf('#') == 0) {
				$('#modal_content').html($($(this).attr('href')).html());
			} else {
				$('#modal').jqm({ ajax:href + '?fragment=true', onHide:eb.closeModal });
				window.location.hash = href.split('/')[href.split('/').length - 1];
			}
			window.scroll(0, 0);
			$('#modal').jqmShow();
			pageTracker._trackEvent('modal', 'view', href);
			return false;
		});
		if (window.location.hash && window.location.hash.length > 1) {
			var identifier = window.location.hash.substring(1, window.location.hash.length)
			if ($('.modal[href$=' + identifier + ']').length != 0) {
				$('.modal[href$=' + identifier + ']').click();
			}
		}
	}
	
	eb.closeModal = function(modal) { 
		modal.w.hide();
		modal.o.remove();
		$('#subscribe').jqmHide();
		$('#modal .video').empty();
		window.location.hash = ''; 
	};
	
	eb.broadcasters = '';
	
	eb.setupShare = function() {
		eb.broadcasters = $('#share #broadcasters').html();
		$('#share').livequery(function() {
			$(this).jqm({overlay:0, onHide: eb.shareClose});
			$(this).jqmAddClose('#close_share');
		});
		$('#share #broadcasters a').livequery('click', function() {
			$('#share').jqmHide();
			return true;
		});
		$('.share').livequery('click', function() {
			var additionalTopOffset = 5;
			var additionalLeftOffset = 160;
			$('#share').css('top', $(this).offset().top + additionalTopOffset);
			$('#share').css('left', $(this).offset().left - additionalLeftOffset);
			var link = $(this);
			var shortShare =	{
		    shorten : function(e) {
		      var url = '';
		      if (link.attr('href').indexOf('http') == -1) {
		        url += 'http://www.ebayinc.com';
		      }
		      url += link.attr('href');
		      BitlyClient.shorten(url, 'eb.bitly');
		    }
			}
			shortShare.shorten();	
			return false;
		});
	}
	
	eb.shareClose = function(share) { 
		share.w.hide();
		$('#share #broadcasters').html(eb.broadcasters);
		window.location.hash = ''; 
	};
	
	eb.bitly = function(data) {
    var bitly_link = null;
    for (var r in data.results) {
        bitly_link = data.results[r]['shortUrl']; 
        break;
    }
		$('#share #broadcasters a').each(function() {
			$(this).attr('href', $(this).attr('href').replace('#', bitly_link));
		});
		$('#share').jqmShow();
	}
	
	eb.readers = '';
	
	eb.setupSubscribe = function() {
	  eb.readers = $('#subscribe #readers').html();
		$('#subscribe').livequery(function() {
			$(this).jqm({overlay:0, onHide: eb.subscribeClose});
			$(this).jqmAddClose('#close_subscribe');
		});
		$('#subscribe #readers a').livequery('click', function() {
			$('#subscribe').jqmHide();
			return true;
		});
		$('.subscribe').livequery('click', function() {
			var additionalTopOffset = 5;
			var additionalLeftOffset = 160;
			$('#subscribe').css('top', $(this).offset().top + additionalTopOffset);
			$('#subscribe').css('left', $(this).offset().left - additionalLeftOffset);
			var link = this;
			$('#subscribe #readers a').each(function() {
				$(this).attr('href', $(this).attr('href').replace('#', escape($(link).attr('href'))));
			});
			$('#subscribe').jqmShow();
			return false;
		});
	}
	
	eb.subscribeClose = function(modal) { 
		modal.w.hide(); 
		$('#subscribe #readers').html(eb.readers);
		window.location.hash = ''; 
	};
	
	eb.setupPrint = function() {
		if ($('.print').length == 0) return;
		$('.print').click(function() {
			window.print();
			return false;
		});
	}
	
	eb.setupJumpSelects = function() {
		if ($('.jump_select').length == 0) return;
		$('.jump_select').change(function() {
			if ($(this).val() != '') {
				window.open($(this).val());
			}
		});
	}
	
	eb.setupEditButtonsInModals = function() {
		$('#modal .cms-edit').livequery(function() {
			$(this).parent().mouseenter(function() {
				parent = this;
				setTimeout(function() {
					$(parent).find('.cms-edit').show();	
				}), 500;
			});
			$(this).parent().mouseleave(function() {
				$(this).find('.cms-edit').hide();
			});
		});
	}
	
	eb.validate = function(form) {
	  var $form = $(form);
	  var $fields = $form.find('.field');
	  var errorMessage = 'The following fields must be valid:\n\n';
	  var hasErrors = false;
	  var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	  $fields.each(function() {
			$field = $(this);
			if (!$field.hasClass('required') && !$field.hasClass('email')) {
				return true;
			}
			$input = $field.find('input').eq(0);
			if (($field.hasClass('required') && $input.val() == '') || ($field.hasClass('email') && !emailFilter.test($input.val()))) {
        hasErrors = true;
        var label = $field.find('label').eq(0).html();
        errorMessage += label + '\n';
        $field.addClass('error');
			} else {
				$field.removeClass('error');
			}
		});
	  if (hasErrors) {
	    alert(errorMessage);
	    return false;
	  } else {
	    return true;
		}
	}
	
	$.fn.scramble = function() {
		function randomize() { 
			return(Math.round(Math.random()) - 0.5); 
		}

		return($(this).each(function() {
    	var $this = $(this);
    	var $children = $this.children();
    	var childCount = $children.length;
 
    	if (childCount > 1) {
      	$children.remove();
 
      	var indices = [];
      	for (i = 0; i < childCount; i++) { 
					indices[indices.length] = i; 
				}
      	indices = indices.sort(randomize);

      	$.each(indices, function(j, k) { 
					$this.append($children.eq(k));
				}); 
    	}
  	}));
	}
	
	eb.prettyDate = function(date) {
		var dateString = '';
		var suffix = 'ago';
		var now = new Date();
		var timeDifference = now.getTime() - date.getTime();
		if (timeDifference < 0) {
			suffix = 'from now';
			timeDifference = timeDifference * -1;
		}
		var weeksDifference = timeDifference / (1000 * 60 * 60 * 24 * 7);
		var daysDifference = timeDifference / (1000 * 60 * 60 * 24);
		var hoursDifference = timeDifference / (1000 * 60 * 60);
		var minutesDifference = timeDifference / (1000 * 60);
		var secondsDifference = timeDifference / (1000);
		if (weeksDifference > 2) {
			var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
			return months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear();
		} else if (weeksDiference > 0) {
			dateString = weeksDifference + ' week' + (weeksDifference == 1 ? '' : 's');
		} else if (daysDiference > 0) {
			dateString = daysDifference + ' day' + (daysDifference == 1 ? '' : 's');
		} else if (hoursDiference > 0) {
			dateString = hoursDiference + ' hour' + (hoursDiference == 1 ? '' : 's');
		} else if (minutesDiference > 0) {
			dateString = minutesDiference + ' minute' + (minutesDiference == 1 ? '' : 's');
		} else if (secondsDiference > 0) {
			dateString = secondsDiference + ' second' + (secondsDiference == 1 ? '' : 's');
		}
		return dateString + ' ' + suffix;;
	}
	
	eb.setupVideos = function() {
		$('.oembed').livequery(function() {
			var video = $(this);
			$.get('/view/page.landing/oembed', { url: video.attr('href'), maxwidth: video.parent().innerWidth() }, function(data) {
				//fix for z-index issue in ie when a user clicks to share content from the video modal
				if (data.html.indexOf("wmode") < 0) {
				    data.html = data.html.replace("<embed ", "<param name=\"wmode\" value=\"transparent\"></param>\n<embed ");
            data.html = data.html.replace("<embed ", "<embed wmode=\"transparent\"");
						data.html = data.html.replace(/width="\d*"/g, "width=\"350\"");
						data.html = data.html.replace(/height="\d*"/g, "height=\"260\"");
        }	
				video.replaceWith(data.html).removeClass('spinner');
			}, 'json');
		});
	}
	
	eb.setupTwitterFeed = function () {
		if ($('#chatter').length == 0) return;
		$(".tweets").twitterFeed({
			userName: "ebayinc",
			numTweets: 6,
			delay: 4000,
			loaderText: "Loading tweets..."
		});	
	}
	
	eb.setupDailyDeals = function() {
		if ($('#daily_deals .deals').length == 0) return;
		$('#daily_deals .deals').cycle({ 
			timeout: 4000,
			speed: 'slow',
			pause: true,
			pauseOnPagerHover: true
		});
	}
	
	eb.setupAdFeeds = function() {
		if ($('#blog_posts').length == 0) return;
		$('#blog_posts').cycle({ 
			timeout: 4000,
			speed: 'slow',
			pause: true,
			pauseOnPagerHover: true
		});
	}
	
	eb.setupFastFacts = function() {
		if ($('#fast_facts').length == 0) return;
		$('#fast_facts').scramble();
		$('#fast_facts').cycle({ 
	    fx: 'fade',
			speed: 'slow',
			timeout: 8000,
			pause: true,
			pauseOnPagerHover: true,
			next: '#next_fact',
			cleartype: true,
			cleartypeNoBg: true,
			after: function() {
				sIFR.replace(gothamLight, {
				  selector: '.fast_fact h3',
					wmode: 'transparent',
				  css: [ '.sIFR-root { color: #ffffff; }'	]
				});
			}
		});
	}
	
	$(document).ready(function() {
		eb.setupNav();
		eb.setupFooter();
		eb.setupSlideshow();
		eb.setupTooltips();
		eb.setupToggles();
		eb.setupFilterTables();
		eb.setupTabs();
		eb.setupModals();
		eb.setupSubscribe();
		eb.setupShare();
		eb.setupPrint();
		eb.setupJumpSelects();
		eb.setupEditButtonsInModals();
		eb.setupReflections();
		eb.setupVideos();
		eb.setupDailyDeals();
		eb.setupFastFacts();
		eb.setupTwitterFeed();
		eb.setupAdFeeds();
	});

})(jQuery);

var gothamLight = { src: '/assets/flash/fonts/gotham_light.swf' };
var gothamBook = { src: '/assets/flash/fonts/gotham_book.swf' };
var gothamBold = { src: '/assets/flash/fonts/gotham_bold.swf' };

function setupFonts() {
	sIFR.activate(gothamLight, gothamBook, gothamBold);
	sIFR.replace(gothamLight, {
	  selector: '.ad h3.white',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #ffffff; }'	]
	});
	sIFR.replace(gothamLight, {
	  selector: '.ad h3.red',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #ef3d43; }'	]
	});
	sIFR.replace(gothamLight, {
	  selector: '.ad h3.green',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #478d34; }'	]
	});
	sIFR.replace(gothamLight, {
	  selector: '.ad h3.blue',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #1461ab; }'	]
	});
	sIFR.replace(gothamLight, {
	  selector: '.ad h3.lightblue',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #00aeef; }'	]
	});
	sIFR.replace(gothamLight, {
	  selector: '#profile h3',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #e29f19; }'	]
	});
	sIFR.replace(gothamBook, {
	  selector: '.intro p, #featured_executives h3',
		wmode: 'transparent',
	  css: [ '.sIFR-root { color: #4c4c4c; }', '.sIFR-root a { color: #4c4c4c; }', '.sIFR-root a:hover { color: #4c4c4c; }'	]
	});
	sIFR.replace(gothamBold, {
	  selector: '.panel_top_dark h2',
		wmode: 'transparent',
		forceSingleLine: true,
	  css: [ '.sIFR-root { color: #ffffff; text-transform: uppercase; }'	]
	});
}

setupFonts();