self.gg = {
	countryCode : '',
	stateCode : ''
};

function resize(newWidth, newHeight, alterHeight) {
	var browserHeight = jQuery(window).height();
	var browserWidth = jQuery(window).width();
	
	if (alterHeight) {
		if (browserHeight > 700) {
			newHeight = alterHeight;
		}
	}
		
	var newWidthMargin = -(newWidth/2);
	var newHeightMargin = -(newHeight/2);

	jQuery('#TB_window').css({
		'width': newWidth,
		'margin-left': newWidthMargin,
		'margin-top': newHeightMargin
	});
	jQuery('#TB_iframeContent').css({
		'width': newWidth-1,
		'height': newHeight
	});
};

function addFriend(UserID,FriendID) {
	alert("The script to add FriendID " + FriendID + " to the friends list of " + UserID + " would be performed here.");
	jQuery('#popBox').css('display', 'none');	
};

function blockUser(UserID,FriendID) {
	alert("The script to Block User " + FriendID + " to the friends list of " + UserID + " would be performed here.");
	jQuery('#popBox2').css('display', 'none');	
};

function pop(URL,width,height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+"');");
};

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=200,left = 490,top = 300');");
};

function add_stat(url){
	jQuery.post(url, { type: "stats"}, function(data){});    
};

function set_st(value, my_type) {
	if (my_type == "AccountType") {
		jQuery.post("/json/set/f/SignupType/", { type: value },function(data){});
		if (value == "SugarDaddy") {
			jQuery('#LookingForBaby').attr('selected', 'selected');	
		} else {
			jQuery('#LookingForDaddy').attr('selected', 'selected');	
		}
	} else if (my_type == "LookingForType") {
		if (value == "SugarDaddy") {
			jQuery('#AccountTypeBaby').attr('selected', 'selected');	
		} else {
			jQuery('#AccountTypeDaddy').attr('selected', 'selected');	
		}
	} else {
		jQuery.post("/json/set/f/SignupType/", { type: value },function(data){});
	}	   
};

function set_state(value) {
	jQuery.post("/ajax/", {type:value}, function(data){
		jQuery('#StateCode').html(data);
	});
};

function capitalize_word(loc) {
	loc = loc.toLowerCase();
	var Rx= /\b([a-z]+)\b/ig;
	loc = loc.replace(Rx,function(w){
		return w.charAt(0).toUpperCase()+w.substring(1);
	});
	return loc;
};

function get_geo() {
	jQuery.ajax({
		url: "/json/getgeo/", 
		async: false,
		dataType: 'json',
		success: function(data){
			gg.countryCode = data.COUNTRY_CODE;
			gg.stateCode = data.STATECODE;
			var loc = capitalize_word(data.CITY)+", "+data.STATECODE;
			if (jQuery('#location')) {
				jQuery('#location').html('in '+loc);
			}
		}
	});
};

function get_region() {
	jQuery.getJSON("/json/getgeo/", function(data){
		var loc = capitalize_word(data.CITY+", "+data.REGION);
    });
};

function get_search(gender, thumbCount, showLogin, countryCode, stateCode) {
	if (thumbCount === null) {
		thumbCount = 0;
	}
	if (gender === null) {
		gender = "SugarBaby";
	}
	var container = jQuery('#thumbs-container');
	var clearList = jQuery('<div class="clear"></div>');
	container.empty();
	jQuery.ajax({
		url: "/json/getsearch/",
		async: false,
		data: {'st': gender, 'le': thumbCount, 'hi': 1, 'cc': countryCode, 'sc': stateCode},
		dataType: 'json',
		success: function(data){
			jQuery.each(data, function(i,item) {
				var info = item.DisplayName+", "+item.Age;
				var thumbClass;
				if (i == thumbCount-1) {
					thumbClass = 'thumbs thumbs-end';
				} else {
					thumbClass = 'thumbs';
				}
				var thumb = jQuery('<div>').attr({
					'id': 'thumb'+i,
					'class': thumbClass
				});
				
				var thumbi = jQuery('<img>').attr({
					'src': item.DefaultPicture,
					'alt': item.DisplayName,
					'width': 80,
					'height': 80	
				});
				
				if (showLogin) {
					var imagea = jQuery('<a>').attr('href', '/search/profile/member/'+item.SeoLink);
					imagea.append(thumbi);
					thumb.append(imagea);
				} else {
					thumb.append(thumbi);
				}
				thumb.append(jQuery('<div>').attr({
					'id': 'thumb'+i+'-info',
					'class': 'thumb-info'
				}).html(info));
				if (showLogin) {
					thumb.append(jQuery('<div>').attr({
						'id': 'thumb'+i+'-login',
						'class': 'thumb-login'
					}).html(item.DisplayLastLogin+' ago'));
				}
				container.append(thumb);
			});
			container.append(clearList);
		}
	});
};

function set_gender(gender) {
	if (gender == "SugarDaddy") {
		gender = "Sugar Daddies";
	} else {
		gender = "Sugar Babies";
	}
	jQuery('#location-gender').html(gender);	
};

function close_redirect() {
	//thickbox close
	tb_remove();
	window.location = "http://"+location.host+"/login/";
};

function run_index(gender, thumbCount) {
	get_geo();
	get_search(gender, thumbCount, false, gg.countryCode, gg.stateCode);
};

function run_results(gender, thumbCount, countryCode, stateCode) {
	get_search(gender, thumbCount, true, countryCode, stateCode);
	get_region();
};

function set_status(value, my_type, thumbCount) {
	var gender;
	if (value == "SugarDaddy") {
		gender = "SugarBaby";
	} else {
		gender = "SugarDaddy";
	}
	get_geo();
	set_st(value, my_type);
	get_search(gender, thumbCount, false, gg.countryCode, gg.stateCode);
	if (jQuery('#location-gender')) {
		set_gender(gender);	
	} 
};

function initLogin() {
	jQuery('#loginForm').validate({
		rules: {
			Email: {
				required: true,
				email: true
			},
			Password: "required"
		},
		messages: {
			Email: "Please enter your email address",
			Password: "Please enter your password"
		}
	});
};

function initMessage() {
	if (jQuery('.message-status').length > 0) {
		setTimeout(function() {
			jQuery('.message-status').fadeOut('slow', function() {
				jQuery(this).remove();
			});
		}, 2000);	
	}
};

jQuery(document).ready(function() {
	initMessage();
	
	jQuery("#searchForm").submit(function() {
		var errorMessage = "";
		var error = false;	
		/*
		if (jQuery('#AccountType:checked').val() === null) {
			errorMessage = "Please choose if you are a sugar daddy or baby.\n";
			error = true;
		}
		if (jQuery('#LookingForType:checked').val() === null) {
			errorMessage += "Please choose if you want to meet a sugar daddy or baby.\n";
			error = true;
		}
		*/
		
		if (jQuery('#StateCode').val() == "" && jQuery('#StateCode option:selected').text() == "Select") {
			errorMessage = "Please choose your search state.\n";
			error = true;
		}
		if(jQuery('#CountryCode').val() == "" && jQuery('#CountryCode option:selected').text() == "Select") {
			errorMessage = "Please choose your search country.\n";
			error = true;
		}
		
		if(error) {
			alert(errorMessage);
			return false;
		}
	});
});
