function setRowMovers(aTag){
	for(i=0;i<aTag.length;i++){
		if(aTag[i].id.indexOf('up_') != -1){
			aTag[i].addEvent('click',function(){
				param = this.id.split('_');
				if(this.parentNode.parentNode){
					cRowId = parseInt(this.parentNode.parentNode.id.split('_')[1]);
					if( $('row_'+(cRowId-1)) != null ){
						chkA = $$('#row_'+(cRowId-1)+' a');
						for(i=0;i<chkA.length;i++){
							if(chkA[i].id.indexOf('up_'+param[1]) != -1){
								cRowId = parseInt(this.parentNode.parentNode.id.split('_')[1]);
								storeThis = $('row_'+cRowId).get('html');
								storePrev = $('row_'+(cRowId-1)).get('html');
								$('row_'+cRowId).set('html',storePrev);
								$('row_'+(cRowId-1)).set('html',storeThis);
								
								orderThis = $$('#'+this.parentNode.id+' div')[0].get('html');
								orderPrev = $$('#'+$(chkA[i].id).parentNode.id+' div')[0].get('html');
								$$('#'+this.parentNode.id+' div')[0].set('html',orderPrev);
								$$('#'+$(chkA[i].id).parentNode.id+' div')[0].set('html',orderThis);

								setRowMovers( $$('#contentRows a') );
							}
						}
					}
				}
				return false;
			});
		}
		if(aTag[i].id.indexOf('down_') != -1){
			aTag[i].addEvent('click',function(){
				param = this.id.split('_');
				if(this.parentNode.parentNode){
					cRowId = parseInt(this.parentNode.parentNode.id.split('_')[1]);
					if( $('row_'+(cRowId+1)) != null ){
						chkA = $$('#row_'+(cRowId+1)+' a');
						for(i=0;i<chkA.length;i++){
							if(chkA[i].id.indexOf('down_'+param[1]) != -1){
								storeThis = $('row_'+cRowId).get('html');
								storeNext = $('row_'+(cRowId+1)).get('html');
								$('row_'+cRowId).set('html',storeNext);
								$('row_'+(cRowId+1)).set('html',storeThis);
								
								orderThis = $$('#'+this.parentNode.id+' div')[0].get('html');
								orderNext = $$('#'+$(chkA[i].id).parentNode.id+' div')[0].get('html');
								$$('#'+this.parentNode.id+' div')[0].set('html',orderNext);
								$$('#'+$(chkA[i].id).parentNode.id+' div')[0].set('html',orderThis);
								
								setRowMovers( $$('#contentRows a') );
							}
						}
					}
				}
				return false;
			});
		}
	}
}

function updateContentType(type, fillObj){
	var log = fillObj.addClass('ajax-loading');
	var req = new Request({
		url:'/admin/index.php?class=content&action=display&view=admin.'+type+'&tmpl=maincomponent',
		onComplete: function(response) { 
			log.removeClass('ajax-loading');
			log.set('html', response);
			window.onload();
		},
		onFailure : function(){
			log.removeClass('ajax-loading');
			log.set('html', 'Error - Check you are still logged in');
		}
	});
	req.send();
}

window.addEvent('domready', function() {
	if( $('contentRows') != null ){
		setRowMovers( $$('#contentRows a') );
	}
	if( $('contentTypeSelect') != null ){
		$('contentTypeSelect').addEvent('change',function(){
			//tinyMCE.execCommand('mceRemoveControl', true, 'mceEditor');
			updateContentType(this.options[this.options.selectedIndex].value, $('contentType'));
		});
	}
});


