var Export = function(key, value) {
	window[key] = value;
};

HubSpotFormSpamCheck_LeadGen_ContactForm_46178_m0 = function() {
	var key = document.getElementById('LeadGen_ContactForm_46178_m0spam_check_key').value;
	var sig = '';
	for (var x = 0; x< key.length; x++ ) {
		sig += key.charCodeAt(x)+13;
	}
	document.getElementById('LeadGen_ContactForm_46178_m0spam_check_sig').value = sig; 
	/* Set the hidden field to contain the user token */
	var results = document.cookie.match ( '(^|;) ?hubspotutk=([^;]*)(;|$)' );
	if (results && results[2]) {
		document.getElementById('LeadGen_ContactForm_46178_m0submitter_user_token').value =  results[2];
	} else if (window['hsut']) {
		document.getElementById('LeadGen_ContactForm_46178_m0submitter_user_token').value = window['hsut'];
	}
	return true;
};

$(function() {
	var LINE_BREAK = "\r\n";
	var SAMPLE_TEMPLATE = "<div class='span-5'> <img src='{{image}}' width='141' /> <p class='byline small'>{{quote}}</p> </div> <div class='span-10'> <h3 class='white'>{{heading}}</h3> <p>{{text}}</p> </div>";

	var PRODUCT_TEMPLATE = "<div class='clearfix'>{{#videos}}<div class='column'><a href='{{player}}' class='cta-video'><img src='{{thumbnail}}' alt='{{description}}' height='100' /></a></div>{{/videos}}</div>";
	/*
	 * Parse this format into something reasonable
	 *
	{{{
	video: http://player.backlight.tv/player/?video_code=qn83R14x0XTxFe5DSqdh5A6239EQ90L6239EQ90L
	title: Here Is A Headline =
	description: Here is some description text. here is some more text.... ==
	image: image/url
	by: Dr. So And So - Standford University
	}}}
	 */
	var parse_overlay_payload= function(payload) {
		var i, j, line, m, match;
		var content = payload.post_content;
		var res = {
			heading: null,
			text: null,
			swf_url: null,
			image: null,
			quote: null,
		}

		bits = content.split(LINE_BREAK);

		var matchers = {
			heading: /^title: (.+)$/,
			text: /^description:(.+)$/,
			swf_url: /^video:(.+)$/,
			image: /^image:(.+)$/,
			quote: /^by: (.+)$/,
			playlist: /^playlist: (.+)$/,
		};

		for ( i=0; i<bits.length; i++ ) {
			line = bits[i].trim();
			for ( m in matchers ) {
				if ( matchers.hasOwnProperty(m) ) {
					match = line.match(matchers[m]);
					if ( match && (match.length === 2)) {
						res[m] = match[1];
					}
				}
			}
		}
		return res;
	};
	
	var signup_page_stuff = function() {
		if ( !$("#LeadGen_ContactForm_46178_m0_Description").length ) {
			$("#post-40 form").append("<input type='hidden' id='LeadGen_ContactForm_46178_m0_Description' />");
		}
		$("#LeadGen_ContactForm_46178_m0_Description").val("");
		$('a.toggle').bind('click', function(e) {
				var anchor = $(e.target),
				on = anchor.hasClass('yes'),
				id = anchor.attr('id');

				var selected = $('#LeadGen_ContactForm_46178_m0_Description').val();
				if ( selected.trim() === "" ) {
					selected = "{}";
				}
				selected = $.parseJSON(selected);

				if ( on ) {
					anchor.removeClass('yes').addClass('no');
					selected[id] = false;
				} else {
					anchor.removeClass('no').addClass('yes');
					selected[id] = true;
				}

				// persist the changes back
				$('#LeadGen_ContactForm_46178_m0_Description').val(JSON.stringify(selected));
				console.log(selected);
				return false;
		});

		$('input.clearonfocus').bind('focus', function(e) {
			var target = $(e.target),
			    val = target.val().trim(),
			    orig = target.attr('orig');

			if ( typeof orig === 'undefined' || val === orig ) {
				target.attr('orig', val);
				target.val('');
			}
			return true;
		});

		$('input.clearonfocus').bind('blur', function(e) {
			var target = $(e.target),
			    val = target.val().trim(),
			    orig = target.attr('orig');

			if ( val === "" ) {
				target.val(orig);
			}
			return true;
		});
	};

	var samples_page_stuff = function() {
		$('table.products a').click(function(e) {
			$('#video-descr').empty();
			show_overlay();
			var url = $(this).attr('href');
			$.ajax({
				url: url,
				context: document.body,
				success: function(payload) {
					var parsed = parse_overlay_payload(payload);
					set_video(parsed.swf_url);
					$('#video-descr').append(Mustache.to_html(SAMPLE_TEMPLATE, parsed));
					window.location.hash = 'main';
				}
			});
			return false;
		});
	};

	var PAGE_HANDLERS = {
		"/samples" : samples_page_stuff,
		"/sign-up" : signup_page_stuff,
	};

	for ( i in PAGE_HANDLERS ) {
		if ( PAGE_HANDLERS.hasOwnProperty(i) ) {
			var path = i;
			if ( path === window.location.pathname ) {
				PAGE_HANDLERS[i]();
			}
		}
	}
	
	Export('SWF_WIDTH','720');
	Export('SWF_HEIGHT','428');
});

