var active_feature = 0;
var next_feature;
var total_features;

Event.observe(window,"load",function(ev) {

	$$(".default").each(function(el) {
		el.observe("focus",function(ev) {
			if (this.value == this.defaultValue) {
				this.value = "";
				if (this.readAttribute("name") == "password") {
					this.replace(new Element("input", { type: "password", name: "password" }));
					$$("input[name=password]")[0].focus();
				}
			}
		});
		el.observe("blur",function(ev) {
			if (this.value == "") {
				this.value = this.defaultValue;
			}
		});
	});
	
	if ($("features")) {
		ftimer = setInterval("switchFeatures()",8000);
		total_features = $$("#features li").length;
	}
	
	$$(".sc_swaps a").click(function(ev) {
		ev.stop();
		new Ajax.Updater("sc_popup","http://www.text4swap.com/ajax/swap-info/", { parameters: { swap: this.readAttribute("href").substr(1) }, onComplete: function(ev) {
			$("sc_popup").show();
			$$("#sc_popup .close").click(function(ev) {
				ev.stop();
				$("sc_popup").hide();
			});
			$$(".counteroffer").click(function(ev) {
				ev.stop();
				new Effect.toggle("show_counteroffer","blind", { duration: 0.3 });
			});
		}});
	});
});

function switchFeatures() {
	next_feature = active_feature + 1;
	ffeatures = $$("#features li");
	if (!ffeatures[next_feature])
		next_feature = 0;
	new Effect.Parallel([new Effect.Fade(ffeatures[active_feature]), new Effect.Appear(ffeatures[next_feature])], { duration: 1, afterFinish: function() { active_feature = next_feature; } });
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250,left = 520,top = 325');");
}

var TextTip = Class.create({

	Tip: false,
	Element: false,

	initialize: function(element,text) {
		this.Element = $(element);
		this.Tip = new Element("div", { style: "position: absolute; z-index: 5000; border: 2px solid #BBB; background: #FFF; color: #333; padding: 5px; max-width: 250px; line-height: 16px; font-size: 12px; display: none;" });
		this.Tip.innerHTML = "<p>" + text + "</p>";
		$$("body")[0].insert({ bottom: this.Tip });
		Event.observe(this.Element,"mouseenter",this.Enter.bind(this));
	},
	
	Enter: function(event) {
		this.Tip.show();
		offset = this.Element.cumulativeOffset();
		dims = this.Element.getDimensions();
		this.Tip.setStyle({ left: (offset.left + dims.width - Math.round(dims.width / 4)) + "px", top: (offset.top + dims.height - Math.round(dims.height / 4)) + "px" });

		Event.observe(this.Element,"mouseleave",this.Leave.bind(this));
	},
	
	Leave: function(event) {
		this.Tip.hide();
		this.Element.stopObserving("mouseleave");
	}
});

