/**
 * @author Wingweb Communications
 */

var GrowFocus = {
	'growSize': null,
	'growSpeed': null,
	'growStep': 0,
	'growTimer': null,
	'growElement': null,
	'keepOnTop': 100,
	'breakChar': ',',
	'loadTimeout': null,
	'labelOpacity': 60, //%
	
	'init': function(growSize, growSpeed){
		this.growSize = growSize || 30;
		this.growSpeed = growSpeed || 5;
		
		GrowFocus.loadTimeout = setInterval(function(){
			GrowFocus.prepareImages();
		}, 750);
	},
	
	'prepareImages': function()
	{
		var tmpImageList = document.getElementsByTagName('img');

		for (var i = 0; i < tmpImageList.length; i++){
			if (tmpImageList[i].className.match(/^GrowFocus$|^.*\sGrowFocus$|^GrowFocus\s.*$|^.*\sGrowFocus\s.*$/gi)) {
			
				tmpImageList[i].onload = function(){
				
					var originalLayer = this;
					var wrapLayer = document.createElement('div');
					var labelLayer = null;
					var labelTextLayer = null;
					var touchLayer = document.createElement('div');
					var linkHiddenText = document.createElement('a');
					
					var offsetDifWidth = null;
					var offsetDifMargin = null;

					if (originalLayer.alt.length > 0) {
						labelLayer = document.createElement('div');
						labelTextLayer = document.createElement('div');
					}
					
					//Hijack the original layer
					originalLayer.parentNode.insertBefore(wrapLayer, originalLayer);
					wrapLayer.appendChild(originalLayer);
					
					// Append all layers in the given order
					if (originalLayer.alt.length > 0) {
						wrapLayer.appendChild(labelLayer);
						wrapLayer.appendChild(labelTextLayer);
					}
					
					if (wrapLayer.parentNode.tagName.toLocaleLowerCase() == 'a') {
						touchLayer = wrapLayer.parentNode;
						var grandParent = wrapLayer.parentNode.parentNode;
						grandParent.insertBefore(wrapLayer, touchLayer);
						wrapLayer.appendChild(touchLayer);
					}else
					{
						touchLayer = document.createElement('a');
						wrapLayer.appendChild(touchLayer);
					}
					
					// Wrap layer
					wrapLayer.style.display = 'block';
					GrowFocus.setDimensions(wrapLayer,
						originalLayer.offsetWidth, originalLayer.offsetHeight, 
						0, 0);
					
					// Original layer
					originalLayer.style.display = 'block';
					originalLayer.style.position = 'absolute';
					
					offsetDifWidth = Math.floor(originalLayer.offsetWidth - originalLayer.clientWidth);
					offsetDifMargin = Math.floor((originalLayer.offsetWidth - originalLayer.clientWidth) / 2);
					
					// Label layer
					if (originalLayer.alt.length > 0) {
						labelTextLayer.innerHTML = originalLayer.alt.split(GrowFocus.breakChar)[0];
						
						labelTextLayer.style.fontWeight = 'bold';
						labelTextLayer.style.display = 'block';
						labelTextLayer.style.position = 'absolute';
						labelTextLayer.style.overflow = 'hidden';
						labelTextLayer.style.textAlign = 'center';
						labelTextLayer.style.padding = '0';

						GrowFocus.setDimensions(labelTextLayer,
							originalLayer.clientWidth, null,
							(originalLayer.offsetWidth - originalLayer.clientWidth) / 2, (originalLayer.offsetHeight - labelTextLayer.offsetHeight) - offsetDifMargin);
						
						labelLayer.style.fontWeight = 'bold';
						labelLayer.style.display = 'block';
						labelLayer.style.position = 'absolute';
						labelLayer.style.backgroundColor = '#FFF';
						labelLayer.style.overflow = 'hidden';
						labelLayer.style.opacity = '.' + GrowFocus.labelOpacity;
						labelLayer.style.filter = 'alpha(opacity=' + GrowFocus.labelOpacity + ')';
						
						GrowFocus.setDimensions(labelLayer,
							originalLayer.clientWidth, labelTextLayer.offsetHeight,
							(originalLayer.offsetWidth - originalLayer.clientWidth) / 2, (originalLayer.offsetHeight - labelTextLayer.offsetHeight) - offsetDifMargin);
														
					}
					
					// Touch Layer
					touchLayer.style.display = 'block';
					touchLayer.style.position = 'absolute';
					touchLayer.style.cursor = 'pointer';
					touchLayer.style.backgroundColor = '#F00';
					touchLayer.style.opacity = .0;
					touchLayer.style.filter = 'alpha(opacity=0)';
					touchLayer.style.width = originalLayer.offsetWidth + 'px';
					touchLayer.style.height = originalLayer.offsetHeight + 'px';
					
					linkHiddenText.href = touchLayer.href;
					linkHiddenText.style.display = 'none';
					linkHiddenText.appendChild(document.createTextNode('(link)'));
					wrapLayer.appendChild(linkHiddenText);
					
					touchLayer.onmouseover = function(){
						var currentElm = this;
						var wrapLayer = this.parentNode;
						var originalLayer = wrapLayer.childNodes[0];
						var labelTextLayer = null;
						
						if (originalLayer.alt.length > 0) {
							labelTextLayer = wrapLayer.childNodes[2];
							
							labelTextLayer.innerHTML = originalLayer.alt.split(GrowFocus.breakChar)[0];
							
							if(originalLayer.alt.split(GrowFocus.breakChar)[1]){
								labelTextLayer.style.visibility = 'hidden';
								labelTextLayer.appendChild(document.createElement('br'));
								labelTextLayer.innerHTML += originalLayer.alt.split(GrowFocus.breakChar)[1];
	
								var labelLeft = ((originalLayer.offsetWidth - originalLayer.clientWidth) / 2);
								var labelTop = ((originalLayer.offsetHeight - labelLayer.clientHeight) - offsetDifMargin);
								
								GrowFocus.setDimensions(labelTextLayer, 
									null, null,
									labelLeft, labelTop);
									
								GrowFocus.setDimensions(labelLayer,
									null, labelTextLayer.offsetHeight,
									labelLeft, labelTop);
								
								labelTextLayer.style.visibility = 'visible';
							}
						}
						
						wrapLayer.style.zIndex = ++GrowFocus.keepOnTop;
						GrowFocus.growTimer = setInterval(function(){
							GrowFocus.growAction(currentElm);
						}, GrowFocus.growSpeed);
					};
					
					touchLayer.onmouseout = function(){
						var currentElm = this;
						var wrapLayer = this.parentNode;
						var originalLayer = wrapLayer.childNodes[0];
						var labelTextLayer = null;

						clearInterval(GrowFocus.growTimer);
						
						if (originalLayer.alt.length > 0) {
							labelTextLayer = wrapLayer.childNodes[2];
							labelTextLayer.innerHTML = originalLayer.alt.split(GrowFocus.breakChar)[0];
						}
						
						GrowFocus.cleanUp(currentElm);
					};
					
				}; //function
						
				if (tmpImageList[i].complete)
					tmpImageList[i].onload();
				
			}//e:if
		}//e:for
		clearInterval(GrowFocus.loadTimeout);
	},
	
	'initLabel':function(label, labelText){
		alert(label);
	},
	
	'growAction': function(touchLayer)
	{
		var wrapLayer = touchLayer.parentNode;
		var originalLayer = wrapLayer.childNodes[0];
		var labelLayer = null;
		var labelTextLayer = null;
		
		var offsetDifWidth = originalLayer.offsetWidth - originalLayer.clientWidth;
		var offsetDifMargin = Math.floor((originalLayer.offsetWidth - originalLayer.clientWidth) / 2);
		
		if(originalLayer.alt.length > 0){
			labelLayer = wrapLayer.childNodes[1];
			labelTextLayer = wrapLayer.childNodes[2];
		}
		
		if(GrowFocus.growStep > GrowFocus.growSize)
			GrowFocus.growStep = 0;
		
		var grownSize = Math.floor(Math.atan(GrowFocus.growStep / (GrowFocus.growSize / GrowFocus.growStep)) * GrowFocus.growSize);
		
		GrowFocus.setDimensions(touchLayer,
			wrapLayer.offsetWidth + grownSize, (wrapLayer.offsetWidth + grownSize) * (wrapLayer.clientHeight / wrapLayer.clientWidth),
			((touchLayer.clientWidth - wrapLayer.clientWidth) / 2) * -1, ((touchLayer.clientHeight - wrapLayer.clientHeight) / 2) * -1);
		GrowFocus.setDimensions(originalLayer,
			touchLayer.clientWidth - offsetDifWidth, touchLayer.clientHeight - offsetDifWidth,
			(((touchLayer.clientWidth) - (wrapLayer.clientWidth)) / 2) * -1, (((touchLayer.clientHeight) - (wrapLayer.clientHeight)) / 2) * -1);
		
		if (labelLayer != null) {
			var labelTop = ((originalLayer.offsetHeight - labelLayer.clientHeight) - offsetDifMargin) - Math.floor((touchLayer.clientHeight - wrapLayer.clientHeight) / 2);
			var labelLeft = ((grownSize / 2) - offsetDifMargin) * -1;

			GrowFocus.setDimensions(labelTextLayer,
				originalLayer.clientWidth, null,
				labelLeft, labelTop);
			GrowFocus.setDimensions(labelLayer,
				originalLayer.clientWidth, labelTextLayer.offsetHeight,
				labelLeft, labelTop);
		}
		
		if (++GrowFocus.growStep > GrowFocus.growSize)
			clearInterval(GrowFocus.growTimer);
	},
	
	'cleanUp': function(touchLayer){
		var wrapLayer = touchLayer.parentNode;
		var originalLayer = wrapLayer.childNodes[0];
		var labelLayer = null;
		var labelTextLayer = null;
	
		var offsetDifWidth = Math.ceil(originalLayer.offsetWidth - originalLayer.clientWidth);
		var offsetDifMargin = Math.ceil((originalLayer.offsetWidth - originalLayer.clientWidth) / 2);
		
		GrowFocus.growStep = 0;
		
		if(originalLayer.alt.length > 0){
			labelLayer = wrapLayer.childNodes[1];
			labelTextLayer = wrapLayer.childNodes[2];
		}
		
		GrowFocus.setDimensions(touchLayer,
			wrapLayer.clientWidth, wrapLayer.clientHeight,
			0, 0);
		GrowFocus.setDimensions(originalLayer,
			wrapLayer.clientWidth - offsetDifWidth, wrapLayer.clientHeight - offsetDifWidth,
			0, 0);
		
		if (labelLayer){
			var labelTop = ((originalLayer.offsetHeight - labelTextLayer.clientHeight) - offsetDifMargin);
			var labelLeft = (originalLayer.offsetWidth - originalLayer.clientWidth) / 2;
			
			GrowFocus.setDimensions(labelTextLayer,
				originalLayer.clientWidth, null,
				labelLeft, labelTop);
			GrowFocus.setDimensions(labelLayer,
				labelLayer.clientWidth, labelTextLayer.offsetHeight,
				labelLeft, labelTop);
		}
	},
	
	'setDimensions': function(elm, newWidth, newHeight, newLeft, newTop){
		newWidth = Math.floor(newWidth) || null;
		newHeight = Math.floor(newHeight) || null;
		newTop = Math.floor(newTop) || '0';
		newLeft = Math.floor(newLeft) || '0';
		
		if(newWidth != null)
			elm.style.width = newWidth + 'px';
		if(newHeight != null)
			elm.style.height = newHeight + 'px';
			
		elm.style.margin = newTop + 'px 0px 0px ' + newLeft + 'px';
	}
	
};