Event.addBehavior({
	//'.profile a.edit' : function(e){this.remove();},
	'div.profile a.edit' : function(e){this.setStyle({display:"none"})},
	'div.myprofile' : function(e){this.addClassName("myprofile_ajax");},
	'div.myprofile_ajax .bday' : function(e){this.observe('click', function(event){profileEdit(this,Event.pointerX(event),Event.pointerY(event));});this.observe('mouseover', function(event){this.setStyle({background:"#FF9",cursor:"pointer"});});this.observe('mouseout', function(event){this.setStyle({background:"none",cursor:"auto"});});},
	'div.myprofile_ajax .location' : function(e){this.observe('click', function(event){profileEdit(this,Event.pointerX(event),Event.pointerY(event));});this.observe('mouseover', function(event){this.setStyle({background:"#FF9",cursor:"pointer"});});this.observe('mouseout', function(event){this.setStyle({background:"none",cursor:"auto"});});},
	'div.myprofile_ajax .summary' : function(e)
	{
		this.observe('click', function(event){profileEdit(this,Event.pointerX(event),Event.pointerY(event));});
		this.observe('mouseover', function(event){this.childElements().each(function(child){child.setStyle({background:"#FF9",cursor:"pointer"});});});
		this.observe('mouseout', function(event){this.childElements().each(function(child){child.setStyle({background:"none",cursor:"auto"});});});
	},
	'div.myprofile_ajax .listing .thumbnail a.setpicture' : function(e){this.observe('click',function(event){Event.stop(event);setPicture(this,Position.cumulativeOffset(this)[0],Position.cumulativeOffset(this)[1]);},false);},
	'#profile-edit' : function(e){this.observe('submit',validateEdit,false);},
	'#combox form.sendFlirt' : function(e)
	{
		this.observe('submit',sendFlirt,false);
		
	}
});

function sendFlirt(e)
{
	Event.stop(e);
	
	flirtForm=e.element();
	url="/ajax/response_mail.xml";//?content_type=xml&request_type=ajax
	pars=flirtForm.serialize();//alert(pars);
	new Ajax.Request(url,
	{
		method:'post',
		postBody:pars,
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport)
		{
			var newFlirtAlerts=transport.responseXML.getElementsByTagName("alert");
			for(i=0;i<newFlirtAlerts.length;i++){highAlertQueue.push(newFlirtAlerts[i].cloneNode(true));}
			$$("body")[0].focus();
			flirtForm.disable();
			
		}
	});
}





function profileEdit(element,x,y)
{
	var firstElementClass = element.className;
	if(firstElementClass.indexOf(" ")>0){firstElementClass = element.className.substring(0,element.className.indexOf(" "));}
	var popupSpecs = {'name':'profile-edit','css':'box-profile-edit','action':'/ajax/response_editprofile.xml','top':y,'left':x-120,'backdrop':'no','closeButton':'yes'};
	var fetchList = new Array();
	var group="";
	switch(firstElementClass)
	{
		case "bday": fetchList=["dob_row","action_row","button_row"];group="age";break;
		case "location": fetchList=["country_row","postal_row","region_row","locality_row","action_row","button_row"];group="location";break;
		case "summary": fetchList=["genderpref_row","marital_row","intent_row","action_row","button_row"];group="summary";break;
		case "marital-status": fetchList=["marital_row","action_row","button_row"];group="marital";break;
		case "activity-grp": fetchList=["intent_row","action_row","button_row"];group="intent";break;
		case "look-grp": fetchList=["datepref_row","action_row","button_row"];group="gender";break;
	}
  var content = document.createElement('div');
	url="/action/editprofile.html?content_type=xml&ajax=yes&group="+group;
	new Ajax.Request(url,
	{
		method:'get',
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport)
		{
			FormRows = transport.responseXML.getElementsByTagName("form")[1].getElementsByTagName("div");//Will have to change so it actually gets the right node and not just the first form
			for(i=0;i<fetchList.length;i++)
			{
				for(j=0;j<FormRows.length;j++)
				{
					if(FormRows[j].getAttribute("id"))
					{
						if(fetchList[i]==FormRows[j].getAttribute("id"))
						{
							for(k=0;k<FormRows[j].childNodes.length;k++)
							{
								if(FormRows[j].childNodes[k].nodeName=="select"){FormRows[j].childNodes[k].setAttribute('onChange',"validate(this,'live')");}
								if(FormRows[j].childNodes[k].nodeName=="input"){FormRows[j].childNodes[k].setAttribute('onChange',"validate(this,'live')");}
								//DEBUG//alert("row found");
							}
	  		 			try{content.appendChild(FormRows[j]);}
						  catch(e){content.innerHTML+=serializeNode(FormRows[j]);}//if(Prototype.Browser.IE){content.innerHTML+=serializeNode(FormRows[j]);}else{content.appendChild(FormRows[j]);}
						}
					}
				}
			}
			//DEBUG//alert(i+":"+j+":"+k);
			createPopup(content,popupSpecs);
			/*HARDCODED REMOVE FOR POSTAL ROWS WHEN THE COUNTRY IS NOT CANADA OR UNITED STATES*/
			if($$("#country_row select")[0])
			{
				var defaultCountry = $$("#country_row select")[0].getValue();
				if(defaultCountry!="CA" && defaultCountry!="US"){$("postal_row").update('<div class="field_row" id="postal_row"><input type="hidden" name="postal" id="postal" class="postal" value=""/><label for="postal">Postal/ZIP Code</label><div class="message" id="msg-postal">.</div></div>');}
			}
		}
	});
}

function validateEdit(e){Event.stop(e);validate('all','submit');if(noErrors()){sendProfileEditForm()}}

function sendProfileEditForm(editForm)
{
	url="/ajax/response_editprofile.xml";//"/ajax/response_editprofile.xml";//editForm.attributes.action.nodeValue;
	pars=$("profile-edit").serialize();
	new Ajax.Request(url,
	{
		method:'post',
		postBody:pars,
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport){$("popupLayer").remove();refreshProfile();}
	});
	return false;
}


function setPicture(element,x,y)
{

	url=element.readAttribute('href')+"?content_type=xml";
	//alert(url);
	//alert(x+","+y);
	var popupSpecs = {'name':'set-picture','css':'box-set-picture','action':url,'top':y-2,'left':x-12,'backdrop':'no','closeButton':'yes'};
	var content = document.createElement('div');
	new Ajax.Request(url,
	{
		method:'get',
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport)
		{
			instructions=transport.responseXML.getElementsByTagName("form")[1].getElementsByTagName("p")[0];
			var uplink="";
			try{uplink=instructions.getElementsByTagName("a")[0].attributes["href"].value;}
			catch(e){uplink=instructions.getElementsByTagName("a")[0].attributes[0].value}/*for IE*/			
			content.innerHTML+=serializeNode(instructions);
			content.innerHTML+='<input type="hidden" name="action" value="set_default_media"/>';
			FormPictureItems = transport.responseXML.getElementsByTagName("form")[1].getElementsByTagName("li");//Will have to change so it actually gets the right node and not just the first form
			if(FormPictureItems)
			{
				var pictureList = document.createElement('ol');
				for(i=0;i<FormPictureItems.length;i++)
				{
					var radioButton=FormPictureItems[i].childNodes[0];
					if(i<FormPictureItems.length)
					{
						if(Prototype.Browser.IE){FormPictureItems[i].childNodes[0].setAttribute('onclick','sendSetPictureForm($("set-picture"))');}
						else{FormPictureItems[i].childNodes[0].setAttribute('onchange','sendSetPictureForm($("set-picture"))');}
						pictureList.innerHTML+=serializeNode(FormPictureItems[i]);
					}
					else{pictureList.innerHTML+="<li>empty</li>";}
				}
				try{content.appendChild(pictureList);}
				catch(e){content.innerHTML+=serializeNode(pictureList);}//if(Prototype.Browser.IE){content.innerHTML+=serializeNode(pictureList);}else{content.appendChild(pictureList);}
				createPopup(content,popupSpecs);
			}
		}
	});
}

function changeStatus(element)
{
	element=$(element);
	if(element.hasClassName("approved")){$("picture-status").update("");$("picture-status").setStyle({'color':"#00CC00"});}
	if(element.hasClassName("pending")){$("picture-status").update("*Pending approval");$("picture-status").setStyle({'color':"#CC0000"});}
	if(element.hasClassName("rejected")){$("picture-status").update("*Not for your profile photo. Please select another.");$("picture-status").setStyle({'color':"#CC0000"});}
	if(element.hasClassName("unsetpic")){$("picture-status").update("");$("picture-status").setStyle({'color':"#999999"});}
}
function clearStatus()
{
	$("picture-status").update("");
	$("picture-status").setStyle({'color':'black'});
}

function sendSetPictureForm(editForm)
{
	url=editForm.readAttribute("action");
	if(Prototype.Browser.IE){pars=$("set-picture").serialize();}
	else{pars=editForm.serialize();}
	new Ajax.Request(url,
	{
		method:'post',
		postBody:pars,
		onCreate:function(){ajaxCursorStart();},
		onComplete:function(){ajaxCursorEnd();},
		onSuccess:function(transport){refreshProfile();}
	});
	return false;
}



function refreshProfile()
{
	var username=$("loggedUser").innerHTML;
	var profileStyle = $(username).classNames()+"";
	profileStyle=profileStyle.replace(/gold/,"")
	profileStyle=profileStyle.replace(/premium/,"")
	profileStyle=profileStyle.replace(/basic/,"")
	profileStyle=profileStyle.replace(/myprofile_ajax/,"")
	profileStyle=profileStyle.replace(/myprofile/,"")
	profileStyle=profileStyle.replace(/profile/,"")
	profileStyle.strip();
	profileStyle=profileStyle.replace(/\s{1,}/,"");
	profileStyle=profileStyle.replace(/\s{1,}/,"");
	switch(profileStyle){
		case "pmenu":profileStyle="menu";break;
		case "phome":profileStyle="home";break;
		case "pfloat":profileStyle="float";break;
		case "pthumb":profileStyle="thumb";break;
		default:profileStyle="normal";
	}
	url="/ajax/response_profile.xml";
	pars="display_mode="+profileStyle+"&username="+username;
	new Ajax.Request(url,
	{
		method:'post',
		postBody:pars,
		onSuccess:function(transport)
		{
			var newHTML = serializeNode(transport.responseXML.documentElement.firstChild);
			$(username).replace(newHTML);
			$(username).addClassName("myprofile");
		}
	});
}
