jQuery.noConflict();

(function($) {

var Video = {
	el: function() {
		return $('#youtube');
	},
	cont: function() {
		return $('#lightbox');
	},
	init: function() {
		var el = this.el();
		this.template = new Template(el, true);
		
		function parseVideo(url) {
			var results = /(?:#|&)video=([^&]*)(?:$|&)/.exec(url);
			if (results) {
				return results[1];
			}
		}
		
		var videos = {};
		$('#frames li').each(function(i, el) {
			var video = $(el).find('a').attr('href');
			if (video) {
				video = parseVideo(video);
				if (video) {
					var title = $(el).find('p').html();
					
					title = title.replace(/<br>/g, ' ').replace(/<[^>]+>/g, '');
					
					videos[video] = { title: title };
				}
			}
		});
		
		$('#content a, #masthead a').each(function(i, el) {
			var video = parseVideo(el.href);
			
			videos[video] = { title: el.title };
		});
		
		this.videos = videos;
	},

	_stop: function() {
		this.el().prev().after(this.el().remove()); // Required for IE, otherwise video doesn't stop playing
		this.el().css('display', 'none');
	},
	
	close: function() {
		this.isOpen = false;
		ShareBox.isOpen = false;
		this._stop();
		this.cont().fadeOut(1000);
	},
	
	open: function(animated, cb) {
		if (animated) {
			var that = this;
			this.cont().fadeIn(1000, function() {
				that.isOpen = true;
				if (cb) cb.call(that);
			});
		}
		else {
			this.cont().css('display', 'block');


			cb.call(this);
		}
	},
			
	setVideo: function(video, animated) {
		function run() {
			this.template.substitute({
				'video-id': video
			});
		}
		
		if (this.isOpen) {
			run.call(this);
		}
		else {
			this.open(animated, run);
		}
		this.currentVideo = video;
	}
};

var shareTemplate;
function loadPage(url, first) {
	var results = /(?:#|&)video=([^&]*)(?:$|&)/.exec(url);
		
	if (results) {
		Video.setVideo(results[1], true);
		shareTemplate.substitute({
			url: url,
			title: Video.videos[Video.currentVideo].title
		});
		$('.twitter-share-button').each(function() {
			//new twttr.TweetButton(this).render();
		});
	}
	else if (first) {
		Video.cont().css('display', 'none');
	}
}

var scrollPos;

function getOffset(width, anchor) {
	var offset = { left: 0, top: anchor.offset().top + anchor.outerHeight() };
	
	var x = anchor.offset().left;
	if (x + width > $(window).width()) {
		offset.left = $(window).width() - width;
	}
	else {
		offset.left = x;
	}
	
	return offset;
}

function reposition(el, anchor) {
	var offset = getOffset(el.outerWidth(), anchor);
	el.offset(offset);
}

var ShareBox = {
	viewInits: {
		email: function() {
			$('#share-email-message-sent').hide();
		}
	},
	viewEls: {
		options: function() { return $('#share-options') },
		email: function() { return $('#share-email') }
	},
	
	cont: function() { return $('#sharebox') },
	_elForViewName: function(viewName) {
		return this.viewEls[viewName]();
	},
	open: function(animate) {
		var cont = this.cont();

		if (animate) {
			cont.fadeIn();
		}
		else {
			cont.css('display', 'block');
		}
		reposition(cont, $('#share'));
		this.isOpen = true;
	},
	close: function(animate) {
		var cont = this.cont();
		if (animate) {
			cont.fadeOut();
		}
		else {
			cont.css('display', 'none');
		}
		this.isOpen = false;
	},
	
	setView: function(viewName, animated) {
		var init;
		if (init = this.viewInits[viewName]) {
			init();
		}
	
		if (animated && this.currentViewName) {
			var el = this._elForViewName(this.currentViewName);
			var cont = this.cont();
			var nextEl = this._elForViewName(viewName);
			
			el.fadeOut(function() {
				var offset = getOffset(nextEl.outerWidth(), $('#share'));
				offset.left -= cont.offsetParent().offset().left;
				offset.top -= cont.offsetParent().offset().top;
				
				cont.animate($.extend({
					width: nextEl.outerWidth(),
					height: nextEl.outerHeight()
				}, offset), function() {
					nextEl.fadeIn();
				});
			});
		}
		else {
			if (this.currentViewName) {
				var el = this._elForViewName(this.currentViewName);
				el.css('display', 'none');
			}
			
			var el = this._elForViewName(viewName);
			
			var cont = this.cont();
			
			el.css('display', 'block');
			cont.css({
				width: el.outerWidth(),
				height: el.outerHeight()
			});
		}
		this.currentViewName = viewName;
	}
};

$(document).ready(function() {
	shareTemplate = new Template($('#sharebox'), true);
	
	Video.init();
	loadPage(document.location.href, true);
	
	$('#close').click(function() {
		Video.close();
		return false;
	});
			
	$('a').live('click', function() {
		if ($(this).attr('href').indexOf('#') != -1) {
			loadPage(this.href);
		}
	});
	
	$(document).delegate('#share', 'click', function() {
		if (ShareBox.isOpen) {
			ShareBox.close(true);
		}
		else {
			ShareBox.open(true);
			ShareBox.setView('options');
		}
		return false;
	});

	$(document).delegate('#emailshare a', 'click', function() {
		ShareBox.setView('email', true);
		return false;
	});

	var timerID;
	$(document).delegate('#cancel, #emailclose', 'click', function() {
		ShareBox.setView('options', true);
		clearTimeout(timerID);
		return false;
	});
	
	$(document).delegate('#emailSubmit', 'click', function() {
		var form = $('#share-email form').get(0);
		
		var data = {
			email: form.email.value,
			recipients: form.recipient.value,
			message: form.message.value,
			'message-suffix': form['message-suffix'].value,
			subject: form.subject.value
		};
		
		$.ajax({
			url: '/social_email.php',
			data: data,
			type: 'post'
		});
		
		$('#share-email-message-sent').fadeIn(function() {
			timerID = setTimeout(function() {
				ShareBox.close(true);
			}, 2000);
		});
		
		return false;
	});
});

})(jQuery);

