Bw.Widgets.Filefield =
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Filefield",
	
	create: function ()
	{
		var obj = document.createElement ("div");
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Filefield");
		
		return obj;
	},

	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);
		
		this.csspath = this.getAttribute("csspath");
		this.uploadDir = this.getAttribute("uploadDir");
		this.modified = false;
		
		this.action = this.getAttribute("action") + "?uploadDir=/.." + this.uploadDir;
		
		this.initvalue = null;
		this.value = null;
		
		//this.draw();
	},
	
	draw: function (newstyle)
	{
		/*
		while(this.firstChild != null )
		{
			this.removeChild(this.firstChild);	
		}
		*/
		
		if( this.firstChild != null )
		{
			this.innerHTML = "";	
		}
		
		var f = document.createElement ("iframe");
		f.frameBorder = 0;
		
		//this.loading = document.createElement ("div");
		//this.loading.style.styleFloat = this.loading.style.cssFloat = "left";
		//this.loading.innerHTML =  '<img src="' + contextPath + '/common/cms/img/loading.gif">';
		//this.loading.style.display = "none";
		
		with (f.style)
		{
			var w = this.style.width;
			var h = this.style.height;
		
			width = (w != "") ? w : "300px";
			height = (h != "") ? h : "30px";
			
			styleFloat = cssFloat = "left";
			
		}		
		
		//this.appendChild(this.loading);
		
		this.appendChild (f);
		this.canvas = f;
		
		if( newstyle )
		{
			this.renderField();
		}
		else
		{
			this.renderFieldOld();
		}
		
	},
	
	renderFieldOld: function()
	{
		var d = this.getEditedDocument();
		
		d.open();
		
		
		var name = this.initvalue;
		
		while( name != null && name.indexOf("/") > -1 )
		{
			name = name.substring( name.indexOf("/") + 1, name.length);	
		}
		
		var filename = name;
		
		if( name != null && name.length > 30 )
		{
			name = name.substring(0, 30) + "...";
		}
		
		d.writeln ('<html><head>');
		d.writeln ('<meta http-equiv="Content-Type" content="text/html; charset=' + (document.charset || document.characterSet) + '">');
		d.writeln ('<meta http-equiv="Pragma" content="no-cache">');
		d.writeln ('<meta http-equiv="Cache-Control" content="no-cache">');
		if( document.all )
		{
			d.writeln ('<link href="' + this.csspath + '" rel="stylesheet" type="text/css">');
		}
		d.writeln ('</head><body style="margin:0px;padding:0px;" class="bwfilefieldbody">');
	
		this.canvas.style.height = "45px";
		this.canvas.style.width = "300px";
		this.canvas.style.overflow = "hidden";
		
		d.writeln ('<form name="file" action="' + this.action + '" method="post" enctype="multipart/form-data">');
		
		if( this.initvalue != null && this.initvalue.length > 0 )
		{
			d.writeln ('<a class="bwfilefieldlink" target="_blank" title="' + this.initvalue + '" href="' + this.uploadDir + '/' + this.initvalue + '">' + name + '</a>&nbsp;<input id="delete_' + this.id + '" type="checkbox" onclick="window.parent.Bw.Widgets.Filefield.deleteFile(\'' + this.id + '\', \'' + filename + '\', this);"><label class="bwlabel" for="delete_' + this.id + '">Supprimer</label>');
		}
		else
		{
			d.writeln('<div style="clear: both;">&nbsp;</div>');
		}
		
		d.writeln ('<input class="bwfilefield" name="myfile" type="file">');
		d.writeln ('<input name="id" type="hidden" value="' + this.id + '">');
		d.writeln ('</form>');
		d.writeln ('</body></html>');
		
		d.close();	
		
		if( !document.all )
		{
			var html = d.documentElement;
			var head = html.firstChild;
			var link = document.createElement ("LINK");
			link.rel="StyleSheet";
			link.href=this.csspath;
			head.appendChild (link);
		}
	},
	
	renderField: function()
	{
		
		var d = this.getEditedDocument();
		
		var name = this.initvalue;
		
		while( name != null && name.indexOf("/") > -1 )
		{
			name = name.substring( name.indexOf("/") + 1, name.length);	
		}
		
		var filename = name;
		
		if( name != null && name.length > 30 )
		{
			name = name.substring(0, 30) + "...";
		}
		
		var s = '';
		s += ('<html><head>');
		s +=  ('<meta http-equiv="Content-Type" content="text/html; charset=' + (document.charset || document.characterSet) + '">');
		s += ('<meta http-equiv="Pragma" content="no-cache">');
		s += ('<meta http-equiv="Cache-Control" content="no-cache">');
		if( document.all )
		{
			s += ('<link href="' + this.csspath + '" rel="stylesheet" type="text/css">');
		}
		s += ('</head>');
		
		//d.head.innerHTML = s;
		
		//s = ''<body style="margin:0px;padding:0px;" class="bwfilefieldbody">');
	
		this.canvas.style.height = "45px";
		this.canvas.style.width = "300px";
		this.canvas.style.overflow = "hidden";
		s  = '';
		s += ('<form name="file" action="' + this.action + '" method="post" enctype="multipart/form-data">');
		
		if( this.initvalue != null && this.initvalue.length > 0 )
		{
			s += ('<a class="bwfilefieldlink" target="_blank" title="' + this.initvalue + '" href="' + this.uploadDir + '/' + this.initvalue + '">' + name + '</a>&nbsp;<input id="delete_' + this.id + '" type="checkbox" onclick="window.parent.Bw.Widgets.Filefield.deleteFile(\'' + this.id + '\', \'' + filename + '\', this);"><label class="bwlabel" for="delete_' + this.id + '">Supprimer</label>');
		}
		else
		{
			s += ('<div style="clear: both;">&nbsp;</div>');
		}
		
		s += ('<input class="bwfilefield" name="myfile" type="file">');
		s += ('<input name="id" type="hidden" value="' + this.id + '">');
		s += ('</form>');
		s += ('</body></html>');
		
		d.body.innerHTML = s;	
		
		if( !document.all )
		{
			var html = d.documentElement;
			var head = html.firstChild;
			var link = document.createElement ("LINK");
			link.rel="StyleSheet";
			link.href=this.csspath;
			head.appendChild (link);
		}
	},
	
	getEditedDocument: function ()
	{
		return this.getEditedWindow().document;
	},
	
	getEditedWindow: function ()
	{
		return this.canvas.contentWindow;
	},
	
	setValue: function(value, newstyle)
	{
		this.initvalue = value;
		this.value = null;
		
		this.draw(newstyle);	
		
		this.sent = null;
		this.mustDelete = null;
	},
	
	getValue: function()
	{
		if( this.value == null )
		{
			if( this.mustDelete )
			{
				return "";	
			}
			else
			{
				return this.initvalue;	
			}
		}
		
		return this.value;
		
	},
	
	send: function(callback)
	{
		var d = this.getEditedDocument();
		
		if( d.file.myfile.value.length == 0 )
		{
			return false;	
		}
		
		this.sent = true;
		
		this.callback = callback;
		
		this.startLoading();
		
		d.file.submit();
		
		return true;
	},
	
	uploadFinished: function(id, value)
	{
		var self = window.parent.document.getElementById(id);
		
		if( self == null )
		{
			return;	
		}
		
		self.stopLoading();
		
		self.setValue(value, this.isNewStyle());
		
		self.modified = true;
		
		self.callback();
	},
	
	deleteFile: function(id, file, checkbox)
	{
		var self = window.parent.document.getElementById(id);
		
		if( self == null )
		{
			return;	
		}
		
		if( !checkbox.checked )
		{
			self.mustDelete = null;	
			return;
		}
		
		self.mustDelete = file;
	},
	
	startLoading: function()
	{
		/*
		if( this.loading.firstChild != null )
		{
			this.loading.removeChild(this.loading.firstChild);	
		}
		
		var i = document.createElement("image");
		
		i.src = contextPath + "/common/cms/img/loading.gif";
		
		this.loading.appendChild( i );	
		*/
		
		//this.loading.style.display = "inline";
	},
	
	stopLoading: function()
	{
		/*
		if( this.loading.firstChild != null )
		{
			this.loading.removeChild(this.loading.firstChild);	
		}
		*/
		
		//this.loading.style.display = "none";	
	},
	
	isNewStyle: function()
	{
		var navigator = window.navigator;
		var userAgent = navigator.userAgent;
		var isFirefox = (userAgent.indexOf("Firefox/") >= 0);
		
		if (isFirefox)
		{
			var version      = parseFloat (navigator.appVersion);
			var majorVersion = -1;
			var reg          = new RegExp ("Firefox" + '/?\\s?;?([\\d.]*)');
			var res          = userAgent.match (reg);

			if (res.length == 2)
			{
				version       = res[1];
				res           = version.match (/\d+/g);
				majorVersion  = (res[0] ? parseInt(res[0]) : 0);
				minorVersion  = (res[1] ? parseInt(res[1]) : 0);
				minorVersion2 = (res[2] ? parseInt(res[2]) : 0);
			}
			
			if (majorVersion == -1)
			{
				majorVersion = version;
			}
			
			return (majorVersion >= 3);
		}
		
		return false;
	}
}

