$(document).ready(function() {
	
	var pageURL = encodeURIComponent(window.location.href);
	var productName = encodeURIComponent(document.title.split(' - ', 1));

	$('.saving-promo').click(
		function(e) {
			promoTitle = $(this).next().children('.title').text();
			promoText = $(this).next().children('.copy').html();
			$('#saving-overlay h6').html(promoTitle);
			$('#saving-overlay p').html(promoText);
			$('#saving-overlay').css('top', (e.pageY-225)+'px');
			$('#saving-overlay').fadeIn('fast');
			return false;
		}
	);
	
	//Bit.ly Tweet & Track
	var TweetAndTrack = {};
	
	BitlyCB.alertResponse = function(data) {
		var msg = 'Check out the '+productName;
		var first_result;
		for (var r in data.results) {
			first_result = data.results[r]; break;
		}
		//var short = first_result.shortUrl;
		var tweet_url = "http://twitter.com/home?status=" + msg + " " + encodeURIComponent(first_result.shortUrl);
		window.open(tweet_url,'twitter','');
		return false;
	};
	
	$('#s-twitter a').click(function(e) {
		pageURL = document.location.toString();
		BitlyClient.call('shorten', {'longUrl': pageURL}, 'BitlyCB.alertResponse');
		return false;
	});
	
	$('#s-linkedin a').click(function(e) {
		var pageURL = document.location.toString();
		var desc = $('meta[name=description]').attr('content');
		var linkedin = 'http://www.linkedin.com/shareArticle?mini=true&url='+pageURL+'&title='+productName+'&summary='+desc;
		window.open(linkedin,'ptli','height=400,width=500').focus();
	});
	
	$('#s-myspace a').click(function(e) {
		var tar = 'http://www.myspace.com/Modules/PostTo/Pages/?u='+pageURL;
		window.open(tar,'ptm','height=400,width=500').focus();
		return false;
	});
	
	$('#s-digg a').click(function(e) {
		var desc = $('meta[name=description]').attr('content');
		var tar = 'http://digg.com/submit?url='+pageURL+'&title='+productName+'&bodytext='+desc+'&topic=tech_news';
		window.open(tar,'digg','').focus();
		return false;
	});
	
	$('#s-delicious a').click(function(e) {
		var tar = 'http://delicious.com/save?v=5&noui&jump=close&url='+pageURL+'&title='+productName;
		window.open(tar,'delicious','toolbar=no,width=550,height=550').focus();
		return false;
	});

	$('.next a, .prev a, a.jump-to-tab').livequery('click', function() {
		var targetTab = parseInt($(this).attr('rel'));
		$.scrollTo('530px', 400, {onAfter: function() {
			$('#tabs-cnt').tabs('select', targetTab);
		}});
		return false;
	});
	
	
	$('#notify-me-form').validate({
		onfocusout: false,
		onkeyup: false,
		rules: {
			EMAIL_ADDRESS_NOTIFY: {
				required: true,
				email: true
			}
		},
		messages: {
			EMAIL_ADDRESS_NOTIFY: {
				required: 'Oops, it looks you forgot to enter your email address.',
				email: 'Oops, it looks like the email address you entered is invalid.'
			}
		},
		errorPlacement: function(error, element) {
			$('#oos label.error[generated="true"]').remove();
			$('#notify-me-error').css('display','none');
			error.insertBefore('#notify-me-form');
		}
	});
	
	$('#notify-me-btn').click(function() {
		if ($('#notify-me-form').valid())
			return notify();
	});
	
	var notify = function() {
		var SuccessMsg = 'We will email you a notification';
		$('#oos label.error[generated="true"]').remove();
		$('#notify-me-error').css('display','none');
		$('#notify-me-btn').addClass('disabled');
		$('#notify-me-btn').attr('disabled','disabled');
		var destination = window.location.protocol + '//' + window.location.hostname + $('#notify-me-form').attr('action');
		$.post(destination, $('#notify-me-form').serialize(), function(statusCode, errorLevel) {
			if($.trim(errorLevel) == 'success' && (statusCode.match(SuccessMsg) != null))
			{
				$('#notify-me-form').css('display','none');
				$('#tag').css('display','none');
				$('#notify-me-success').css('display','block');
			}
			else
			{
				$('#notify-me-error').css('display','block');
				$('#notify-me-btn').removeAttr('disabled');
				$('#notify-me-btn').removeClass('disabled');
			}
		}, 'text');
		return false;
	};
});
