galleryEditor=function(){
	var obj=this;
	jQuery.extend(this,arguments[0] || {});
	jQuery(document).bind('ready',function(){
				obj.init();
	}
		);
};

galleryEditor.prototype={
	items:[],
	lastIndex:0,
	table:{main:null,head:null,body:null,footer:null},
	div:null,
	filesInput:[],
	images:[],
	removed:[],
	init:function(){
		this.makeTable();
		for(var i=0;i<this.items.length;i++){
			this.addRow(this.items[i].file);
		}
	},
	makeTable:function(){
		this.table.main=this.cr('table');
		if (jQuery('#'+this.div))
			jQuery('#'+this.div).append(this.table.main);
		this.table.head=this.cr('thead');
		this.table.main.appendChild(this.table.head);
		var tr=this.cr('tr');
		this.table.head.appendChild(tr);
		var th=this.cr('th');
		th.innerHTML='File';
		tr.appendChild(th);
		var th=this.cr('th');
		th.innerHTML='Actions';
		tr.appendChild(th);
		this.table.body=this.cr('tbody');
		this.table.main.appendChild(this.table.body);
		this.table.footer=this.cr('tfoot');
		this.table.main.appendChild(this.table.footer);
		this.addFileRow();
		for(var i=0;i<this.images.length;i++)
			this.addUploadedImg(this.images[i].url,this.images[i].width,this.images[i].height);
		this.dialog=this.cr('div');
		this.dialog.id='galleryDialog';
		this.dialogImg=this.cr('img');
		this.dialog.appendChild(this.dialogImg);
		document.body.appendChild(this.dialog);
		jQuery(this.dialog).dialog({
			autoOpen: false,
			show: 'blind',
			hide: 'explode'
		});
                var obj=this;
                jQuery('#imageForm').bind('submit',function(){obj.submit()});
	},
	submit:function(){
		var f=jQuery('#imageForm');
		for(var i=0;i<this.removed.length;i++){
			var inp=this.cr('input');
			inp.name='galleryRemoved[]';
			inp.value=this.removed[i];
                        inp.style.display='none';
			f.append(inp);
		}
	},
	addFileRow:function(){
		var obj=this;
		var nextIndex=this.filesInput.length;
		var tr=this.cr('tr');
		var td=this.cr('td');
		tr.appendChild(td);
		tr.id='fileTr_'+nextIndex;
		this.table.footer.appendChild(tr);
		this.filesInput[nextIndex]={input:this.cr('input'),button:this.cr('input')};
		this.filesInput[nextIndex].input.type='file';
		this.filesInput[nextIndex].input.size=40;
		this.filesInput[nextIndex].input.id='file_'+nextIndex;
		this.filesInput[nextIndex].input.name='newGalleryFiles[]';
		jQuery(this.filesInput[nextIndex].input).bind('change',function(e){
				var id=e.target.id.split('_');
				jQuery('#fileButton_'+id[1]).css('display','');
				obj.addFileRow();
			});

		td.appendChild(this.filesInput[nextIndex].input);
		this.filesInput[nextIndex].button.type='button';
		this.filesInput[nextIndex].button.id='fileButton_'+nextIndex;
		this.filesInput[nextIndex].button.value='-';
		this.filesInput[nextIndex].button.style.display='none';
		td=this.cr('td');
		tr.appendChild(td);
		td.appendChild(this.filesInput[nextIndex].button);
		jQuery(this.filesInput[nextIndex].button).bind('click',function(){var id=this.id.split('_'); obj.removeFileRow(id[1])});
	},
	removeFileRow:function(id){
		jQuery('#fileTr_'+id).remove();
	},
	addUploadedImg:function(url,width,height){
		var obj=this;
		var tr=this.cr('tr');
		this.table.body.appendChild(tr);
		var td=this.cr('td');
		tr.appendChild(td);
		td.innerHTML=url;

		var td=this.cr('td');
		tr.appendChild(td);
		var a=this.cr('a');
		td.appendChild(a);

		a.href='javascript:;';
		a.innerHTML='View';
		jQuery(a).bind('click',function(){
			obj.dialogImg.src=url;
			if (jQuery(obj.dialog).dialog( "isOpen" ))
				jQuery(obj.dialog).dialog( "close" )
			jQuery(obj.dialog).dialog("option", "width",parseInt(width)+30);
			jQuery(obj.dialog).dialog("option", "height",parseInt(height)+50);
			jQuery(obj.dialog).dialog('open');
		});
		//td.innerHTML+='&nbsp;&nbsp;';
		td.appendChild(document.createTextNode(' '));
		var a1=this.cr('a');
		td.appendChild(a1);
		a1.href='javascript:;';
		a1.innerHTML='Delete';
		jQuery(a1).bind('click',function(){
			obj.removed[obj.removed.length]=url;
			jQuery(tr).remove();
		});
	},
	cr:function(name){ return document.createElement(name)}
}
