var nlData = {
	'optin' : [
	{
		'value' : 'gacnl',
		'html' : '<strong>Great American Country Network News: </strong>Country music news, sweepstakes and TV highlights.'
	},
	{
		'value' : 'real',
		'html' : '<strong>FrontDoor Unlocked: </strong>The latest features from FrontDoor.com, the real estate website powered by HGTV. Search homes for sale nationwide. Expert advise for buyers and sellers.'
	},
	{
		'value' : 'market',
		'html' : '<strong>HGTV Shopping: </strong>Shop home and garden products including products seen on HGTV shows.'
	},
	{
		'value' : 'foodnl',
		'html' : '<strong>Food Network Newsletter: </strong>Food ideas, TV highlights, sweepstakes and special events.'
	},
	{
		'value' : 'hgtvnl',
		'html' : '<strong>HGTV Ideas: </strong>Home and garden inspiration, sweepstakes news and TV highlights.'
	},
	{
		'value' : 'decor',
		'html' : '<strong>HGTV Decorating: </strong>Design advice from HGTV\'s leading experts, plus trends, tips and latest colors.'
	},
	{
		'value' : 'gardnl',
		'html' : '<strong>HGTV Gardening: </strong>Timely advice from HGTV\'s master gardener, plus Q&amp;As, cool products and more.'
	},
	{
		'value' : 'kitch',
		'html' : '<strong>HGTV Kitchen Design: </strong>Create your dream kitchen with our monthly guide to new design ideas, favorite products and fun DIY projects.'
	},
	{
		'value' : 'bath',
		'html' : '<strong>HGTV Bath Design: </strong>Make your bathroom the ultimate retreat with our monthly guide to new design trends, great products and fresh decor ideas.'
	},
	{
		'value' : 'quilt',
		'html' : '<strong>HGTV Quilts Online: </strong>Updates on newly available videos, quilt projects, instructions, patterns and more.'
	},
	{
		'value' : 'hgpro',
		'html' : '<strong>HGTV Pro Newsletter: </strong>News, products and best practices for builders, contractors and remodelers.'
	},
	{
		'value' : 'diynl',
		'html' : "<strong>DIY Network Newsletter: </strong>Stay tuned to the latest news on upcoming DIY series and workshops, the hottest DIY projects and exciting new sweepstakes. You'll also receive exclusive e-mail features and subscriber-only projects."
	},
	{
		'value' : 'finenl',
		'html' : '<strong>FINE LIVING Network News: </strong>Best-kept secrets, TV Highlights, special offers and sweepstakes.'
	},
	{
		'value' : 'sweeps',
		'html' : '<strong>Sweepstakes: </strong>News from DIY, HGTV, Food Network, Fine Living, Great American Country Network, and HGTVPro.'
	},
	{
		'value' : '3party',
		'html' : '<strong>Special Offers: </strong>Receive news from Scripps Networks, its affiliates and its third-party sponsors.'
	},
	{
		'value' : 'merch',
		'html' : '<strong>Food Network Store: </strong>Timely updates on new products, sales and more.'
	},
	{
		'value' : 'cookies',
		'html' : '<strong>Food Network 12 Days of Cookies Newsletter: </strong>Terrific cookie recipes delivered in December.'
	},
	{
		'value' : 'fnrod',
		'html' : '<strong>Food Network Recipe of the Day: </strong>Get delicious recipes daily from your favorite Food Network Chefs.'
	},
	{
		'value' : 'food2',
		'html' : '<strong>Food2: </strong>Entertaining, cooking and cocktailing with a fresh, hip approach.'
	},
	{
		'value' : 'health',
		'html' : '<strong>Healthy Eats: </strong>Lighter recipes, nutrition news and get-healthy tips and advice.'
	},
	{
		'value' : 'fnprog',
		'html' : '<strong>Food Network TV Highlights: </strong>Get the inside scoop on new shows and specials.'
	}]
};

(function($) {
	$.fn.createNewsletters = function(options) {
		var defaults = {
			nlList		: [],
			startNum	: -1,
			idPrefix	: 'fvOptIn'
		};
		var options = $.extend(defaults, options);
		return this.each(function(i) {
			var nlCount = options.startNum != -1 ? options.startNum : 2;
			
			/*
				if startNum is being used and nlCount exist already, continue from nlCount
				(this prevents id reuse e.g [loop 1] id="fvOptIn01", id="fvOptIn02", id="fvOptIn03" [loop 2] id="fvOptIn01")
			*/
			if ( options.startNum == -1 )
				if ( !isNaN(parseInt(jQuery.data(document, 'nlCount'))) )
					nlCount = parseInt(jQuery.data(document, 'nlCount'));

			if ( !$(this).get(0) ) return;
			for ( var i=0; i<options.nlList.length; i++ ) {
				var found = false;
				for ( j=0; j<nlData.optin.length; j++ ) {
					if ( options.nlList[i] == nlData.optin[j].value ) {
						var value = nlData.optin[j].value;
						var html = nlData.optin[j].html;
						var idC = nlCount.toString().length == 1 ? '0' + nlCount : nlCount;
						var id = options.idPrefix + idC;
						
						var div = document.createElement('div');
						$(div).addClass('gffu_nl_tbl');
						$(this).append(div);
						
						var input = document.createElement('input');
						$(input)
							.attr({
								'type': 'checkbox',
								'id': id,
								'name': id
							})
							.val(value);
						$(div).append(input);
						
						var label = document.createElement('label');
						$(label)
							.attr('for', id)
							.html(html);
						$(div).append(label);
						
						nlCount++;
						break;
					}
				}
			}
			jQuery.data(document, 'nlCount', nlCount);
		});
	};
})(jQuery);

jQuery.sweepsUtils = {
	applyParamsToInputs: function( options ) {
		var defaults = { list: [] };
		var options = $.extend(defaults, options);
		for ( var i=0; i<options.list.length; i++ ) {
			$('input[name=' + options.list[i].split(':')[0] + ']').val(jQuery.sweepsUtils.getParam(options.list[i].split(':')[1]));
		}
	},
	getParam: function( param ) {
		var q = document.location.search || document.location.hash;
		if ( param == null ) return q;
		if ( q ) {
			var pairs = q.substring(1).split('&');
			for ( var i=0; i < pairs.length; i++ ) {
				if ( pairs[i].substring(0, pairs[i].indexOf('=')) == param ) {
					return pairs[i].substring((pairs[i].indexOf('=')+1));
				}
			}
		}
		return '';
	},
	createNewsletterList: function( brand ) {
		jQuery('#mainNLWrap, #additionalNLWrap').html('');
		switch ( brand ) {
			case 'diy': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['diynl']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'market', 'hgtvnl', 'hgpro', 'foodnl', 'gacnl', 'finenl', 'sweeps', '3party']});
				break;
			}
			case 'gac': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['gacnl']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'hgtvnl', 'decor', 'gardnl', 'market', 'kitch', 'bath', 'hgpro', 'diynl', 'foodnl', 'cookies', 'fnrod', 'food2', 'health', 'sweeps', '3party']});
				break;
			}
			case 'food': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['foodnl', 'merch', 'cookies', 'fnprog']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'market', 'hgtvnl', 'hgpro', 'diynl', 'finenl', 'gacnl', 'sweeps', '3party']});
				break;
			}
			case 'hgtv': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['hgtvnl', 'decor', 'gardnl', 'kitch', 'bath', 'quilt']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'market', 'foodnl', 'hgpro', 'diynl', 'finenl', 'gacnl', 'sweeps', '3party']});
				break;
			}
			case 'hpro': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['hgpro']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'market', 'hgtvnl', 'diynl', 'foodnl', 'gacnl', 'finenl', 'sweeps', '3party']});
				break;
			}
			case 'fine': {
				jQuery('#mainNLWrap').createNewsletters({nlList: ['finenl']});
				jQuery('#additionalNLWrap').createNewsletters({nlList: ['real', 'market', 'hgtvnl', 'hgpro', 'foodnl', 'gacnl', 'diynl', 'sweeps', '3party']});
				break;
			}
		}
	}
};

/*****************/
/* Sweeps Cookie */
/*****************/
/* this object assumes that jQuery is present */
/**********************************************/
function SweepsCookie(options) {
	this.aryFields = [];
	this.aryCookies = [];
	var defaults = {
		cookieNamePrefix		: 'scVal',
		numSeparateEntries		: 1,
		domain					: ''
	};
	this.options = jQuery.extend(defaults, options);
	this.checkCookies();
}

SweepsCookie.prototype.addField = function( field ) {
	this.aryFields.push(field);
};

SweepsCookie.prototype.addFields = function( wrap ) {
	var all = jQuery('input', wrap);
	var sc = this;
	jQuery.each(all, function(i) {
		sc.aryFields.push(this);
	});
};

SweepsCookie.prototype.checkCookies = function() {
	for ( var i=0; i<this.options.numSeparateEntries; i++ ) {
		var tmp = null;
		if ( jQuery.cookie(this.options.cookieNamePrefix + i) )
			tmp = jQuery.cookie(this.options.cookieNamePrefix + i);
		if ( tmp != null || tmp == '' )
			this.aryCookies[this.options.cookieNamePrefix + i] = tmp;
	}
	/* use this line if you wish to create a popup div showing the links of all saved email addresses */
	//if (this.getNumCookies() > 1) this.createLinksPopup(i);
}

SweepsCookie.prototype.parseEmail = function( str ) {
	return str.split('|')[2];
};

SweepsCookie.prototype.addValuesToCookie = function( emailVal, cookieValue ) {
	/*
		here is where we will try to shift any existing
		vals in aryCookies down (this.options.cookieNamePrefix[1] - this.options.cookieNamePrefix[9])
		if they do not match email vals
	*/
	var iNewEmail = 1;
	if ( this.getNumCookies() > 0 ) {
		for ( var i=0; i<this.getNumCookies(); i++ ) {
			var curEmail = emailVal;
			var aryCurEmail = this.parseEmail(this.aryCookies[this.options.cookieNamePrefix + i]);
			if ( curEmail != aryCurEmail ) {
				jQuery.cookie(this.options.cookieNamePrefix + iNewEmail, this.aryCookies[this.options.cookieNamePrefix + i], { expires: 3650, path: '/', domain: this.options.domain });
				iNewEmail++;
			}
		}
	}
	/* add the email val to this.options.cookieNamePrefix[0] */
	jQuery.cookie(this.options.cookieNamePrefix + '0', cookieValue, { expires: 3650, path: '/', domain: this.options.domain });
}

SweepsCookie.prototype.createCookieString = function() {
	var str = '';
	for (var i=0; i<this.aryFields.length; i++) {
		switch ( this.aryFields[i].type.toLowerCase() ) {
			case 'text': case 'select-one': {
				if ( this.aryFields[i].value != '' )
					str += this.aryFields[i].name + ':' + this.aryFields[i].value + '|';
				break;
			}
			case 'radio': {
				if ( this.aryFields[i].checked )
					str += this.aryFields[i].name + ':' + this.aryFields[i].value + '|';
				break;
			}
			case 'checkbox': {
				if ( this.aryFields[i].checked )
					str += this.aryFields[i].name + ':1' + '|';
				break;
			}
		}
	}
	//alert(str.length)
	this.addValuesToCookie(jQuery('#fvEmail').val(), str.substring(0, str.length - 1));
};

SweepsCookie.prototype.getNumCookies = function() {
	var count = 0;
	for ( x in this.aryCookies )
		count++;
	return count;
}

SweepsCookie.prototype.fillWithVal0 = function() {
	var numCookies = 0;
	numCookies = this.getNumCookies();
	for ( var i=0; i<numCookies; i++ )
		this.applyFieldValues(this.aryCookies[this.options.cookieNamePrefix + '0']);
}

SweepsCookie.prototype.testFieldPopulation = function(emailVal) {
	for ( var i=0; i<this.getNumCookies(); i++ )
		if ( this.parseEmail(this.aryCookies[this.options.cookieNamePrefix + i]).indexOf(emailVal) != -1 )
			this.applyFieldValues(this.aryCookies[this.options.cookieNamePrefix + i]);
}

SweepsCookie.prototype.applyFieldValues = function( aryCookieNum ) {
	var nameValPair = aryCookieNum.split('|');
	for ( var j=0; j<nameValPair.length; j++ ) {
		var name = nameValPair[j].split(':')[0];
		var value = nameValPair[j].split(':')[1];
		for ( var i=0; i<this.aryFields.length; i++ ) {
			if ( this.aryFields[i].name == name ) {
				switch ( this.aryFields[i].type.toLowerCase() ) {
					case 'text': {
						this.aryFields[i].value = value;
						break;
					}
					case 'select-one': {
						for ( var x=0; x<this.aryFields[i].length; x++ )
							if ( this.aryFields[i].options[x].value == value )
								this.aryFields[i].selectedIndex = x;
						break;
					}
					case 'radio': {
						this.aryFields[i].checked = false;
						if ( this.aryFields[i].value == value )
							this.aryFields[i].checked = true;
						break;
					}
					case 'checkbox': {
						this.aryFields[i].checked = true;
						break;
					}
				}
			}
		}
	}
}