// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var Cookies = Class.create();
Object.extend(Cookies.prototype, {
	initialize: function() {
		var allCookies = document.cookie.split('; ');
		
		for (var i = 0; i < allCookies.length; i++) {
			var cookiePair = allCookies[i].split('=');
			
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	
	create: function(/* String */ name, /* String */ value, /* integer */ days) {
		if (null == name) 
			return;
		
		if (null == value) 
			value = "";
		
		var expires = -1;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 86400000));
			expires = date.toGMTString();
		}
		
		var secure = "";
		if (new String(window.location).indexOf("https:") >= 0) 
			secure = ";secure";
		
		document.cookie = name+"="+value+";expires="+expires+";path=/"+secure;
		this[name] = value;
	},
	
	erase: function(/* String */ name) {
		this.create(name);
		this[name] = null;
	}
});

var TextStyleSwitcher = Class.create();
Object.extend(TextStyleSwitcher.prototype, {
	/* Required */
	increaseIcon: 	null,
	decreaseIcon: 	null,
	
	/* Optional */
	defaultStyle: 	0,
	currentStyle: 	0,
	styles: 		{},
	
	limitLower: 	0,
	limitUpper: 	2,
	
	hideIcons: 		true,
	
	initialize: function(options) {
		this.styles = new Array();
		
		for (i in options) 
			this[i] = options[i];
		
		if (this.styles.length <= 0) {
			var links = $A(document.getElementsByTagName("LINK"));
			
			while (links.length) {
				var link = links.pop();
				if (link.getAttribute("type") == "text/css" && link.getAttribute("rel").indexOf("alt") >= 0) 
					this.styles.push(link);
			}
		}
		
		this.getTextStyleCookie();
		
		if (isNaN(this.currentStyle)) {
			this.currentStyle = 0;
		}
		
		if (this.currentStyle != this.defaultStyle)
			this.setTextStyle();
		
		Event.observe(this.increaseIcon, "click", function(evt) {
			if (textStyleSwitcher.limitUpper > textStyleSwitcher.currentStyle) {
				textStyleSwitcher.switchTextStyle(true);
			}
		});
		
		Event.observe(this.decreaseIcon, "click", function(evt) {
			if (textStyleSwitcher.limitLower < textStyleSwitcher.currentStyle) {
				textStyleSwitcher.switchTextStyle(false);
			}
		});
		
		if (this.hideIcons) {
			if (this.currentStyle >= this.limitUpper) 
				this.showIcon(this.increaseIcon, false);
			
			if (this.currentStyle <= this.limitLower) 
				this.showIcon(this.decreaseIcon, false);
		}
	},
	
	getTextStyleCookie: function() {
		if (null == window.cookies) 
			window.cookies = new Cookies();
		
		this.currentStyle = cookies["textStyle"];
	},
	
	setTextStyleCookie: function() {
		if (null == window.cookies) 
			window.cookies = new Cookies();
		
		cookies.create("textStyle", this.currentStyle, 1825);
		
		var compatString = "smalltext"
		switch (this.currentStyle) {
			case 0: compatString = "smalltext"; break;
			case 1: compatString = "mediumtext"; break;
			case 2: compatString = "largetext"; break;
			default: compatString = "smalltext";
		}
		cookies.create("defaultCSS", compatString, 1825);
	},
	
	switchTextStyle: function(/* boolean */ increase) {
		var showIncrease = true;
		var showDecrease = true;

		if (increase) {
			this.currentStyle++;
			if (this.currentStyle >= this.limitUpper) {
				showIncrease = false;
			}
		} else {
			this.currentStyle--;
			if (this.currentStyle <= this.limitLower) {
				showDecrease = false;
			}
		}
	
		if (this.hideIcons) {
			this.showIcon(this.increaseIcon, showIncrease);
			this.showIcon(this.decreaseIcon, showDecrease);
		}
	
		this.setTextStyle();
		this.setTextStyleCookie();
	},
	
	setTextStyle: function() {
		for (var i = 0; i < this.styles.length; i++) {
			var disable = true;
			if (i == this.currentStyle) 
				disable = false;
			
			this.styles[i].disabled = disable;
		}
	},
	
	showIcon: function(/* Element */ link, /* boolean */ show) {
		var state = show ? "visible" : "hidden";
		Element.setStyle(link, { visibility: state });
	}
});

var AdSenseRenderer = Class.create();
Object.extend(AdSenseRenderer.prototype, {
	container: {},
	
	initialize: function(/* Element */ container) {
		this.container = container;
	},
	
	render: function(/* Array */ ads) {
		this.container.getElementsByClassName("ad_heading")[0].href = google_info.feedback_url;
		
		for (var i = 0; i < ads.length; i++) {
			var ad = ads[i];
			
			var adDiv = document.createElement("DIV");
			adDiv.className = "text-ad-ent";
			
			var adLink = document.createElement("A");
			adLink.className = "ad-link-ent";
			adLink.href = ad.url;
			adLink.title = ad.visible_url;
			Event.observe(adLink, "mouseover", function(evt) {
				window.status = Event.element(evt).getAttribute("title");
			});
			Event.observe(adLink, "mouseout", function() {
				window.status = "";
			});
		    adLink.innerHTML = new String(ad.line1);
			adDiv.appendChild(adLink);
			
			adDiv.appendChild(document.createElement("BR"));
			
			var text = "";
			for (var n = 2; ad["line" + n]; n++) 
				text += ad["line" + n] + " ";
			
		    var adText = document.createElement("SPAN");
		    adText.innerHTML = new String(text);
			adDiv.appendChild(adText);
			
			adDiv.appendChild(document.createElement("BR"));
			
			var adUrl = document.createElement("A");
			adUrl.className = "ad-link-ent";
			adUrl.href = ad.url;
			adUrl.title = ad.visible_url;
			Event.observe(adUrl, "mouseover", function(evt) {
				window.status = Event.element(evt).getAttribute("title");
			});
			Event.observe(adUrl, "mouseout", function() {
				window.status = "";
			});
			adUrl.appendChild(document.createTextNode(ad.visible_url));
			adDiv.appendChild(adUrl);
			
			this.container.appendChild(adDiv);
		}
	}
});


// Onload stuff goes here
Event.observe(window, "load", function() {
	window.textStyleSwitcher = new TextStyleSwitcher({
		increaseIcon: $("text_increase"),
		decreaseIcon: $("text_decrease")
	});
	
	if (null != $("most_tabs")) {
		var tabs = $A($("most_tabs").getElementsByClassName("tabs")[0].getElementsByTagName("LI")).flatten();
		for (var i = 0; i < tabs.length; i++) {
			Event.observe(tabs[i].getElementsByTagName("A")[0], "click", function(evt) {
				var tab = Event.element(evt).parentNode;
				Element.addClassName(tab, "selected");
				var tabs = $A(tab.parentNode.getElementsByTagName("LI")).flatten();
				var panels = $A($("most_tabs").getElementsByClassName("tab_panel")).flatten();
				for (var i = 0; i < tabs.length; i++) {
					if (tabs[i] == tab) 
						Element.addClassName(panels[i], "selected");
					else {
						Element.removeClassName(tabs[i], "selected");
						Element.removeClassName(panels[i], "selected");
					}
				}
			});
		}
	}
});


