jQuery.extend({
	head: function( url, data, callback ) {
		if ( jQuery.isFunction( data ) ) {
			callback = data;
			data = {};
		}
		return jQuery.ajax({
			type: "HEAD",
			url: url,
			data: data,
			complete: function (XMLHttpRequest, textStatus) {
				var headers = XMLHttpRequest.getAllResponseHeaders().split("\n");
				var new_headers = {};
				for (key in headers) {
					if (headers[key].length != 0) {
						header = headers[key].split(": ");
						new_headers[header[0]] = header[1];
					}
				}
				if (jQuery.isFunction(callback)) {
					callback(new_headers);
				}
			}
		});
	}
});

statusBar = {
	timer: null,
	
	show: function(txt,type,auto_hide,show_loader) {
		clearTimeout(statusBar.timer);
		
		if (type==1) $('#status-bar').attr('class','status-bar-error');
		else $('#status-bar').attr('class','status-bar-basic');
		
		if (show_loader==1) $('#status-bar-txt').attr('class','loader');
		else ($('#status-bar-txt').attr('class','na'));
		
		if (auto_hide==1) statusBar.timer = setTimeout(function(){ statusBar.hide();clearTimeout(statusBar.timer);}, 8000);
		
		$('#status-bar-txt').html(txt);
		$('#status-bar-wrap').animate({top: '0px'}, 550 );
	},
	
	hide: function() {
		clearTimeout(statusBar.timer);
		$('#status-bar-wrap').animate({top: '-55px'}, 550 );
	}
}

join = {
	init: function() {
		$('#user_name').keyup(function(){
			join.user_name_key();
		});
	},
	check_type: function(el) {
		dd = $(el);
		if (dd.val() == 4) 
			txt = 'Restaurant\'s Name:';
		else if (dd.val() == 10)
			txt = 'Charity\'s Name:';
		else if (dd.val() > 3)
			txt = 'Company\'s Name:';
		else 
			txt = 'Name:';

		
		$('#name-label').html(txt);
	},
	
	user_name_key: function() {
		$('#username-label').html($('#user_name').val());
		if ($('#user_name').val() == '') $('#username-label').html('your_user_name');
	},
	
	validate: function() {
		var type = $('#user_type').val();
		if (!type) {
			statusBar.show('Signups are currently offline due to maintenance. Try again later.',1,0,0);
			return false;
		}
		
		util.submit_go('submit_join','please wait...');
		
		var fields = [
		"profile_name--Please enter your name.",
		"user_name--Please create a username.",
		"user_password--Please create a password.",
		"user_email--Please enter your email address.",
		"profile_zip--Please enter your postal code."
		];

		var x = util.validate_blanks(fields);
		
		if (x==false) util.submit_reset('submit_join','submit');
		
		return x;
	}	
}


follow = {
	start: function(follower_id,following_user_id,following_user_name,show_status,callback) {
		$.post('/kungfu?action=follow', {submit_add_following:'submit_add_following',following_user_name:following_user_name,follower_id:follower_id, following_user_id: following_user_id},
			function(msg) {
				if (show_status==1) {
					if (msg.error==1) et=1; else et=0;
					statusBar.show(msg.msg,et,1,0);
				}
				if (msg.error!=1 && callback){
					callback.call();
				}
		},"json");
	},
	
	stop:function(follower_id,following_user_id,following_user_name,show_status,callback) {
		$.post('/kungfu?action=unfollow', {submit_remove_follower:'submit_remove_follower',following_user_name:following_user_name,follower_id:follower_id, following_user_id:following_user_id},
			function(msg) {
				if (show_status==1) {
					if (msg.error==1) et=1; else et=0;
					statusBar.show(msg.msg,et,1,0);
				}
				if (msg.error!=1 && callback){
				 	callback.call();
				}
		},"json");  
	}
}

profile = {

	bites_current_page:1,   
	
	follow: function(follower_id,following_user_id, following_user_name) {                
		follow.start(follower_id,following_user_id,following_user_name,1,function() {
			$('#follow-start').hide();
			$('#follow-stop').show();
		});
	},    
	
	unfollow: function(follower_id, following_user_id, following_user_name) {
		c = confirm("Are you sure you want to stop following "+following_user_name+"?");
		if (c){    
			follow.stop(follower_id, following_user_id, following_user_name,1,function(){
				$('#follow-stop').hide();
				$('#follow-start').show();    
			});
		}
	},
	
	bites_more: function(user_id) {
		profile.bites_current_page++;
		var bites = $('#bites-container');
		var div = $('<div>&nbsp;</div>').appendTo(bites);
		$('#show-more').html( '<div class="loader-dots-24">&nbsp;</div>' );
		div.load('/profile?action=show_more&id='+user_id+'&page='+profile.bites_current_page+'&ajax=1',null,function(){            
			$('#show-more').remove();
		}); 
	}
}

publisher = {
	max_text: 160,
	default_header: 'What are you eating?',
	
	init: function(btext) {
		
		$('#bite_text').val('');
		$('#bite_text').focus();
		if (btext!='') $('#bite_text').val(btext);	
	
		/* set event handlers on bite_text */
		$('#bite_text').keypress(function(event){
			publisher.keyup();
		});
		$('#bite_text').keyup(function(event){
			publisher.keyup();
		});
		$('#add-link').click(function(){
			publisher.show_link();
			return false;
		});
		$('#attach-link > a.x-16').click(function(){
			publisher.hide_link();
			return false;
		});
		$('#reset-btn').click(function(){
			publisher.reset();
			return false;
		});
	},
	
	/* updates character counter */
	keyup: function() {
		t = $('#bite_text');
		tv = t.val();

		current_count = t.val().length;
		difference = Number(publisher.max_text - current_count);
		/*change the counters colors based on number of chr's remaining */
		if (difference < 30) $('#text-counter').addClass('low').removeClass('uhoh');
		if (difference < 15) $('#text-counter').addClass('uhoh').removeClass('low');
		if (difference > 30) $('#text-counter').removeClass('low').removeClass('uhoh');
		
		if (jQuery.trim(tv)=='') 
			$('#submit_bite').addClass('f-btn-cancel').removeClass('f-btn');
		else 
			$('#submit_bite').removeClass('f-btn-cancel').addClass('f-btn');
		
		/*if they pass the limit we remove any text beyond it and make sure the counter stays at 0 */
		if (difference < 0) difference = '0';
		if (current_count > publisher.max_text)
			t.val( t.val().substring(0, publisher.max_text) );
		
		$('#text-counter').html( difference );
		
	},
	
	set: function(bite_type_id, bite_secondary_id, bite_secondary_text, from_username, bite_text) {
		$('#bite_type_id').val(bite_type_id);
		$('#bite_secondary_id').val(bite_secondary_id);
		$('#bite_secondary_text').val(from_username + '[|]' + bite_secondary_text);
		$('#bite_text').val('');
		$('#bite_text').focus();
		$('#bite_text').val(bite_text);
		if (bite_type_id==2 || bite_type_id==3) {
			$('#row-quote').show();
			$('#row-quote').html('<span class="quote">Attaching:</span> &quot; <span>'+from_username+'</span> '+bite_secondary_text+ ' &quot;');
			$('#reset-btn').show();
		}
		
		if (bite_type_id==2)
			$('#bite-header').html('Reply to: '+from_username);
		
		if (bite_type_id==3)
			$('#bite-header').html('Sharing '+from_username+"'s bite.");
		
		publisher.keyup();
		
		window.scroll(0,0);
	},
	
	reset: function() {
		$('#submit_bite').attr('disabled','');
		$('#bite_text').attr('disabled','');
		$('#bite_type_id').val(0);
		$('#bite_parent_id').val(0);
		$('#bite_secondary_id').val(0);
		$('#bite_secondary_text').val('');
		$('#bite_text').val('');
		$('#reset-btn').hide();
		$('#row-quote').hide();
		$('#bite_link').val('http://');
		publisher.hide_link();
		$('#bite-header').html(publisher.default_header);
		publisher.keyup();
	},
	
	add_bite: function() {
		bti = $('#bite_type_id').val();
		bsi = $('#bite_secondary_id').val();
		bst = $('#bite_secondary_text').val();
		bt = $('#bite_text').val();
		bl = $('#bite_link').val();
		tk = $('#token').val();
		
		bt = jQuery.trim(bt);
		if ( bt == '' ) {
			statusBar.show("Oops, you left the text field blank!",1,1,0);
			return false;
		}
		
		$('#submit_bite').attr('disabled','disabled');
		$('#bite_text').attr('disabled','disabled');
			
		$.post('/home?action=add&ajax=1', {submit_bite:'submit_bite',token:tk,bite_type_id:bti, bite_secondary_id:bsi, bite_secondary_text:bst, bite_text:bt, bite_link:bl}, function(msg){
			if (msg) {
				if (msg.e==0) {
					et = 0;
					publisher.reset();
					home.load_home();
					bites.update_bite_count(1);
					msg_txt = msg.msg;
				}else{
					et = 1;
					$('#submit_bite').attr('disabled','');
					$('#bite_text').attr('disabled','');
					msg_txt = msg.msg;
				}
			} else {
				msg_txt = "";
				et = 1;
			}
			statusBar.show(msg_txt,et,1,0);
		},"json");
		
		return false;
	},
	
	show_link: function() {
		$('#bite_link').val('http://');
		$('#attach-link').slideDown(100, function(){$('#add-link').hide();});
	},
	
	hide_link: function() {
		$('#bite_link').val('');
		$('#attach-link').slideUp(100,function(){$('#add-link').show();});
	}
}

home = {
	current_page: 1,
	
	init: function() {
		url =  window.location.hash;
		if(url=='#mentions') home.load_mentions();
		else if(url=='#friends') home.load_friends();
		bites.attach_events(true);
		home.attach_events(); 
	},
	
	attach_events: function() {
		$('#load-home').click(function(){
			home.load_home();
			return false;
		});
		$('#load-friends').click(function(){
			home.load_friends();
			return false;
		});
		$('#load-mentions').click(function(){
			home.load_mentions();
			return false;
		});
		$('#load-everyone').click(function(){
			home.load_everyone();
			return false;
		});
	},
	
	show_loader: function() {		
		$("div.bites > div.header").html( '<div class="bites-loader">&nbsp;</div>' );
		 
		home.reset_active();
		home.current_page = 1;
	},
	reset_active: function() {
		$('#load-home').attr('class','side-tab');
		$('#load-mentions').attr('class','side-tab');
		$('#load-friends').attr('class','side-tab');
		$('#load-everyone').attr('class','side-tab');		
	},
	
	load_home: function() {
		home.show_loader();
		$("#bites-container").load('/home?action=load&p=following&ajax=1',null,function() {
			window.location.hash = 'following';
			$('#bites-container').css('min-height',null);
			$('#load-home').attr('class','side-tab-active');
		});    
	},
	
	load_mentions: function() {
		home.show_loader();
		$("#bites-container").load('/home?action=load&p=mentions&ajax=1', null, function(){
			window.location.hash = 'mentions';
			$('#bites-container').css('min-height',null);
			$('#load-mentions').attr('class','side-tab-active');
			$('#reply-count').remove();
		});
	},
	
	load_friends: function(user_id) {
		home.show_loader();
		$("#bites-container").load('/home?action=load&p=friends&ajax=1',function(){
			window.location.hash = 'friends';
			$('#bites-container').css('min-height',null);
			$('#load-friends').attr('class','side-tab-active');
		});     
	},
	
	load_everyone: function() {
		home.show_loader();
		$("#bites-container").load('/home?action=load&p=everyone&ajax=1',function(){
			window.location.hash = 'everyone';
			$('#bites-container').css('min-height',null);
			$('#load-everyone').attr('class','side-tab-active');
		});     
	},
	
	more: function(page) {
		home.current_page++;
		var bites = $('#bites-container');
		var div = $('<div>&nbsp;</div>').appendTo(bites);
		$('#show-more').html( '<div class="loader-dots-24">&nbsp;</div>' );
		div.load('/home?action=load&p='+page+'&ajax=1&page='+home.current_page,null,function(){            
			$('#show-more').remove();
		});
	}
	
}

bites = {
	attach_events: function(clicks) {
		$('a.x-16').live('click', function(){
			var bite_id = $(this).parent().parent().attr("id").split("_")[1];
			if (bite_id==null || bite_id=='') return false;
			bites.remove(bite_id);
			return false;
		});	
		if (clicks==true) {
			$('a.at-16').live('click', function(){
				var bite_id = $(this).parent().parent().attr("id").split("_")[1];
			 	var bite_text = $(this).parent().parent().find('span.usrtxt').html();
			 	var bite_user = $(this).parent().parent().find('a.usrname').html();
				if (bite_id==null || bite_id=='' || bite_user==null || bite_user=='' || bite_text==null || bite_text=='') return false;
				publisher.set(2,bite_id,bite_text,bite_user,'@'+bite_user+' ');
				return false;
			});
			$('a.share-16').live('click', function(){
				var bite_id = $(this).parent().parent().attr("id").split("_")[1];
			 	var bite_text = $(this).parent().parent().find('span.usrtxt').html();
			 	var bite_user = $(this).parent().parent().find('a.usrname').html();
				if (bite_id==null || bite_id=='' || bite_user==null || bite_user=='' || bite_text==null || bite_text=='') return false;
				publisher.set(3,bite_id,bite_text,bite_user,'');
				return false;
			});			
		}
	},
	
	remove: function(bite_id) {
		c = confirm("Are you sure? This cannot be undone");
		if (c) {
			bc = $('#bites-container');
			util.save_height(bc);
			$.get('/kungfu?action=trashbite',{bite_id:bite_id},function(msg){
				if (msg.error==1) {
					statusBar.show(msg.msg,1,1,0);
					return false;
				}
				bites.update_bite_count(-1);
				$('#bite_'+bite_id).slideUp(300);
			},"json");
		}
		return false;
	},
	
	/* x can be 1 or -1 */
	update_bite_count: function(x) {
		current = $('#bite-count').html();
		new_count = Number(current) + (Number(x));
		if (new_count==null || new_count < 0 || new_count=='') new_count = 0;
		$('#bite-count').html( String(new_count) );
	},
	
	toggle_reply: function(el_id) {
		$('#'+el_id).toggle();
	}
}

bite = {
	current_page:1,
	show_more: function(bite_id) {
		bite.current_page++;
		var bites = $('#bites-container');
		var div = $('<div>&nbsp;</div>').appendTo(bites);
		$('#show-more').html( '<div class="loader-dots-24">&nbsp;</div>' );
		div.load('/bite?action=show_more&id='+bite_id+'&page='+bite.current_page+'&ajax=1',null,function(){            
			$('#show-more').remove();
		}); 
	}

}


messager = {
	max_text: 160,
	init: function() {
		$('#message_text').val('');
		$('#message_text').focus();	
	
		/* set event handlers on bite_text */
		$('#message_text').keypress(function(event){
			messager.keyup();
		});
		$('#message_text').keyup(function(event){
			messager.keyup();
		});
		$('#add-link').click(function(){
			messager.show_link();
			return false;
		});
		$('#attach-link > a.x-16').click(function(){
			messager.hide_link();
			return false;
		});	
	},
	validate: function() {
		util.submit_go('submit_message','please wait...');
		var fields=['message_text--Oops, did you leave the text field blank?'];
		var x = util.validate_blanks(fields);
		if (x==false) util.submit_reset('submit_message','send');
		return x;	
	},
	keyup: function() {
		t = $('#message_text');
		tv = t.val();

		current_count = t.val().length;
		difference = Number(publisher.max_text - current_count);
		/*change the counters colors based on number of chr's remaining */
		if (difference < 30) $('#text-counter').addClass('low').removeClass('uhoh');
		if (difference < 15) $('#text-counter').addClass('uhoh').removeClass('low');
		if (difference > 30) $('#text-counter').removeClass('low').removeClass('uhoh');
		
		if (jQuery.trim(tv)=='') 
			$('#submit_message').addClass('f-btn-cancel').removeClass('f-btn');
		else 
			$('#submit_message').removeClass('f-btn-cancel').addClass('f-btn');
		
		/*if they pass the limit we remove any text beyond it and make sure the counter stays at 0 */
		if (difference < 0) difference = '0';
		if (current_count > publisher.max_text)
			t.val( t.val().substring(0, publisher.max_text) );
		
		$('#text-counter').html( difference );
		
	},
	
	show_link: function() {
		$('#bite_link').val('http://');
		$('#attach-link').slideDown(100, function(){$('#add-link').hide();});
	},
	
	hide_link: function() {
		$('#bite_link').val('');
		$('#attach-link').slideUp(100,function(){$('#add-link').show();});
	}
}

messages = {
	init: function() {
	
	},
	remove: function() {
		c = confirm("Are you sure? This cannot be undone.");
		if (c) {
			return true;
		}
		return false;
	}
}

cards = {
	follow: function(follower_id,following_user_id, following_user_name, card_id) {                
		follow.start(follower_id,following_user_id,following_user_name,1,function() {
			$('#card-'+card_id+'-follow').remove();
			$('#card-'+card_id+'-follower').hide();
			$('#card-'+card_id+'-following').css('display','inline');		
		});
	},    
	
	unfollow: function(follower_id, following_user_id, following_user_name, card_id) {
		c = confirm("Are you sure you want to stop following "+following_user_name+"?");
		if (c) {
			follow.stop(follower_id, following_user_id, following_user_name,1,function(){
				$('#card-'+card_id).fadeOut("slow");
			});        
		}    
	}
}

util = {
	save_height: function(el) {
		oh = $(el).css('height');
		$(el).css('min-height',oh);	
	},
	reset_height: function(el) {
		$(el).css('min-height','');
	},
	clear_if: function(el, txt) {
		e = $(el);
		if (!e) return false;
		tag = e[0].tagName;
		tag = tag.toLowerCase();
		if ( e.val() == txt ) {
			e.removeClass('default');
			e.addClass('basic');
			e.val('');
		}
	},
	
	replace_if: function(el, txt) {
		e = $(el);
		if (!e) return false
		tag = e[0].tagName;
		tag = tag.toLowerCase();
		if (e.val()=='') {
			e.removeClass('basic');
			e.addClass('default');
			if (txt!=null) e.val(txt);
		}    
	},
	
	length_validate: function(txt_element, counter_element, max) {
		t = $(txt_element);
		c = $(counter_element);
		
		
		if (!max) max = 160;
		
		var count = t.val().length;
		
		diff = max - count;
		if (diff < 0) diff = 0;
		if (count > max)
			t.val( t.val().substring(0, max) );
		
		if (counter_element) c.html(diff);
	},
	
	show_error_bg: function(el) {
		el.css({'border-color':'#ff8b73','background-color':'#ffeeea'});
		el.focus();
	},
	
	check_bg: function(el) {
		if (el.css('border-top-color')!='rgb(255, 255, 255)')
			el.css({'border-bottom-color':'#e0e0e0','border-right-color':'#e0e0e0','border-top-color':'#999999','border-left-color':'#999999','background-color':'white'}); 
	},
	
	is_blank: function(s) {
		var chars = ' ' || "\\s";
		s = s.replace(new RegExp("^[" + chars + "]+", "g")); 
		if( s == '')
			 return true;
		else 
			 return false;
	},
	
	validate_blanks: function(els) {
		for(x in els) {
			el_id = els[x].split('--')[0];
			el_msg = els[x].split('--')[1];
			el = $('#'+el_id);
			if (util.is_blank($('#'+el_id).val())) {
				util.show_error_bg(el);
				statusBar.show(el_msg,1,1);
				return false;			
			} else {
				util.check_bg(el);
			}
		}
		
		return true;
	},
	
	validate_explore_search: function() {
		txt = $('#keywords').val();
		x = util.is_blank(txt);
		if (x) return false;
		else return true;
	},
	
	submit_go: function(submit_id,submit_val) {
		$('#'+submit_id).attr('disabled','disabled').addClass('f-btn-loading').removeClass('f-btn');
		if (!util.is_blank(submit_val)) $('#'+submit_id).val(submit_val);
		$('#wait').css('display','inline');	
	},
	
	submit_reset: function(submit_id,submit_val) {
		$('#'+submit_id).attr('disabled','').addClass('f-btn').removeClass('f-btn-loading');
		if (!util.is_blank(submit_val)) $('#'+submit_id).val(submit_val);
		$('#wait').css('display','none');		
	},
	
	close_keep_alive: function() {
		if (/AppleWebKit/.test(navigator.userAgent)) {
			$.head("/index.php",{'blah':1},function(headers) {
				$.each(headers,function(key,header){ console.log(key+':--:'+header);});
			});
		}
	},
	
	kungfu: function(p, a) {
		a = a.split(".");
		for (i in a) {
			var key = a[i];
			if (p[key] == null)
				return '';
			p = p[key];
		}
		return p;
	}
}


recipe = {
	ingCount: 1,
	methCount: 1,
	current_cat: 0,
	current_main: 0,
	current_cuisine: 0,
	
	add_ingredient: function() {
		recipe.ingCount++;
		var mc = $('#i_container');
		var div = $('<div></div>').appendTo(mc).attr('class','irow clearfix').attr('id','d_ing_'+recipe.ingCount);
		var div_txt = $('<div></div>').appendTo(div).attr('class','txtd');
		var txt = $('<input type="text" class="txtfield" name="ingredient[]" />').appendTo(div_txt).attr('id','t_ing_'+recipe.ingCount);
		var btn = $('<a></a>').appendTo(div).attr('href','#').attr('class','x');
		btn.click(function(){
			recipe.remove_ingredient(div);
		});
		txt.focus();
	},
	
	remove_ingredient: function(div) {
		div.remove();
		recipe.ingCount--;
		if (recipe.ingCount<0) recipe.ingCount = 0;
	},
	
	add_meth: function() {
		recipe.methCount++;
		var mc = $('#m_container');
		var div = $('<div></div>').appendTo(mc).attr('class','irow clearfix').attr('id','d_meth_'+recipe.methCount);
		var div_txt = $('<div></div>').appendTo(div).attr('class','txtd');
		var txt = $('<textarea class="txtarea" name="method[]" rows="4" ></textarea>').appendTo(div_txt);
		var btn = $('<a></a>').appendTo(div).attr('href','#').attr('class','x');
		btn.click(function(){
			recipe.remove_meth(div);
		});
		txt.focus();
	},
	
	remove_meth: function(div) {
		div.remove();
		recipe.methCount--;
		if (recipe.methCount<0) recipe.methCount = 0;
	},
	
	validate: function() {
		/*safari fix*/
		util.close_keep_alive();
		
		util.submit_go('submit_recipe','please wait...');
		
		var fields = [
		"recipe_name--Please name your recipe.",
		"recipe_category_id--Please select a category.",
		"recipe_region_id--Please select a region.",
		"recipe_cook_time--Please enter a cook/prep time.",
		"recipe_servings--Please select a serving size.",
		"recipe_description--Please enter a description."
		];
		
		x = util.validate_blanks(fields);
		if (x==false) util.submit_reset('submit_recipe','submit');
		return x;
	},
	
	remove: function(recipe_id) {
		var c = confirm("Are you sure? This cannot be undone.");
		if (c) {
			window.location.href="/recipes?action=delete&id="+recipe_id;
		}
	},
	
	delete_recipe : function (recipe_id,recipe_name) {
		if(confirm('Do you really want to remove this recipe')){
			$.post('/controllers/recipe.php', {submit_delete_recipe:'submit_delete_recipe', recipe_id: recipe_id,ajax:1}, 
				function(data) {
					if(data.error == 0) {
						statusBar.show(data.msg,0,1,0);
						$("#"+recipe_id).fadeOut('slow');
					} else if (data.error == 1)  {
						statusBar.show(data.msg,0,1,0); 
					}
			},"json");
		}  
	}
}

settings = {
	account_init: function() {
		$('#user_name').keyup(function(){
			settings.user_name_key();
		});
	},
	location_init: function() {
		
	},
	select_country: function(el,address_info) {
		dd = $(el);
		if (dd.val()=="US") {
			$('#zip-label').html("Zip Code:");
		} else {
			$('#zip-label').html("Postal Code:");
		}
		$('#profile_zip').val('');
	},
	load_regions: function(country_code) {
		$('#profile_state').load('/settings?action=load_regions&country_code='+country_code);
	},
	update_account: function() {
		old_country = $('#old_country').val();
		old_zip = $('#old_zip').val();
		country=$('#profile_country').val();
		zip=$('#profile_zip').val();
		
		util.submit_go('submit_account','please wait...');
		var fields=[
		"user_password--Verify your password to change account settings.",
		"user_name--Please enter a username.",
		"user_email--Please enter your email address.",
		"profile_zip--Please enter your postal code."
		];
		var x = util.validate_blanks(fields);
		if (x==false) {
			util.submit_reset('submit_account','save');
			return false;
		}
		
		if ( (old_country != country) || (old_zip != zip) ) {
			gma.get_loc(zip,country,function(){
				$('#gma_c').val(gma.city);
				$('#gma_s').val(gma.region);
				$('#gma_la').val(gma.lat);
				$('#gma_lo').val(gma.lon);
				$('#gma_co').val(gma.country);
				to = setTimeout(function(){$('#acc-form').submit()},1000);
			});
		} else {
			$('#acc-form').submit();
		}
	},
	validate_info: function(type) {
		if (type=='' || type==false) return false;
		util.submit_go('submit_info','please wait...');
		
		var fields=[
		"profile_name--Hey! you left your name blank."
		];
		if (type==1||type==2||type==3) {
			fields.push(
			"profile_gender--Please select your gender.",
			"profile_age_limit--Please select your age.",
			"profile_location--Please enter a location -where are you from?"
			);
		}		
		if (type==4||type==5||type==6||type==7||type==8||type==10) {
			fields.push(
			"profile_address--Please enter your address.",
			"profile_city--Please enter your city.",
			"profile_state--Please select your state/region",
			"profile_phone--Please enter your phone number."
			);			
		}
		
		if (type==9) {
			fields.push(
			"profile_city--Please enter your city.",
			"profile_state--Please select your state/region"
			);			
		}
		
		var x = util.validate_blanks(fields);
		
		if (x==false) util.submit_reset('submit_info','save');
		return x;
	},
	validate_photo: function(frm) {
		frm.submit();
		util.close_keep_alive();
		util.submit_go('submit_photo','please wait...');
		return true;
	},
	validate_password: function() {
		util.submit_go('submit_password','please wait...');
		var fields=[
		"user_password--Verify your password to change account settings.",
		"new_password--Please create a new password.",
		"new_password2--Plese re-type your new password."
		];
		var x = util.validate_blanks(fields);
		if (x==false) util.submit_reset('submit_password','save');
		return x;		
	},
	user_name_key: function() {
		$('#username-label').html($('#user_name').val());
		if ($('#user_name').val() == '') $('#username-label').html('your_user_name');
	}
}

/*GOOGLE MAPS API*/
gma = {

	address:'',country:'',city:'',region:'',code:'',lat:'',lon:'',
	
	reset: function() {
		gma.country='';
		gma.address='',
		gma.city='';
		gma.region='';
		gma.code='';
		gma.lat='';
		gma.lon='';		
	},
	
	fix_country_code: function(code) {
		if (code=="")
			code="US";
		
		if (code=="GB") 
			code="UK";
		
		return code;
	},
	
	get_point: function(postal_code, country_code) {
		country = gma.fix_country_code(country_code);
		
		var g = new GClientGeocoder();
		g.setBaseCountryCode(country);
		g.getLatLng(postal_code, function(point){
			if (point) {

			} else {
				return false;
			}
		});
	},
	
	get_loc: function(address, country_code, callback){
		gma.reset();
		country = gma.fix_country_code(country_code);
		
		var g = new GClientGeocoder();
		g.setBaseCountryCode(country);
		g.getLocations(address, function(response){
			if (!response || response.Status.code != 200) {
				gma.address='';
				gma.country='';
				gma.city='';
				gma.region='';
				gma.code='';
				gma.lat='';
				gma.lon='';
			} else {
				place = response.Placemark[0];
				/*GLog.write(place.address + "\n");*/
				gma.country = util.kungfu(place,"AddressDetails.Country.CountryNameCode");
				gma.region = util.kungfu(place,"AddressDetails.Country.AdministrativeArea.AdministrativeAreaName");
				gma.city  = util.kungfu(place, "AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName");
				gma.code = util.kungfu(place, "AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber")
				gma.lat = util.kungfu(place,"Point.coordinates")[1];
				gma.lon = util.kungfu(place,"Point.coordinates")[0];
				/*GLog.write(gma.city+", "+gma.region+" "+gma.code+", "+g.getBaseCountryCode() + " : " + gma.lat + " / " + gma.lon);*/
			}
			
			callback.call();
			
		});
		
	}
}


