var FrameworkTools = {
	
	initialize: function() {
		FrameworkTools.gallery.initialize();
		FrameworkTools.gallery2.initialize();
		FrameworkTools.imprint.initialize();
	},
	
	imprint: {
		toggled: false,
		initialize: function() {
			var self = FrameworkTools.imprint;
			var box1 = $('imprintBox');
			var box2 = $('imprintSlideBox');
			var link = $('imprintLink');
			var amount = 463;
			var effect;
			
			var move = function(target) {
				if (effect) effect.cancel();
				effect = new Effect.Morph(box2, {
					style: 'margin-top:' + target + 'px;',
					duration: 0.8,
					transition: Effect.Transitions.sinoidal,
					afterFinish: function() {
						if (box2.style.marginTop == amount + 'px') {
							self.toggled = false;
							box1.hide();
						} else self.toggled = true;
					}
				});
			};
			
			link.observe('mouseover', function() {
				box1.show();
				move(0);
			});
			
			box1.observe('mouseout', function(e) {
				var element;
				
				if (element = e.relatedTarget) {
					do if (element == this) return;
					while (element = element.parentNode);
				}
				move(amount);
			});
			
			box1.hide().removeClassName('hiddenBox');
			box2.setStyle({ marginTop: amount + 'px' });
		}
	},
	
	gallery: {
		container: null,
		items: [],
		itemIndex: 0,
		initialize: function() {
			FrameworkTools.gallery.itemIndex = 0;
			var container = FrameworkTools.gallery.container = $('gallery');
			var items = FrameworkTools.gallery.items = container.select('.galleryCollection');
			for (var i = 0; i < items.length; items++) {
				items[i].style.left = '0px';
				items[i].style.top = '0px';
				items[i].style.display = (i == 0 ? 'block' : 'none');
			}
		},
		navigate: function(by) {
			var newIndex = Math.max(0, Math.min(FrameworkTools.gallery.itemIndex + by, FrameworkTools.gallery.items.length-1));
			if (newIndex != FrameworkTools.gallery.itemIndex) {
				var oldItem = FrameworkTools.gallery.items[FrameworkTools.gallery.itemIndex];
				FrameworkTools.gallery.itemIndex = newIndex;
				var newItem = FrameworkTools.gallery.items[FrameworkTools.gallery.itemIndex];
				var direction = (by > 0 ? -1 : 1);
				
				if (oldItem) {
					Effect.Fade(oldItem, { duration: 0.9 });
				}
				
				if (newItem) {
					newItem.style.left = Math.round( (FrameworkTools.gallery.container.getWidth()*direction*-1)/1.6 ) + 'px';
					newItem.setOpacity(0);
					newItem.style.display = 'block';
					Effect.Fade(newItem, { delay: 0.6, from: 0, to: 1, duration: 0.2 });
					Effect.MoveBy(newItem, 0, 0, { delay: 0.6, duration: 0.5, mode: "absolute", transition: Effect.Transitions.spring  });
				}
			}
		}
	},
	
	gallery2: {
		container: null,
		items: [],
		itemIndex: 0,
		initialize: function() {
			FrameworkTools.gallery2.itemIndex = 0;
			var container = FrameworkTools.gallery2.container = $('gallery2');
			var items = FrameworkTools.gallery2.items = container.select('.galleryCollection');
			for (var i = 0; i < items.length; items++) {
				items[i].style.left = '0px';
				items[i].style.top = '0px';
				items[i].style.display = (i == 0 ? 'block' : 'none');
			}
		},
		navigate: function(by) {
			var newIndex = Math.max(0, Math.min(FrameworkTools.gallery2.itemIndex + by, FrameworkTools.gallery2.items.length-1));
			if (newIndex != FrameworkTools.gallery2.itemIndex) {
				var oldItem = FrameworkTools.gallery2.items[FrameworkTools.gallery2.itemIndex];
				FrameworkTools.gallery2.itemIndex = newIndex;
				var newItem = FrameworkTools.gallery2.items[FrameworkTools.gallery2.itemIndex];
				var direction = (by > 0 ? -1 : 1);
				
				if (oldItem) {
					Effect.Fade(oldItem, { duration: 0.9 });
				}
				
				if (newItem) {
					newItem.style.left = Math.round( (FrameworkTools.gallery2.container.getWidth()*direction*-1)/1.6 ) + 'px';
					newItem.setOpacity(0);
					newItem.style.display = 'block';
					Effect.Fade(newItem, { delay: 0.6, from: 0, to: 1, duration: 0.2 });
					Effect.MoveBy(newItem, 0, 0, { delay: 0.6, duration: 0.5, mode: "absolute", transition: Effect.Transitions.spring  });
				}
			}
		}
	},	
	
	
	IE6PNGFix: {
		background: function(field) {
			
			var fields = field
				? "length" in field
					? field
					: [field]
				: FrameworkTools.IE6PNGFix.collectAffectedFieldsByClass(document.body, 'Background');
				
			for (var i = 0, field = null; field = fields[i]; i++) {
				var backgroundPath = (field.currentStyle.backgroundImage.match(/^url\([\"\']?(.*?)[\"\']?\)$/) || [])[1];
				if (!backgroundPath) continue;
				
				if (!field.canHaveChildren) {
					// Should try outer DIV to fix this better...
					field.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + backgroundPath + "', sizingMethod='crop')";
					field.style.backgroundImage = 'none';
					field.style.position = 'relative';
					continue;
				}
				
				field.className = field.className.replace(/fixPNGBackground/, 'fixedPNGBackground'); // Prevent to get fixed multiple times
				
				if (field.currentStyle.position == 'static')
					field.style.position = 'relative';
				
				
				var imgContainer = document.createElement('div');
				imgContainer.style.zIndex = 1;
				imgContainer.style.position = 'absolute';
				imgContainer.style.left = "-"+field.currentStyle.paddingLeft;
				imgContainer.style.top = '0px';
				imgContainer.style.width = (field.offsetWidth + 'px');
				imgContainer.style.height = (field.offsetHeight + 'px');
				imgContainer.style.overflow = 'hidden';
				
				var img = imgContainer.appendChild(document.createElement('div'));
				img.style.width = '1px';
				img.style.height = '1px';
				img.style.position = 'absolute';
				img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + backgroundPath + "', sizingMethod='crop')";
				
				var x = field.currentStyle.backgroundPositionX;
				var xOfs = '0px';
				if (x == 'right') x = '100%';
				var m = x.match(/(\d+)(px|\%)/);
				if (m) {
					xOfs = m[1]+m[2];
					if (m[2] == '%') {
						img.loadedNegativeXMargin = m[1];
						img.style.visibility = 'hidden';
						xOfs = Math.round(field.offsetWidth*m[1]/100) + 'px';
					}
				}
				
				var y = field.currentStyle.backgroundPositionY;
				var yOfs = '0px';
				if (y == 'bottom') yAlg = '100%';
				var m = y.match(/(\d+)(px|\%)/);
				if (m) {
					yOfs = m[1]+m[2];
					if (m[2] == '%') {
						img.loadedNegativeYMargin = m[1];
						img.style.visibility = 'hidden';
						yOfs = Math.round(field.offsetHeight*m[1]/100) + 'px';
					}
				}
				
				img.style.left = xOfs;
				img.style.top = yOfs;
				
				var imgSizer = img.appendChild(document.createElement('img'));
				imgSizer.src = backgroundPath;
				imgSizer.style.visibility = 'hidden';
				
				imgSizer.loadedChecker = window.setInterval(
					function(rem) {
						var _rem = rem;
						return function() {
							if (_rem.complete) {
								window.clearInterval(_rem.loadedChecker);
								if ("loadedNegativeXMargin" in _rem.parentNode) {
									_rem.parentNode.style.marginLeft = (Math.round(_rem.width*_rem.parentNode.loadedNegativeXMargin/100)*-1 + 'px');
								}
								if ("loadedNegativeYMargin" in _rem.parentNode)
									_rem.parentNode.style.marginTop = (Math.round(_rem.height*_rem.parentNode.loadedNegativeYMargin/100)*-1 + 'px');
							
								_rem.parentNode.style.visibility = 'visible';
							}
						};
					}(imgSizer), 100
				);
				
				field.style.backgroundImage = 'none';
                
				var contentContainer = document.createElement('div');
				contentContainer.style.zIndex = 2;
				contentContainer.style.position = 'relative';
				//contentContainer.style.left = '0px';
				//contentContainer.style.top = '0px';
				//contentContainer.style.overflow = 'hidden';
				//contentContainer.style.width = '100%';
				//contentContainer.style.height = '100%';
				
				while(field.firstChild) contentContainer.appendChild(field.firstChild);
				
				field.appendChild(imgContainer);
				field.appendChild(contentContainer);
			}
		},
		image: function(field) {
			var fields = field
				? "length" in field
					? field
					: [field]
				: FrameworkTools.IE6PNGFix.collectAffectedFieldsByClass(document.body, 'Image');
			
			for (var i = 0, field = null; field = fields[i]; i++) {
				field.style.visibility = 'hidden';
				field.loadedChecker = window.setInterval(
					function(rem) {
						var _rem = rem;
						return function() {
							if (_rem.complete) {
								window.clearInterval(_rem.loadedChecker);
								_rem.className = _rem.className.replace(/fixPNGImage/, 'fixedPNGImage'); // Prevent to get fixed multiple times
								_rem.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src='" + _rem.src + "', sizingMethod='crop')";
								_rem.style.overflow = 'hidden';
								_rem.style.width = _rem.offsetWidth + 'px';
								_rem.style.height = _rem.offsetHeight + 'px';
								_rem.src = "pics/framework/global/dummy.gif";
								_rem.style.visibility = 'visible';
							}
						};
					}(field), 100
				);
			}
		},
		collectAffectedFieldsByClass: function(searchInsideElement, type) {
			var collectedFields = [];
			var regEx = new RegExp('fixPNG' + type);
			if (searchInsideElement.hasChildNodes()) {
				for (var i = 0, childs = searchInsideElement.childNodes, currElement = null; currElement = childs[i]; i++) {
					if (currElement.nodeType != 1) continue;
					if (currElement.className.search(regEx) != -1) {
						collectedFields.push(currElement);
					}
					if (currElement.hasChildNodes())
						collectedFields = collectedFields.concat(FrameworkTools.IE6PNGFix.collectAffectedFieldsByClass(currElement, type));
				}
			}
			return collectedFields;
		}
	}
};
