function _loading()
{
	jQuery("#dialog").html("<br/><br/><br/><center><h1>"+txt_loading+"</h1></center>");
	jQuery("#dialog").dialog("open");
}
function _onCloseReload(url)
{
	jQuery("#dialog").bind( "dialogclose", function(event, ui) {
		if ( !url )
			location.reload(true);
		else
			window.location.href = url;
	});
}
function _dialogClose()
{
	jQuery("#dialog").dialog("close");
}
function _missing_info()
{
	jQuery.ajax({
		type: "POST",
		url: "/dialog?a=missing_info",
		dataType: "html",
		success: function(data, textStatus, jqXHR) {
			jQuery("#missing_info").html(data);
		}
	});
}
function dialog_success(data, textStatus, jqXHR)
{
	jQuery("#dialog").dialog("close");
	jQuery("#dialog").html(data);
	jQuery("#dialog").dialog("open");
	jQuery("#dialog").dialog( "option", "position", 'center' );
}
function dialog_error(jqXHR, textStatus, errorThrown)
{
	jQuery("#dialog").dialog("close");
	jQuery("#dialog").html(textStatus);
	jQuery("#dialog").dialog("open");
}

function getCompanyName(rel)
{
	if ( jQuery(rel).attr("companyName") )
		return jQuery(rel).attr("companyName");
	else if ( jQuery("#company-edit-form :input[name=\"companySiteAddress\"]").val() )
		return jQuery("#company-edit-form :input[name=\"companySiteAddress\"]").val();
}//undefined

function getCompanyId(rel)
{
	if ( jQuery(rel).attr("companyId") )
		return jQuery(rel).attr("companyId");
	else if ( jQuery("#company-edit-form :input[name=\"company_id\"]").val() )
		return jQuery("#company-edit-form :input[name=\"company_id\"]").val();
}

jQuery(function ()
{
	jQuery("#dialog").dialog({ autoOpen: false, modal: true, width: 550, close: closeDialog });
	jQuery(".reachTextEditor").live("click",function (){
		var textareaId = jQuery(this).attr("textareaId");
		var title = jQuery(this).attr("title")||jQuery(this).html();
		jQuery("#reachTextEditorDialog").remove();
		jQuery('body').append(jQuery('<div id="reachTextEditorDialog"></div>'));
		jQuery(".formError").remove();
		jQuery("#reachTextEditorDialog").dialog({
			autoOpen: true,
			modal: true,
			width: 550,
			title: title
		});
		jQuery("#reachTextEditorDialog").bind( "dialogclose", function(event, ui) {
			//jQuery("#reachTextEditorTextarea").tinymce().remove();
			if ( jQuery("#reachTextEditorTextarea").length )
				jQuery("#reachTextEditorTextarea").remove();
			jQuery("#reachTextEditorDialog").dialog("destroy");
			jQuery("#reachTextEditorDialog").remove();
		});
		jQuery.ajax({
			type: "GET",
			url: "/jtip?page=reachTextEditor&textareaId="+textareaId,
			dataType: "html",
			success: function(data, textStatus, jqXHR){
				jQuery("#reachTextEditorDialog").html(data);
				jQuery("#reachTextEditorDialog").dialog( "option", "position", 'center' );
			},
			error: function(){}
		});
		return false;
	});
	jQuery(".company_branding").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=company_branding&companyName="+jQuery(this).attr("companyName"),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".group_new_topic_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=group_new_topic&groupId="+jQuery(this).attr("groupId"),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".group_new_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=group_new",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_education_list_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_education_list",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_education_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_education"+( jQuery(this).attr("educationId") ? "&educationId="+jQuery(this).attr("educationId") : "" ),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_employment_list_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_employment_list",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_employment_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_employment"+( jQuery(this).attr("employmentId") ? "&employmentId="+jQuery(this).attr("employmentId") : "" ),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_contacts_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_contacts",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_activities_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_activities",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_photo_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_logo",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_photo_delete_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_logo_delete&picId="+jQuery(this).attr("picId"),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_birthday_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_birthday",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_gender_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_gender",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_link_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_link",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_spoken_language_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_spoken_language",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_searching_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_searching",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_offer_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_offer",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_member_of_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_member_of",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".profile_interests_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/dialog?a=user_interests",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	// End of Profile
	jQuery(".photo_edit_dialog").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/pic?a="+jQuery(this).attr("picType")+"&picId="+jQuery(this).attr("picId"),
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
		return false;
	});
	jQuery(".company_edit_product").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/products&services?a=edit&productId="+jQuery(this).attr("productId"),
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: function(data, textStatus, jqXHR) {
				jQuery("#dialog").dialog("close");
				jQuery("#dialog").html(data);
				jQuery("#dialog").dialog("open");
				jQuery("#dialog").dialog( "option", "width", 600 );
				jQuery("#dialog").dialog( "option", "height", jQuery(window).height()-80 );
				jQuery("#dialog").dialog( "option", "position", 'center' );
				//jQuery(".product-popup-area").css("height", jQuery(window).height()-100 );
			},
			error: dialog_error
		});
		return false;
	});
	jQuery("#company_create_news, .company_create_news").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/news/edit/ajax",
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_news, .company_edit_news").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/news/edit/ajax/"+jQuery(this).attr("newsId"),
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_description, .company_edit_description").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/description",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_logo, .company_edit_logo").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/logo",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_link, .company_edit_link").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/link",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_contacts, company_edit_contacts").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/contacts",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_offices, .company_edit_offices").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/offices",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_moderators, .company_edit_moderators").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/moderators",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_employees, .company_edit_employees").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/employees",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});
	jQuery("#company_edit_publicity, .company_edit_publicity").live("click",function (){
		jQuery("#dialog").dialog("option","title",jQuery(this).attr("title")||jQuery(this).html());
		_loading();
		jQuery.ajax({
			type: "POST",
			url: "/companies/"+getCompanyName(this)+"/ajax-edit/publicity",
			data: { company_id: getCompanyId(this) },
			dataType: "html",
			success: dialog_success,
			error: dialog_error
		});
	});

	jQuery("#close, .close_dialog").live("click",function (){
		jQuery("#dialog").dialog("close");
	});


	function closeDialog(event, ui)
	{
		jQuery(".formError").remove();
	}
});

