var people = {
	saints: [],
	start_index: 1,
	count: 49,
	find: function (success) {
		$.ajax({
			type: "GET",
			url: "/ws/people/search/",
			dataType: "xml",
			data: {
				"start_index": people.start_index,
				"count": people.count,
				"genders": $("#genders option:selected").val(),
				"ages": $("#ages option:selected").val(),
				"ethnicities": $("#ethnicities option:selected").val(),
				"religions": $("#religions option:selected").val(),
				"text": ($("#text").val() == 'Enter First/Last Name, Heritage, Country, State/Region, City or Keywords') ? '' : $("#text").val(),
				"type": "finder"
			},
			success: success,
			error: function (data) {
				//alert("error: " + data); // If this is used, the alert comes up if the page doesn't finish loading before you click "back"
				console.log("error: " + data);
			}
		});
	},
	find_orig: function () {
		people.reset_index();
		people.find(people.display_new);
	},
	find_new: function () {
		var list = [];
		gender=$("#genders option:selected").val();
		if(gender==""){
				gender ="Select a Gender";
		} else {
				if(gender =="M"){gender="Male"; } else if (gender=="F"){gender="Female";}
				list.push(gender);
		}

		ethnicity=$("#ethnicities option:selected").val();
		if(ethnicity==""){ethnicity ="Select an Ethnicity";} else {list.push(ethnicity);}

		religion=$("#religions option:selected").val();
		if(religion==""){religion ="Select Previous Religion";} else {list.push(religion);}

		age=$("#ages option:selected").val();
		if(age==""){age ="Select Age";} else {list.push(age);}

		text=$("#text").val();
		if(text==""){text ="No Text";} else {list.push(text);}

		s.prop14 = list.join(',');
		s.eVar9= gender
				+" | "+ethnicity
				+" | "+religion
				+" | "+age
				+" | "+text;

		s.prop10 = s.eVar9;
		s.pageName = "Mormon.org: Our People: Search Results";
		s.t();

		people.reset_index();
		people.find(people.display_new);
	},
	find_more: function () {
		people.find(people.display_more);
	},
	find_default: function() {
		people.find_new();
		return false;
	},
	display_new: function (data) {
		people.reset();
		people.save(data);
		if (people.no_results()) {
			$("#picture-finder,#list-finder .people-list").html('<div class="no-results">No results</div>');
			people.toggle_more(false);
		} else {
			people.display_picts();
			people.display_list();
		}
	},
	display_more: function (data) {
		people.save(data);
		people.display_list();
	},
	reset_index: function () {
		people.start_index = 1;
	},
	inc_index: function () {
		people.start_index += people.count;
	},
	toggle_more: function (tgl) {
		$("#more").toggle(tgl);
	},
	save: function (data) {
		var i = 1;
		$(data).children("people").eq(0).find("person").each(function () {
			people.saints.push({
				"img_med": $(this).attr("img_med"),
				"img_thumb": $(this).attr("img_thumb"),
				"name": $(this).attr("name"),
				"intro": $(this).attr("intro"),
				"profileid": $(this).attr("profile-id"),
				"aboutme": $(this).children("about-me").eq(0).text(),
				"index": i
			});
			++i;
		});
	},
	reset: function () {
		people.saints = [];
		$("#picture-finder").html('<div class="spotlight"></div>');
		$("#list-finder .people-list").html('');
	},
	no_results: function () {
		return people.saints.length == 0;
	},
	display_picts: function () {
		$(people.saints).each(function () {
			$("#picture-finder").append(
				"<img class='size" + $(this).attr('index') + " secondary' src='" + $(this).attr('img_med') + "' />"
			);
		});
		$("#picture-finder img").each(function (index, val) {
			$(this).data("profileid", people.saints[index]['profileid']);
		});
		var size1 = $(".size1");
		size1.removeClass("secondary");
		people.set_spotlight(people.find_saint("profileid", size1.data("profileid")));
		$("#picture-finder img").each(function () {
			$(this).fadeIn();
		});
	},
	display_list: function () {
		var any_new = false;
		$(people.saints).each(function (index, val) {
			if ((index + 1) >= people.start_index) {
				$("#list-finder .people-list").append(
					"<dl><dt><img src='" + $(this).attr('img_thumb') + "'/></dt><dd><p>" + $(this).attr('name') + "</p><p>" +
					$(this).attr("aboutme") + "</p><a href='/me/" + $(this).attr("profileid") + "/' class='link big'>Read " +
					$(this).attr('name') + "'s Profile</a></dd></dl><div class='clear'>&nbsp;</div><hr/>"
				);
				any_new = true;
			}
		});
		if (any_new) {
			people.inc_index();
			people.toggle_more(true);
		} else {
			people.toggle_more(false);
		}
	},
	set_spotlight: function (p) {
		if (p != undefined) {
			$(".spotlight").html(
				"<span>Hi, I'm " + p.attr('name') + "</span>" + p.attr('intro') +
				"<a class='link big' href='/me/" + p.attr('profileid') + "/'>Read " + p.attr('name') + "'s Profile</a>"
			).fadeIn();
			$(".size1").live("click", function () {
				window.location = "/me/" + p.attr('profileid') + "/";
			});
		} else {
			$(".spotlight").hide();
		}
	},
	find_saint: function (attr, val) {
		var saint = null;
		$(people.saints).each(function () {
			if ($(this).attr(attr) == val) {
				saint = $(this);
				return false;
			}
		});
		return saint;
	}
};


var picts = {
	trading: '',
	wait_shrink: false,
	style_size: new Object,
	set_as_spotlight: function() {
		var act = picts.getAct(this);
		if (picts.trading != '' || (act != 'big' && act != '')) return;

		picts.trading = true;
		picts.setAct(this, 'trade');

		var oldClass = $(this).attr('class');
		var goingToFrontLeft = $(this).css('left');
		var goingToFrontTop = $(this).css('top');
		var goingToFrontWidth = $(this).css('width');
		var goingToFrontHeight = $(this).css('height');
		var text = $(this).attr('rel');

		$('.spotlight').hide();

		$('.size1').animate({
			height: goingToFrontHeight,
			width: goingToFrontWidth,
			left: goingToFrontLeft,
			top: goingToFrontTop
		}, 500, function(){
			$(this).attr('class', '').addClass(oldClass);
			if ($.browser.msie && $.browser.version < 9) $(this).css('z-index', '990');
			picts.setAct(this, act);
			picts.deflateThumb(this);
		});

		$(this).addClass('top').animate({
			height: '268px',
			width: '304px',
			left: '312px',
			top: '88px'
		}, 500, function(){
			$(this).attr('class', '').addClass('size1');
			if ($.browser.msie && $.browser.version < 9) $(this).css('z-index', '1002');
			picts.trading = false; // move to size1 animate call back if size1 animate is set to animate longer than this.
			people.set_spotlight(people.find_saint("profileid", $(".size1").data("profileid")));
			$('.spotlight').fadeIn('slow').html(text);
		});
	},
	toplessClass: function (clazz) {
		return(clazz.replace(' top', ''));
	},
	imageSizes: function (thumb){
		var clazz = picts.toplessClass($(thumb).attr('class'));
		if (typeof(picts.style_size[clazz]) == 'undefined') {
			picts.style_size[clazz] = {
				origHeight: parseInt($(thumb).css('height')),
				origWidth: parseInt($(thumb).css('width')),
				origTop: parseInt($(thumb).css('top')),
				origLeft: parseInt($(thumb).css('left'))
			};
		}
		var img = { // grow and shrink width, height, top and left
			gh: picts.style_size[clazz].origHeight + 10,
			gw: picts.style_size[clazz].origWidth + 10,
			gt: picts.style_size[clazz].origTop - 5,
			gl: picts.style_size[clazz].origLeft - 5,
			sh: picts.style_size[clazz].origHeight,
			sw: picts.style_size[clazz].origWidth,
			st: picts.style_size[clazz].origTop,
			sl: picts.style_size[clazz].origLeft
		};
		return img;
	},
	getAct: function (thumb) {
		var clazz = picts.toplessClass($(thumb).attr('class'));
		if (typeof(picts.style_size[clazz]) == 'undefined') picts.imageSizes(thumb);
		if (typeof(picts.style_size[clazz].act) == 'undefined') picts.style_size[$(thumb).attr('class')].act = '';
		return(picts.style_size[clazz].act);
	},
	setAct: function (thumb, value) {
		var clazz = picts.toplessClass($(thumb).attr('class'));
		if (typeof(picts.style_size[clazz]) == 'undefined') picts.imageSizes(thumb);
		picts.style_size[clazz].act = value;
		return;
	},
	inflateThumb: function (thumb) {
		if (picts.getAct(thumb) != '') return;
		picts.setAct(thumb, 'grow');
		var img = picts.imageSizes(thumb);
		$(thumb).animate({ width: img.gw, height: img.gh, left: img.gl, top: img.gt }, 150, function(){
			if (picts.wait_shrink){
				$(thumb).animate({ width: img.sw, height: img.sh, left: img.sl, top: img.st }, 150, function(){
					picts.setAct(thumb, '');
					picts.wait_shrink = false;
				});
			} else {
				picts.setAct(thumb, 'big');
			}
		});
	},
	deflateThumb: function (thumb) {
		if (picts.getAct(thumb) == 'grow') picts.wait_shrink = true;
		if (picts.getAct(thumb) != 'big' || picts.getAct(thumb) == 'trade') return;
		var img = picts.imageSizes(thumb);

		picts.wait_shrink = false;
		picts.setAct(thumb, 'shrink');
		$(thumb).animate({ width: img.sw, height: img.sh, left: img.sl, top: img.st }, 150, function(){
			picts.setAct(thumb, '');
		});
	}
};

// TODO: Simplify
var results = {
	select_body: 'picture',
	show_pict_results: function() {
		if (results.select_body == 'picture') return;
		results.select_body = 'picture';

		// fade out list-finder and fade in picture-finder
		$('#list-finder').animate({ 'opacity': 0 }, 250, function(){
			$('#list-finder').hide();
			$('#picture-finder').show();
			$('#picture-finder').animate({ 'opacity': 1 }, 250);
		});

		// swap selector image and classes
		$('#select-picture img').attr('src', '/bc/assets/images/people/ico-picture-on.gif');
		$('#select-picture').addClass('select-on');
		$('#select-picture').removeClass('select-off');

		$('#select-list img').attr('src', '/bc/assets/images/people/ico-list-off-orange.gif');
		$('#select-list').addClass('select-off');
		$('#select-list').removeClass('select-on');

		$('#select-arrow img').css('left', 44);
	},
	show_list_results: function(){
		if (results.select_body == 'list') return;
		results.select_body = 'list';

		// fade out picture-finder and fade in list-finder
		$('#picture-finder').animate({ 'opacity': 0 }, 250, function(){
			$('#picture-finder').hide();
			$('#list-finder').show();
			$('#list-finder').animate({ 'opacity': 1 }, 250);
		});

		// swap selector image and classes
		$('#select-list img').attr('src', '/bc/assets/images/people/ico-list-on.gif');
		$('#select-list').addClass('select-on');
		$('#select-list').removeClass('select-off');

		$('#select-picture img').attr('src', '/bc/assets/images/people/ico-picture-off-orange.gif');
		$('#select-picture').addClass('select-off');
		$('#select-picture').removeClass('select-on');

		$('#select-arrow img').css('left', 120);
	}
};

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}


$(document).ready(function() {
	$('#our-people').addClass('selected');
	people.find_orig();

	$("#search").live("click", people.find_new);
	$("#more").live("click", people.find_more);
	$("#peopleSearch").live("submit", people.find_default);

	$('#select-picture').live("click", results.show_pict_results);
	$('#select-list').live("click", results.show_list_results);

	$('#picture-finder img.secondary').live('mouseover',function(){ picts.inflateThumb(this); });
	$('#picture-finder img.secondary').live('mouseout',function(){ picts.deflateThumb(this); });

	$('.secondary').live('click', picts.set_as_spotlight);

	if ($.browser.msie && $.browser.version < 9){
		$('.secondary').live('mouseover', function (){ $(this).css('z-index', 1000) });
		$('.secondary').live('mouseout', function (){ $(this).css('z-index', 990) });
	}
});

