function write_flash(o){document.write("<object type=\"application/x-shockwave-flash\" data=\""+o.path+"\" width=\""+o.width+"\" height=\""+o.height+"\" class=\""+(o.cname?o.cname:"")+"\"><param name=\"wmode\" value=\"opaque\"><param name=\"movie\" value=\""+o.path+"\"><img src=\"/data/spacer.gif\" width=\""+o.width+"\" height=\""+o.height+"\" alt=\"\"></object>")}function jumpTo(e){var u=e.options[e.selectedIndex].value;if(u.length>0)document.location=u}function inputDefault(w){if(w.className.indexOf(" default")!=-1){w.className=w.className.replace(/ default/,"");w.origValue=w.value;w.value=""}}function inputDoDefault(w){if(w.value==""&&w.origValue){w.value=w.origValue;w.className+=" default"}}function $(w){return document.getElementById(w)}function getParent(n,w){var p=w.parentNode;while(p.tagName.toLowerCase()!=n&&p.parentNode)p=p.parentNode;return p}function cellOver(w){w.className+=" over"}function cellOut(w){w.className=w.className.replace(/ ?over/g,"")}function cellClick(w){document.location=w.getElementsByTagName("a")[0].href}function stopBubble(e){if(!e)e=window.event;e.cancelBubble=true;if(e.stopPropagation)e.stopPropagation()}function toggleCode(w,name,e){var inner="";if(name=="url")inner="http://example.com";if(name=="img")inner="http://example.com/image.jpg";e.value+="["+name+"]"+inner+"[/"+name+"]";focusAtEnd(e)}function insertText(txt,e){e.value+=txt;focusAtEnd(e)}function encode(str){return escape(str).replace(/\//g,"%2F").replace(/\?/g,"%3F").replace(/=/g,"%3D").replace(/&/g,"%26").replace(/@/g,"%40").replace(/\+/g,"%2B")}function focusAtEnd(e){if(e.scrollHeight)e.scrollTop=e.scrollHeight;if(e.createTextRange){var f=e.createTextRange();f.moveStart("character",e.value.length);f.collapse(false);f.select()}else{if(e.selectionStart||e.selectionStart=='0')e.selectionStart=e.value.length;e.focus()}}function $_GET(v){var a=window.location.search.substring(1).split("&");for(var i=0;i<a.length;i++){var p=a[i].split("=");if(p[0]==v)return p[1]}return false}/* The HTTP function is (c) 2005 Jeff McGlynn, use only with permission, e-mail httpfunc@jeffrules.com */function HTTP(u,cb,m,d){if(u=="")return false;if(!m)var m="GET";var o=false;
/*@cc_on
@if(@_jscript_version>=5)try{o=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{o=new ActiveXObject("Microsoft.XMLHTTP")}catch(E){o=false}}
@end @*/if(!o&&typeof(XMLHttpRequest)!="undefined"){try{o=new XMLHttpRequest()}catch(e){o=false}}var s=false,x=false,r=false;if(!cb)cb=function(){};if(typeof(cb)=="function"){s=cb}else{if(cb.success)s=cb.success;if(cb.error)x=cb.error;if(cb.raw)r=cb.raw}if(!o){if(x)x("Browser not supported.");return false}function c(){if(o.readyState==4){if(o.status==200){if(r)r(o.responseText);var rs;try{rs=eval("("+o.responseText+")")}catch(E){if(x){x("Response error.");return}}if(rs&&rs.error&&x)x(rs.error);else if(s)s(rs)}else if(x){x("Server error.")}}}o.open(m,u,true);o.onreadystatechange=c;if(d)o.setRequestHeader("Content-Type","application/x-www-form-urlencoded");o.send(d?d:null);return true}function photoRate(){if(current_photo&& $("rating"))HTTP("/gallery?photo_rate="+current_photo+"&score="+($("rating").selectedIndex));$("rate_good").style.display="inline"}

var current_photo;

function loadThumb(p,sc,pid,views)
{
	if($("rate_good"))
		$("rate_good").style.display="none";

	if(current_photo==pid)
		return;

	var i=new Image(),t=$("photoViewer");
	t.src="/data/spacer.gif";
	var comp=i.onload=function()
		{
			var r=1;
			if(i.width>472)
				r=472/i.width;
			t.width=i.width*r;
			t.height=i.height*r;
			t.src=i.src;
			if($("rating"))
			{
				$("rating").selectedIndex=sc;
				$("rating").disabled=false;
			}
			$("viewcount").innerHTML=views
			t.scrollIntoView();
		};

	i.src=p;
	if($("rating"))
		$("rating").disabled=true;
	current_photo=pid;

	if(i.complete)
		comp();
	else
		HTTP("/gallery?photo_view="+pid);
}

var menu_expanded = new Array();
function toggleMenu(w)
{
	if(w.tagName != "LI")
		w = getParent("li",w);

	if(w.className.indexOf("expanded") >= 0)
	{
		menu_expanded.remove(w.id);
		w.className=w.className.replace(/expanded/,"contracted");
	}
	else if(w.className.indexOf("contracted") >= 0)
	{
		menu_expanded.push(w.id);
		w.className=w.className.replace(/contracted/,"expanded");
	}
}

function initPersistentMenu(default_expanded)
{
	var saved_expanded = new Array();
	if( GetCookie('MenuState') )
		saved_expanded = GetCookie('MenuState').split(',');
	else if( typeof default_expanded == 'string')
		saved_expanded = default_expanded.split(',');

	for(var i = 0; i < saved_expanded.length; i++)
	{
		toggleMenu($(saved_expanded[i]));
	}

	window.onunload = function()
	{
		SetCookie('MenuState', menu_expanded.join(','));
	}

}


function popPhoto(n,w,h){
	var nn="/data/user_pics/full/"+n;
	var w=window.open("","p_"+n.substring(0,n.indexOf('.')),"directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,width="+w+",height="+h);
	w.document.open();
	w.document.write("<html><head><title>Image</title><style type=\"text/css\">body{margin:0;padding:0}.Xable{cursor:pointer}</style></head><body><table style=\"width:100%;height: 100%;\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"center_vh\"><img src=\""+nn+"\" class=\"Xable\" onclick=\"window.close();\"></td></tr></table></body></html>");
	w.document.close();
	w.document.title="Image";
	w.focus()
}

var current_bartender;
function switchBartender(id){
	if ($("bt_"+current_bartender))$("bt_"+current_bartender).className="photo_desel";

	current_bartender = id;
	if ($("bt_"+id))$("bt_"+id).className="photo_current";
	var bid=contestants[id];

	$("bname").innerHTML = bid.name;
	$("bclub").innerHTML = bid.club;
	if (bid.fav_drink != "") {
		$("bfavdrinkbox").style.display = "block";
		$("bfavdrink").innerHTML = bid.fav_drink;
	} else {
		$("bfavdrinkbox").style.display = "none";
	}
	if (bid.callsign != "") {
		$("bcallsignbox").style.display = "block";
		$("bcallsign").innerHTML = "<a href=\"/contests?contest_id=1&selected=" + id + "\">" + bid.callsign + "</a>";
	} else {
		$("bcallsignbox").style.display = "none";
	}
	$("bbackground").innerHTML = bid.background;
	if (bid.moreinfo == "None.") {
		$("bmoreinfobox").style.display = "none";
	} else {
		$("bmoreinfobox").style.display = "block";
		$("bmoreinfo").innerHTML = bid.moreinfo;
	}

	if(bid.photos >= 1)
	{
		$("bphoto1").src = "/data/contests/"+contest_id+"/"+current_bartender+"/thumb/1.jpg";
		$("bphoto1").style.display = "";
	}
	else
	{
		$("bphoto1").src = "";
		$("bphoto1").style.display = "none";
	}

	if(bid.photos >= 2)
	{
		$("bphoto2").src = "/data/contests/"+contest_id+"/"+current_bartender+"/thumb/2.jpg";
		$("bphoto2").style.display = "";
	}
	else
	{
		$("bphoto2").src = "";
		$("bphoto2").style.display = "none";
	}

	if(bid.photos >= 3)
	{
		$("bphoto3").src = "/data/contests/"+contest_id+"/"+current_bartender+"/thumb/3.jpg";
		$("bphoto3").style.display = "";
	}
	else
	{
		$("bphoto3").src = "";
		$("bphoto3").style.display = "none";
	}

	if($("contest_vote_btn"))$("contest_vote_btn").disabled=false;
	if($("contest_vote_stat"))$("contest_vote_stat").innerHTML="";

	expandContestPhoto(1);
}

function expandContestPhoto(id) {
	$("main_photo").src = "/data/contests/"+contest_id+"/"+current_bartender+"/"+id+".jpg";
}

function voteFor() {
	var btn=$("contest_vote_btn");
	var st=$("contest_vote_stat");
	function cv_c(v){
		if(v&&v.success){
			btn.disabled=true;
			st.innerHTML="<b style=\"color:#38CF42\"> Vote casted.</b>";
		}else if(v&&!v.success){
			btn.disabled=true;
			st.innerHTML="<b style=\"color:red\"> You have already voted for this bartender today.</b>";
		}else{
			if (v.error){
				st.innerHTML="<b style=\"color:red\"> Failed: " + v.error + ".</b>";
			}else{
				st.innerHTML="<b style=\"color:red\"> Failed: " + v + ".</b>";
			}
		}
	}
	function cv_e(e){cv_c(e)}

	HTTP("/contests?contest_id="+contest_id+"&action=cast",
		{success:cv_c,error:cv_e},
		"POST",
		"for="+current_bartender
	);
}

function JukeboxWin(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function SetCookie( name, value, expires, path, domain, secure )
{
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function GetCookie( name )
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function DeleteCookie( name, path, domain )
{
	if ( GetCookie( name ) )
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
};

Array.prototype.clear = function () {
    this.length = 0;
};

Array.prototype.remove = function (element) {
	var result = false;
	var array = [];
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			result = true;
		} else {
			array.push(this[i]);
		}
	}
	this.clear();
	for (var i = 0; i < array.length; i++) {
		this.push(array[i]);
	}
	array = null;
	return result;
};

