﻿//Create Profile Namespace
var $Profile = TGN.namespace("TGN.Ancestry.Community.Profile");







var __infoDlg = null;
var __aboutDlg = null;
var __photoDlg = null;
var __willhelpDlg = null;

// Global Variables for Carousel
var carouselSlots = 5;
// these names used in other js files so don't change
var photosCarousel;	
var photoTreesCarousel;

// set the carousel size depending on the size of the browser window
$Profile.resizeCarousel = function()
{
	var clientWidth = YAHOO.util.Dom.getClientWidth();
	var changingSize = false;
	if (clientWidth >= 920 )
	{
		if (carouselSlots != 5)
		{
			changingSize = true;
			carouselSlots = 5;
		}
	}
	else
	{
		if (carouselSlots != 4)
		{
			changingSize = true;
			carouselSlots = 4;
		}
	}
	if (photosCarousel != null && changingSize)
	{
		photosCarousel.setProperty("numVisible", carouselSlots, true);
		photosCarousel.setProperty("scrollInc", carouselSlots, true);
		photosCarousel.hide();
		photosCarousel.show();
		if (photosCarousel.getFirstVisible() == 1)
		{
			showButtons(photosCarousel);
		}
	}
}

YAHOO.util.Event.addListener(window, 'load', $Profile.resizeCarousel);
YAHOO.util.Event.addListener(window, 'resize', $Profile.resizeCarousel);
// Customer Content - photo carousel
$Profile.InitCustContent = function(username, ancestryDomain, treesDomain, cache)
{
	// this TreesCarousel has extended data for the Trees version of the Carousel
	photoTreesCarousel = new TreesCarousel(
		{
			"ajaxHandler":		"ajax.ashx",
			"ajaxParams":		"&f=ListPublicObjects",
			"treeId":			"",
			"personId":			"",
			"userId":			v_uid,
			"userName":			username,
			"ancestryDomain":	ancestryDomain,
			"treesDomain":		treesDomain,
			"currPgArgs":		"",
			"rightArrow":			cache + "/trees/i/arrow_r.gif",
			"rightArrowDisabled":	cache + "/trees/i/arrow_r_disabled.gif",
			"leftArrow":			cache + "/trees/i/arrow_l.gif",
			"leftArrowDisabled":	cache + "/trees/i/arrow_l_disabled.gif",
			"totalCount":		v_photoCount,
			"useIndex":			false
		});
	photosCarousel = new YAHOO.extension.Carousel("photo-carousel", 
		{
			"numVisible":       carouselSlots,
			"scrollInc":        carouselSlots,
			"animationSpeed":   0.75,
			"animationMethod":  YAHOO.util.Easing.easeBoth,
			"navMargin":        28,
			"prevElement":		"prev-arrow-photo",
			"nextElement":		"next-arrow-photo",
			"loadInitHandler":	loadInitialPhotosAjax,
			"loadNextHandler":	loadNextPhotosAjax,
			"loadPrevHandler":	loadPrevPhotosAjax,
			"prevButtonStateHandler":	handlePrevPhotosButtonState,
			"nextButtonStateHandler":	handleNextPhotosButtonState
		});
}

$Profile.ProfileDialog = function() { 
	this.modalId = "";
	this.saveId = "";
	this.cancelId = "";
};

$Profile.ProfileDialog.prototype = {
	init: function() {
		this.dialog = new YAHOO.widget.Panel(this.modalId, 
				{
					close: true,
					underlay: "matte",
					modal: true,
					zIndex: 1001,
					visible: false,
					draggable: false,
					hideonsubmit: false,
					constraintoviewport:true,
					effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration:.25}
				});
		this.callback = {success: this.success,
						 failure: this.failure};
		this.dialog.validate = this.validate;

		this.dialog.render();
		this.initDialog();

        YAHOO.util.Event.addListener(this.formId, "keypress", this.keyCheck, this, true);
		YAHOO.util.Dom.setStyle(this.modalId, 'display', 'block');

		this.show();
	},
	initDialog: function() {
		// this function should be defined by the subclass
	},
	keyCheck: function(e) {
		var target = YAHOO.util.Event.getTarget(e);
		if (target.type != "textarea") {
			var charCode = e.charCode ? e.charCode : e.keyCode;
			if (charCode == 13) {
				this.save();
			}
		}
	},
	validate: function() {
		// this function should be defined by the subclass
		return true;
	},
	show: function() {
		var dlgXpos = (YAHOO.util.Dom.getViewportWidth() - this.width) / 2;
		var dlgYpos = (YAHOO.util.Dom.getViewportHeight() - this.height) / 3;
		this.dialog.cfg.setProperty("x", dlgXpos);
		this.dialog.cfg.setProperty("y", dlgYpos);
		this.dialog.show();
	},
	save: function() {
		if (this.validate()) {
			this.doSave();
			this.dialog.hide();
		}
	},
	doSave: function() {
		var formObject = document.getElementById(this.formId);
		YAHOO.util.Connect.setForm(formObject);
		var request = YAHOO.util.Connect.asyncRequest('POST', "ajax.ashx", this.callback);
	},
	cancel: function() {
		this.dialog.hide();
	},
	success: function() {
		window.location.reload();	// default: reload the page
	},
	failure: function() {
		window.location.reload();	// default: reload the page
	},
	setErrorMessage: function(id, value) {
		var el = document.getElementById(id);
		if (value != null) {
			el.innerHTML = value;
		}
	}
}

//
// UserInfoDialog
// set username, location, and About You text
$Profile.UserInfoDialog = function() { 
	this.modalId = "info_modal";
	this.formId = "info_form";
	this.width = 486;
	this.height = 415;
	
	this.initDialog = function() {
		this.username = document.getElementById("info_username").value;
		this.pAuto = document.getElementById("pAuto").value;
		this.aboutMe = document.getElementById("aboutme").value;
		YAHOO.util.Event.addListener("info_save", "click", this.save, this, true);
		YAHOO.util.Event.addListener("info_cancel", "click", this.cancel, this, true);
		YAHOO.util.Event.addListener("info_username", "blur", this.validateUsername, this, true);
		YAHOO.util.Event.addListener("info_username", "keypress", function(){document.getElementById("info_radio1").checked = true;});
	}
	this.validate = function() {
		var formObj = document.getElementById(this.formId);
		if (formObj.dnf[0].checked) {	// if username radio button checked
			var uname = formObj.info_username.value;
			if (uname != null && uname.length > 0 && uname != this.username)
			{
				if (!this.validateUsername()) 
					return false;

				var request = new TGN.Util.AjaxRequest("GET", "/ajax.ashx", "f=SetUsername&username="+uname, { success:this.usernameSuccess, failure:this.usernameFailure, scope:this });
				request.invoke();
				return false;	// validation will be completed by the callbacks
			}
		}
		return true;
	}
	this.validateUsername = function()
	{
		var formObj = document.getElementById(this.formId);
		if (formObj.dnf[0].checked) {	// if username radio button checked
			var uname = formObj.info_username.value;
			var v = uname.match(new RegExp("^[a-zA-Z0-9\xC0-\xFF_:]*$"));
			if (v == null) {
				this.setErrorMessage("usernameValidator", v_errInvalidChars);
				return false;
			}
		}
		this.setErrorMessage("usernameValidator", "");
		return true;
	}
	this.usernameSuccess = function(o) {
		if (o.responseText == "success")
		{
			this.scope.doSave();
			this.scope.username = document.getElementById("info_username").value;
			this.scope.dialog.hide();
		} else {
			var input = document.getElementById("info_username")
			this.scope.setErrorMessage("usernameValidator", v_errNameUnavailable.replace("{0}", input.value));
			input.value = this.scope.username;
			input.select();
		}
	}
	this.usernameFailure = function(o) {
		var input = document.getElementById("info_username")
		this.scope.setErrorMessage("usernameValidator", v_errUsernameFailed);
		input.value = this.scope.username;
		input.select();
	}
	this.init();
};

$Profile.UserInfoDialog.prototype = new $Profile.ProfileDialog();

//
// UserAboutDialog
// set profile attributes like Gender, Age, Education, Experience, Favorite Websites, etc.
$Profile.UserAboutDialog = function() { 
	this.modalId = "about_modal";
	this.formId = "about_form";
	this.width = 460;
	this.height = 550;
	
	this.initDialog = function() {
		YAHOO.util.Event.addListener("about_save", "click", this.save, this, true);	
		YAHOO.util.Event.addListener("about_cancel", "click", this.cancel, this, true);	
		
		// initialize panel tabs
		var tabs = document.getElementById("m_tabs").getElementsByTagName('li');
		tabs[0].className = "activeTab";
		this.activeTab = tabs[0].id;
		YAHOO.util.Event.addListener(tabs, "click", this.tabClick, this, true);	
		
		// initialize language and lineage dropdowns
		this.loadList({menuId:"lan",divId:"lanDisp",hiddenId:"lanList"});
		this.loadList({menuId:"lin",divId:"linDisp",hiddenId:"linList"});
		YAHOO.util.Event.addListener(document.getElementById("lan"), "change", this.enableAdd, this, true);
		YAHOO.util.Event.addListener(document.getElementById("lin"), "change", this.enableAdd, this, true);
		
		// initialize website fields
		var homeTitle = document.getElementById("hpname");
		YAHOO.util.Event.addListener(homeTitle, "focus", this.clearField, {dflt:v_enterSiteName}, true);
		YAHOO.util.Event.addListener(homeTitle, "blur", this.defaultField, {dflt:v_enterSiteName}, true);
		if (homeTitle.value == v_enterSiteName)
			homeTitle.style.color = "#999999";

		var homeUrl = document.getElementById("hpurl");
		YAHOO.util.Event.addListener(homeUrl, "focus", this.clearField, {dflt:v_enterSiteUrl}, true);
		YAHOO.util.Event.addListener(homeUrl, "blur", this.defaultField, {dflt:v_enterSiteUrl}, true);
		if (homeUrl.value == v_enterSiteUrl)
			homeUrl.style.color = "#999999";
	}
	
	// validate the dialog
	this.validate = function() {
		return true;
	}
	
	// load language and lineage display lists 
	this.loadList = function(s) {
		document.getElementById(s.divId).innerHTML = "";
		var menu = document.getElementById(s.menuId);
		var hidden = document.getElementById(s.hiddenId);
		var ids = hidden.value.split(',');
		for (i = 0; i < ids.length; i++) {
			if (ids[i].length > 0) {
				s.item = menu.options[ids[i]].text;
				s.itemId = ids[i];
				this.addToList2(s);
			}
		}
	}
	
	// handle clicks in the tabs
	this.tabClick = function(e) {
		document.getElementById(this.activeTab).className = "";
		document.getElementById("about_"+this.activeTab).style.display = "none";
		
		var target = YAHOO.util.Event.getTarget(e);
		this.activeTab = target.parentNode.id;
		document.getElementById(this.activeTab).className = "activeTab";
		document.getElementById("about_"+this.activeTab).style.display = "block"
	}
	
	// enable or disable the add buttons for languages and lineages
	this.enableAdd = function(e) {
		var sel = e.type == "change" ? YAHOO.util.Event.getTarget(e) : e;
		var button = document.getElementById(sel.id+"Add");
		button.className = sel.selectedIndex == 0 ? "smallbtn dim" : "smallbtn act";
		if (sel.selectedIndex == 0) {
			button.className = "smallbtn dim";
			button.removeAttribute("href");
			YAHOO.util.Event.removeListener(button, "click", this.addClick);
		} else {
			button.className = "smallbtn act";
			button.setAttribute("href", "javascript:;");
			YAHOO.util.Event.addListener(button, "click", this.addClick, this, true);
		}
	}
	
	// 
	this.addClick = function(e) {
		var id = YAHOO.util.Event.getTarget(e).id.substring(0,3);
		this.addToList({menuId:id,divId:id+"Disp",hiddenId:id+"List"});
	}
	
	this.addToList = function(s) {
		var menu = document.getElementById(s.menuId);
		s.item = menu.options[menu.options.selectedIndex].text;
		s.itemId = menu.options[menu.options.selectedIndex].value;
		var itemCheck = document.getElementById(s.divId).innerHTML.indexOf(s.item);
		var hidden = document.getElementById(s.hiddenId);
		if (menu.selectedIndex > 0 && itemCheck == -1) 
		{
			if (hidden.value != "")
				hidden.value += ',';
			hidden.value += s.itemId;
			
			this.addToList2(s);
			menu.selectedIndex = 0;
			this.enableAdd(menu);
		}
	}

	this.addToList2 = function(s) {
		// <span>English<a id="lan1" href="#" class="deleteItem"></a></span>
		s.linkId = s.menuId + s.itemId;
		
		var span = document.createElement("span");
		span.innerHTML = s.item;
		
		var link = document.createElement("a");
		link.href = "javascript:;";
		link.id = s.linkId;
		
		var img = document.createElement("img");
		img.src = v_cache + "/i/community/trashcan.gif";
		img.className = "deleteItem";
		
		link.appendChild(img);
		span.appendChild(link);
		
		document.getElementById(s.divId).appendChild(span);
		YAHOO.util.Event.addListener(link, "click", this.removeFromList, {item:s.item,itemId:s.itemId,divId:s.divId,hiddenId:s.hiddenId,linkId:s.linkId}, true);
	}

	this.removeFromList = function() {
		hidden = document.getElementById(this.hiddenId);
		hval = hidden.value += ',';
		rex = new RegExp(this.itemId + ',');
		hval = hval.replace(rex, '');
		hidden.value = hval.substring(0, hval.length-1);

		var link = document.getElementById(this.linkId);
		YAHOO.util.Event.removeListener(link, "click");
		var span = link.parentNode;
		span.parentNode.removeChild(span);
	}
	// 
	this.clearField = function(e) {
		var target = YAHOO.util.Event.getTarget(e);
		var input = document.getElementById(target.id);
		if (input.value == this.dflt)
		{
			input.value = "";
			input.style.color = "";
		}
	}
	
	// 
	this.defaultField = function(e) {
		var target = YAHOO.util.Event.getTarget(e);
		var input = document.getElementById(target.id);
		if (input.value.length == 0)
		{
			input.value = this.dflt;
			input.style.color = "#999999";
		}
	}
	this.init();
};

$Profile.UserAboutDialog.prototype = new $Profile.ProfileDialog();

//
// UserPhotoDialog
// set profile photo and thumbnail
$Profile.UserPhotoDialog = function() { 
	this.modalId = "photo_modal";
	this.formId = "photo_form";
	this.width = 484;
	this.height = 255;
	
	this.initDialog = function() {
		this.callback.upload = this.upload;
		YAHOO.util.Event.addListener("photo_save", "click", this.save, this, true);	
		YAHOO.util.Event.addListener("photo_cancel", "click", this.cancel, this, true);	
		YAHOO.util.Event.addListener("photo_remove", "click", this.remove, this, true);	
		document.getElementById("delPhoto").value = "";
	}		
	this.doSave = function() {
		var formObject = document.getElementById(this.formId);
		YAHOO.util.Connect.setForm(formObject, true);
		var request = YAHOO.util.Connect.asyncRequest('POST', "ajax.ashx", this.callback);
	},
	this.validate = function() {
		return true;
	}
	this.upload = function() {
		document.getElementById("photoFile").value = "";
		window.location.reload();
	},
	this.remove = function() {
		document.getElementById("removeDiv").style.display = "none";
		document.getElementById("delPhoto").value = "1";
		var photo = document.getElementById("userPhoto");
		if (photo != null)
			photo.style.display = "none";
		var photo = document.getElementById("blankPhoto");
		if (photo != null)
			photo.style.display = "block";
	},
	this.cancel = function() {
		this.dialog.hide();
		document.getElementById("photoFile").value = "";
		var photo = document.getElementById("userPhoto");
		if (photo != null) {
			photo.style.display = "block";
			
			photo = document.getElementById("blankPhoto");
			if (photo != null)
				photo.style.display = "none";
			document.getElementById("removeDiv").style.display = "block";
			document.getElementById("delPhoto").value = "";
		}
	}
	this.init();
};

$Profile.UserPhotoDialog.prototype = new $Profile.ProfileDialog();

//
// WillHelpDialog
// set willing to help community
$Profile.WillHelpDialog = function() { 
	this.modalId = "willhelp_modal";
	this.formId = "willhelp_form";
	this.width = 540;
	this.height = 330;
	
	this.initDialog = function() {
		YAHOO.util.Event.addListener("willhelp_save", "click", this.save, this, true);	
		YAHOO.util.Event.addListener("willhelp_cancel", "click", this.cancel, this, true);	
	}		
	this.validate = function() {
		return true;
	}
	this.init();
};

$Profile.WillHelpDialog.prototype = new $Profile.ProfileDialog();

function EditUserInfo() {
	if (__infoDlg == null) {
		__infoDlg = new $Profile.UserInfoDialog();
	}
	__infoDlg.show();
}

function EditUserAbout() {
	if (__aboutDlg == null) {
		__aboutDlg = new $Profile.UserAboutDialog();
	}
	__aboutDlg.show();
}

function EditUserPhoto() {
	if (__photoDlg == null) {
		__photoDlg = new $Profile.UserPhotoDialog();
	}
	__photoDlg.show();
}

function EditWillingToHelp() {
	if (__willhelpDlg == null) {
		__willhelpDlg = new $Profile.WillHelpDialog();
	}
	__willhelpDlg.show();
}

