(function($)
{
	var ccbforms = {
		'cc':'https://bill.ccbill.com/jpost/billingCascade.cgi?clientAccnum=925325&clientSubacc=0003&cascadeId=3691',
		'ck':'https://bill.ccbill.com/jpost/billingCascade.cgi?clientAccnum=925325&clientSubacc=0003&cascadeId=3846',
		'tl':'https://bill.ccbill.com/jpost/billingCascade.cgi?clientAccnum=925325&clientSubacc=0003&cascadeId=3848'
	}
	
	$(function()
	{
		$('a.form-submit').click(function(e)
		{
			e.preventDefault();
			$( this.hash ).submit();
		});
		
		//init signup forms actions
		$('.payment_select').change(function(e)
		{
			var parent_form = $(this).parents('form');
			parent_form.attr('action', ccbforms[$(this).val()] );
			
			if( $(this).value() == 'tl' )
				$('input[name="subscriptionTypeId"]', parent_form).attr('name', 'subscriptionTypeId_disabled').attr('disabled','disabled');
			else
				$('input[name="subscriptionTypeId_disabled"]', parent_form).attr('name', 'subscriptionTypeId').removeAttr('disabled');
		});
		
		//initialize modal window controllers
		var overlay = $('<div id="overlay"/>').css({
			"position":"absolute",
			"display":"none",
			"top":0,
			"left":0,
			"width":"100%",
			"height":"100%",
			"z-index":99,
			"background":"#000",
			"opacity":0.85
		})
		.height( $(document).height() )
		.click(modal_close)
		.prependTo('body');
		
		$('.modal-opener').click(function(e)
		{
			e.preventDefault();
			
			modal_close();
			
			var modal = $( this.hash ).addClass('modal');
			
			//center the modal window
			modal.css({
				position:'fixed',
				top:$(window).height() / 2 - modal.height() / 2,
				left:$(document).width() / 2 - modal.width() / 2,
				"z-index":100
			});
			
			//fade in the overlay and modal window
			overlay.fadeIn('fast');
			modal.fadeIn('fast');
			
			//focus on the first input found
			var input = modal.find('input');
			if( input.length ) input.get(0).focus();
		});
		
		$('.modal-close').click(modal_close);
	});
	
	function modal_close( e )
	{
		if( e ) {
			e.preventDefault();
			/*$('.modal').find('form').get(0).reset();
			$.taglist.clear();*/
		}		
		
		//fade out the overlay and modal
		$('#overlay').fadeOut('fast');
		$('.modal').fadeOut('fast');
	}
})(jQuery);