ax_links = {};

LinksComponent = Class.create();
LinksComponent.prototype = {
	
	initialize: function(app, id, options) {
		
		this.id = id;
		this.app = app;
		
		this.options = options || {mode:'display'};
		this.options.template = this.options.template || 'link_render';				
		updaters[this.id+'_update'] = new Updater(this.id + '_update', this.app, 'Update', {parent:this, status_style:''+this.options.status_style, color:''+this.options.color});
		
		if (this.options.mode == 'admin') {			
			$(this.id+'_update_admin').style.display = '';	
			$(this.id+'_wrapper').style.display = '';

			ax_forms[this.id+'_update_form'] = new AxForm(this.id+'_update_form', {updater:this.id+'_update',use_return:true});				
			
			if (this.options.orderable) {
				$(this.id+'_listing').style.display = '';	
			}
			
			listManagers[this.id+'_update_content'] = new ListManager(this.id+'_update_content', '/sys/components/Links/index.php', {updater:this.id+'_update'});
			
		}
		
		var me = this;
		page_init.scripts[this.id] = function() {
			//me.loadListing();
		};
			
	},
	
	ajaxUpdate: function(ajaxResponse) {
		updaters[this.id+'_update'].update(ajaxResponse);
		updaters[this.id+'_update'].status.hide();
		if (this.options.mode == 'display') {
			var has_links = ajaxResponse.getElementsByTagName('has_links')[0];
			if (has_links) {
				$(this.id+'_wrapper').style.display = '';
			}
		}
	},
	
	loadListing: function() {
		var callParams = [];		
		callParams.push('type=' + this.options.type);
		callParams.push('code=' + this.options.code);
		callParams.push('content_id=' + this.options.content_id);
		callParams.push('tpl=' + this.options.template);		
		updaters[this.id+'_update'].run(callParams, 'Update');	
	},	
	
	mediaInsert: function(res) {
		var parts = res.split('|');
		if (console) console.log("Inserting Media " + parts[1]);
		var regExp = /\.(\w+)$/	;	
		regExp.test(parts[1]);
		var ext = RegExp.$1;
		if (console) console.log("EXTENSION: " + ext);
		var temp = {					
					index: parts[0],
					src: parts[1]					
					};
	
		if (console) console.log(this.links);
		this.links.push(temp);
		this.linkcomp_new_link_file_id.value = temp.index;
	},
	
	makeSortable: function() {
		listManagers[this.id+'_update_content'].makeSortable(this.id, '', 'UpdateOrder');
	},
	
	disableSorting: function() {
		listManagers[this.id+'_update_content'].disableSorting(this.id, '', 'UpdateOrder');
	}
}