function Uploader(namespace, inputId)
{	
	this.namespace		= namespace;
	this.inputId		= inputId;
	this.inputSelector	= '#' + this.inputId;


	this.add = function(url) {
		this.setURL(url);
		this.showHideObject();
		return false;
	}


	this.uploading = function() {
		this.addWaiting();
	}


	this.uploaded = function(response) {
		this.removeWaiting();
		this.error('');

		var status = 'failed';
		var message = 'Atsiprasome, technines problemos. Meginkite veliau.';

		// IE special approach, nenaudojamas nei XML, nei Json objektai
		if(typeof response == 'string') {
			var sm = response.split(';;', 2);
			if(sm.length == 2) {
				status = sm[0];
				message = sm[1];
			}
		}

		if(status == 'failed') {
			this.error(message);
		}
		else {
			this.add(message);
		}
	}


	this.error = function(message) {
		$('#' + this.namespace + 'Error').text(message);
	}


	this.addWaiting = function() {
		$('#' + this.namespace + 'Title').addClass('uploadWaiting');
	}


	this.removeWaiting = function() {
		$('#' + this.namespace + 'Title').removeClass('uploadWaiting');
	}


	this.remove = function() {
		this.setURL('');
		this.showHideObject();
	}


	this.flipButtons = function() {
		var addButton = $('#' + this.namespace + 'Add');
		var removeButton = $('#' + this.namespace + 'Rem');

		if(this.getURL()) {
			addButton.hide();
			removeButton.show();
		}
		else {
			addButton.show();
			removeButton.hide();
		}
	}


	this.getURL = function() {
		var url = $(this.inputSelector).val();
		if(url.length > 1) {
			return url;
		}
		return false;
	}


	this.setURL = function(url) {
		$(this.inputSelector).val(url);
	}
}


function AreasSelection(namespace, width, height)
{
	this.namespace		= namespace;
	this.dialogSelector = '#' + this.namespace + 'Dialog';
	var _this = this;


	this.open = function() {
		$(this.dialogSelector).dialog('open');
		this.collapseAll();
		this.count();
		return false;
	}


	this.close = function() {
		$(this.dialogSelector).dialog('close');
		this.update();
		return false;
	}


	this.update = function() {
		var areasSelection = $('#areasSelection');
		var checkedAreas   = $(this.dialogSelector + ' .selectionBar input:checked');
		
		areasSelection.empty();
		
		var areasList = '';
		checkedAreas.each(function(i) {
				$('#areasSelection').append('<input type="hidden" name="company_areas[]" value="' + this.value + '" />');

				var areaTitle = $('#areaTitle' + this.value);
				areasList += '<div class="separator">' + areaTitle.text() + '</div>';
			}
		);
		
		if(areasList.length == 0) {
			areasList = '<div class="separator">Nepasirinkta</div>';
		}

		areasSelection.append(areasList);
	}


	this.count = function() {
		var areasTopics   = $(this.dialogSelector + ' .selectionTopic');
		areasTopics.each( function(i) {
				var count = $('#' + this.id + ' input:checked').size();
				$('#' + this.id + 'Count').text(count);
		}
		);
	}


	this.expandCollapse = function(id) {
		var topic = $('#selectionTopicTitle' + id);
		var areas = $('#selectionArea' + id);
		if(topic.hasClass('topicCollapsed')) {
			topic.removeClass('topicCollapsed');
			topic.addClass('topicExpanded');
			areas.show();
		}
		else {
			topic.removeClass('topicExpanded');
			topic.addClass('topicCollapsed');
			areas.hide();
		}
		return false;
	}


	this.collapseAll = function() {
		var topics = $('.topicTitleLink');
		var areas = $('.selectionArea');
		topics.removeClass('topicExpanded');
		topics.addClass('topicCollapsed');
		areas.hide();
	}


	this.expandAll = function() {
		var topics = $('.topicTitleLink');
		var areas = $('.selectionArea');
		topics.removeClass('topicCollapsed');
		topics.addClass('topicExpanded');
		areas.show();
	}


	this.uncheckAll = function() {
		var allAreas = $(this.dialogSelector + ' input[type="checkbox"]');
		allAreas.each(function(i) {
			this.checked = false;
		}
		);
		this.count();

		return false;
	}


	$("input[type='checkbox']").change(function() {
		_this.count();
	}	
	);


	$(this.dialogSelector).dialog({autoOpen: false});
	$(this.dialogSelector).dialog('option', 'modal', true);
	$(this.dialogSelector).dialog('option', 'position', 'center');
	$(this.dialogSelector).dialog('option', 'resizable', false);
	$(this.dialogSelector).dialog('option', 'width', width);
	$(this.dialogSelector).dialog('option', 'height', height);
}


/*
 * Records
 */

function addRecord(namespace)
{
	var recordTemplate = eval(namespace + '_template');
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7SingleQuote/g, "'");
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7DoubleQuote/g, '"');
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7LessThen/g, "<");
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7GreaterThen/g, ">");
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7Ampersand/g, "&");
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7minus/g, "-");
	recordTemplate = recordTemplate.replace(/XQwpM6LSizEuvB4A7Index/g, company_websites_index.toString());
	company_websites_index++;
	$('#' + namespace + '_id > tbody').append(recordTemplate);
	showHideTitles(namespace);
	return false;
}


function removeRecord(namespace, id)
{
	$('#' + namespace + '_' + id).remove();
	showHideTitles(namespace);
	return false;
}


function initRecords(namespace)
{
	showHideTitles(namespace);
}


function showHideTitles(namespace)
{
	var recordsCount = $('#' + namespace + '_id tr').size();
	if(recordsCount == 1) {
		$('#' + namespace + '_id').hide();
		$('#' + namespace + '_info').hide();
	}
	else {
		$('#' + namespace + '_id').show();
		$('#' + namespace + '_info').show();
	}
}