/* main javascript file - nimbus
	"nimbus" - nu framework
	(c) 2010 nuonium media enterprises	*/
	
function previewImage(ele){
	if(!ele){
		return false;
	}
	
//	var id=parseInt(ele.id.toString().charAt(ele.id.toString().length-1));
	var id=parseInt(array_last(ele.id.split('-')));
	
	_log('!!!',id);
	
	var img=document.getElementById('img_preview-'+id);
	var img_wrapper=img.parentNode;
	var img_fieldset=img_wrapper.parentNode;
	var img_field=img_fieldset.parentNode;
	var img_info=getElementsByClassName('photo_upload-info',img_fieldset)[0];
	
	var last=img_field.parentNode.lastChild;
	last_link=last.getElementsByTagName('a')[0];
	
	if(last==img_field || (last!=img_field && !hasClass(last,'empty'))){
		//if we're changing the last node, set it to not empty and add one
		var new_field=last.cloneNode(true);
		if(last==img_field){
			removeClass(last,'empty');
		}else{
			addClass(new_field,'empty');
		}
		
		var new_link=new_field.getElementsByTagName('a')[0];
		
		var tmp = new_link.id.split('-');
		tmp[tmp.length-1]=(id+1);
		new_link.id=tmp.join('-');
		
//		new_link.id=new_link.id.substring(0,new_link.id.length-1)+(id+1);
		
		tmp = new_link.name.split('-');
		tmp[tmp.length-1]=(id+2);
		new_link.name=tmp.join('-');
						
//		new_link.name=new_link.name.substring(0,new_link.name.length-1)+(id+2);
		
		var new_img=new_field.getElementsByTagName('img')[0];
		tmp = new_img.id.split('-');
		tmp[tmp.length-1]=(id+1);
		new_img.id=tmp.join('-');
		
//		new_img.id=new_img.id.substring(0,new_img.id.length-1)+(id+1);

	
/*		var new_file=new_field.getElementsByTagName('input')[0];
		new_file.id=new_file.id.substring(0,new_file.id.length-1)+(id+1);	
		new_file.value='';*/
		
		var new_inputs=new_field.getElementsByTagName('input');
		var new_file, new_photo_delete;
		for(var i=0; i<new_inputs.length; i++){
			if(new_inputs[i].type=='file'){
				new_file=new_inputs[i];
			}else if(new_inputs[i].type=='checkbox'){
				new_photo_delete=new_inputs[i];
			}
		}

		tmp = new_file.id.split('-');
		tmp[tmp.length-1]=(id+1);
		new_file.id=tmp.join('-');

//		new_file.id=new_file.id.substring(0,new_file.id.length-1)+(id+1);	
		new_file.value='';
		
		if(new_photo_delete){
			new_photo_delete.id='photo_delete-'+(id+1);
			new_photo_delete.name='photo_delete['+(id+1)+']';
			new_photo_delete.checked=false;
			new_label=new_field.getElementsByTagName('label')[0];
			new_label.id='photo_delete-label-'+(id+1);
			new_label.setAttribute('for',new_photo_delete.id);
		}
		
		var new_legend=new_field.getElementsByTagName('legend')[0];
		var new_text=new_legend.firstChild;

		tmp = new_text.nodeValue.split('#');
		tmp[tmp.length-1]=(id+2);
		new_text.nodeValue=tmp.join('#');

//		new_text.nodeValue=new_text.nodeValue.substring(0,new_text.nodeValue.length-1)+(id+2);

	
	}else if(hasClass(last,'empty')){
		//if we're not changing the last node and the last one is empty, don't add another node
	}else{
		//if we're not changing the last node and the last one isn't empty, 
	}
	
	try{
      var reader = new FileReader();

      reader.onload = (function(file) {
        return function(e) {
          img.src=reader.result;
        };
      })(ele.files[0]);

      reader.readAsDataURL(ele.files[0]);
	}catch(err){
		img.src=ele.value;
	}
	
	var str=img.className;
	str=str.replace('-empty','');
	
	img.className=str;
	
//	img_info.style.height=(parent_img_wrapper.clientHeight-parseInt(img_info.style.paddingTop)-parseInt(photo_info.style.paddingBottom))+"px";
	
	show(img_info);
	
	if(last==img_field)
		img_field.parentNode.appendChild(new_field);
	
	window.location.href='#'+(!new_link ? last_link.name : new_link.name);
}

function getImageDataURL(url, success, error) {
	var data, canvas, ctx;
	var img = new Image();
	img.onload = function(){
		// Create the canvas element.
	    canvas = document.createElement('canvas');
	    canvas.width = img.width;
	    canvas.height = img.height;
		// Get '2d' context and draw the image.
		ctx = canvas.getContext("2d");
	    ctx.drawImage(img, 0, 0);
		// Get canvas data URL
		try{
			data = canvas.toDataURL();
			success({image:img, data:data});
		}catch(e){
			error(e);
		}
	}
	// Load image URL.
	try{
		img.src = url;
	}catch(e){
		error(e);
	}
}

function _log(){
	if(this.console){
		window.console.log( Array.prototype.slice.call(arguments) );
	}
/*
	if(!window.console)
		return false;
	console.log.apply(null,arguments);*/
}
