
function init() {
	$('container').style.display = "block";
}

function viewContent(obj) {
	var fieldName = obj.id;
	if (fieldName == "account") {
		$('accountContent').style.display = "block";
		$('settingContent').style.display = "none";
	} else {
		$('accountContent').style.display = "none";
		$('settingContent').style.display = "block";
	}

}

function populateData() {
	var selectItem = $("profile.country");
	var chosenOption = selectItem.options[selectItem.selectedIndex];
	var country = chosenOption.value;

	var url = contextPath + '/extlink/changeCityAndState.htm?country='
			+ country;
	new Ajax.Request(url, {
		method :'POST',
		onComplete :updateAddress
	});
}

function updateAddress(getList) {
	var result = getList.responseText.evalJSON();
	var stateList = result.state
	var cityList = result.city;
	var state = $("state");
	var city = $("city");
}

function showWebSite(webUrlDiv) {
	var size = $$("#" + webUrlDiv + " INPUT");
	var x = 0;
	size.each( function(value) {
		x++;
	});
	if (x < 3) {
		var div = $('addUrl');
		var newInputBox = document.createElement('input');
		var newSpan = document.createElement('span');
		newInputBox.setAttribute('id', "webUrl" + x);
		newInputBox.setAttribute('name', "webUrl");
		div.appendChild(newInputBox);
		var newBr = document.createElement('br');
		div.appendChild(newBr);
		newSpan.setAttribute('id', "webUrl" + x + ".errors");
		newSpan.className = 'errorClass';
		newSpan.style.display = "none";
		div.appendChild(newSpan);
		$("webUrl" + x).onblur = function() {
			validateInputField(this, 'edit');
		};
		x++;
	}
}

function checkAboutMe() {
	viewLess();
}

function viewMore() {
	var des = $('hiddenAboutMe').value;
	var aboutMe = $('aboutMe');
	$(aboutMe).innerHTML = des
			+ "<br /><a href='javascript:viewLess();'>view less</a>";
}

function viewLess() {
	var aboutMe = $('hiddenAboutMe');
	var len = aboutMe.value.length;
	var val = aboutMe.value;
	if (len > 280) {
		$("aboutMe").innerHTML = val.substring(0, 280) + "...."
				+ "<br /><a href='javascript:viewMore();'>view more</a>";
	}

}

var id;

function calculatePosForDescription(element, source) {
	source = $(source);
	var position = Element.cumulativeOffset(source);
	var parent = source.parentNode;
	$(element).setStyle( {
		position :'absolute',
		left :(position.left + 84) + 'px',
		display :'block',
		top :((position.top - parent.scrollTop) + 20) + 'px'
	});
	Element.show(element);

}

function viewDescription() {
	var description = $('descriptionHiddenForList');

	var len = description.value.length;
	var val = description.value;
	if (len > 200) {
		$("descriptionForListOfList").innerHTML = val.substring(0, 200)
				+ "...." + "<a href='javascript:viewMore();'>view more</a>";
	} else
		$("descriptionForListOfList").innerHTML = description.value;

}

function showDescription(listName, descriptionDiv) {
	calculatePosForDescription(descriptionDiv, listName);
}

function removeDescription(descriptionDiv) {
	$(descriptionDiv).style.display = "none";

}

function showHelp() {
	$('showHelp').style.display = "block";
}
function closeDiv(divId) {
	$(divId).style.display = "none";
}

function seeAllFriend() {

}

function enableCheckBox() {
	$('review').disabled = false;
	$('commentary').disabled = false;
	$('comment').disabled = false;
}

function disableCheckBox() {
	$('review').disabled = true;
	$('commentary').disabled = true;
	$('comment').disabled = true;

}

function showImageUploadPopup() {
	var imagePopup = $('imagePopup');
	Popup.show(imagePopup);
	imagePopup.style.top = "150px";
	imagePopup.style.left = "478px";
}

function showTab(tab_id, header) {
	$$('.useredit_tab_active').each( function(menu_item) {
		menu_item.className = 'useredit_tab';
	});
	$(tab_id + '_li').className = 'useredit_tab_active';
	$$('.user_edit_tabs').each( function(tab) {
		tab.hide();
	});
	$('edituser_mainheader').update('My ' + $(tab_id + '_text').innerHTML);
	$(tab_id).show();

}

function shortList() {
	Sortable.create('id_of_container', [ options ]);
}

function serialize(container, name) {
	'Serialization of ' + $(container).id + ' is: <br/><pre>'
			+ Sortable.serialize(container, {
				name :name
			}) + '</pre>';
}

function openEmailEdit() {
	$('changeEmail').show();
	$('changeMailLink').hide();
}

function closeEmailEdit() {
	$('changeEmail').hide();
	$('changeMailLink').show();
	$('email.errors').update('');
	$('email').value = $('existingEmailId').value;
}

function openPasswordEdit() {
	$('changePassword').show();
	$('changePasswordLink').hide();
}

function closePasswordEdit() {
	$('changePassword').hide();
	$('changePasswordLink').show();
	$('oldPassword.errors').update('');
	$('newPassword.errors').update('');
}

function selectList(divId, firstRow, firstListId) {
	var currentRow = $$("#" + divId + " DIV");
	currentRow.each( function(value) {
		if (value.id == firstRow) {
			$(value.id).addClassName('row');
		}
		Event.observe(value, 'click', function() {
			var selectedRow = $$("#" + divId + " .row");
			$(selectedRow[0].id).removeClassName('row');
			$(value.id).addClassName('row');
		});
	});
}

function reorderUserLists(listsId) {
	if ($('editListRank')) {
		$('editListRank').replace(obje);
	}
	$$('#' + listsId + ' .userlistsRowDiv').each( function(row, index) {
		row.down('.userlistsNumberDiv').innerHTML = index + 1;
	});

}


function userListsRowMouseOver(row) {
	row.addClassName('userlistRowMouseOver');
}

function userListsRowMouseOut(row) {
	row.removeClassName('userlistRowMouseOver');
}

function makeUserListsSortable(containerId) {
	if ($(containerId) != null) {
		Sortable.create(containerId, {
			tag :'div',
			scroll :containerId,
			ghosting :true,
			hoverclass :'dragOver',
			onUpdate :reorderUserLists.curry(containerId)
		});
	}
}

function userListsSave() {
	userListsAjaxCall('userlistsBody', '/extlink/editlistoflist.htm',
			'user_lists_throbber');
}

function userListsAjaxCall(containerId, url, throbberId) {
	var rows = $$('#' + containerId + ' .userlistsRowDiv');
	var listIds = new Array();
	for ( var index = 0, len = rows.length; index < len; ++index) {
		var item = rows[index];
		var listId = item.down('.userlistsListId').innerHTML
		var listPos = item.down('.userlistsNumberDiv').innerHTML - 1;
		listIds[listPos] = listId;
	}
	var url = contextPath + url;
	$(throbberId).show();
	new Ajax.Request(url, {
		method :"post",
		parameters : {
			listIds :listIds
		},
		onComplete : function() {
			$(throbberId).hide();
			showSaveSuccessfullMessage();
		}
	});
}

function showSaveSuccessfullMessage() {
	$('saveSuccessfullMessage').show();
	Effect.Fade('saveSuccessfullMessage', {
		duration :3
	});
}

function fitElementValue(className) {
	$$('.' + className).each( function(nameElement) {
		nameElement.setStyle( {
			overflow :'auto'
		});
		fitName(nameElement);
		nameElement.setStyle( {
			overflow :'hidden'
		});
	});
}

function submitUserEditForm(formName, containerId) 
{
	var form = document.forms[formName];
	var throbber = $(formName + 'Throbber');
	throbber.show();
	new Ajax.Updater(containerId, form.action, {
		parameters :Form.serialize(form),
		evalScripts :true
	});
}

var obje;
function makeEditable(obj, listType, rankCounter) {
	obje = obj;
	$('placeHolder').value = listType;
	var divId = obj.id;
	$('listLength').value = rankCounter;
	divId = divId.substring(divId.lastIndexOf('_') + 1);
	if (listType == 'userListRow')
		$('listDiv').value = 'userListRow' + divId;
	else
		$('listDiv').value = 'favListRow' + divId;
	var value = $(obj.id).innerHTML;
	$('oldValue').value = value.strip();
	$(obj.id)
			.replace(
					"<input type='text' id='editListRank' value='' name='newOrder' onkeydown='if(event.keyCode == 13){changeOrderOfList();}else if(event.keyCode < 48 || event.keyCode > 57){return false;}' class='userlistsNumberDiv' onblur='changeOrderOfList();'/>");
	$('editListRank').focus();
}

function changeOrderOfList() {
	var newValue = $('editListRank').value;
	$('editListRank').replace(obje);
	var listDiv = $('listDiv').value;
	var placeHolder = $('placeHolder').value;
	var oldValue = $('oldValue').value;
	var listLength = $('listLength').value;
	if (newValue != '' && parseInt(listLength) >= parseInt(newValue)
			&& newValue > 0) {
		$(obje.id).innerHTML = newValue;
		var theNewOrderRow = $(listDiv);
		var placeHolderDiv = $(placeHolder + newValue);
		if (oldValue > newValue)
			placeHolderDiv.insert( {
				before :theNewOrderRow
			});
		else if (oldValue < newValue)
			placeHolderDiv.insert( {
				after :theNewOrderRow
			});

		if (placeHolder == 'userListRow') {
			reorderUserLists('userlistsBody');
			listIdOrder('userlistsBody', placeHolder, 'userlistNumberDiv_');
		} else {
			reorderUserLists('userlistsBody_fav');
			listIdOrder('userlistsBody_fav', placeHolder, 'favlistNumberDiv_');
		}

	} else {
		$(obje.id).innerHTML = oldValue;
		oldValue = 0;
	}
}
function listIdOrder(listsId, placeHolder, numberDiv) {
	$$('#' + listsId + ' .userlistsRowDiv').each( function(row, index) {
		row.id = placeHolder + (index + 1);

	});
	$$('#' + listsId + ' .userlistsNumberDiv').each( function(row, index) {
		row.id = numberDiv + (index + 1);

	});

}

function deleteAccount() {
	document.forms['deleteAccountForm'].submit();
}

function deleteUserAccount() {
	var answer = confirm("Are you sure you want to delete this account?");
	if (answer) {
		deleteAccount();
	}
}

function addUser(followingId, imgSrc)
{
	var url = contextPath + '/extlink/addfollowers.htm?followingId=' + followingId; 
	new Ajax.Updater('followers' ,url, 
	{
			method :'POST',
			evalScripts:true,
			onComplete: function(transport)
			{
				$('follow_section').update("<a onclick=\"javascript:removeFollowing('" + followingId + "', '"+ imgSrc +"');\"><img src='"+imgSrc+"'  height=\"28\" width=\"87\" title=\"Unfollow\"/></a>");
			}
	});
	
}
function removeFollowing(followingId, imgSrc)
{
	var url = contextPath + '/extlink/removefollowing.htm?followingId=' + followingId; 
	new Ajax.Updater('followers', url, 
	{
			method :'POST',
			evalScripts:true,
			onComplete: function(transport)
			{
				$('follow_section').update("<a onclick=\"javascript:addUser('"+ followingId +"', '"+ imgSrc +"');\"><img src='"+imgSrc+"'  height=\"28\" width=\"87\" title=\"Follow\"/></a>");
			}
	});
	
}

function updateFollowers(formName, containerId)
{
	var checkBox = $$('#'+containerId+' input[type="checkbox"]');
	var i = 0;
	var form = document.forms[formName];
	
	checkBox.each( function(value) {
		if(value.checked)
			i++;
		if(!value.checked)
		{
			if(value.value != "selectAllForFollowers")
			{
				$("followersUserId" + value.value).value = 0;
			}
		}
	});
	
	if(i > 0) 
	{
		new Ajax.Updater(containerId, form.action, {
			parameters : Form.serialize(form),
			evalScripts :true
			});
	}
}

function updateFollowingUser(formName, containerId)
{
	var checkBox = $$('#'+containerId+' input[type="checkbox"]');
	var i = 0;
	var form = document.forms[formName];
	
	checkBox.each( function(value) {
		if(value.checked)
			i++;
		if(!value.checked)
		{
			if(value.value != "selectAllForFollowing")
			{
				$("followingUserId" + value.value).value = 0;
			}
		}
	});
	
	if(i > 0)
	{
		new Ajax.Updater(containerId, form.action, {
			parameters : Form.serialize(form),
			evalScripts :true
			});
	}
}

function selectAllorUnselectAllCheckBox(divId, checkBoxId)
{
	var checkBox = $$('#'+divId+' input[type="checkbox"]');
	checkBox.each( function(value) {
		value.checked = $(checkBoxId).checked ? true : false;
	});
}

function checkSelectAllorUnselectAll(divId, checkBoxId)
{
	var checkBox = $$('#'+divId+' input[type="checkbox"]');
	var checkFlag = true;
	checkBox.each( function(value) {
		if(!value.checked && value.id != checkBoxId)
		{
			checkFlag = false;
		}
	});
	$(checkBoxId).checked = checkFlag;
}
