window.addEvent("domready", function() {


	$$(".statusmsg").each(function(el) {
		if(el.innerHTML.length > 0) {
			el.setStyle("padding", 5);
			el.effect("opacity", {duration: 1000, transition: Fx.Transitions.Sine.easeInOut}).start(0, 1);
		}
	});
	
	if($("errormsg") && $("errormsg").innerHTML.length > 0) {
		ENGINE.Utility.popupBox($("errormsg").innerHTML, "box_error");
	}
	
	var tooltips = $$(".tooltip");

	if(tooltips.length > 0) {
		new Tips(tooltips, {
			initialize: function() {
				this.fx = new Fx.Style(this.toolTip, "opacity", {duration: 300, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
		});
	}	
});

var Superdraft = { };

Superdraft.inArray = function(array, value) {
	for (i = 0; i < array.length; i++) {
		if (array[i] == value) {
			return true;
		}
	}
	return false;
}

Superdraft.initSlots = function() {
	Superdraft.slots = 0;
	
	for(i = 0;i < 100;i++) {
		if($('player_id_' + (i + 1)))
			Superdraft.slots++;
		else
			break;
	}
}

Superdraft.updatePicks = function() {
	Superdraft.picks = 0;
	Superdraft.picksId = [];
	
	for(i = 0;i< Superdraft.slots;i++) {
		if($('player_id_' + (i + 1)).value != '')
			Superdraft.picks++;
		Superdraft.picksId[i] = $('player_id_' + (i + 1)).value;
	}
}

Superdraft.entryFormSubmit = function() {
	if(Superdraft.slots > Superdraft.picks) {
		if(confirm('You have not filled all available slots - are you sure you want to proceed?')) {
			$("entryform").submit();
		}
	} else {
		$("entryform").submit();
	}
}

Superdraft.initPool = function() {
	Superdraft.initPoolControls();

	$("pool_filter").addEvent("submit", function(event) {
		event = new Event(event);
		event.stop();
		Superdraft.doLoading();
		new Ajax(window.location, {
			method: "post",
			postBody: "pool=" + $("pool").value + "&filter=" + $("pool_filter_value").value,
			update: "standings_data",
			onSuccess: function() {
				Superdraft.initPoolControls();
			}
		}).request();
	});
}

Superdraft.doLoading = function() {
	$("standings_data").empty();
	
	new Element("img", {
		"src": "/modules/superdraft/images/loading.gif",
		"alt": "loading",
		"styles": {
			"display": "block",
			"margin": "0 auto"
		}
	}).injectInside("standings_data");
}

Superdraft.initPoolControls = function() {
	$$(".pagecontrol").addEvent("click", function(e) {
		new Event(e).stop();
		Superdraft.doLoading();
		var page = this.id.substr(5);
		var pool = $("pool").value;
		
		new Ajax(window.location, {
			method: "post",
			postBody: "pool=" + pool + "&page=" + page + "&filter=" + $("pool_filter_value").value,
			update: "standings_data",
			onSuccess: function() {
				Superdraft.initPoolControls();
			}
		}).request();
	});

	$$(".sort").each(function(el) {
		var sort_type = el.id.substr(5);

		el.addEvent("click", function(event) {
			event = new Event(event);
			event.stop();
			Superdraft.doLoading();
			new Ajax(window.location, {
				method: "post",
				postBody: "pool=" + $("pool").value + "&filter=" + $("pool_filter_value").value + "&sort=" + sort_type,
				update: "standings_data",
				onSuccess: function() {
					Superdraft.initPoolControls();
				}
			}).request();
		});
	});

	$$(".pooldetailcontrol").addEvent("click", function(e) {
		new Event(e).stop();
		var id = this.id.substr(4);
		if(!$("pd_" + id)) {
			var weekid = 0;
			
			if($("week")) {
				weekid = $("week").value;
			}
	
			new Ajax(window.location, {
				method: "post",
				postBody: "entryid=" + id + "&week=" + weekid,
				onSuccess: function() {
					var row = $("p_" + id);
					var td = new Element("td", {"colspan": "11", "styles": {"padding": "0"}});
					td.innerHTML = this.response.text;
				
					new Element("tr", {
						"class": row.hasClass("alt") ? "alt" : "",
						"id": "pd_" + id
					}).injectAfter(row).appendChild(td);

					$("pdc_" + id).getParent().getParent().setStyle("font-weight", "bold");
					$("pd_" + id).style.display = "";
					$("pdc_" + id).removeClass("closed").addClass("open");
					$("pdc_" + id).innerHTML = "[-]";
				}
			}).request();
		} else { /* firefox didn't like the request not being async */
			if(this.hasClass("open")) {
				this.getParent().getParent().setStyle("font-weight", "normal");
				this.removeClass("open").addClass("closed");
				this.innerHTML = "[+]";
				$("pd_" + id).setStyle("display", "none");
			} else {
				this.getParent().getParent().setStyle("font-weight", "bold");
				$("pd_" + id).style.display = "";
				this.removeClass("closed").addClass("open");
				this.innerHTML = "[-]";
			}
		}
	});
	$$(".buddy_add").addEvent("click", function(e) {
			new Ajax("ajax.php", {
				method: "post",
				postBody: "function=add_buddy&buddy_id=" + this.id +"&pool_id="+$('pool').value ,
				onSuccess: function() {
					Superdraft.createBuddyList(this.response.text);
				}
			}).request();
		});
	$$(".buddy_del").addEvent("click", Superdraft.removeBuddy);
},
Superdraft.createBuddyList = function(htmlstr){
	$('buddylist').innerHTML = "";
	$('buddylist').innerHTML = htmlstr;
	$$(".buddy_del").addEvent("click", Superdraft.removeBuddy);
},
Superdraft.removeBuddy = function(){
			new Ajax("ajax.php", {
				method: "post",
				postBody: "function=del_buddy&buddy_id=" + this.id +"&pool_id="+$('pool').value ,
				onSuccess: function() {
					Superdraft.createBuddyList(this.response.text);
				}
			}).request();
} 