function getScrollTop()
{
	var scrollTop = 0;
	if (window.pageYOffset)
		scrollTop = window.pageYOffset;
	else if (document.documentElement.scrollTop)
		scrollTop = document.documentElement.scrollTop;
	else if (document.body.scrollTop)
		scrollTop = document.body.scrollTop;
	return scrollTop;
}

function numberFormat(number, decimals, dec_point, thousands_sep)
{
	// Formats a number with grouped thousands
	//
	// version: 1102.614
	// discuss at: http://phpjs.org/functions/number_format
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +     bugfix by: Michael White (http://getsprink.com)
	// +     bugfix by: Benjamin Lupton
	// +     bugfix by: Allan Jensen (http://www.winternet.no)
	// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +     bugfix by: Howard Yeend
	// +    revised by: Luke Smith (http://lucassmith.name)
	// +     bugfix by: Diogo Resende
	// +     bugfix by: Rival
	// +      input by: Kheang Hok Chin (http://www.distantia.ca/)
	// +   improved by: davook
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +      input by: Jay Klehr
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +      input by: Amir Habibi (http://www.residence-mixte.com/)
	// +     bugfix by: Brett Zamir (http://brett-zamir.me)
	// +   improved by: Theriault
	// +      input by: Amirouche
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// *     example 1: number_format(1234.56);
	// *     returns 1: '1,235'
	// *     example 2: number_format(1234.56, 2, ',', ' ');
	// *     returns 2: '1 234,56'
	// *     example 3: number_format(1234.5678, 2, '.', '');
	// *     returns 3: '1234.57'
	// *     example 4: number_format(67, 2, ',', '.');
	// *     returns 4: '67,00'
	// *     example 5: number_format(1000);
	// *     returns 5: '1,000'
	// *     example 6: number_format(67.311, 2);
	// *     returns 6: '67.31'
	// *     example 7: number_format(1000.55, 1);
	// *     returns 7: '1,000.6'
	// *     example 8: number_format(67000, 5, ',', '.');
	// *     returns 8: '67.000,00000'
	// *     example 9: number_format(0.9, 0);
	// *     returns 9: '1'
	// *    example 10: number_format('1.20', 2);
	// *    returns 10: '1.20'
	// *    example 11: number_format('1.20', 4);
	// *    returns 11: '1.2000'
	// *    example 12: number_format('1.2000', 3);
	// *    returns 12: '1.200'
	// *    example 13: number_format('1 000,50', 2, '.', ' ');
	// *    returns 13: '100 050.00'
	number = (number + '').replace(',', '').replace(' ', '');
	var n = !isFinite(+number) ? 0 : +number,
	prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	s = '',
	toFixedFix = function (n, prec)
	{
		var k = Math.pow(10, prec);
		return '' + Math.round(n * k) / k;
	};
	// Fix for IE parseFloat(0.55).toFixed(0) = 0;
	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	if (s[0].length > 3)
	{
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
	if ((s[1] || '').length < prec)
	{
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}

$jq(function()
{
	$jq(".featured-preview").hover(function() {
		$jq('.featured-hover').hide();
		$jq('#fh-'+$jq(this).attr('id').substr(3)).show();
	},
	function() {
		$jq('.featured-hover').hide();
	});
});

$jq(document).ready(function() {
	var fo = new SWFObject(projectPath+"website/good-hobby/template/images/flash/good_hobby_new.swf", "movie_player", "1024", "415", 7, "#000000");
	fo.addParam("wmode", "transparent");
	fo.write("top_flash");
	var fo = new SWFObject(projectPath+"website/good-hobby/template/images/flash/good_hobby_cat.swf", "movie_player", "350", "70", 7, "#000000");
	fo.addParam("wmode", "transparent");
	fo.write("bottom_flash");

	var subscribeBusy = false;

	$jq('.subscribe').live('click', function() {
		if (subscribeBusy) return false;

		var a = this.id.substr(1);
		var b = a.split('-');

		var loadingImg = $jq('#l'+a);
		var e = $jq('#m'+a).val();

		$jq('#done'+a).hide();

		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);

		loadingImg.show();
		subscribeBusy = true;

		JsHttpRequest.query(
			projectPath+'ecommerce/ajax.php',
			{'Action': 'Subscribe', 'PageID': b[0], 'ItemID': b[1], 'Email': e, 'Rand': randomString},
			function(result, errors)
			{
				subscribeBusy = false;
				loadingImg.hide();
				if (result.Error)
				{
					$jq('#done'+a).removeClass('correct');
					$jq('#done'+a).addClass('wrong');
					$jq('#done'+a).html(result.Error);
					$jq('#done'+a).fadeIn();
				}
				else if (result.Message)
				{
					$jq('#done'+a).removeClass('wrong');
					$jq('#done'+a).addClass('correct');
					$jq('#done'+a).html(result.Message);
					$jq('#done'+a).fadeIn();
				}
			}
		);

		return false;
	});

	var shoppingCartBusy = false;

	$jq('.add-cart').live('click', function() {
		if (shoppingCartBusy) return false;

		var a = this.id.substr(1);
		var b = a.split('-');
		var q = 1;

		var loadingImg = $jq('#l'+a);
		var q = $jq('#m'+a).val();

		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);

		loadingImg.show();
		shoppingCartBusy = true;

		JsHttpRequest.query(
			projectPath+'ecommerce/ajax.php',
			{'Action': 'AddToCart', 'PageID': b[0], 'ItemID': b[1], 'Quantity': q, 'Rand': randomString},
			function(result, errors)
			{
				shoppingCartBusy = false;
				loadingImg.hide();
				$jq('#shopping-cart-items').html(result.Quantity);
				$jq('#shopping-cart-amount').html(result.Amount);
				for (i = 0; i < result.ItemList.length; i++)
				{
					if (result.ItemList[i].ItemID == b[1])
					{
						$jq('#v'+a).html(result.ItemList[i].Quantity);
					}
				}
				$jq('#incart'+a).show();
				$jq('#i'+a).val(GetTranslation('add-more-to-cart'));
			}
		);

		return false;
	});

	var shoppingCartUpdate = {};
	var shoppingCart = {};

	$jq('input[name=ShoppingCartItems]').blur(function() {
		var a = this.id.substr(1);
		var b = a.split('-');
		if (isNaN(shoppingCart[b[1]]))
			shoppingCart[b[1]] = parseInt(this.defaultValue);
		this.value = shoppingCart[b[1]];
	});

	$jq('input[name=ShoppingCartItems]').keyup(function() {
		var a = this.id.substr(1);
		var b = a.split('-');

		if (isNaN(shoppingCart[b[1]]))
			shoppingCart[b[1]] = parseInt(this.defaultValue);

		var q = parseInt(this.value);
		if (isNaN(q))
		{
			this.value = '';
			return;
		}

		if (shoppingCart[b[1]] == q)
			return;

		shoppingCart[b[1]] = q;
		this.value = q;

		if (shoppingCartUpdate[b[1]])
			shoppingCartUpdate[b[1]]++;
		else
			shoppingCartUpdate[b[1]] = 1;
		var queue = shoppingCartUpdate[b[1]];

		$jq('#p'+a).hide();
		$jq('#shl'+a).show();

		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);

		JsHttpRequest.query(
			projectPath+'ecommerce/ajax.php',
			{'Action': 'UpdateCart', 'PageID': b[0], 'ItemID': b[1], 'Quantity': q, 'Rand': randomString},
			function(result, errors)
			{
				if (queue < shoppingCartUpdate[b[1]])
					return;

				$jq('#shl'+a).hide();
				$jq('#p'+a).show();

				$jq('#shopping-cart-items').html(result.Quantity);
				$jq('#shopping-cart-amount').html(result.Amount);
				$jq('#shopping-cart-items1').html(result.Quantity);
				$jq('#shopping-cart-goods1').html(result.Goods);
				$jq('#shopping-cart-amount1').html(result.Amount);

				var found = false;
				for (i = 0; i < result.ItemList.length; i++)
				{
					if (result.ItemList[i].ItemID == b[1])
					{
						$jq('#a'+a).html(result.ItemList[i].InitialAmountFmt);
						if (result.ItemList[i].Error)
							$jq('#e'+a).html(result.ItemList[i].Error);
						else
							$jq('#e'+a).html('');
						found = true;
					}
				}
				if (!found) $jq('#a'+a).html('0');
				if (result.Quantity == 0) $jq('#checkout').hide();
				else $jq('#checkout').show();
				if (parseFloat(result.DiscountPercent) > 0)
				{
					$jq('#discount-percent').html(result.DiscountPercent);
					$jq('#discount-amount').html(result.DiscountAmountFmt);
					$jq('#discount').css({'display' : ''});
				}
				else
				{
					$jq('#discount').css({'display' : 'none'});
				}
			}
		);
	});

	$jq('#shopping-cart-table a.delete').click(function() {
		var a = this.id.substr(1);
		var b = a.split('-');

		if (shoppingCartUpdate[b[1]])
			shoppingCartUpdate[b[1]]++;
		else
			shoppingCartUpdate[b[1]] = 1;
		var queue = shoppingCartUpdate[b[1]];

		$jq('#d'+a).hide();
		$jq('#dl'+a).show();

		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);

		JsHttpRequest.query(
			projectPath+'ecommerce/ajax.php',
			{'Action': 'UpdateCart', 'PageID': b[0], 'ItemID': b[1], 'Quantity': 0, 'Rand': randomString},
			function(result, errors)
			{
				if (queue < shoppingCartUpdate[b[1]])
					return;

				$jq('#shopping-cart-items').html(result.Quantity);
				$jq('#shopping-cart-goods').html(result.Goods);
				$jq('#shopping-cart-amount').html(result.Amount);
				$jq('#shopping-cart-items1').html(result.Quantity);
				$jq('#shopping-cart-goods1').html(result.Goods);
				$jq('#shopping-cart-amount1').html(result.Amount);

				$jq('#r'+a).remove();

				if (result.Quantity == 0) $jq('#checkout').hide();
				else $jq('#checkout').show();
			}
		);

		return false;
	});

	var couponBusy = false;

	$jq('#coupon-btn,#remove-coupon').live('click', function() {
		if (couponBusy) return false;

		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);

		$jq('#coupon-loading').show();
		$jq('#coupon-loading2').show();
		couponBusy = true;

		if (this.id == 'remove-coupon')
			var code = '';
		else
			var code = $jq('#CouponCode').val();

		JsHttpRequest.query(
			projectPath+'ecommerce/ajax.php',
			{'Action': 'ApplyCoupon', 'PageID': $jq('#PageID').val(), 'CouponCode': code, 'Rand': randomString},
			function(result, errors)
			{
				couponBusy = false;
				$jq('#coupon-loading').hide();
				$jq('#coupon-loading2').hide();
				$jq('#CouponCode').val('');
				$jq('#shopping-cart-amount').html(result.Amount);
				$jq('#shopping-cart-amount1').html(result.Amount);
				if (parseFloat(result.DiscountPercent) > 0)
				{
					$jq('#discount-percent').html(result.DiscountPercent);
					$jq('#discount-amount').html(result.DiscountAmountFmt);
					$jq('#discount').css({'display' : ''});
				}
				else
				{
					$jq('#discount').css({'display' : 'none'});
				}

				if (parseFloat(result.CouponCode))
				{
					$jq('#coupon-apply').hide();
					$jq('#coupon-applied').show();
					$jq('#applied-coupon').html(result.CouponCode);
				}
				else
				{
					$jq('#coupon-applied').hide();
					$jq('#coupon-apply').show();
					if (code != '')
					{
						alert(GetTranslation('coupon-not-found'));
					}
				}
			}
		);

		return false;
	});

	$jq('#reg_inactive').click(function() {
		$jq('#reg_inactive').hide();
		$jq('#reg_active').show();
		$jq('#new_active').hide();
		$jq('#new_inactive').show();
		$jq('#new_content').hide();
		$jq('#reg_content').show();
		$jq('#login-error').html('').hide();
		return false;
	});

	var newLoaded = false;

	$jq('#new_inactive').click(function() {
		if (!newLoaded)
		{
			JsHttpRequest.query(
				projectPath+'form/ajax.php',
				{'PageID': 17, 'Show': 'r'},
				function(result, errors)
				{
					$jq('#new_content').html(result.Data.Content);
				}
			);
			newLoaded = true;
		}
		$jq('#new_inactive').hide();
		$jq('#new_active').show();
		$jq('#reg_active').hide();
		$jq('#reg_inactive').show();
		$jq('#reg_content').hide();
		$jq('#new_content').show();
		$jq('#register-error').html('').hide();
		return false;
	});
});
