$(function() {
	var referrer = document.referrer;
	if(!referrer.match(/(\w+\.)?flowtown\.com/)) {
		var code = getUrlVars()['code'];
		if(!$.cookie('code') && code)
			$.cookie('code', code, {expires: 360,path: '/',domain: '.'+document.domain.match(/(\w+\.\w+)$/)[0]});	
		if(!$.cookie('referer'))
			$.cookie('referer', referrer, {expires: 360,path: '/',domain: '.'+document.domain.match(/(\w+\.\w+)$/)[0]});
	}
	
	$("form#try-discovery").submit(function(e){
		e.preventDefault();
		$.post($(this).attr('action'), {email: $(this).find("input#email").val()}, function(html){
			$("#pulled_data").replaceWith(html);
		});
	})
	
	$("input#email").focus();
	
  $("#try_again").live('click', function(e) {
    e.preventDefault();
    $("input#email").focus().select();
  });

})

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
