//INITIALIZE
Controller = function(p_nFbId) {
	
	this.l_aItems = [];
	this.l_aPopular = [];

	//category page size
	this.l_nPageSize = 8;
	
	//latest page size
	this.l_nHomePageSize = 12;
	
	this.l_nNextPage = 1;
	
	this.l_nFilterType = -1; //1 = art, 2 = blog, 3 = event
	
	this.l_sFBID;
	this.l_sName;
	this.l_nUserId;
	this.l_nUserTerms;

	this.l_aBadSlugs = [];
	this.l_aBadSlugs[0] = 'photo';
	this.l_aBadSlugs[1] = 'blogs';
	this.l_aBadSlugs[2] = 'events';
	
	this.l_bShareFixed = 0;
	this.l_nHeaderType = 1; //2 = detail page
	
	this.l_sSlug;
	
	this.l_nLongitude;
	this.l_nLatitude;
	this.l_sRegion;
	
	this.l_bInProxSearch = 0;
	this.l_nMiles;
	
	this.l_bDebug = 0;
	
	this.l_sDomain = 'http://rvyou.com/';
	//this.l_sDomain = 'http://rvyou.snackmachinemedia.com/';
	//this.l_sDomain = 'http://abc.rvyou.com';

	this.l_nRegionTimeStamp = 0;
	this.l_nLocTimeout = 7000;
	
	this.l_sUserLang = '';
	
}


Controller.prototype.initialize = function() {
	var thisController = this;
	this.setActiveLink();
	
	
//	if (false) {
	if (this.l_nFilterType >= 0) {
		this.getCoordsIP();
	}
	
	if (this.l_nFilterType > 0 && this.l_nLongitude && this.l_nLatitude) {
		$('#prox_5').click();
	} else if (this.l_nFilterType > 0) {
		$('#content_loader').fadeOut(700);
		$('#items_wrapper').delay(900).fadeIn(700);
	}
	
	
	
	//alert(thisController.l_sUserLang);
}

Controller.prototype.showLogin = function() {
	var thisController = this;
	this.l_sFBID = '';
	this.l_nUserId = 0;
	$.cookie('rvyou_name','X', { expires: 1, path: '/'});
	$('#hat_loader').fadeOut(700);
	$('#hat_name').fadeOut(700);
	$('#hat_login').delay(900).fadeIn(700);
	$('#hat_badge').addClass('inactive');
	$('#hat_badge').removeClass('expanded');
	$('#badge_trans').hide();
	$('#badge_contents').hide();
	$('.delete_item').hide();
	
	FB.XFBML.parse(document.getElementById('hat_login'));
	l_xGreenlight.reset();
	l_xGreenlight.drawInitial();
}

Controller.prototype.logOut = function() {
	var thisController = this;
	$.cookie('rvyou_name','X',{ expires: 1, path: '/'});
	FB.logout(function(response) {
		thisController.showLogin();
	});
}


Controller.prototype.displayUser = function() {
	var thisController = this;
	var l_sThumbSrc = 'http://graph.facebook.com/' + thisController.l_sFBID + '/picture?type=square';
	var l_sThumbHtml = '<img src="' + l_sThumbSrc + '" />';
	$('#hat_thumb').html(l_sThumbHtml);
	$('#hat_thumb img').load(function() {
		$(this).fadeIn(600);
	});
	
	$('#hello span').html(thisController.l_sName);
	$('#hat_login').fadeOut(700);
	$('#hat_loader').fadeOut(700);
	$('#hat_name').delay(900).fadeIn(700);
	
	l_xGreenlight.drawInitial();
	l_xGreenlight.getBadge();
	
	if (thisController.l_nFilterType<0) thisController.displayDelete();
	
}

Controller.prototype.displayDelete = function() {
	
	var thisController = this;
		
	$.post("/lib/controllerAjax.php?action=showDelete", { user_id: thisController.l_nUserId, slug: thisController.l_sSlug },
		function(data){

			if (data.answer!=0) {
				$('.delete_item').show();
			} else {
			
			}
		}, "json");
}
	

Controller.prototype.checkUserExists = function(p_nFbId,p_nCallbackType) {
	
	//alert('a');
	var thisController = this;
	$.post("/lib/controllerAjax.php?action=checkUserExists", { fbid: p_nFbId },
		function(data){

			if (data.answer!=0) {
				//alert('b');
				thisController.l_nUserId = data.userid;
				thisController.l_sName = data.name;
				thisController.l_sFBID = data.fbid;
				thisController.l_nUserTerms = data.agreed_to_terms;
				$.cookie('rvyou_name',thisController.l_sName, { expires: 1, path: '/'});
				
				thisController.displayUser();
				thisController.finishFormSubmission(p_nCallbackType);
				
			} else {
				//alert('c');
				thisController.processUser(p_nFbId,p_nCallbackType);
				
			}
		}, "json");
	
}

Controller.prototype.deleteItem = function(p_nId) {
	
	var thisController = this;
	
	
	$(function() {
			// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
			//$( "#dialog:ui-dialog" ).dialog( "destroy" );

			$( "#dialog-confirm" ).dialog({
				resizable: false,
				height:140,
				width:350,
				modal: true,
				buttons: {
					"Delete": function() {
						$.post("/lib/controllerAjax.php?action=deleteItem", { id: p_nId, user_id: thisController.l_nUserId },
							function(data){
								if (data.answer!=0) {
									window.location.replace('/');
								} else {
									alert('An error occurred. Please try again. [5]');
								}
							}, "json");
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				}
			});
		});
	
	/*$.post("/lib/controllerAjax.php?action=deleteItem", { id: p_nId, user_id: thisController.l_nUserId },
		function(data){

			if (data.answer!=0) {
				window.location.replace('/');
			} else {
				alert('An error occurred. Please try again. [5]');
			}
		}, "json");*/
	
}

Controller.prototype.finishFormSubmission = function(p_nCallbackType) {
	var thisController = this;
						
	if (p_nCallbackType == 1) {
		l_xForm.prepItemPhoto();
	} else if (p_nCallbackType == 2) {
		l_xForm.prepBlog();
	} else if (p_nCallbackType == 3) {
		l_xForm.prepItemEvent();
	}

}
	


Controller.prototype.processUser = function(p_nFbId,p_nCallbackType) {
	
	var thisController = this;
	var l_sApiQuery = 'SELECT name FROM user WHERE uid=' + p_nFbId;
	var l_sApiMethod = 'fql.query';
	
	FB.api({method: l_sApiMethod, query: l_sApiQuery}, function(response) {
		
		thisController.l_sName = response[0].name;
		$.cookie('rvyou_name',thisController.l_sName, { expires: 1, path: '/'});
		thisController.l_sFBID = p_nFbId;
		
		$.post("/lib/controllerAjax.php?action=processUser", { fbid: thisController.l_sFBID, name: thisController.l_sName },
			function(data){

				if (data.answer!=0) {
					//alert('d');
					thisController.l_nUserId = data.userid;
					thisController.l_nUserTerms = data.agreed_to_terms;
					
					thisController.displayUser();
					if (p_nCallbackType > 0) {
						
						//alert('e');
						if (p_nCallbackType == 1) {
							l_xForm.prepItemPhoto();
						} else if (p_nCallbackType == 2) {
							l_xForm.prepBlog();
						} else if (p_nCallbackType == 3) {
							l_xForm.prepItemEvent();
						}
												
					}
					
				} else {
					alert('An error occurred. Please try again. [1]');
				}
			}, "json");
		
		
	});
}



Controller.prototype.setActiveLink = function() {
	var thisController = this;
	
	if (thisController.l_nFilterType==0) {
		$('#popular').addClass('active');
	} else if (thisController.l_nFilterType==1) {
		$('#photos').addClass('active');
	} else if (thisController.l_nFilterType==2) {
		$('#blogs').addClass('active');
	} else if (thisController.l_nFilterType==3) {
		$('#events').addClass('active');
	}
	
}



Controller.prototype.getItems = function() {
  
	var thisController = this;
	thisController.l_bInProxSearch = 0;
	
	$.post("/lib/controllerAjax.php?action=getItems", { p_nFilterType: thisController.l_nFilterType, p_nNextPage: thisController.l_nNextPage, p_nPageSize: thisController.l_nPageSize },
		function(data){
			
			
			if (data.answer!=0) {
				thisController.l_aItems = data.items;
				if (thisController.l_aItems.length==0) {
					thisController.noMoreItems();
				} else {
					thisController.l_nNextPage += 1;
					thisController.addItems(data.more);
					
					if (thisController.l_aItems.length == thisController.l_nPageSize || data.more==1) {
						$('#more_items').fadeIn(700);
					} else {
						$('#more_items').fadeOut(700);
					}
				}
			
			} else {
				alert('An error occurred. Please try again. [3]');
			}
		}, "json");
}


Controller.prototype.getCloseEvents = function() {
  	
	var thisController = this;
	thisController.l_nMiles;
	thisController.l_bInProxSearch = 1;
	
	$.post("/lib/controllerAjax.php?action=getCloseEvents", { p_nNextPage: thisController.l_nNextPage, p_nPageSize: thisController.l_nPageSize, p_nLat: thisController.l_nLatitude,  p_nLong: thisController.l_nLongitude, p_nMiles: thisController.l_nMiles, p_nType: thisController.l_nFilterType },
		function(data){
			
			if (data.answer!=0) {
		
				thisController.l_aItems = data.items;
				if (thisController.l_aItems.length==0) {
					thisController.noMoreItems();
				} else {
					thisController.l_nNextPage += 1;
					thisController.addItems(data.more);
					
				}
			
			} else {
				alert('An error occurred. Please try again. [3]');
			}
		}, "json");
}


Controller.prototype.getPopular = function() {
  
	var thisController = this;

	
	$.post("/lib/controllerAjax.php?action=getPopular", {  },
		function(data){
			
			if (data.answer!=0) {
				thisController.l_aPopular = data.items;
				thisController.addItemsPopular();
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}

Controller.prototype.getPopularAjax = function() {
  
	var thisController = this;

	$('#popular_inner').html('');
	
	$.post("/lib/controllerAjax.php?action=getPopular", {  },
		function(data){
			
			if (data.answer!=0) {
				thisController.l_aPopular = data.items;
				thisController.addItemsPopular();
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}


Controller.prototype.getPopularProx = function() {
  
	var thisController = this;

	$('#popular_inner').html('');
	
	$.post("/lib/controllerAjax.php?action=getPopularProx", { p_nLat: thisController.l_nLatitude,  p_nLong: thisController.l_nLongitude, p_nMiles: thisController.l_nMiles },
		function(data){
			
			if (data.answer!=0) {
				thisController.l_aPopular = data.items;
				thisController.addItemsPopular();
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}


Controller.prototype.getLatest = function() {
  
	var thisController = this;
	
	
	$.post("/lib/controllerAjax.php?action=getLatest", { p_nNextPage: thisController.l_nNextPage, p_nPageSize: thisController.l_nHomePageSize },
		function(data){
			
			
			if (data.answer!=0) {
				thisController.l_aItems = data.items;
				if (thisController.l_aItems.length==0) {
					thisController.noMoreItems();
				} else {
					thisController.l_nNextPage += 1;
					thisController.addItemsHome(data.more);
				}
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}

Controller.prototype.getLatestProx = function() {
  
	var thisController = this;
	
	
		$('#home_inner').html('');
	
	//	alert(thisController.l_nNextPage + ' - ' + thisController.l_nHomePageSize + ' - ' + thisController.l_nLatitude + ' - ' + thisController.l_nLongitude + ' - ' + thisController.l_nMiles);
	
	$.post("/lib/controllerAjax.php?action=getLatestProx", { p_nNextPage: thisController.l_nNextPage, p_nPageSize: thisController.l_nHomePageSize, p_nLat: thisController.l_nLatitude,  p_nLong: thisController.l_nLongitude, p_nMiles: thisController.l_nMiles },
		function(data){
			
			
			if (data.answer!=0) {
				thisController.l_aItems = data.items;
				if (thisController.l_aItems.length==0) {
					thisController.noMoreItems();
				} else {
					thisController.l_nNextPage += 1;
					thisController.addItemsHome(data.more);
				}
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}

Controller.prototype.getLatestAjax = function() {
  
	var thisController = this;
	
	$('#home_inner').html('');
	
	$.post("/lib/controllerAjax.php?action=getLatest", { p_nNextPage: thisController.l_nNextPage, p_nPageSize: thisController.l_nHomePageSize },
		function(data){
			
			
			if (data.answer!=0) {
				thisController.l_aItems = data.items;
				if (thisController.l_aItems.length==0) {
					thisController.noMoreItems();
				} else {
					thisController.l_nNextPage += 1;
					thisController.addItemsHome(data.more);
				}
			
			} else {
				alert('An error occurred. Please try again. [4]');
			}
		}, "json");
}



Controller.prototype.noMoreItems = function() {
	var thisController = this;
	
	if (thisController.l_nNextPage==0) {
		$('#content_loader').fadeOut(700);
		$('#items_wrapper').fadeOut(700);
		$('#no_items').delay(900).fadeIn(700);
		$('#no_items2').show();
		$('#more_items_home').hide();
	}
	
	$('#more_items').fadeOut('700');
	

}

Controller.prototype.addItems = function(p_bMore) {
	var thisController = this;
	
	var l_xItem;
	var l_sThumbSrc;
	
	var l_sHtml = '';
	
	$('#more_items').hide();

	for (i in this.l_aItems) {
		
		l_xItem = this.l_aItems[i];
		
		if (this.l_nNextPage==1) {
			l_sHtml += '<li class="item_photo">';
		} else {
			l_sHtml += '<li class="new item_photo">';
		}
		
		//photos
		if (this.l_nFilterType==1) {
			
			l_sThumbSrc = 'http://graph.facebook.com/' + l_xItem.fbid + '/picture?type=square';

			l_sHtml += '<div class="item_thumb"><a href="/' + l_xItem.slug  + '/"><img src="/thumbs/' + l_xItem.slug + '.jpg" /></a></div>';
			
			l_sHtml += '<div class="item_middle">';
			l_sHtml += '<div class="item_title"><a href="/' + l_xItem.slug  + '/">' + l_xItem.title + '</a></div>';
			l_sHtml += '<div class="item_social">';
			
			l_sHtml += "<div id='gl_" + l_xItem.id + "' class='gl gl_cat'><a href='#'>GreenLight</a><div class='count'></div><br class='clear' /></div>";
			
			
			l_sHtml += '<div class="item_detail_like"><fb:like href="' + this.l_sDomain + '/' + l_xItem.slug + '/" send="true" layout="button_count" width="90" show_faces="false"></fb:like></div>';
			
			l_sHtml += '<div class="item_detail_tweet"><a href="https://twitter.com/share" data-url="' + this.l_sDomain + '/' + l_xItem.slug + '/" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script></div><br class="clear" />';
			
			l_sHtml += '</div>';
			l_sHtml += '</div>';
			
			l_sHtml += '<div class="item_by">Shared by:<br />' + l_xItem.fbname + '</div>';
			
			
			l_sHtml += '<div class="item_right">';
			l_sHtml += '<img src="' +  l_sThumbSrc + '" />';
			l_sHtml += '</div>';
			l_sHtml += '<div class="clear"></div>';
		}
		
		//blogs
		else if (this.l_nFilterType==2) {

			l_sThumbSrc = 'http://graph.facebook.com/' + l_xItem.fbid + '/picture?type=square';

			if (l_xItem.has_thumb==1) {
				l_sHtml += '<div class="item_thumb"><a href="/' + l_xItem.slug  + '/"><img src="/thumbs/' + l_xItem.slug + '.jpg" /></a></div>';
			} else {
				l_sHtml += '<div class="item_thumb"><a href="/' + l_xItem.slug  + '/"><img src="/images/rvyou_blog_thumb.gif" /></a></div>';
			}
			l_sHtml += '<div class="item_middle">';
			l_sHtml += '<div class="item_title"><a href="/' + l_xItem.slug  + '/">' + l_xItem.title + '</a></div>';
			l_sHtml += '<div class="item_social">';
			l_sHtml += "<div id='gl_" + l_xItem.id + "' class='gl gl_cat'><a href='#'>GreenLight</a><div class='count'></div><br class='clear' /></div>";
			l_sHtml += '<div class="item_detail_like"><fb:like href="' + this.l_sDomain + '/' + l_xItem.slug + '/" send="true" layout="button_count" width="90" show_faces="false"></fb:like></div>';

			l_sHtml += '<div class="item_detail_tweet"><a href="https://twitter.com/share" data-url="' + this.l_sDomain + '/' + l_xItem.slug + '/" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script></div><br class="clear" />';

			l_sHtml += '</div>';
			l_sHtml += '</div>';

			l_sHtml += '<div class="item_by">Shared by:<br />' + l_xItem.fbname + '</div>';


			l_sHtml += '<div class="item_right">';
			l_sHtml += '<img src="' +  l_sThumbSrc + '" />';
			l_sHtml += '</div>';
			l_sHtml += '<div class="clear"></div>';
		}
		
		else if (this.l_nFilterType==3) {

			l_sThumbSrc = 'http://graph.facebook.com/' + l_xItem.fbid + '/picture?type=square';

			if (l_xItem.has_thumb==1) {
				l_sHtml += '<div class="item_thumb"><a href="/' + l_xItem.slug  + '/"><img src="/thumbs/' + l_xItem.slug + '.jpg" /></a></div>';
			} else {
				l_sHtml += '<div class="item_thumb"><a href="/' + l_xItem.slug  + '/"><img src="/images/rvyou_blog_thumb.gif" /></a></div>';
			}
			l_sHtml += '<div class="item_middle">';
			l_sHtml += '<div class="item_title"><a href="/' + l_xItem.slug  + '/">' + l_xItem.title + '</a></div>';
			l_sHtml += '<div class="item_social">';
			l_sHtml += "<div id='gl_" + l_xItem.id + "' class='gl gl_cat'><a href='#'>GreenLight</a><div class='count'></div><br class='clear' /></div>";
			l_sHtml += '<div class="item_detail_like"><fb:like href="' + this.l_sDomain + '/' + l_xItem.slug + '/" send="true" layout="button_count" width="90" show_faces="false"></fb:like></div>';

			l_sHtml += '<div class="item_detail_tweet"><a href="https://twitter.com/share" data-url="' + this.l_sDomain + '/' + l_xItem.slug + '/" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script></div><br class="clear" />';

			l_sHtml += '</div>';
			l_sHtml += '</div>';

			l_sHtml += '<div class="item_by">Shared by:<br />' + l_xItem.fbname + '</div>';


			l_sHtml += '<div class="item_right">';
			l_sHtml += '<img src="' +  l_sThumbSrc + '" />';
			l_sHtml += '</div>';
			l_sHtml += '<div class="clear"></div>';
		}
		
		l_sHtml += '</li>';
	}
	
		
	
	if (this.l_nNextPage==1) {
		$('#items_wrapper ul').append(l_sHtml);
		
		$('#content_loader').fadeOut(700);
		$('#items_wrapper').delay(900).fadeIn(700);
		FB.XFBML.parse(document.getElementById('items_wrapper'));
	} else {
		$('#items_wrapper ul').append(l_sHtml);
		
		
		$('#items_wrapper ul li.new').delay(200).fadeIn(1000);
		FB.XFBML.parse(document.getElementById('items_wrapper'));
		setTimeout(function() {
			$('#items_wrapper ul li.new').removeClass('new');
		},1500);
	}
	
	if (p_bMore==1) {
		$('#more_items').fadeIn(700);
	}
	
	l_xGreenlight.drawInitial();
	
}



Controller.prototype.addItemsHome = function(p_bMore) {
	var thisController = this;
	
	//if (thisController.l_aItems.length==0) {
	//	$('#no_items2').show();
	//	$('#more_items_home').hide();
	//	return false;
	//} else {
	$('#no_items2').hide();
	//}
	
	var l_xItem;
	var l_sThumbSrc;
	
	var l_sHtml = '';
	var l_sCat = '';
	var l_sCatHref = '';
	var l_sRowEnd = '';
	
	$('#more_items_home').hide();
	
	for (i in this.l_aItems) {
		
		
		l_xItem = this.l_aItems[i];
		
		if (i%6 == 5) {
			l_sRowEnd = ' rowEnd';
		} else {
			l_sRowEnd = '';
		}
		
		if (this.l_nNextPage==1) {
			l_sHtml += '<div class="homeItem'+ l_sRowEnd +'">';
		} else {
			l_sHtml += '<div class="homeItem new'+ l_sRowEnd +'">';
		}
		
		
		l_sHtml += '<div class="home_title"><a href="/' + l_xItem.slug +  '/">' + l_xItem.title  + '</a></div>';
		
		if (l_xItem.type!=1 && l_xItem.has_thumb==0) {
			l_sHtml += '<div class="home_thumb"><a href="/' + l_xItem.slug +  '/"><img src="/images/home_thumb.gif" /></a></div>';
		} else {
			l_sHtml += '<div class="home_thumb"><a href="/' + l_xItem.slug +  '/"><img src="/thumbs/' + l_xItem.slug + '.jpg" /></a></div>';
		}
		
		
		
		if (l_xItem.type==1) {
			l_sCat = 'Photo';
			l_sCatHref = '/photo/';
		} else if (l_xItem.type==2) {
			l_sCat = 'Blog';
			l_sCatHref = '/blogs/';
		} else if (l_xItem.type==3) {
			l_sCat = 'Event';
			l_sCatHref = '/events/';
		}
		
		l_sHtml += '<div class="home_cat"><a href="'  + l_sCatHref +  '">' + l_sCat  + '</a></div>';
		
		
		l_sHtml += '</div>';
	}
	
	l_sHtml += '<br class="clear" />';

	
	if (this.l_nNextPage==1) {
		$('#home_wrapper div#home_inner').append(l_sHtml);
		$('#content_loader').fadeOut(700);
		$('#home_wrapper').delay(900).fadeIn(700);
	} else {
		$('#home_wrapper div#home_inner').append(l_sHtml);
		$('#home_wrapper div div.new').delay(200).fadeIn(1000);
		setTimeout(function() {
			$('#home_wrapper div div.new').removeClass('new');
		},1500);
	}
	
	if (p_bMore==1) {
		setTimeout(function() {
			$('#more_items_home').show();
		},500);
		
	}
	
}

Controller.prototype.addItemsPopular = function() {
	
	var thisController = this;
	
	if (thisController.l_aPopular.length==0) {
		$('#no_items1').show();
		return false;
	} else {
		$('#no_items1').hide();
	}
	
	var l_xItem;
	var l_sThumbSrc;
	
	var l_sHtml = '';
	var l_sCat = '';
	var l_sCatHref = '';
	var l_sRowEnd = '';
	
	
	for (i in this.l_aPopular) {

		l_xItem = this.l_aPopular[i];
		if (i%6 == 5) {
			l_sRowEnd = ' rowEnd';
		} else {
			l_sRowEnd = '';
		}
		if (this.l_nNextPage==1) {
			l_sHtml += '<div class="homeItem'+ l_sRowEnd +'">';
		} else {
			l_sHtml += '<div class="homeItem'+ l_sRowEnd +'">';
		}
		
		l_sHtml += '<div class="home_title"><a href="/' + l_xItem.slug +  '/">' + l_xItem.title  + '</a></div>';
		
		if (l_xItem.type!=1 && l_xItem.has_thumb==0) {
			l_sHtml += '<div class="home_thumb"><a href="/' + l_xItem.slug +  '/"><img src="/images/home_thumb.gif" /></a></div>';
		} else {
			l_sHtml += '<div class="home_thumb"><a href="/' + l_xItem.slug +  '/"><img src="/thumbs/' + l_xItem.slug + '.jpg" /></a></div>';
		}
		
		if (l_xItem.type==1) {
			l_sCat = 'Photo';
			l_sCatHref = '/photo/';
		} else if (l_xItem.type==2) {
			l_sCat = 'Blog';
			l_sCatHref = '/blogs/';
		} else if (l_xItem.type==3) {
			l_sCat = 'Event';
			l_sCatHref = '/events/';
		}
		
		l_sHtml += '<div class="home_cat"><a href="'  + l_sCatHref +  '">' + l_sCat  + '</a></div>';
		
		
		l_sHtml += '</div>';
	}
	
	l_sHtml += '<br class="clear" />';

		$('#popular_wrapper div#popular_inner').append(l_sHtml);
		$('#content_loader_popular').fadeOut(700);
		$('#popular_inner').delay(900).fadeIn(700);
	
}




Controller.prototype.abortRegionCheck = function() {
	//alert('!');
	
	var thisController = this;
	
	var l_nCurrentTimeStamp = new Date().getTime();
	
	if (thisController.l_nRegionTimeStamp==0) {
		return false;
	} else {
		l_xForm.showRegion(false);
	}
}


	
Controller.prototype.getCoordsIP = function() {
    
	var thisController = this;
	
	thisController.l_nRegionTimeStamp = new Date().getTime();
	setTimeout(function() {
		thisController.abortRegionCheck();
	},thisController.l_nLocTimeout);
	
	
	var l_sRegion = $.cookie('rvyou_region');
	var l_nLatitude = $.cookie('rvyou_latitude');
	var l_nLongitude = $.cookie('rvyou_longitude');
	
	if (l_sRegion && l_nLatitude && l_nLongitude) {
		thisController.l_sRegion = l_sRegion;
		thisController.l_nLatitude = l_nLatitude;
		thisController.l_nLongitude = l_nLongitude;
		l_xForm.showRegion(true);
		return true;
	}
	
	var loc = {};
    var geocoder = new google.maps.Geocoder();
    
	if(google.loader.ClientLocation) {
        loc.lat = google.loader.ClientLocation.latitude;
        loc.lng = google.loader.ClientLocation.longitude;

        var latlng = new google.maps.LatLng(loc.lat, loc.lng);
        geocoder.geocode({'latLng': latlng}, function(results, status) {
            if(status == google.maps.GeocoderStatus.OK) {
				thisController.setRegion(loc.lat,loc.lng,results[0].address_components);
			} else {
				thisController.getCoordsTriangulate();
			}
        });
    } else {
		thisController.getCoordsTriangulate();
	}
}

Controller.prototype.getCoordsTriangulate = function() {
	
	var thisController = this;
	
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position){
			var outputString = "Lat: " +position.coords.latitude + ", Lon: " + position.coords.longitude;
			
			thisController.l_nLatitude = position.coords.latitude;
			thisController.l_nLongitude = position.coords.longitude;
			
			var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
	        var geocoder = new google.maps.Geocoder();
			geocoder.geocode({'latLng': latlng}, function(results, status) {
	            if(status == google.maps.GeocoderStatus.OK) {
	            	thisController.setRegion(thisController.l_nLatitude,thisController.l_nLongitude,results[0].address_components);
				};
	        });

		},
		function(error){
		},
		{enableHighAccuracy: false, maximumAge: 120000});
	}

}


Controller.prototype.setRegion = function(p_nLatitude,p_nLongitude,result) {
	var thisController = this;

	var l_sCity;
	var l_sState;
	
	var l_sRegion;
	
	for (var i=0;i<result.length;++i) {
		if (result[i].types[0]=="sublocality") {
			l_sCity = result[i].long_name;
		}
		if (result[i].types[0]=="locality") {
			l_sCity = result[i].long_name;
		}
		if (result[i].types[0]=="administrative_area_level_1") {
			l_sState = result[i].long_name;
		}
  

	}
	

	if (l_sCity && l_sState) {
		l_sRegion = l_sCity + ', ' + l_sState;
	} else if (l_sCity) {
		l_sRegion = l_sCity;
	} else if (l_sState) {
		l_sRegion = l_sState;
	} else {
		l_sRegion = '';
		l_xForm.showRegion(false);
	}
	//alert(l_sRegion);
	
	$.cookie('rvyou_region',l_sRegion, { expires: 30, path: '/'});
	$.cookie('rvyou_latitude',p_nLatitude, { expires: 30, path: '/'});
	$.cookie('rvyou_longitude',p_nLongitude, { expires: 30, path: '/'});
	
	thisController.l_sRegion = l_sRegion;
	thisController.l_nLatitude = p_nLatitude;
	thisController.l_nLongitude = p_nLongitude;
	
	l_xForm.showRegion(true);
	return true;
}



Controller.prototype.setRegionFromAddress = function(p_sAddress,p_bReSearch){

	var thisController = this;
	var geo = new google.maps.Geocoder;

	geo.geocode({'address':p_sAddress},function(results, status){
		if (status == google.maps.GeocoderStatus.OK) {
			var l_nLatitude = results[0].geometry.location.lat();
			var l_nLongitude = results[0].geometry.location.lng();
			
			$.cookie('rvyou_region',p_sAddress, { expires: 30, path: '/'});
			$.cookie('rvyou_latitude',l_nLatitude, { expires: 30, path: '/'});
			$.cookie('rvyou_longitude',l_nLongitude, { expires: 30, path: '/'});

			thisController.l_sRegion = p_sAddress;
			thisController.l_nLatitude = l_nLatitude;
			thisController.l_nLongitude = l_nLongitude;
			
			$('.region_current2 span').html(p_sAddress);
			$('.region_current span').html(p_sAddress);
			
			if (p_bReSearch==1 && thisController.l_bInProxSearch==1) {
				$('#no_items').hide();
				thisController.l_nNextPage = 0;
				thisController.getCloseEvents();
			}
			
		} else {
			//alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

Controller.prototype.changeProximity = function(p_sId){
	
	var thisController = this;
	
	if (p_sId!='prox_all' && (!this.l_sRegion || !this.l_nLatitude || !this.l_nLongitude)) {
		alert('We\'re having trouble figuring out your location. Please enter a valid region first.');
		return false;
	}
	this.l_nNextPage = 0;
	
	$('.prox').removeClass('active');
	$('#'+p_sId).addClass('active');
	
	$('#items_wrapper').hide();
	$('#items_wrapper ul').html('');
	$('#no_items').hide();
	$('#content_loader').show();
	
	if (p_sId=='prox_all') {
		this.getItems();
	} else {
		var l_aId = p_sId.split('_');
		var l_nMiles = parseInt(l_aId[1]);
		this.l_nMiles = l_nMiles;
		this.getCloseEvents();
	}
		
}


Controller.prototype.changeProximityHome = function(p_sId){
	
	var thisController = this;
	
	if (p_sId!='prox_all' && (!this.l_sRegion || !this.l_nLatitude || !this.l_nLongitude)) {
		alert('We\'re having trouble figuring out your location. Please enter a valid region first.');
		return false;
	}
	this.l_nNextPage = 0;
	
	$('.prox').removeClass('active');
	$('#'+p_sId).addClass('active');
	
	$('#items_wrapper').hide();
	$('#items_wrapper ul').html('');
	$('#no_items').hide();
	$('#content_loader').show();
	
	if (p_sId=='prox_all') {
		this.getLatestAjax();
		this.getPopularAjax();
	} else {
		var l_aId = p_sId.split('_');
		var l_nMiles = parseInt(l_aId[1]);
		this.l_nMiles = l_nMiles;
		this.getLatestProx();
		this.getPopularProx();
	}
		
}

	


