var publish_url = '/provider-directory/api/provider_data_value/publish';
var save_url =  '/provider-directory/api/provider_data_value/save_draft';
var save_feedback_url = '/provider-directory/api/provider_data_value/save_feedback';
var save_verification_code_url = '/provider-directory/api/provider_data_value/save_verification_code';
var del_value_url = '/provider-directory/api/provider_data_value/delete';
var del_draft_url = '/provider-directory/api/provider_data_value/delete_draft';
var move_value_url = '/provider-directory/api/provider_data_value/move_value';

$(document).ready(function() {
	var user_role = $('#user_role').val();
	
	var proofpoints =$('.edit-proofpoint'); 
	if(proofpoints.size() > 0){
		proofpoints.each(function(){
			$(this).EditProofpoint();
		});
	}
	
	
	$('#company-name').make_editable({
		'type': 1008,
		'fields': [
			{'name': 'value', 'validator': [validators.company_name]}
		],
		'renderer': function(fields) {
			$('.my-ffc-header .sol-title').html(fields.value);
		},
		'instructions': 'The name under which your company provides the solution. Max 50 characters.'
	});

	$('#web-site').make_editable({
		'type': 3,
		'fields': [
			{'name': 'value', 'validator': [validators.url, validators.max_length(50)]}
		],
		'instructions': 'Please provide the URL for your corporate website. Max 50 characters.'
	});

	$('#primary-business').make_editable({
		'type': 4,
		'fields': [
			{'name': 'title', 'type': function() { return this.find('.title_options').clone(); }, 'validator': [validators.selected], 'required': true},
			{'label': 'Comment', 'name': 'comment', 'validator': [validators.max_length(1000)],'type': 'textarea', 'required': false}
		],
		'instructions': 'Please specify your company\'s sector from the list of categories provided. Max 1000 characters in comments box.'
	});

	$('#description').make_editable({
		'type': 5,
		'fields': [
			{'name': 'value', 'validator': [validators.min_length(20), validators.max_length(1000)]}
		],
		'instructions': 'Please provide a short text description of the company providing the solution. Max 1000 characters.'
	});

	$('#long-description').make_editable({
		'type': 6,
		'fields': [
			{'name': 'value', 'validator': [validators.min_length(20), validators.max_length(5000)]}
		],
		'instructions': 'Please provide a more in-depth description of the company providing the solution. Max 5000 characters.'
	});

	$('#addresses').brochure({
		'type': 1001,
		'fields': [
			{'name': 'location_name', 'label': 'Location Name', 'validator': [validators.max_length(30)], 'required': true},
			{'name': 'street_address', 'label': 'Street Address', 'validator': [validators.max_length(30)], 'required': true},
			{'name': 'city', 'label': 'City', 'validator': [validators.max_length(30)], 'required': true},
			{'name': 'state_province', 'label': 'State/Province', 'required': true, 'type': function() { return this.find('.us-states:first').clone(); }},
			{'name':'postal_code', 'label':'Zip Code / Postal Code', 'validator': [validators.max_length(10)], 'required': true},
			{'name': 'country', 'label': 'Country', 'required': true, 'type': function() { return this.find('.countries-list:first').clone(); }}
		],
		'edit_save_handler': function(data) {
			data.success(data.items);
		},
		'add_save_handler': function(data) {
			data.success(data.items);
		},
		'delete_handler': function(data) {
		},
		'edit_renderer': function(data) {
			data.edit.find('[name=country]').change(function() {
				var action = $(this).val() == 'US' ? 'show' : 'hide'; 
				data.edit.find('[name=state_province]').parent()[action]();
				if (action == 'hide') {
					data.edit.find('[name=state_province]')[0].value = "";
				}
			}).change();
		},
		'change': function(container) {
			var first = container.find('.items>.bitem:first');
			if (first && first.find('[name=country].published').length >= 1) {
				var country = first.find('[name=country].published')[0].value.toLowerCase();
				var city = first.find('[name=city].published')[0].value;
				var state = first.find('[name=state_province].published')[0].value;
				$('.company-info .address-info-header').html('<img class="flag" src="/img/flags/' + country + '.gif" width="16" height="11" /><span class="city">' + city + (state ? ', ' + state : '') + '</span><br/>');
			}
		},
		'instructions': 'Please provide the postal address of your company\'s office. Multiple locations can be created.'
	});

	$('#contact-person').make_editable({
		'type': 1002,
		'fields': [
			{'label': 'First name', 'name': 'first_name', 'validator': [validators.max_length(20)], 'required': false},
			{'label': 'Last name', 'name': 'last_name', 'validator': [validators.max_length(20)], 'required': false},
			{'label': 'Title', 'name': 'title', 'validator': [validators.max_length(30)], 'required': false},
			{'label': 'Phone (including country code, if outside US)', 'name': 'phone', 'required': false, 'validator': [validators.phone]},
			{'label': 'Extension', 'name': 'extension', 'validator': [validators.max_length(10)], 'required': false},
			{'label': 'E-mail', 'name': 'email', 'required': false, 'validator': [validators.email]}
		],
		'instructions': 'Please provde contact information for inquiries by eCommerce Know-How Users.'
	});

	$('#parent-company').make_editable({
		'type': 1,
		'fields': [
			{'name': 'value', 'validator': [validators.max_length(30)], 'required': false}
		],
		'instructions': 'If your company is owned and operated by a parent company, please provide the name of that company. Max 30 characters.'
	});

	$('#year-founded').make_editable({
		'type': 13,
		'fields' : [
			{'name': 'value', 'validator': [validators.year]}
		],
		'renderer': function(fields) {
			$('.company-info .founded-in').html(fields.value);
		},
		'instructions': 'Please enter the year the company was founded.'
	});

	$('#corporate-history').make_editable({
		'type': 14,
		'fields': [
			{'name': 'value', 'validator': [validators.min_length(20), validators.max_length(1000)]}
		],
		'instructions': 'Briefly describe the company\'s history. Max 1000 characters.'
	});

	$('#number-of-employees').make_editable({
		'type': 7,
		'fields': [
			{'label': 'Employees', 'name': 'employees', 'required': false, 'type': function() { return this.find('.employees-options').clone(); }, 'validator': [validators.selected]},
			{'label': 'Comment', 'name': 'comment', 'validator': [validators.max_length(1000)],'type': 'textarea', 'required': false}
		],
		'instructions': 'Please provide the total number of people the company employs. Max 1000 characters in comments box.'
	});

	$('#financials').make_editable({
		'type': 11,
		'fields': [
			{'name': 'value', 'validator': [validators.max_length(1000)]}
		],
		'instructions': 'Please provide a brief insight to the financial history / state of the company. Max 1000 characters.'
	});

	$('#annual-revenue').make_editable({
		'type': 12,
		'fields': [
			{'name': 'value', 'required': false, 'type': function() { return this.find('.annual-revenue-options').clone(); }, 'validator': [validators.selected]},
			{'label': 'Comment', 'name': 'comment', 'validator': [validators.max_length(1000)],'type': 'textarea', 'required': false}
		],
		'instructions': 'How much revenue does the company earn annually? Max 1000 characters.'
	});

	$('#company-ownership').make_editable({
		'type': 1007,
		'fields': [
			{'name': 'public', 'type': 'checkbox', 'text': 'Public'},
			{'name': 'exchange', 'type': function() { return this.find('.stocks-list:first').clone(); }, 'required': false, 'validator': [validators.selected_any]},
			{'name': 'ticker_symbol', 'label': 'Stock Symbol', 'validator': [validators.max_length(10)], 'required': false}
		],
		'renderer': function(fields) {
			$('.company-info .stock-info').html('<a href="http://www.google.com/finance?q=' + fields.exchange + ':' + fields.ticker_symbol + '">' + fields.exchange + ': <span class="symbol">' + fields.ticker_symbol + '</span></a>');
		},
		'instructions': 'Please state whether the company is private or public, and if applicable please enter a stock symbol.<br>NOTE:  if the checkbox is deselected, the company will be displayed as PRIVATE.'
	});

	$('#executives').brochure({
		'type': 1003,
		'fields': [
			{'name': 'first_name', 'label': 'First Name', 'validator': [validators.max_length(20)], 'required': true},
			{'name': 'last_name', 'label': 'Last Name', 'validator': [validators.max_length(20)], 'required': true},
			{'name': 'title', 'label': 'Title', 'validator': [validators.max_length(30)], 'required': true}
		],
		'edit_save_handler': function(data) {
			data.success(data.items);
		},
		'add_save_handler': function(data) {
			data.success(data.items);
		},
		'delete_handler': function(data) {
		},
		'instructions': 'Please provide name and position of the company\'s executives.'
	});

	$('#strategic-partners').make_editable({
		'type': 8,
		'fields': [
			{'name': 'value', 'validator': [validators.max_length(1000)]}
		],
		'instructions': 'Please list any companies designated as strategic partners to your organization. Max 1000 characters.'
	});

	$('#client-base').make_editable({
		'type': 9,
		'fields': [
			{'name': 'value', 'validator': [validators.max_length(1000)]}
		],
		'instructions': 'Please describe your target vertical markets, business models, and or geographic targeting for your company. Max 1000 characters.'
	});

	$('#number-of-clients').make_editable({
		'type': 10,
		'fields': [
			{'name': 'value', 'validator': [validators.integer]}
		],
		'instructions': 'How many clients does your company currently have?'
	});

	$('#qualification').brochure({
		'type': 1004,
		'fields': new Array(
			{'name': 'content', 'label': 'Content', 'required': false, 'type': 'textarea'}
		),
		'edit_save_handler': function(data) {
			data.success(data.items);
		},
		'add_save_handler': function(data) {
			data.success(data.items);
		},
		'delete_handler': function(data) {
		},
		'instructions': 'Please provide any awards or qualifications the company has been awarded.'
	});

	$('#additional-information').brochure({
		'type': 1009,
    'is_file': true,
		'fields': [
			{'name': 'file', 'label': 'File', 'required': false, 'type': 'file'},
			{'name': 'description', 'label': 'Description', 'validator': [validators.max_length(100)], 'required': true}
		],
		'edit_save_handler': function(data) {
			data.success(data.items);
		},
		'add_save_handler': function(data) {
			data.success(data.items);
		},
		'delete_handler': function(data) {
		},
		'instructions': 'Please select a document and upload it.'
	});

	$('.view-full-description').live('click', function() {
		var item = $(this).parent();
		var full_description = item.parent().find('[name=value].published').val();
		item.html(full_description);
		item.hide().slideDown();
		return false;
	});

	/*
	 * @todo: Issue #1010
	$('.company-info .company-logo, .company-info .edit-field-btn').click(function() {
		var dialog = $('#logo-edit');
		dialog.dialog({
			'modal': true,
			'title': 'Change company logo',
			'close': function() {
				dialog.dialog('destroy');
			},
			'width': 480,
			'height': 250
		});
		return false;
	});
	*/

	if (user_role == 'ffc') {
		var ffc_fields = ['company-name', 'web-site', 'primary-business', 'description', 'long-description', 'addresses', 'contact-person', 'parent-company', 'year-founded', 'corporate-history', 'number-of-employees', 'financials', 'annual-revenue', 'company-ownership', 'executives', 'strategic-partners', 'client-base', 'number-of-clients', 'qualification'];
		
		$.each(ffc_fields, function(i, ffc_field) {
			$('#' + ffc_field + '-ffc').make_editable({
				'fields': [{'name': 'value'}],
				'parent_field': $('#' + ffc_field),
				'actions': {
					'save': false,
					'del': false
				}
			});
		});

		$('.ch_active_icon').change_status();
	}

	help_block($('.help-block'));
});

$('[helptip]')
	.live('mouseover', function() {
		el = $(this);
		if (el.attr('helptipid')) {
			return;
		}
		var tip = $('<div style="padding: 13px; position: absolute; background-color: #FCFEE0; color: #727272; border: solid 1px #E2E2E0;"></div>');
		var tip_id = 'tip_' + Math.floor(Math.random() * 11);
		tip.attr('id', tip_id);
		tip.html(el.attr('helptip')).hide();
		$('body').append(tip);
		var offset = el.offset();
		tip.css({
			'top': offset.top - 18,
			'left': offset.left + 20
		}).show();
		el.attr('helptipid', tip_id);
	})
	.live('mouseout', function() {
		var el = $(this);
		if (!el.attr('helptipid')) {
			return;
		}
		$('#'+el.attr('helptipid')).remove();
		el.removeAttr('helptipid');
	})
	.live('click', function() {
		return false;
	});


