$(function() {
	var OVERLAY_VIDEO_SEL = '#overlay_video';
	var OVERLAY_BG_SEL = '#overlay-bg';
	var MAIN_OVERLAY_SEL = '#main-overlay';

	var set_video = function(swf_url) {
		swfobject.embedSWF(swf_url, "overlay_video", SWF_WIDTH, SWF_HEIGHT, "9.0.0");
	};

	var clear_video = function() {
		$("#overlay_video_wrapper").html("<div id='overlay_video'></div>");
		$("#video-descr").empty();
	};

	var show_overlay = function() {
		$(OVERLAY_BG_SEL).show();
		$(MAIN_OVERLAY_SEL).show();
	};

	var hide_overlay = function() {
		$(OVERLAY_BG_SEL).hide();
		$(MAIN_OVERLAY_SEL).hide();
	}

	$('#overlay-close a').click(function(e) {
		clear_video();
		hide_overlay();
	});

	var render_playlist = function(playlist) {
		var PLAYLIST_TEMPLATE = "<div class='clearfix'>{{#videos}}<div class='span-5 column'><a href='{{player}}' class='cta-video block'><img src='{{thumbnail}}' height='100' /></a><p class='small prepend-top video-descr'>{{title}}</div>{{/videos}}</div>",
			proxy_url = "http://www.cantaloupe.tv/wp-content/themes/cantaloupe/playlist_proxy.php?feed=" + encodeURIComponent(playlist);

			$.ajax({
				url: proxy_url,
				context: document.body,
				success: function(resp) {
					var json = $.parseJSON(resp);
					if ( typeof json.Success !== 'undefined' ) {
						var success = json.Success;
						var title = success.title;
						var items = success.items;
						if ( items.length > 0 ) {
							var first_item = items[0];
							var swf_url = first_item.player;
							items = items.map(function(i) {
								var d = i.description;
								if ( typeof d === 'undefined' || d === null ) {
									d = '';
								}
								if ( d.length > 150 ) {
									d = d.substring(0, 150) + '...';
									i.description = d;
								}
								return i;
							});
							$('#video-descr').empty().append(Mustache.to_html(PLAYLIST_TEMPLATE, {videos: items}));
							bind_click_handlers();
							set_video(swf_url);
							show_overlay();
							window.location.hash = 'main';
						}
					}
				}
			});
	};

	var CTA_HANDLERS = {
		video : function(e) {
			var a = $(this),
			swf = $(a).attr('href');

			set_video(swf);
			show_overlay();
			window.location.hash = 'main';
			return false;
		},
		frame : function(e) {
			var a = $(this),
			frame_src = $(a).attr('href');

			var frame = $('<iframe/>')
			frame.attr('width', '720');
			frame.attr('height', '577');
			frame.attr('src', frame_src)
			frame.attr('scrolling', 'no')
			frame.attr('frameBorder', '0');

			show_overlay();
			$(OVERLAY_VIDEO_SEL).append(frame);
			window.location.hash = 'main';
			return false;
		},
		playlist : function(e) {
			var url = $(this).attr("href");
			render_playlist(url);
			return false;
		} 
	};

	var selectors_to_handlers = {
		'a.cta-video' : CTA_HANDLERS['video'],
		'a.play-btn' : CTA_HANDLERS['video'],
		'a.cta-playlist' : CTA_HANDLERS['playlist'],
		'a.cta-frame' : CTA_HANDLERS['frame']
	};

	var bind_click_handlers = function() {
		for ( i in selectors_to_handlers ) {
			if ( selectors_to_handlers.hasOwnProperty(i) ) {
				console.log("binding click events from " + i);
				$(i).bind('click', selectors_to_handlers[i]);
			}
		}
	};

	Export("show_overlay", show_overlay);
	Export("set_video", set_video);
	Export("bind_click_handlers", bind_click_handlers);
	Export("render_playlist", render_playlist);

	bind_click_handlers();


	/**
	 * Start New CTAs
	 */
	var tabs = $('#bottom-cta-tabs a');
	$(tabs).click(function(e) {
		tabs.removeClass('b');
		$(this).addClass('b');
		var tab = $(this).attr('id');
		tab = $('#' + tab.replace('-link', ''));
		$('.tab').addClass('hide');
		tab.removeClass('hide');
		return false;
	});
});

