

function login_init() {
    document.a.username.focus();
}

function selectTeam(team_id) {
    document.getElementById("pick" + team_id).click();
    document.getElementById("submitUpper").click();
}


function showPanel(p, maxp) {

	for (i=1; i<= maxp; i++) {
		//alert("i: " + i + " - p: " + p);
		if (i == p) {
			document.getElementById('panel_' + i).style.display="block";
			document.getElementById('tab_' + i).className="current";
		} else {
			document.getElementById('panel_' + i).style.display="none";
			document.getElementById('tab_' + i).className="notcurrent";
		}
	}

}

function showWatchlistStuff(elem, vis) {
	var def = document.getElementById(elem);
	var anchor = document.getElementById(elem+"-anchor");

	if(vis) {
		def.style.display="block";
		anchor.href="javascript:showWatchlistStuff('" + elem + "',false);";
		anchor.innerHTML="Hide";
		if (elem == "add")
			document.getElementById("listname").focus();

	} else {
		def.style.display="none;"
		anchor.href="javascript:showWatchlistStuff('" + elem + "',true);";
		switch (elem) {
			case "def":
				anchor.innerHTML="What is a watchlist?";
				break;
			case "add":
				anchor.innerHTML="Add a new watchlist";
				break;
		}

	}
}

function chooseLeague(league) {
	if (league != 0) {
		document.location.href = "?league=" + league;
	}
}

function chooseList(list) {
	if (list == 0) {
		list = "all";
	}
	document.location.href = "?list=" + list;
}

function showHideListName(action) {
	if (action == 'show')
		disp = "block";
	else
		disp = "none";

	var listname = document.getElementById("list-name");
	var listspan = document.getElementById("list-chooser");

	var l = getAbsoluteLeft(listspan);
	var t = getAbsoluteTop(listspan) + listspan.offsetHeight;
	listname.style.display = disp;
	listname.style.left = l + "px";
	listname.style.top = t + "px";

	document.getElementById("listname").focus();
}

function addList(name) {
	// watchlist_edit.php?list=add
}



//GETS REAL TOP VALUE WITH RESPECT TO CLIENT AREA
function getAbsoluteTop(elem)
{
	var topPosition = 0;

	while (elem) {
		if (elem.tagName == 'BODY') {
			break;
		}
		topPosition += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return topPosition;
}

//GETS REAL LEFT VALUE WITH RESPECT TO CLIENT AREA
function getAbsoluteLeft(elem)
{
	var leftPosition = 0;

	while (elem) {
		if (elem.tagName == 'BODY') {
			break;
		}
		leftPosition += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return leftPosition;
}

function transferEntry(entry) {
	var url = "entries.php?transferone=" + entry;
	document.location.href = url;
}

function transferAllEntries(user) {
	var url = "entries.php?transferall=" + user;
	document.location.href = url;
}


function setPick(week, entry, pick_seq, dd) {

    pickStatus(entry, pick_seq, "progress");

    var url = "setpick.php?week=" + week + "&entry=" + entry + "&pick_seq=" + pick_seq
        + "&pick=" + dd.value;
    $.ajax({
        url: url,
        dataType: "json",
        success: function(data, status, jqXHR) {
            if( status == "success" && data.ok) {
                pickStatus(entry, pick_seq, "success");
                $("#picks_msg").hide();
            } else {
                pickFailed(entry, pick_seq, data.error);
            }
        },
        error: function(jqXHR, status, error) {
            pickFailed(entry, pick_seq, error);
        }
    });
}

function pickStatus(entry, pick_seq, status) {
    var elem = $("#" + status + "_" + entry + "_" + pick_seq);
    elem.show();
    elem.siblings().hide();
}

function pickFailed(entry, pick_seq, error) {
    var entry_name = $("#entryname_" + entry).text();
    var msg = "Entry '" + entry_name +
        "' pick was not set.  The server said, '" + error +
        "'.  Please try again, including " +
        "<a href=\"picks.php\">refreshing the Picks page</a> " +
        "and trying again.  Remember, if the Picks page or site " +
        "in general is not working, your picks must go to " +
        "<a href=\"mailto:dallaskoth@yahoo.com\">dallaskoth@yahoo.com</a>.";
    pickStatus(entry, pick_seq, "fail");
    console.log(msg);
    $("#picks_msg").html(msg).show();
}

// jquery combobox stuffs
(function( $ ) {
    $.widget( "ui.combobox", {
	_create: function() {
	    var self = this,
	    select = this.element.hide(),
	    selected = select.children( ":selected" ),
	    value = selected.val() ? selected.text() : "";
	    var input = this.input = $( "<input>" )
		.insertAfter( select )
		.val( value )
		.autocomplete({
		    delay: 0,
		    minLength: 0,
		    source: function( request, response ) {
			var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
			response( select.children( "option" ).map(function() {
			    var text = $( this ).text();
			    if ( this.value && ( !request.term || matcher.test(text) ) )
				return {
				    label: text.replace(
					new RegExp(
					    "(?![^&;]+;)(?!<[^<>]*)(" +
						$.ui.autocomplete.escapeRegex(request.term) +
						")(?![^<>]*>)(?![^&;]+;)", "gi"
					), "<strong>$1</strong>" ),
				    value: text,
				    option: this
				};
			}) );
		    },
		    select: function( event, ui ) {
			ui.item.option.selected = true;
			self._trigger( "selected", event, {
			    item: ui.item.option
			});
                        select.trigger("change");
		    },
		    change: function( event, ui ) {
			if ( !ui.item ) {
			    var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
			    valid = false;
			    select.children( "option" ).each(function() {
				if ( $( this ).text().match( matcher ) ) {
				    this.selected = valid = true;
				    return false;
				}
			    });
			    if ( !valid ) {
				// remove invalid value, as it didn't match anything
				$( this ).val( "" );
				select.val( "" );
				input.data( "autocomplete" ).term = "";
				return false;
			    }
			}
		    }
		})
		.addClass( "ui-widget ui-widget-content ui-corner-left" );

	    input.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
		    .data( "item.autocomplete", item )
		    .append( "<a>" + item.label + "</a>" )
		    .appendTo( ul );
	    };

	    this.button = $( "<button type='button'>&nbsp;</button>" )
		.attr( "tabIndex", -1 )
		.attr( "title", "Show All Items" )
		.insertAfter( input )
		.button({
		    icons: {
			primary: "ui-icon-triangle-1-s"
		    },
		    text: false
		})
		.removeClass( "ui-corner-all" )
		.addClass( "ui-corner-right ui-button-icon" )
		.click(function() {
		    // close if already visible
		    if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
			input.autocomplete( "close" );
			return;
		    }

		    // work around a bug (likely same cause as #5265)
		    $( this ).blur();

		    // pass empty string as value to search for, displaying all results
		    input.autocomplete( "search", "" );
		    input.focus();
		});
	},

	destroy: function() {
	    this.input.remove();
	    this.button.remove();
	    this.element.show();
	    $.Widget.prototype.destroy.call( this );
	}
    });
})( jQuery );

$(function() {
    $( ".picks_select" ).combobox().change(function() {
        //console.log(this.value);
    });

});

