var QvPenny = 
{
	windowID: 1,
	helpId: -1,

	infoWindow: function(url, width, height)
	{
		var win = new Window({
			className: "greylighting", 
			url: url,
			width: width,
			height: height, 
			showEffectOptions: 
			{
				duration:1.5
			},
			maximizable: false,
			minimizable: false,
			draggable: false
		}); 
		
		win.showCenter(true);
		win.setZIndex(100);
	},	

	infoWindow2: function(element, width, height)
	{
		var win = new Window({
			className: "greylighting", 
			width: width,
			height: height, 
			showEffectOptions: 
			{
				duration:1.5
			},
			maximizable: false,
			minimizable: false,
			draggable: false
		}); 

		win.setContent(element, true, true);		
		win.showCenter(true);
		win.setZIndex(100);
	},	

	
	closeWindow: function()
	{
		Windows.closeAll();	
	},
	
	fancy: function()
	{
		this.fancyColumns();
		//this.fancyMenu();
		this.fancySubmits();
		//this.fancyHeaders();

		this.columnLayout();
		this.checkHelpAvailable();
	},
	
	fancyColumns: function()
	{
		var self = this;
		var columns = $$(".column");
		columns.each(function(column) 
		{
			if (column.hasClassName("penny-fancy"))
			{
				return;
			}
			column.toggleClassName("penny-fancy");
		
			var boxcolor = "FFFFFF";
			var fontcolor = "FFFFFF";
				
			column.select("h2").each(function(h2){

				if (h2.hasClassName("penny-fancy"))
				{
					return;
				}
				h2.toggleClassName("penny-fancy");

				var text = h2.innerHTML;								
				var bgcolor = h2.getStyle("background-color");
				var fgcolor = h2.getStyle("color");
				var font = "adlib";
				
				var fontFamily = h2.getStyle("font-family");
				if (fontFamily && fontFamily.indexOf("custom-") > 0)
				{
					var icustom = fontFamily.indexOf("custom-");
					font = fontFamily.substring(icustom);
					font = font.replace("custom-", "");
				}
				
				bgcolor = QvPenny.toHexColor(bgcolor);
				fgcolor = QvPenny.toHexColor(fgcolor);
				
				boxcolor = bgcolor;
				fontcolor = fgcolor;
				
				var img = new Element("img", {
					"src": "/image-title.php?t=" + encodeURIComponent(text) + 
							"&s=14&f="+font+"&fg=" + fgcolor + 
							"&bg=" + bgcolor + "&a=bg&m=2",
					"alt": text,
					"title": text
				});				
				
				h2.update(img);							
			});
			
			var arc = 8;
			var width = column.getWidth();
			
			var header = new Element("img", 
			{
				"src": "/column-top.php?a="+arc+"&c="+boxcolor+"&w="+width,
				"height": arc+"px",
				"width": width+"px",
				"alt": "header",
				"style": "margin:0px; padding: 0px; display:block;"
			});
			
			var footer = new Element("img", 
			{
				"src": "/column-bottom.php?a="+arc+"&c="+boxcolor+"&w="+width,
				"height": arc+"px",
				"width": width+"px",
				"alt": "footer",
				"style": "margin:0px; padding: 0px; display:block;"
			});
			
			column.select(".content").each(function(contentElm)
			{
				if (!contentElm.hasClassName("custom-bg"))
				{
					contentElm.setStyle(
					{
						"background": "#ffffff url(\"/column-bg.php?c=" + 
							boxcolor + "&w=" + column.getWidth() + "\")"
					});
				}
			});
			
			column.setStyle(
			{
				"padding": "0px"
			});
			column.insert({ "top": header });				
			column.insert({ "bottom": footer });			
		});		
	},
	
	toHexColor: function(rgb)
	{
		if (rgb.indexOf("rgb") < 0)
		{
			return rgb.replace("#", "");
		}
		
		rgb = rgb.replace("rgb", "");
		rgb = rgb.replace("(", "");
		rgb = rgb.replace(")", "");
		rgb = rgb.replace(" ", "");
		
		var result = "";
		var split = rgb.split(",");
		for(var i=0; i < split.length; i++)
		{
			var c = split[i];
			var n = new Number(c);
			var hex = n.toString(16);
			if (hex.length == 1)
			{
				hex = "0"+hex;
			}
			result = result + hex;
		}
		
		return result;
	},
	
	columnLayout: function()
	{
		var self = this;
		var columns = $$(".column");	
		var heights = new Array(0, 0, 0, 0);
		var columnsElm = $("columns"); 
		
		if (columnsElm == null || columnsElm.hasClassName("autocolumns") == false)
		{
			return;
		}
		
		var row = 0;
		var col = 0;
		var defaultw = 229 + 10;
		var z = 3;
		
		columns.each(function(column) 
		{			
			var colw = 1;
			colw = column.hasClassName("columnw1") ? 1 : colw;
			colw = column.hasClassName("columnw2") ? 2 : colw;
			colw = column.hasClassName("columnw3") ? 3 : colw;
			colw = column.hasClassName("columnw4") ? 4 : colw;
			
			var dim = column.getDimensions();			
			
			// Max col height in all cols
			var maxh = 0;
			for(var i=0; i < colw && 
						(col+i) < heights.length; i++)
			{
				maxh = Math.max(heights[col+i], maxh);
			}
			
			var newstyle = {
				"position": "absolute",
				"left": (col * defaultw) + "px",
				"top": maxh + "px",
				"float": "none"
			};			
			
			column.setStyle(newstyle);			
			
			// Add column height
			for(var i=0; i < colw; i++) 
			{
				heights[col+i] += (dim.height + 10);
			}
			
			// Set current column
			col += colw;
									
			if (col >= 4)
			{
				col = 0;
			}			
		});
		
		
		var maxh = 0;
		for(var i=0; i < 4; i++)
		{
			maxh = Math.max(heights[i], maxh);
		}
		
		columnsElm.setStyle({
			"height": (maxh + 10) + "px"
		});
	},
	
	fancyMenu: function()
	{
		var self = this;
		var menu = $("menu_bar");
		
		if (menu.hasClassName("penny-fancy"))
		{
			return;
		}		
		
		menu.toggleClassName("penny-fancy");
		menu.select("li").each(function(li) 
		{		
			if (li.hasClassName("nobutton"))
			{
				return;
			}
		
			li.select("a").each(function(anchor)
			{
				var text = anchor.innerHTML;
				text = text.replace("\r", " ");
				text = text.replace("\n", " ");
				text = text.replace("  ", " ");
				
				var bg = anchor.getStyle("color");
				bg = self.toHexColor(bg);
								
				var img = new Element("img", 
				{
					"src": "/image-button.php?t="+encodeURIComponent(text)+"&w=150&h=30&bg="+bg,
					"alt": text,
					"width": "150",
					"height": "30",
					"style": "border: 0px;"
				});	
				
				anchor.update(img);
			});		
		});	
	},
	
	fancySubmits: function()
	{
		var self = this;
		
		$$("input").each(function(submit) 
		{			
			if ((submit.type != "submit" && submit.type != "button") || 
				!submit.hasClassName("fancy-submit") || 
				submit.hasClassName("penny-fancy"))
			{
				return;
			}
			
			submit.toggleClassName("penny-fancy");		
			var text = submit.value;			
			var bg = QvPenny.toHexColor(self.firstStyle(submit, "color"));
						
			if(bg == "000000" || bg == "ffffff")
			{
				bg = "FC42B4";
			}

			var url = "/image-button.php?t="+encodeURIComponent(text)+"&a=6&h=26&s=10&bg="+bg;		
			var img = new Element("input", 
			{
				"type": "image",
				"src": url,
				"alt": text,
				"height": 26,
				"border": "0",
				"class": submit.classNames().collect(function(s) { 
					return s; 
				}).join(" ") + " penny-fancy"
			});
			
			submit.replace(img);
			
			img.observe('mouseover', function(event)
			{
				var elm = Event.element(event);
				elm.src = url + "&over=true";
			});
			
			img.observe('mouseout', function(event)
			{
				var elm = Event.element(event);
				elm.src = url;
			});
			
		});	
	},
	
	fancyHeaders: function()
	{
		var headers = $$("h3", "h2", "h4", "h1");
		
		var sizes = new Array();
		sizes["h1"] = 20;
		sizes["h2"] = 18;
		sizes["h3"] = 16;
		sizes["h4"] = 14;
		sizes["h5"] = 12;
				
		for(var i=0; i < headers.length; i++)
		{
			var header = headers[i];			
			
			if (header == null || !header.hasClassName || header.hasClassName("penny-fancy"))
			{
				return;
			}
			
			header.toggleClassName("penny-fancy");
			
			var text = header.innerHTML;
			var bgcolor = this.firstStyle(header, "background-color");
			var height = sizes[header.tagName.toLowerCase()];
			var margin = 2;
			var parent = header;			
			var fgcolor = header.getStyle("color");
			var font = "adlib";
			
			var fontFamily = header.getStyle("font-family");
			if (fontFamily && fontFamily.indexOf("custom-") > 0)
			{
				var icustom = fontFamily.indexOf("custom-");
				font = fontFamily.substring(icustom);
				font = font.replace("custom-", "");
			}
			
			bgcolor = QvPenny.toHexColor(bgcolor);
			fgcolor = QvPenny.toHexColor(fgcolor);
			
			var img = new Element("img", {
				"src": "/image-title.php?t=" + encodeURIComponent(text) + 
						"&s=" + height + "&f=" + font + "&fg=" + fgcolor + 
						"&bg=" + bgcolor + "&a=bg&m=" + margin,
				"alt": text,
				"title": text
			});				
			
			header.update(img);							
		}
	},
	
	firstStyle: function(element, style)
	{
		var bgcolor = element.getStyle(style);
		var parent = element;
		
		while(parent.parentNode &&
				parent.parentNode != null && 
				parent.parentNode != window &&
				bgcolor == "transparent")
		{
			if (parent.getStyle)
			{
				bgcolor = parent.getStyle(style);
			}
			
			parent = parent.parentNode;
		}
		
		return bgcolor;
	},
	
	/** 
		Show a help popin for the current-page 
	*/
	showHelp: function(templateUrl) 
	{
		new QvAjax.Container("popin-window",
		{
			url: templateUrl + this.helpId,
			effect: new QvAjax.Effect.Window(
			{
				width: 600,
				height: 400,
				onComplete: function()
				{
					QvPenny.fancy();
				}
			})
		});
	},
	
	checkHelpAvailable: function()
	{
		
	}
};

Event.observe(window, "load", QvPenny.fancy.bindAsEventListener(QvPenny));