/**
 + ===================================================================================
 * column widget, multioption 
 + ================================================================================ */
lawidget = new Object();
lawidget.columnWidget = function(setId){
	this.styles = new Array();
	this.marginTop = "margin-top: 0px;";


	this.setId = setId;
	this.addTitle('title' + this.setId);
	this.addText("text" + this.setId);
	this.addBorderColor("borderColor" + this.setId);
	
	this.addBackgroundColor("backGroundColor" + this.setId);
	this.addBackgroundImage("backGroundImage" + this.setId);
	
	this.addColorCode("colorCode" + this.setId);
	

	this.addLink("link" + this.setId);
	this.addLinkTarget("linkTarget" + this.setId);
	this.addDisplayMode("displayMode" + this.setId);
	
}

lawidget.columnWidget.prototype = {
	//public methods
	addTitle:			function(titleDiv){
		var elem = this.getElement(titleDiv);
		this.title = elem.text;
		
	},
	addText:			function(textDiv){
		var elem = this.getElement(textDiv);
		this.text = elem.text;
	},
	getBorder:			function(){
		return (this.borderColor?'border: solid #' + this.borderColor  + ' 1px; ':'');
	},


	addBorderColor:		function(borderColorDiv){
		var elem = this.getElement(borderColorDiv);
		this.borderColor = elem.text;
	},
	addBackgroundColor:	function(backgroundColorDiv){
		var elem = this.getElement(backgroundColorDiv);
		this.backgroundColor = elem.text;
	},
	
	addColorCode:		function(colorCodeDiv){
		var elem = this.getElement(colorCodeDiv);
		

		if(elem.text){
			switch(elem.text){
				//-1 = default.;
				case '-1':
					this.borderColor 		= '';
					this.backgroundColor 	= '';
					this.setStyle('TEXT_PADDING', 'flexelemRIGHTText');
					this.setStyle('TEXT_NO_PADDING', 'flexelemRIGHTTextNoPadding')
					break;
			}
		}else{
			this.borderColor 		= '';
			this.backgroundColor 	= '';
		}
	},
	addBackgroundImage:	function(backgroundImageDiv){
		var elem = this.getElement(backgroundImageDiv, 'img');
		this.backgroundImageId = 'img' + backgroundImageDiv;
		this.backgroundImage = elem.object;
	},
	addLink:			function(linkDiv){
		var elem = this.getElement(linkDiv, 'a');
		if(elem.object){
			this.link = elem.object.href;
			this.linkName = elem.object.innerHTML;
			this.linkTarget = elem.object.target;
		}else{
			this.link = elem.text;
		}
	},
	addDisplayMode:		function(displayModeDiv){
		var elem = this.getElement(displayModeDiv);
		this.displayMode = elem.text;
	},
	addLinkTarget:		function(linkTargetDiv){
		//var elem = this.getElement(linkTargetDiv);
		//this.linkTarget = elem.text;
	},
	setStyle:			function(type, style){
		if(style && (type == 'TEXT_PADDING' || type == 'TEXT_NO_PADDING')){
			this.styles[type] = style;
		}
	},
	getStyle:			function(type){
		style = "";
		if(type == 'TEXT_PADDING' || type == 'TEXT_NO_PADDING'){
			style = this.styles[type]?this.styles[type]:'';
			if(!style){
				style = ((type == 'TEXT_PADDING')?'widgettextpadding':'widgettextnopadding');
			}
		}
		return style;
	},
	getTitleStyle:		function(type){
		return this.styleTitle?this.styleTitle:'flexelemRIGHTTitle';
	},
	setMarginTop:		function(marginTop){
		if(marginTop!=null){
			this.marginTop = 'margin-top: ' + marginTop + 'px;';
		}	
	},
	getMarginTop:		function(){
		return this.marginTop;
	},
	getBackgroundColor:	function(){
		return (this.backgroundColor?('background-color:#' + this.backgroundColor + ';'):'');
	},
	
	disableLinkOverImage:function(disable){
		this.disableLinkOverImage = disable;
	},
	
	enableDebug:function(enable){
		this.enableDebug = 0;
	},
	
	//private methods
	getElement:			function(div, tag){
		var toReturn = new Object();
		var elem = document.getElementById(div);
		if(elem){
			toReturn.text = elem.innerHTML;
			if(tag){
				var tmpElems = elem.getElementsByTagName(tag);
				if(tmpElems){
					toReturn.object = tmpElems[0];
				}
			}			
		}

		//alert(toReturn.text);
		return toReturn;
	},

	// ------------------------------------------------------
	// hard code the class to use for the different possitions
	// ------------------------------------------------------
	setLocationParameters: function(){
		//flex element in right
		if(this.LOCATION){
			this.LocationStyleTop 		= 'flexelem' + this.LOCATION + 'Top';
			this.LocationStyleBottom 	= 'flexelem' + this.LOCATION + 'Bottom';
			this.LocationStyleBG 		= 'flexelem' + this.LOCATION + 'BG';
			this.LocationStyleLink 		= 'flexelem' + this.LOCATION + 'Link';


			this.LocationStyleIMGCont	= 'flexelem' + this.LOCATION + 'IMGCont';
			this.LocationStyleIMG		= 'flexelem' + this.LOCATION + 'IMG';
			this.LocationStyleIMGLink	= 'flexelem' + this.LOCATION + 'IMGLink';

		}
	},

	show:				function(defaultWidth, location){
		this.WIDTH = (defaultWidth?defaultWidth+'px':"250px");
		
		//define the location
		this.LOCATION = (location?location:'RIGHT');
		this.setLocationParameters();
		
	
		var toWrite 		= "";
		var text 			= "";

		//define the text
		if(this.title || this.text){
			//display the text widh padding, only if:
			//1.- the text will be displayed over the image
			//2.- border exists
			
			var textClass = this.getStyle('TEXT_NO_PADDING');
			if(
				this.getBorder() 
				|| (this.backgroundImage && this.backgroundImage.src && this.displayMode == 'OVER')
				|| (this.LOCATION && !this.backgroundImage)
			){
				var textClass = this.getStyle('TEXT_PADDING');
			}
			
			
			var goLink=''
			if(this.link!='' && (this.linkName!=null & this.linkName!='')){
				//goLink='<a href="' + this.link + '" class="' + this.LocationStyleLink + '">' + this.linkName + '</a>';
				goLink='<a href="' + this.link + '" style="float: right; margin-right: 10px;border: 0px; margin-top: -2px;"><img src="./images/dot.gif" border="0" width="18"></a>';
			}
			
			
			
			text =	'<div class="' + textClass + '">' + 
						//display the title only when it is present
						goLink + 
						(this.title?'<font class="' + this.getTitleStyle() + '">' + this.title + '</font><br\><br\>':'') +  
						this.text + 
					'</div>';
		}

		//link definition
		var linkBefore="", linkAfter="", linkOnClick="";
		if(this.link){	
			linkBefore = '<a href="' + this.link + '" target="' + this.linkTarget + '" class="' + this.LocationStyleIMGLink  + '">';
			//linkBefore = '<a href="' + this.link + '" target="' + this.linkTarget + '">';
			linkAfter = '</a>';
			linkOnClick = ' onclick="document.location=\'' + this.link + '\'" ';
		}

		//there is one image
		if(this.backgroundImage){
			var goLink=''
			if(this.link!='' && (this.linkName!=null & this.linkName!='') && (this.title || this.text)){
				goLink='<a href="' + this.link + '" class="' + this.LocationStyleLink + '" target="' + this.linkTarget + '">' + this.linkName + '</a>';
			}
		
		
		
			//define the positions
			if(this.backgroundImage.src && this.displayMode == 'OVER'){
				if(goLink==''){
					linkOnClick = this.disableLinkOverImage?'':linkOnClick;
				}else{
					linkOnClick = '';
				}
				
				
				toWrite =	'<div ' + linkOnClick + 'style="' + 
								this.getBackgroundColor() + 
								this.getBorder() + 
								'width: ' + this.WIDTH + '; ' +
								'overflow: hidden; ' +
								
								'background-image: url(' + this.backgroundImage.src + '); ' + 
								'background-position: 0px 0px; ' +
								'background-repeat: no-repeat; ' +
								this.getMarginTop() +
							'">' + 
								text + 
								(goLink?goLink:'') + 
							'</div>';
			}else{
			
				//define the image
				var image 			= "";
				if(goLink==''){
					image =	'<div class="' + this.LocationStyleIMGCont + '">' + 
								linkBefore + 
									'<img src="' + this.backgroundImage.src + '" id="' + this.backgroundImageId + '" class="' + this.LocationStyleIMG + '" border="0" title="'+this.backgroundImage.title+'" alt="'+this.backgroundImage.alt+'"></img>' + 
								linkAfter +
							'</div>';
				}else{
					image =	'<div class="' + this.LocationStyleIMGCont + '">' + 
								'<img src="' + this.backgroundImage.src + '" id="' + this.backgroundImageId + '" class="' + this.LocationStyleIMG + '" border="0" title="'+this.backgroundImage.title+'" alt="'+this.backgroundImage.alt+'"></img>' + 
							'</div>';
							
					text = text + goLink;
				}

				
				var first			= "";
				var second			= "";
				
				if(this.backgroundImage.src && this.displayMode == 'AFTER'){
					first = image;
					second = text;
				}else if(this.backgroundImage.src && this.displayMode == 'BEFORE'){
					first = text;
					second = image;
				}

				toWrite =	'<div style="' + 
								this.getBackgroundColor() + 
								this.getBorder() + 
								'width: ' + this.WIDTH + '; ' +
								'overflow: hidden; ' +
								'float: left; ' +
								this.getMarginTop() +
							'">' + 
								first + 				
								second + 				
							'</div>';
							
							
			}
		//there is no image
		}else{
			linkOnClick = this.disableLinkOverImage?'':linkOnClick;
			
			if(this.LOCATION){
				//define the link
				/*
				var goLink=''
				if(this.link!='' && (this.linkName!=null & this.linkName!='')){
					//goLink='<a href="' + this.link + '" class="' + this.LocationStyleLink + '">' + this.linkName + '</a>';
					goLink='<a href="' + this.link + '"><img src="./images/dot.gif" border="1" width="10"></a>';
				}
				*/
				
				toWrite =	'<div ' + linkOnClick + ' class="' + this.LocationStyleBG + '">' + 
								'<div class="' + this.LocationStyleTop + '"></div>' + 
								'<div>' + 
								text +
								//(goLink?goLink:'') + 
								'</div>' + 
								'<div class="' + this.LocationStyleBottom + '"></div>' + 
							'</div>';
							
				//alert(text);							
			}else{
				toWrite =	'<div ' + linkOnClick + 'style="' + 
								this.getBackgroundColor() + 
								this.getBorder() + 
								'width: ' + this.WIDTH + '; ' +
								'overflow: hidden; ' +
								this.getMarginTop() +
							'">' + 
								text +
							'</div>';
			}
			
			
		}


		//alert(toWrite);
		
		document.write(toWrite);
	}

}



