function show_loading_graphic(div) {
	$(div).innerHTML='<p><img src="http://carbatch.com/public/images/ajax-loader.gif" height="25px" width="25px"></p>';
}
function hide_loading_graphic(div) {
	$(div).innerHTML='';
}

function print_div(div_id) {
	var a = window.open('','','width=1,height=1');
	a.document.open("text/html");
	a.document.write('<link rel="stylesheet" type="text/css" media="all" href="/public/stylesheets/layout.css" />');
	a.document.write('<link rel="stylesheet" type="text/css" media="all" href="/public/stylesheets/print.css" />');
	a.document.write(document.getElementById(div_id).innerHTML);
	a.document.close();
	a.print();
	a.close();
}

/***************************************
***************************************/

document.observe('dom:loaded', function() { 
	//image upload
	if ($('upload_button')) {
		var action = '/admin/autos/image_upload';
		if ($('auto_id'))
			action = '/admin/autos/image_upload/' + $('auto_id').value;
			
		var button = $('upload_button'), file_li = $$('#upload .files')[0], interval;
		new AjaxUpload(button,{
			action: action,
			name: 'auto_image',
			onSubmit : function(file, ext){
				if (! (ext && /^(jpg|jpeg)$/.test(ext))){
				//if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
					alert('Error: invalid file extension');
					return false;
				}
				else {
					$('ispinner').show();
					this.disable();
				}
			},
			onComplete: function(file, response) {
				$('ispinner').hide();
				this.enable();
				file_li.insert(new Element('li').update(file));
			}
		});
	}
	
	//sortable images
	if ($('auto_images')) {
		Sortable.create('auto_images', { tag: 'div', overlap:'horizontal',constraint:false,
			onChange: function(item) {
				var list = Sortable.options(item).element;
			},
			
			onUpdate: function() {
				new Ajax.Request("/admin/autos/image_reorder", {
					method: "post",
					onLoading: function(){$('ispinner').show()},
					onLoaded: function(){$('ispinner').hide()},
					parameters: { data: Sortable.serialize("auto_images") }
				});
			}
		});
	}	
});

//image delete
function image_delete(id) {
	new Ajax.Request('/admin/autos/image_delete/'+id, {
		asynchronous:true, 
		evalScripts:true, 
		method:'delete',
		onLoading: function(){$('ispinner').show()},
		onLoaded: function(){$('image_'+id).remove();$('ispinner').hide()}
	});
}

/***************************************
***************************************/

