// $('body').bind('sp.loaded', function() {
$(document).ready(function() {
	// Track Main Navigation Clicks
	$('#nav ul li a').click(function(){
		sp.analytics.track('Main Nav', 'click', $(this).html());
	});

	// Tack Department Module Clicks
	$('#departments ul li a').click(function(){
		sp.analytics.track('Departments', 'click', $(this).html());
	});

	// Track Search Box Usage
	$('#cse-search-box').submit(function(){
		sp.analytics.track('Search', 'submit', $(this).find('#search-box').val());
	});
	
	$('.use-tabs').tabs();
	
	// Insert Email Share Form
	if($('.share-email').length){
		sp.share.load_email_form();
	}
	
	// Give margin to related listings
	if($('#content #mod-related').length){
		$('#content .mod .related:even').addClass('left-side');
	}

	//	Insert the Digg badge on content pages
	if($('.DiggThisButton')){
		$.getScript('http://widgets.digg.com/buttons.js');
	}
	
	// Insert the Retweet badge on content pages
	if($('#share-badges').length && typeof(retweet_url) != 'undefined'){
		$('<iframe id="badge-tweet" width="50" height="61" frameborder="0" scrolling="no" src="http://api.tweetmeme.com/button.js?source=SportsPickle&service=bit.ly&style=normal&url=' + retweet_url + '"></iframe>').appendTo('#share-badges');
	}
	
	//Facebook
	if($('.share-facebook-badge').length) {
		$.getScript('http://static.ak.fbcdn.net/connect.php/js/FB.Share');
	}	
	
	//	Insert Recent Tweets Module	
	if($('#twitter-mod').length){
		$.getScript('http://twitter.com/javascripts/blogger.js');
		$.getScript('http://twitter.com/statuses/user_timeline/sportspickle.json?callback=twitterCallback2&count=5');
	}
	
	//	Keepup Module
	$('#keepup-module .input-text').bind('click focus', function(){
		if(this.value == 'Enter your email'){ this.value = ''; }
	});
	
	$('#keepup-module .input-text').blur(function(){
		if(this.value == ''){ this.value = 'Enter your email'; }
	});
	
	$('#keepup-module .input-submit').click(function(){
		sp.newsletter.subscribe('#keepup-module');
		return false;
	});
	
	if($('#keepup-module').length){
		$('#keepup-module a').click(function(){
			sp.analytics.track('Keep Up', 'click', $(this).attr('title'));
		});
	}
	
	//	Alternative to target="blank"
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

$(window).load(function(){
	
	if(sp.cookie.get('newsletter_first_impression') == 3 && sp.cookie.get('newsletter_overlay') != 1 && $('.lightbox-newsletter').length > 0) {
		$('.lightbox-newsletter')
			.find('img')
				.click(function(){
					sp.ajax.ajax({
						url: '/ajax/email_subscription/subscribe',
						options: { parameters: 'parent=.lightbox-newsletter&email='+$(this).siblings('input').val()+'&source=Lightbox' }
					})										 
				})
			.end()
			.modal({
				opacity: 80,
				overlayId: 'overlay_modal',
				overlayClose: true,
				closeClass: 'close',
				minWidth: 432,
				minHeight: 300,
				zIndex: 1499997,
				onOpen: function(dialog) {
					dialog.data.show();
					dialog.overlay.show();
					dialog.container.show();
					
					// user sees the overlay once
					sp.cookie.set('newsletter_overlay', 1, 365);
				},
				onClose: function(dialog) {
					$.modal.close();
				}
			});
			
	}

});
