diggburylock = 0;
var timeid = new Array();

function checkIE() {
    var useragent = navigator.userAgent;
    var pos = useragent.indexOf('MSIE');
    if (pos > -1) {
        return true;
    } else {
        return false;
    }
}

function flash(id) {
    timeid[id] = window.setTimeout("fadeInSpy(7, '"+id+"')",100);
}

function alreadydug() {
    alert("You have already dug this item");
}

function myclearTimeout(id) {
    clearTimeout(timeid[id]);
} 

var digging = -9;
var daction = new Array();
function dig(row, shade, itemd, digcheck) {
    $('diglink'+row).innerHTML = '<a href="javascript:void(0)">digg it</a>';
    if (digging == row) {
        alert('You have already dugg this story');
        return;
    } else {
        digging = row;
    }

	daction[row] = new Effect.FadeKeepSpace('diggs-strong-'+row, {
						 duration: 1
						 } );

    new Ajax.Request("/diginfull", {
        method: 'post',
        parameters: "id="+itemd+"&row="+row+"&digcheck="+digcheck,
        onSuccess: function (xh) {
            var tx = xh.responseText;
            if (tx) {
                if (tx.substr(0,6) == 'ERROR:') {
					daction[row].cancel();
					Effect.AppearKeepSpace('diggs-strong-'+row, {duration: 1});
                    alerttext = tx.replace(/ERROR:/, '');
                    alert(alerttext);
                } else if (tx.substr(0,5) !== '3~--~') {
                    // Do nothing, this is caused 99% of the time by a user abort.
                } else {
					up_diggs(row, tx);
				}
            }
        }
        });
}

function up_diggs(row, tx) {
	// If not finished, wait a bit:
	if (daction[row].state != 'finished') {
		setTimeout('up_diggs(' + row + ',"' + tx + '")', 100);
	} else {
		split = new RegExp("~--~");
		var b = tx.split(split);
		var target1 = $('diggs-strong-'+row);
		// Update if bigger
		if (target1.innerHTML < b[1]) {
			target1.innerHTML = b[1];
		} else {
			target1.innerHTML = parseInt(target1.innerHTML) + 1;
		}
		Effect.AppearKeepSpace('diggs-strong-'+row, {duration: 1});
		var target2 = $('diglink'+row);
		target2.innerHTML = "<span>dugg!</span>";
		// make #1 link
		var t = $('enclosure'+row);
		var h3s = t.getElementsByTagName('h3');
		h3s[0].innerHTML += ' <a id="myn' + row +
	'" class="news-label" href="javascript:addn1(' + b[3] + ',' + row + ",'" + b[4] + "')" + 
	'"><img src="/img/myone-add.gif" alt="Add as My Number One" width="50" height="16" /></a>';
	}
}

ColorSpy = new Array();
ColorSpy[1] = "#FFFFFF";
ColorSpy[2] = "#F1F3F5";
ColorSpy[3] = "#EBEEF1";
ColorSpy[4] = "#DEE2E7";
ColorSpy[5] = "#CCD5DB";
ColorSpy[6] = "#BCC7CF";
ColorSpy[7] = "#ACB9C4";

function fadeInSpy(where, id) {
  if (where >= 1) {
    $('main'+id).style.backgroundColor = ColorSpy[where];
    if (where > 1) {
      where -= 1;
      timeid[id] = window.setTimeout("fadeInSpy("+where+",'"+id+"')", 100);
    } else {
      where -= 1;
      $('main'+id).style.backgroundColor = "transparent";
      myclearTimeout(id);
    }
  }
}

function openSpellChecker() {
    // get the textarea we're going to check
    var text1 = $('title');
    var textarea1 = $('bodytext');
    var speller = new spellChecker( text1, textarea1 );
    speller.openChecker();
}

function openSpellCheckerComment() {
    // get the textarea we're going to check
    var textarea1 = $('comment');
    var speller = new spellChecker( textarea1 );
    speller.openChecker();
}

// cb = confirmblock - Block a user
allowblock = true;
function cb(id, name, check) {
    if (confirm('Are you sure you want to block user '+name+' ?')) {
        allowblock = false;
        var newform = '<form id="newblock" action="/userblock" method="post"><input type="hidden" name="id" value="' + id + '" /><input type="hidden" name="check" value="' + check + '" /></form>';
        var tag = $('container');
        tag.innerHTML += newform;
        $('newblock').submit();
	}
	return false;
}

function rjp(id, code, row) {
    HideandUNhideObj(row);
    new Ajax.Request('/reportj', {
        method: 'post',
        parameters: "id="+id+"&code="+code,
        onSuccess: function(xh) {
            $('enclosure'+row).className += ' news-buried';
            // Need to ensure undiggs look right when buried:
            $('diglink'+row).className = 'digg-it';
            $('diglink'+row).innerHTML = '<span>buried</span>';
        }
        });
}

function showCaptcha(name) {
    var target = $('commentcaptcha');
    target.onclick = null;
    target.style.display = 'block';
    if (!(target.innerHTML.length)) {    
        $('submitbutton').disabled = true;
        new Ajax.Request('/get_captcha', {
            method: 'get',
            parameters: 'keyname='+name,
            onSuccess: function(xh) {
                $('commentcaptcha').innerHTML = xh.responseText;
                $('submitbutton').disabled = false;
            }
            });
    }
}

function enablebutton(button, button2, target) {
  var string = target.value; 
  button2.disabled = false;
  if (string.length > 0) {
    button.disabled = false;
  } else {
    button.disabled = true;
  }
  var target2 = $('commentcaptcha');
}

function checkit(form) {
    var name = escape(form.username.value);
    new Ajax.Request('/checkusername', {
        method: 'post',
        parameters: 'username='+name, 
        onSuccess: function(xh) { 
            var rs = xh.responseText;
            if (rs == 'taken'|| rs == 'not acceptable - user names must have 4-15 characters, and only alphanumeric characters.') {
                $('checkitvalue').innerHTML = '<strong style="background: url(/img/deny.png) 0 1px no-repeat; padding-left: 16px; font-size: 85%; color: #c00;">Sorry, the username "'+name+'" is '+rs+'.</strong>';
                form.submit.disabled = 'disabled';
            } else {
                $('checkitvalue').innerHTML = '<strong style="background: url(/img/confirm.png) 0 1px no-repeat; padding-left: 16px; font-size: 85%; color: #090;">Great, the username "'+name+'" is '+rs+'.</strong>';
                form.submit.disabled = '';
            }
        }
        });
    return false;  
}

function getdpage(itemid, page, friends) {
    new Ajax.Request('/newdiggers', {
        method: 'post',
        parameters: "id="+itemid+"&page="+page+"&friends="+friends, 
        onSuccess: function(xh) { $('diggers').innerHTML = xh.responseText; }
        });
    return false;
}

// For dismissing messages
function dismiss(id, cook, what) {
    SetCookie('dismiss-' + cook, what, 365);
    $(id).style.display = 'none';
    return false;
}

// For setting default homepage
function sethome(what,key) {
    new Ajax.Request('/setdefhome', {
        method: 'post',
        parameters: 'home='+what+'&check='+key, 
        onSuccess: function(xh) {
            if (xh.responseText == 'OK') {
                var t = $('sethome');
                t.className = "tool set-off";
                t.innerHTML = 'Digg Home!';
            }
        }
        });
}

// Make a link be different
function diffclick() {
    window.location.href = mydiffref;
    return false;
}

// My#1 add
function addn1(story, which, check) {
    new Ajax.Request('/addn1', {
        method: 'post',
        parameters: 'item='+story+'&check='+check, 
        onSuccess: function(r) {
            if (r.responseText == 'OK') {
                // Clear them all:
                var i=0;
                var t='';
                while (t = $('myn'+(i++))) {
                    t.innerHTML = '<img src="/img/myone-add.gif" alt="Add as My Number One" width="50" height="16" />';
                    t.onclick = null;
                }
                t = $('myn'+which);
                t.innerHTML = '<img src="/img/myone.gif" alt="Saved as My #1" width="50" height="16" />';
                t.onclick = diffclick;
            }
        }
        });
}

// Undigging
allowun = true;
function undig(story, check) {
    if (allowun) {
        if (confirm('Are you sure you wish to undigg this story?\nYou will not be able to digg it again in the future.')) {
            allowun = false;
            var newform = '<form id="newundigg" action="/undig" method="post"><input type="hidden" name="item" value="' + story + '" /><input type="hidden" name="check" value="' + check + '" /></form>';
            var tag = $('container');
            tag.innerHTML += newform;
            $('newundigg').submit();
        }
    }
}

// Removing a My#1
function unmy1(story, check) {
    if (allowun) {
        allowun = false;
        var newform = '<form id="newunmy1" action="/unmy1" method="post"><input type="hidden" name="item" value="' + story + '" /><input type="hidden" name="check" value="' + check + '" /></form>';
        var tag = $('container');
        tag.innerHTML += newform;
        $('newunmy1').submit();
    }
}

// Needed for IE flash ads - Don't Remove
function phpads_deliverActiveX(content) {
    document.write(content);    
}

// Submit the addtopic hidden form
function addtopic() {
	$('addtopic').submit();
	return false;
}