var TagManager = function(wrapper, tags, folders, chosentags){
	
	this.tags = $(tags);
	this.wrapper = $(wrapper);
	this.folders = $(folders);
	this.chosentags = $(chosentags);
	
	this.serviceUrl = '/json/?controller=directory';
	
	this.bindControls();
}

TagManager.prototype.bindControls = function(){
	
	var tmpObj = this;
	
	this.bindFolders();
	
	// Bind controls to re-populate folders and empty the chosen tags if the business type changes
	$('dl.businessType :radio').click(function(){
		tmpObj.getFolders(this);
		tmpObj.firstPage();
	});
	
	$('li.back').click(function(){
		tmpObj.firstPage();
	});
	
}

TagManager.prototype.getTags = function(element){
		
		tmpObj = this;
		
		$(element).parent('ul').parent('div').animate({left: -397}, 1500 ,'easeOutSine', function(){
			$(tmpObj.tags).css('overflow', 'auto');
		});
		$(element).parent('ul').css('overflow', 'hidden');
		
		var parentId = $(element).attr('id').replace(/folder_/, '');
		
		var tmpObj = this;
		
		$.getJSON(this.serviceUrl + '&method=getTags', 'folderId=' + parentId, function(response){
			
			$('li:not(.back)', tmpObj.tags).remove();
		
			for(var i = 0; i < response.data.length; i++){
					$(tmpObj.tags).append('<li><label><input type="checkbox" name="tags[]" value="' + response.data[i].id + '" />&nbsp;&nbsp;&nbsp;&nbsp;' + response.data[i].name + '</label></li>');
			}
			
			tmpObj.bindTags();
			
		});
}

TagManager.prototype.firstPage = function(){
	tmpObj = this;
	$(tmpObj.tags).css('overflow', 'hidden');
	$('.holder', this.wrapper).animate({left: 0}, 1500 ,'easeOutSine', function(){
		$(tmpObj.folders).css('overflow', 'auto');
	});
	
}

TagManager.prototype.bindFolders = function(){
	tmpObj = this;

	$('ul.folders li').click(function(){
		tmpObj.getTags(this);
	});
}

TagManager.prototype.bindTags = function(){
	var tmpObj = this;
	// Bind controls to click events on tags
	$('li input:checkbox', this.tags).click(function(){
		tmpObj.moveTag(this);
	});
}

TagManager.prototype.getFolders = function(element){

	// Get the parent id
	var id = $(element).val();
	
	var tmpObj = this;
	
	$('li', this.chosentags).remove();
	
	// Make a json call to get all folders
	$.getJSON(this.serviceUrl + '&method=getFolders', 'parentId=' + id, function(response){
		
		// Clear the folder data
		$('li', tmpObj.folders).remove();
		for(var i = 0; i < response.data.length; i++){
			$(tmpObj.folders).append('<li id="' + response.data[i].id + '">' + response.data[i].name + '</li>');
		}
		tmpObj.bindFolders();

		
	});
	
}

TagManager.prototype.moveTag = function(element){
	// check to see if the chosen tags list exists, if it doesn't, create it
	if($('fieldset.chosentags ul').length == 0){
		$('fieldset.chosentags').append('<ul class="tags"></ul>');
	}
	
	var element = element;
	var tmpObj = this;
	
	$(element).parent('label').parent('li').fadeOut('slow', function(){
		
		if (element.checked == true) {
			$(element).parent('label').parent('li').appendTo('fieldset.chosentags ul').fadeIn('fast');
			// $(element).parent('label').parent('li');
		} else if(element.checked == false){
			$(element).remove();
		}
		
	});
	
	
	
}
