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 pushing = -9;
var daction = new Array();
function push(row, shade, eventid, pushcheck) {
    $('diglink'+row).innerHTML = '<a href="javascript:void(0)">追蹤</a>';
    if (pushing == row) {
        alert('追蹤中 請稍待!');
        return;
    } else {
        pushing = row;
    }

	daction[row] = new Effect.FadeKeepSpace('diggs-strong-'+row, {
						 duration: 1
						 } );

    new Ajax.Request("/member/push", {
        method: 'post',
        parameters: "eventid="+eventid+"&row="+row+"&pushcheck="+pushcheck,
        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,3) !== 'OK:') {
                    // Do nothing, this is caused 99% of the time by a user abort.
                } else {
					up_pushes(row, tx);
				}
            }
        },
        onFailure: function(xh) {
        	 alert("目前無法連線,請稍候再試!");
        	 daction[row].cancel();
        	 Effect.AppearKeepSpace('diggs-strong-'+row, {duration: 1});
        }
        });
}
function up_pushes(row, tx) {
	// If not finished, wait a bit:
	if (daction[row].state != 'finished') {
		setTimeout('up_pushes(' + 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>已追蹤</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>';
	}
}
var winning = -9;
var daction2 = new Array();
function votefor(row, votewin, eventid, pushcheck, eventview) {
    $('winlink'+votewin+row).innerHTML = '<a href="javascript:void(0)">'+('Y'==votewin?"中":"摃")+'</a>';
    if (winning == row) {
        alert('連結中 請稍待!');
        return;
    } else {
        winning = row;
    }
 	daction2[row] = new Effect.FadeKeepSpace('winlink'+votewin+'-span-'+row, {
					 duration: 0.8
					 } );

    new Ajax.Request("/member/winnersvote", {
     method: 'post',
     parameters: "eventid="+eventid+"&votefor="+votewin+"&pushcheck="+pushcheck,
     onSuccess: function (xh) {
         var tx = xh.responseText;
         if (tx) {
             if (tx.substr(0,6) == 'ERROR:') {
					daction2[row].cancel();
					Effect.AppearKeepSpace('winlink'+votewin+'-span-'+row, {duration: 1});
                 alerttext = tx.replace('ERROR:', '');
                 alert(alerttext);
             } else if (tx.substr(0,3) !== 'OK:') {
                 // Do nothing, this is caused 99% of the time by a user abort.
             } else {
             		up_winnerscount(row, votewin, tx, eventview);
				}
         }
     },
     onFailure: function(xh) {
     	 alert("目前無法連線,請稍候再試!");
     	 daction[row].cancel();
     	 Effect.AppearKeepSpace('winlink'+votewin+'-span-'+row, {duration: 1});
     }
     });
}
function up_winnerscount(row, votewin, tx, eventview) {
	// If not finished, wait a bit:
	if (daction2[row].state != 'finished') {
		setTimeout('up_winnerscount(' + row + ',"'+ votewin + '","' + tx + '","' + eventview+ '")', 100);
	} else {
		split = new RegExp(":");
		var b = tx.split(split);
		var target1 = $('winlink'+votewin+'-span-'+row);
		// Update if bigger
		if (target1.innerHTML < b[1]) {
			target1.innerHTML = b[1];
		} else {
			target1.innerHTML = parseInt(target1.innerHTML) + 1;
		}
		Effect.AppearKeepSpace('winlink'+votewin+'-span-'+row, {duration: 1});
		var target2 = $('winlinkY'+row);
		var target3 = $('winlinkN'+row);
		target2.innerHTML = "<b>中</b>";
		target3.innerHTML = "<b>摃</b>";
		target2.style.color = "#999";
		target3.style.color = "#999";
		if (eventview=='Y'){
			$('winlink'+votewin+'-span-2').innerHTML = target1.innerHTML;
			Effect.AppearKeepSpace('winlink'+votewin+'-span-2', {duration: 1});
			$('winlinkY2').innerHTML = "<b>我中獎了</b>";
			$('winlinkN2').innerHTML = "<b>我摃了</b>";
			$('winlinkY2').style.color = "#999";
			$('winlinkN2').style.color = "#999";
		}

		//if (votewin=='N' )
		//	popt(row,'沒關係! LuckyDog有 '+target1.innerHTML+'個朋友陪您一起摃獎喔!');
		//else if (votewin=='Y' && eventview=='Y' )
		//	popt(row,'恭喜您中獎囉! 歡迎分享您的中獎心得喔!');
		//else
		//	popt(row,'恭喜您中獎囉! 歡迎到<a href="'+$('winners'+row)+'">這裡</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 reportEventError(id, code, row) {
    HideandUNhideObj(row);
    new Ajax.Request('/member/reporterror', {
        method: 'post',
        parameters: "id="+id+"&type="+code,
        onSuccess: function(xh) {
        	var tx = xh.responseText;
        	if (tx.substr(0,2)=='OK')
        	{
	        	if (code=='N' || code=='D')
	        	{
	            $('enclosure'+row).className += ' news-buried';
	            // Need to ensure undiggs look right when buried:
	            $('diglink'+row).className = 'digg-it';
	            if (code=='N')
	            	$('diglink'+row).innerHTML = '<span>非抽獎</span>';
	            else
	            	$('diglink'+row).innerHTML = '<span>重複</span>';
		      }
		      popt(row,"感謝您的回報,我們會儘快處理!");
		   }
		   else {
		   	popt(row,tx);
		   }
        },
        onFailure: function(xh) {
        	 alert("目前無法連線,請稍候再試!");
        }
        });
}

function showGraphCode(name) {
    var target = $('commentgraphiccode');
    target.onclick = null;
    target.style.display = 'block';
    if (!(target.innerHTML.length)) {
        $('submitbutton').disabled = true;
        new Ajax.Request('/getgraphiccode', {
            method: 'get',
            parameters: 'type='+name,
            onSuccess: function(xh) {
                $('commentgraphiccode').innerHTML = xh.responseText;
                $('submitbutton').disabled = false;
            },
            onFailure: function(xh) {
        	 	 alert("目前無法連線,請稍候再試!");
        		}
            });
    }
}

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: 'get',
        parameters: 'username='+name,
        onSuccess: function(xh) {
            var rs = xh.responseText;
            if (rs == '已有人使用'|| rs == '不合法 - 帳號名稱必須在4-15碼，並只能使用英文、數字及底線') {
                $('checkitvalue').innerHTML = '<strong style="background: url(/img/deny.png) 0 1px no-repeat; padding-left: 16px; font-size: 85%; color: #c00;">很抱歉,您輸入的帳號:  "'+name+'" '+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;">恭喜,帳號: "'+name+'" '+rs+'.</strong>';
                form.submit.disabled = '';
            }
        },
        onFailure: function(xh) {
        	 alert("目前無法連線,請稍候再試!");
        }
        });
    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;
}

/*function sethome(category) {
	new Ajax.Request('/sethome', {
        method: 'get',
        parameters: 'category='+category,
        onSuccess: function(r) {
            if (r.responseText == 'OK') {
            	$('homeSpan').className="tool set-off";$('homeSpan').innerHTML="預設類別&nbsp;";
            }
        }
   });
}*/
