var tfta = window.tfta || {};

(function($) {
	
	tfta = {
		console: function(m,d){
			if(window.console != undefined){console[m](d);}
		},
		subnav: function(){
			$(".header-nav .link:has('.menu')").hover(
				function(){
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			)
			// use modals if not mobile safari
			var isSafariMobile = RegExp(" AppleWebKit/").test(navigator.userAgent) && RegExp(" Mobile/").test(navigator.userAgent);		
			if(isSafariMobile){
				$(".header-nav .link:has('.menu') .tab").bind('click',function(){$(this).parent('.link').toggleClass('hover');return false;});
			}			
		},
		login: function(){
			$("a.login").bind('click',function(){location.href="/member/login/";return false;});
			/*  login modal */
			// $('#login').jqm({overlay:80});
			// $('#login').jqmAddTrigger($("a.login"));
			// $('#login .button').bind('click',function(){$('#login form').submit()});
		},
		features: {
			rotate: 0,
			next: function(n){
				var max = $(".spot-2 .pagination a").length;
				var current = $(".spot-2 .pagination a[class=here]").index();				
				var next = current + n;
				if(next < 0){ next = max - 1; }
				if(next >= max){ next = 0; }								
				this.show(next);
			},
			show: function(id){
				clearTimeout(this.rotate);
				$(".spot-2 li").hide();
				$(".spot-2 .pagination a").removeClass('here');
				$(".spot-2 li:eq(" + id + ")").fadeIn();
				$(".spot-2 .pagination a:eq(" + id + ")").addClass('here');
				this.rotate = setTimeout("tfta.features.next(1)",5000);
			},
			init: function(){
				$(".spot-2 li").each(function(i){
					var page = '<a href="#">' + i + '</a>';
					$(".spot-2 .pagination").append(page);					
				});
				$(".spot-2 .pagination a").each(function(i){
					$(this).bind('click',function(){ tfta.features.show(i); return false; });
				});
				$(".spot-2 .previous").bind("click",function(){tfta.features.next(-1)});
				$(".spot-2 .next").bind("click",function(){tfta.features.next(1)});
				this.show(0);
			}
		},
		equallize: function(id){
			var equal_height = 0;
			$(id).each(function(){
				$(this).css("height","auto");
				var this_height = $(this).outerHeight();
				if(this_height > equal_height){
					equal_height = this_height;
				}
			});
			$(id).each(function(){
				var offset = $(this).outerHeight() - $(this).height();
				var new_height = equal_height - offset;
				$(this).css("height",new_height);
			});
		},
		search: function(){
			$('.search a').bind('click',function(){
				$('.search form').submit();
			});
		},
		register: function(){
			$('form#register .buttun.submit').bind('click',function(){
				$('input[name="phone"]').val($('input[name="phone1"]').val() + "-" + $('input[name="phone2"]').val() + "-" + $('input[name="phone3"]').val());
				$('form#register').submit();
			});
		},
		update: function(){
			$('#update-submit').bind('click',function(){
				$('form#update').submit();
				return false;
			});
		},
		subscribe: function(){
		
			// determine plan_id
			var _getPlanId = function(){
				var plan_id = 0;			
				var selectedPlan = $("#plan_ids").val();
				var isRecurring = $("#recurring").attr("checked");
				switch(selectedPlan){
					case "1001":
					plan_id = (isRecurring)?selectedPlan:"1010";
					break;
					case "1002":
					plan_id = (isRecurring)?selectedPlan:"1011";
					break;
					case "1003":
					plan_id = (isRecurring)?selectedPlan:"1012";
					break;
					case "1004":
					plan_id = (isRecurring)?selectedPlan:"1013";
					break;
					case "1005":
					plan_id = (isRecurring)?selectedPlan:"1014";
					break;
					case "1006":
					plan_id = (isRecurring)?selectedPlan:"1015";
					break;
					case "1007":
					plan_id = (isRecurring)?selectedPlan:"1016";
					break;
					case "1008":
					plan_id = (isRecurring)?selectedPlan:"1017";
					break;
					case "1009":
					plan_id = (isRecurring)?selectedPlan:"1018";
					break;
					case "1020":
					plan_id = "1020";
					break;				
				}
				$('#plan_id').val(plan_id);
			}
			$('#plan_ids').bind('change',function(){
				_getPlanId();
				var plan_id = $('#plan_id').val();
				if(plan_id != "1020"){ $("#variable").hide();$("#flat, #recurring_option").show(); }
				else if(plan_id == ""){ $("#variable, #flat, #recurring_option").hide(); }
				else { $("#flat, #recurring_option").hide(); $("#variable").show(); }
				tfta.equallize("#site-body .page, #site-body .nav");				
			});	
		
			// Calcualte the amount that a grantee should donate
			var _updateGrant = function(){
				var grant = $('#grant_amount').val();				
				grant = grant.replace(",","");
				grant = Math.round(grant / 100);
				if(isNaN(grant) || grant < 100){ grant = 100; } 
				var _addCommas = function(nStr){
					nStr += '';
					x = nStr.split('.');
					x1 = x[0];
					x2 = x.length > 1 ? '.' + x[1] : '';
					var rgx = /(\d+)(\d{3})/;
					while (rgx.test(x1)) {
						x1 = x1.replace(rgx, '$1' + ',' + '$2');
					}
					return x1 + x2;
				}
				$('#grant_percent').val(_addCommas(grant));
				$("#donate input[name='amount']").val(grant);
			}
			$('#grant_amount').bind('keypress',function(){_updateGrant()}).bind('keyup',function(){_updateGrant()});
			_updateGrant();
			
			// about modals
			$('#autorenew').jqm({overlay:80,toTop:true});
			$('#autorenew').jqmAddTrigger($("a.autorenew"));
			$('#securitycode').jqm({overlay:80,toTop:true});
			$('#securitycode').jqmAddTrigger($("a.securitycode"));
			
			// submit the donate form
			$('#donate-submit').bind('click',function(){
				$('form#donate').submit();
				return false;
			});
			
			// submit the membrr subscribe form
			$('#subscribe-submit').bind('click',function(){
				_getPlanId();
				//$('#plan_id').val("1000");
				$('input[name="phone"]').val($('input[name="phone1"]').val() + "-" + $('input[name="phone2"]').val() + "-" + $('input[name="phone3"]').val());
				$('form#subscribe').submit();
				return false;
			});
			
		},
		lazyload: function(){
			var isSafariMobile = RegExp(" AppleWebKit/").test(navigator.userAgent) && RegExp(" Mobile/").test(navigator.userAgent);		
			if(!isSafariMobile){
				$(".lazy").lazyload({
					placeholder:"/assets/global/img/noise.png",
					effect : "fadeIn"
				});
			}
		},
		init: function(){
			this.search();
			this.subnav();
			this.login();
			this.equallize(".spot-4,.spot-5,.spot-6");
			this.equallize("#site-body .page, #site-body .nav");
			this.lazyload();
		}
	}
	
	$(window).bind("load", function() {
		
		tfta.init();
	
	});
	
	
})(jQuery);
