/**
 *	 Copyright 2005 Componence. All rights reserved.
 *	 Last change date: 27-04-2005 21:07:14
 *
 */

/* Extends */
JSAttachmentPlaceConfig.prototype = new JSComponentConfig();

/* Global Variables */
var AttachmentPlaceConfig = new JSAttachmentPlaceConfig();




function /*:JSFactory:*/ JSAttachmentsFactory() {
	this.version = "1.1.20";
	
	/* Required classes */
	if ( !Global.required( "JSTemplate", "JSArrayList", "JSHashMap", "JSDialog" ) ) {
		this.createPlace      = function(){};
		this.removeAttachment = function(){};
		this.addAttachment    = function(){};
		return null;
	}
	this.places = [];
	this.attachmentWindow = null;
	
	/**
	 * @public
	 */
	function /*:AttachmentPlace:*/ createPlace( /*:String:*/ templateBlockName, /*:Structure:*/ attachment ) {
		var /*:place:*/ p = new JSAttachmentPlace( templateBlockName, attachment );
		this.places.push(p);
		return p;
	}
	this.createPlace = createPlace;
	
	/**
	 * @public
	 */
	function /*:void:*/ removeAttachment( name ) {
		for ( var p = 0; p < this.places.length; p++ )
			this.places[p].removeAttachment( name );
	}
	this.removeAttachment = removeAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ removeAllAttachment() {
		this.currentPlace.removeAllAttachment();
	}
	this.removeAllAttachment = removeAllAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ openAttachmentWindow( /*:String:*/ placeName ) {
		var /*:place:*/ p = this.getPlace( placeName );
		if ( p ) {
			this.currentPlace = p;
			p.openAttachmentWindow();
		}
	}
	this.openAttachmentWindow = openAttachmentWindow;
	
	/**
	 * @public
	 */
	function /*:void:*/ addAttachment( attachment ) {
		this.currentPlace.addAttachment( attachment );

	}
	this.addAttachment = addAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ synchronize( attachments ) {
		this.currentPlace.synchronize ( attachments );
	}
	this.synchronize = synchronize;
	
	/**
	 * @public
	 */
	function /*:Object:*/ getPlace ( /*:String:*/ placeName ) {
		for ( var p = 0; p < this.places.length; p++ )
			if ( this.places[p].placeName == placeName )
				return this.places[p];
		return null;
	}
	this.getPlace = getPlace;
}
var /*:Object:*/ AttachmentPlace = new JSAttachmentsFactory();

/**
 * @constructor
 */
function /*:JSClass:*/ JSAttachmentPlace( /*:String:*/ templateBlockName, /*:Structure:*/ attachment ) {
	this.version = "1.1.20";
	
	/* Required classes */
	if ( !Global.required( "JSTemplate", "JSArrayList", "JSHashMap", "JSDialog" ) ) {
		this.create = function(){};
		return null;
	}
	/* Creates new template and loads it from HTML file */
	this.template = new JSTemplate();
	this.template.loadTemplateContent( "main", AttachmentPlaceConfig.scriptPath + "" + AttachmentPlaceConfig.htmlFile, true );
	
	this.attachment = attachment;
	this.templateBlockName = templateBlockName;
	this.emptyHTML = "";
	
	this.vars = ["name", "size", "type", "previewURL", "downloadURL", "deleteURL", "removeAttachment", "addAttachment", "openAttachWindow"];
	this.uniqueId = new Date().getTime();
	this.placeName = "AttachmentPlace" + this.uniqueId;
	
	/**
	 * @public
	 */
	function create() {
		if ( this.attachment.instanceOf( Array ) ) {
			var /*:document:*/ d = document;
			d.write( "<div id='" + this.placeName + "' style='border:solid 0px red'>" );
			var tpl = this.template;
			/*
			tpl.parse( this.templateBlockName + "AttachmentListEmpty" );
			this.emptyHTML = "<span id='" + this.placeName + "_empty' style='border:solid 0px red'>"+tpl.printVar( this.templateBlockName + "AttachmentListEmpty" )+"</span>";
			*/
			
			if ( this.attachment.length > 0 && !this.attachment[ this.attachment.length - 1 ].name )
				this.attachment.pop();

			if ( this.attachment.length == 0 ) {
				tpl.clearVar( this.templateBlockName + "AttachmentList" );
				tpl.parse( this.templateBlockName + "AttachmentListEmpty", false );
				this.emptyHTML = tpl.printVar( this.templateBlockName + "AttachmentListEmpty" );
			} else {
				tpl.parse( this.templateBlockName + "AttachmentListEmpty", false );
				this.emptyHTML = tpl.printVar( this.templateBlockName + "AttachmentListEmpty" );
				tpl.clearVar( this.templateBlockName + "AttachmentListEmpty" );
				
				var items = "";
				for ( var a = 0; a < this.attachment.length; a++ ) {
					var att = this.attachment[a];
					
					for ( var i in att ) {
						if ( i == "size" )
							att[i] = this.formatBytes( att[i] );
						tpl.setVar( i, att[i] );
					}
					
					tpl.setVar( "removeAttachment", "AttachmentPlace.removeAttachment(\"" + att.name + "_" + this.placeName + "\")" );
					
					tpl.parse( this.templateBlockName + "AttachmentListItem", false );
					
					/* Clearing not parsed vars */
					for ( var v = 0; v < this.vars.length; v++)
						tpl.clearVar( this.vars[v] );
					
					/* get attachments */
					items +=  "<span name='" + att.name + "_" + this.placeName + "' id="+att.id+">" + tpl.printVar( this.templateBlockName + "AttachmentListItem" ) + "</span>";
				}
				tpl.setVar( this.templateBlockName + "AttachmentListItem", "<SPAN style='border:solid 0px green'>" + items + "</SPAN>" );
				tpl.parse( this.templateBlockName + "AttachmentList", false );

			}
			tpl.setVar( "openAttachWindow", "AttachmentPlace.openAttachmentWindow(\"" + this.placeName + "\" )" );
			tpl.parse( this.templateBlockName, false );
			d.write( tpl.printVar( this.templateBlockName ) );
			d.write( "</div>" );
		} else
			this.usage();
	}
	this.create = create;
	
	/**
	 * @public
	 */
	function /*:HTMLElement:*/ getPlaceElement() {
		return Global.getObject( "AttachmentPlace" + this.uniqueId );
	}
	this.getPlaceElement = getPlaceElement;
	
	/**
	 * @public
	 */
	function /*:void:*/ addAttachment( attachment ) {
		var newName = attachment.name + "_" + this.placeName;
		var tpl = this.template;
		
		for ( var i in attachment ) {
			if ( i == "size" )
				attachment[i] = this.formatBytes( attachment[i] );
			tpl.setVar( i, attachment[i] );
		}
		tpl.setVar( "removeAttachment", "AttachmentPlace.removeAttachment(\"" + attachment.name + "_" + this.placeName + "\")" );
		tpl.parse( this.templateBlockName + "AttachmentListItem", false );
		
		/* Clearing not parsed vars */
		for ( var v = 0; v < this.vars.length; v++ )
			tpl.clearVar( this.vars[v] );
		
		var span = document.createElement( "SPAN" );
		span.name = newName;
		span.innerHTML = tpl.printVar( this.templateBlockName + "AttachmentListItem" ) ;
		var place = this.getPlaceElement();
		if ( place && place.childNodes && place.childNodes[0] ) {
			var emptyId = this.placeName + "_empty";
			var placeChildNode = (place.childNodes[0].tagName)?place.childNodes[0]:place.childNodes[1]
			if ( placeChildNode.id == emptyId )
				placeChildNode.removeNode( true );

			var itemContainer = placeChildNode;
			var fileExistsMessage = "File '" + attachment.name + "' already exists!\t";
			for (var i = 0; i < itemContainer.childNodes.length; i++ ) {
				if ( itemContainer.childNodes[i] && itemContainer.childNodes[i].getAttribute ) {
					var existsName = itemContainer.childNodes[i].getAttribute( "name" );
					if ( newName == existsName ) {
						if ( this.attachmentWindow )
							this.attachmentWindow.alert( fileExistsMessage );
						else
							alert( fileExistsMessage );
						return null;
					}
				}
			}
			if ( AttachmentPlaceConfig.append )
				itemContainer.appendChild( span );
			else
				itemContainer.insertBefore( span, itemContainer.firstChild );
		}
	}
	this.addAttachment = addAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ removeAttachment( /*:String:*/ name ) {
		var /*:place:*/ p = this.getPlaceElement();
		var placeChildNode = (p.childNodes[0].tagName)?p.childNodes[0]:p.childNodes[1]
		if ( p && p.childNodes && placeChildNode && placeChildNode.childNodes ) {
			var /*:kids:*/ k = placeChildNode.childNodes;
			for ( var c = 0; c < k.length; c++ ) {
				if ( k[c] ) {
					var /*:childName:*/ n = k[c].getAttribute( "name" );
					if ( name == n )
						k[c--].removeNode( true );
				}
			}
			if ( k.length == 0 )
				p.innerHTML = this.emptyHTML + p.innerHTML;
		}
	}
	this.removeAttachment = removeAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ removeAllAttachment() {
		var /*:place:*/ p = this.getPlaceElement();
		var placeChildNode = (p.childNodes[0].tagName)?p.childNodes[0]:p.childNodes[1]
		if ( p && p.childNodes && placeChildNode && placeChildNode.childNodes ) {
			var /*:kids:*/ k = placeChildNode.childNodes;
			for ( var c = 0; c < k.length; c++ )
				if ( k[c] )
					k[c--].removeNode( true );
			if ( k.length == 0 )
				p.innerHTML = this.emptyHTML + p.innerHTML;
		}
	}
	this.removeAllAttachment = removeAllAttachment;
	
	/**
	 * @public
	 */
	function /*:void:*/ synchronize ( attachments ) {
		var /*:attachments:*/ att = attachments;
		var /*:place:*/ p = this.getPlaceElement();
		var placeChildNode = (p.childNodes[0].tagName)?p.childNodes[0]:p.childNodes[1]
		if ( p && p.childNodes && placeChildNode && placeChildNode.childNodes ) {
			var /*:kids:*/ k = placeChildNode.childNodes;
			if ( k ) {
				for ( var c = 0; c < k.length; c++ ) {
					if ( k[c] && k[c].getAttribute ) {
						var /*:childName:*/ cn = k[c].getAttribute( "name" );
						var /*:itemExists*/ e = false;
						for ( var a = 0; a < att.length; a++ ) {
							var /*:newName:*/ n = att[a].name + "_" + this.placeName;
							if ( cn == n ) {
								e = true;
								break;
							}
						}
						if ( !e )
							k[c--].removeNode( true );
					}
				}
				
				for ( var a = 0; a < att.length; a++ ) {
					var /*:newName:*/ n = att[a].name + "_" + this.placeName;
					var /*:itemExists*/ e = false;
					for ( var c = 0; c < k.length; c++ ) {
						if ( k[c] && k[c].getAttribute ) {
							var /*:childName:*/ cn = k[c].getAttribute( "name" );
							if ( cn == n ) {
								e = true;
								break;
							}
						}
					}
					if ( !e )
						this.addAttachment( att[a] );
				}
				
				if ( k.length == 0 )
					p.innerHTML = this.emptyHTML + "" + p.innerHTML;
				else
					p.innerHTML = String( p.innerHTML ).replace( this.emptyHTML, "" );

				var children = p.getElementsByTagName("P");
				if(children.length!=0){
					for (var d=0; d<children.length; d++){
						if (children[d].lastChild.tagName && children[d].lastChild.tagName=="A"){
							var aObj = children[d].lastChild;
							aObj.setAttribute("href",(aObj.lastChild)?aObj.lastChild.getAttribute("downloadURL"):"");
						}
					}
				}
			}
		}
	}
	this.synchronize = synchronize;
	
	/**
	 * @public
	 */
	function /*:void:*/ usage() {
		alert("Error!\t");
	}
	this.usage = usage;
	
	/**
	 * @public
	 */
	function /*:void:*/ openAttachmentWindow() {
		this.attachmentWindow = Dialog.showDialog( AttachmentPlaceConfig.attachmentManagerURL, AttachmentPlaceConfig.attachmentManagerOpenParams );
		AttachmentPlace.attachmentWindow = this.attachmentWindow;
		this.attachmentWindow.owner = AttachmentPlace;
	}
	this.openAttachmentWindow = openAttachmentWindow;
	
	/**
	 * @public
	 */
	function /*:String:*/ formatBytes( /*:Variant:*/ bytes ) {
		var /*:bytes:*/ b = parseInt( bytes );
		var q = "B";
		if ( b > 1024 ) {
			b = b / 1024;
			q = "KB";
		}
		if ( b > 1024 ) {
			b = b / 1024;
			q = "MB";
		}
		b = String( b );
		var /*:dot:*/ d = b.indexOf( "." );
		if ( d != -1 ) {
			b = b.replace( /\./, "," );
			b = b.substr( 0, d + 3  );
		}
		return b + "&nbsp;" + q;
	}
	this.formatBytes = formatBytes;
	
	this.create( templateBlockName, attachment );
}