var storie = {};
$ = jQuery.noConflict();

storie.webinarSelect = function(){
	$('select.webinar-select').change(function(){
		newLoc = $(this).val();
		if(newLoc !== -1 && newLoc !== '-1'){
			window.location = newLoc;
		}
	});
}

storie.showHiddenExcerpt = function(){
	$('a.infopopuplink').hover(function(){
			$(this).parents('div.categorypost').children('div.hiddenentry').fadeIn(400);
	},function(){
			$(this).parents('div.categorypost').children('div.hiddenentry').fadeOut(400);
	});
}

storie.fixSpasticRegistrationForm = function(){
	if($('form#registerform').length > 0){
		
		var fields = Array(
			$('form#registerform .first_name_box'),
			$('form#registerform .last_name_box'),
			$('form#registerform .phone_box'),
			$('form#registerform .email_box'),
			$('form#registerform .username_box'),
			$('form#registerform .password_box'),
			$('form#registerform .password_conf_box'),
			$('form#registerform .terms_conditions_box')
		);
		
		$('<p class="formintro">Your Details</p><p class="formintrodesc">Fields marked with an <span class="required">*</span> are required.</p>').insertBefore('form#registerform .first_name_box');
		
		$(fields).each(function(){
			$(this).children('label:visible').append('<span class="required">*</span>');
		});
		
		$('form#registerform').submit(function(e){
		
			var valid = true;
			$(fields).each(function(){
								
				input = $(this).find('input.required');
				val = $(input).val();
				type = $(input).attr('type');

				if(( type == 'text' || type == 'password') && val.length  < 1){
					console.log(input);
					valid = false;
				}
				
				if(type == 'checkbox' && !( $(input).is(':checked') ) ){
					console.log(input);
					valid = false;
				}
			
			});
			
			if(valid === false){
				alert('Please ensure you have filled out all the required fields.');
				return false;
			}
				
			
		});
		
		$('.view_terms_amp_conditions_box a').click(function(){
			window.open(
				"http://www.storieselling.ekology.com.au/terms-and-conditions",
				"terms",
				"menubar=1,resizable=yes,scrollbars=yes,width=800,height=400"
			); 
		});
		
		
	}
	
	$('<br><span style="color:#999;font-size:10px;">If you have been provided with a Coupon code please enter it here and proceed.</span>').insertAfter('div.coupon_box label:contains("Coupon")');
	$('<br style="clear:both"/>').insertAfter('#registerform div.coupon_box');
	$("#registerform p:empty").remove();
	
	$(".mgm_subs_pack_desc").each(function(){
	
		var text = $.trim($(this).text())
		
		//console.log(text);
		
		if(text.indexOf('year') > 0){
			//Member - $300.00 per 1 year  - for 1 time
			$(this).text('Annual Subscription - one off payment.');
		}
		
		else if(text.indexOf('month') > 0){
			//Member - $29.00 per 1 month - for 12 times 
			$(this).text('Monthly Subscription - 12 recurring monthly payments.');
		}
	
	});

}


$(document).ready(function(){
	storie.webinarSelect();
	storie.showHiddenExcerpt();
	storie.fixSpasticRegistrationForm();
});

