PKG = {
	packageID: 1,
	bannerImage: 'bonus-edition.jpg',
	state: null,
	stateAbbrev: null,
	incTotal: 0,
	subTotal: 0,
	discountFactor: 0,
	discount: 0,
	preShippingTotal: 0,
	showEnabled: true
};
PKG.setVars = function (data) {
	PKG.packageID = data.packageID;
	PKG.discountFactor = (data.discount / -100);
	PKG.bannerImage = data.bannerImage;
}
PKG.show = function (id) {
	if(PKG.showEnabled === true) {
		if($(id).css("display") == 'none') {
			$(id).show();	
		}
		else {
			$(id).hide();	
		}
	}
}
PKG.fMoney = function (num) {
	num = num / 100;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0";
	}
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100+0.50000000001);
	var cents = num % 100;
	num = Math.floor(num / 100).toString();
	if(cents < 10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length-(4 * i + 3)) + ',' + num.substring(num.length-(4 * i + 3));
	}
	return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}
PKG.add = function (value, total) {
	return total + (value * 100)
}
PKG.subtract = function (value, total) {
	return total - (value * 100)
}
PKG.calculate = function () {
	PKG.discount = PKG.subTotal * PKG.discountFactor;
	PKG.preShippingTotal = PKG.subTotal + PKG.discount;
	$('#subTotal').html('Subtotal : ' + PKG.fMoney(PKG.subTotal));
	$('#discount').html('Discount : ' + PKG.fMoney(PKG.discount));
	$('#preShippingTotal').html('** Pre-Shipping Total : ' + PKG.fMoney(PKG.preShippingTotal));
}
PKG.reset = function (item) {
	PKG.incTotal = 0;
	PKG.subTotal = 0;
	PKG.discount = 0;
	PKG.preShippingTotal = 0;
	$(item).empty();
}
PKG.renderBase = function (data, appendTo) {
	appendTo = '#' + appendTo;
	var params = {appendTo: '#package'}
	$('<img/>')
		.attr('src', '/assets/images/packages/' + PKG.bannerImage)
		.attr('alt', '')
		.appendTo(appendTo)
	$('<div/>')
		.attr('id', 'stateSelect')
		.appendTo(appendTo)
	$('<div/>')
		.attr('id', 'package')
		.appendTo(appendTo)
}
PKG.renderRow = function (item, params) {
	if(params.type == 'inc') {
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID)
			.addClass('package-row')
			.hover(
				function () {
			        $(this).css('background-color', '#EEEEEE');
				}, 
				function () {
					$(this).css('background-color', 'transparent');
				}
			)
			.click(function () {
				var id = '#' + $(this).attr('id') + '-Description';
				PKG.show(id);
			})
			.appendTo(params.appendTo);
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Input')
			.addClass('plus')
			.html('+')
			.appendTo('#Item-' + item.ProductID);
		$('<input/>')
			.attr('type', 'hidden')
			.attr('id', 'Item-' + item.ProductID + '-Checkbox')
			.attr('name', 'item-' + item.GNetworksID)
			.attr('value', 1)
			.appendTo('#Item-' + item.ProductID + '-Input');
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Label')
			.addClass('package-row-label')
			.html(item.Label)
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.addClass('price')
			.html(PKG.fMoney(item.Price * 100))
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.css('clear', 'both')
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Description')
			.addClass('package-row-description')
			.html(item.Description)
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.addClass('both')
			.appendTo(params.appendTo);
	}
	if(params.type == 'opt') {
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID)
			.addClass('package-row')
			.hover(
				function () {
			        $(this).css('background-color', '#EEEEEE');
				}, 
				function () {
					$(this).css('background-color', 'transparent');
				}
			)
			.click(function () {
				var id = '#' + $(this).attr('id') + '-Description';
				PKG.show(id);
			})
			.appendTo(params.appendTo);
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Input')
			.addClass('input')
			.appendTo('#Item-' + item.ProductID);
		$('<input/>')
			.attr('type', 'checkbox')
			.attr('id', 'Item-' + item.ProductID + '-Checkbox')
			.attr('name', 'item-' + item.GNetworksID)
			.attr('value', 1)
			.hover(
				function () {
			        PKG.showEnabled = false;
				}, 
				function () {
			        PKG.showEnabled = true;
				}
			)
			.change(function () {
				if($(this).attr('checked')) {
					PKG.subTotal = PKG.add(item.Price, PKG.subTotal)
				}
				else {
					PKG.subTotal = PKG.subtract(item.Price, PKG.subTotal)
				}
				PKG.calculate()
			})
			.appendTo('#Item-' + item.ProductID + '-Input');
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Label')
			.addClass('package-row-label')
			.html(item.Label)
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.addClass('price')
			.html(PKG.fMoney(item.Price * 100))
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.css('clear', 'both')
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.attr('id', 'Item-' + item.ProductID + '-Description')
			.addClass('package-row-description')
			.html(item.Description)
			.appendTo('#Item-' + item.ProductID);
		$('<div/>')
			.addClass('both')
			.appendTo(params.appendTo);
	}
	else if (params.type == 'incTotal') {
		$('<div/>')
			.addClass('total')
			.html('Total of Items Included in this Package : ' + item.total)
			.appendTo(params.appendTo);
	}
	else if (params.type == 'subTotal') {
		$('<div/>')
			.attr('id', 'subTotal')
			.addClass('total')
			.appendTo(params.appendTo);
	}
	else if (params.type == 'discount') {
		$('<div/>')
			.attr('id', 'discount')
			.addClass('total')
			.appendTo(params.appendTo);
	}
	else if (params.type == 'preShippingTotal') {
		$('<div/>')
			.attr('id', 'preShippingTotal')
			.addClass('total')
			.appendTo(params.appendTo);
	}
}
PKG.renderTable = function (data, appendTo) {
	appendTo = '#' + appendTo;
	PKG.reset(appendTo);
	var params = {appendTo: appendTo}
	var options;
	var product;
	if(PKG.state == null) {
		$('#stateImage').html('<h2 style="color:red;">You must select a state (above).</h2>');
	}
	else {
		$('#stateImage').html('<h2>' + PKG.state + '</h2>');
	}
	$('<img/>')
		.attr('src', '/assets/images/states/' + PKG.stateAbbrev + '.gif')
		.attr('alt', '')
		.css('margin-left', '-32px')
		.css('margin-top', '-10px')
		.appendTo('#stateImage')
	if(data.items.inc.length > 0) {
		$('<h2/>')
			.html('The ' + data.label + ' includes the following products:')
			.appendTo(params.appendTo);
		$.each(data.items.inc, function (i,item) {
			product = item.product;
			options = item.productOptions;
			if(options.length !== 0) {
				if(options[PKG.state] != null) {
					product.GNetworksID = options[PKG.state];
				}
				else {
					product.GNetworksID = null;
				}
			}
			if(product.GNetworksID != null) {
				params.type = 'inc';
				PKG.incTotal = PKG.add(product.Price, PKG.incTotal);
				PKG.renderRow(product, params);
			}
		});
		PKG.subTotal = PKG.incTotal;
		params.type = 'incTotal';
		PKG.renderRow({total:PKG.fMoney(PKG.incTotal)}, params)
		if(data.items.opt.length > 0) {
			$('<h2/>')
				.html('The following products are optional but recommended:')
				.appendTo(params.appendTo);
		}
	}
	else {
		$('<h2/>')
			.html('The ' + data.label + ' allows you to pick and choose.')
			.appendTo(params.appendTo);
	}
	if(data.items.opt.length > 0) {
		$.each(data.items.opt, function (i,item) {
			product = item.product
				options = item.productOptions
				if(options.length !== 0) {
					if(options[PKG.state] != null) {
						product.GNetworksID = options[PKG.state];
					}
					else {
						product.GNetworksID = null;
					}
				}
			if(product.GNetworksID) {
				params.type = 'opt';
				PKG.renderRow(product, params);
			}
		});
	}
	params.type = 'subTotal';
	PKG.renderRow({total:PKG.fMoney(PKG.subTotal)}, params)
	params.type = 'discount';
	PKG.renderRow({total:PKG.fMoney(PKG.discount)}, params)
	params.type = 'preShippingTotal';
	PKG.renderRow({total:PKG.fMoney(PKG.preShippingTotal)}, params)
	PKG.calculate();
	$('<input/>')
		.attr('type', 'submit')
		.attr('value', 'Click Here to Order Now!')
		.appendTo(params.appendTo)
}
PKG.renderStateSelect = function (data, appendTo) {
	appendTo = '#' + appendTo;
	var params = {appendTo: '#stateSelect'};
	var options;
	$('<select/>')
		.attr('id', 'state')
		.attr('name', 'state')
		.addClass('required select')
		.change(function () {
			PKG.stateAbbrev = $('#state').val();
			PKG.state = $('#Option-' + PKG.stateAbbrev).html();
			PKG.renderTable(data, 'package');
			var href = $('#optionLink1').attr('href') + '/State/' + PKG.state;
			$('#optionLink1').attr('href', href)
			href = $('#optionLink2').attr('href') + '/State/' + PKG.state;
			$('#optionLink2').attr('href', href)
		})
		.prependTo(params.appendTo);
	$('<div/>')
		.attr('id', 'stateImage')
		.html('<h2>Please select a state (above).</h2>')
		.appendTo('#stateSelect')
	$('<option/>')
		.attr('id', 'Option-XX')
		.attr('value', '')
		.html('Please Select a State')
		.appendTo('#state');
	$.each(data.states, function (i,state) {
		$('<option/>')
			.attr('id', 'Option-' + state.Abbreviation)
			.attr('value', state.Abbreviation)
			.html(state.Name)
			.appendTo('#state');
	});
}