function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function initPosse() {
	var posse_nickname = readCookie('posse_nickname');
        if(posse_nickname==null) posse_nickname='';
        posse_nickname = posse_nickname.replace('!!!SEMICOLON!!!',';');
	posse_nickname = posse_nickname.replace('!!!COLON!!!',',');
	var posse_id = readCookie('posse_id');
	var posse_avatarid = readCookie('posse_avatarid');
	var posse_checksum = readCookie('posse_checksum');
	//if (posse_nickname!=null && posse_nickname!='' && posse_id!=null && posse_id!='' && posse_checksum!=null && posse_checksum!='' && posse_avatarid!=null) {
	if (posse_nickname!=null && posse_nickname!='' && posse_id!=null && posse_id!='' && posse_checksum!=null && posse_checksum!='') {
		posse_id = posse_id.replace(/"/g,'');
		document.getElementById('posse_nickname_slot').innerHTML = posse_nickname;
		document.getElementById('posse_id_slot').innerHTML = '<a style="text-decoration: underline;" target="_blank" href="http://posse.perezhilton.com/accounts/' + posse_id + '">Edit profile</a> ';
                if(posse_avatarid == '' || posse_avatarid == null) {
                        document.getElementById('posse_emptyavatar_note').innerHTML = 'Hey ' + posse_nickname + ' - don\'t be shy, upload your avatar!';
                        document.getElementById('posse_emptyavatar_note').style.display = 'block';
                }
                toggleLoggedIn(1);
		initCommentModding();
	}
	else {
                toggleLoggedIn(0);
	}
}
function logMeOut() {
        eraseCookie('posse_id','.perezhilton.com');
        eraseCookie('posse_avatarid','.perezhilton.com');
        eraseCookie('posse_nickname','.perezhilton.com');
        eraseCookie('posse_checksum','.perezhilton.com');
        document.getElementById('logout-frame').src = 'http://posse.perezhilton.com/static/logout.html';
        toggleLoggedIn(0);
}
function toggleLoggedIn(flag) {
        if (flag) {
                document.getElementById('comment-loggedin').style.display = 'block';
                document.getElementById('comment-login').style.display = 'none';
        }
        else {
                document.getElementById('comment-login').style.display = 'block';
                document.getElementById('comment-loggedin').style.display = 'none';
        }
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;

}
function createCookie(name,domain,value,days) {
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
         }
         else var expires = "";
         document.cookie = name+"="+value+expires+"; path=/; domain=" + domain;
}
function eraseCookie(name,domain) {
        createCookie(name,domain,"",-367);
}
//function createCookie(name,value,days) {
//  if (days) {
//    var date = new Date();
//    date.setTime(date.getTime()+(days*24*60*60*1000));
//    var expires = "; expires="+date.toGMTString();
//  }
//  else var expires = "";
//  document.cookie = name+"="+value+expires+"; path=/";
//}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function docb(id, value) {
  var mysack = new sack('/services/comment_mod/post.php');
  mysack.execute = 1;
  mysack.method = 'GET';
  mysack.setVar( "comment_id", id );
  mysack.setVar( "value", value );
  mysack.encVar( "cookie", document.cookie, false );
  mysack.runAJAX();
  return true;
}
function modCommentUP(id) {
  docb(id,"up");
}
function modCommentDOWN(id) {
  docb(id,"down");
}
function modCommentREPORT(id) {
  if(confirm("You're about to report a comment that is offensive or inappropriate. Are you sure you'd like to proceed?")) docb(id,"report");
}
function modCommentOK(id) {
  document.getElementById('mod_tool_'+id).innerHTML = "<div title='You have already rated this comment' class='voted''><img src='http://img.perezhilton.com/images/commentmod/voted.png'/></div>";
  var cookie = readCookie('comment_modding') != null ? readCookie('comment_modding')+'#'+id : id;
  createCookie('comment_modding', cookie, 365);
}
function initCommentModding() {
  var divs = document.getElementsByTagName('div');
  for(var i=0;i<divs.length;i=i+1) {
    if(divs[i].className == 'comment_mod_container') divs[i].style.display = "block";
  }
  var votes = readCookie('comment_modding');
  if(votes!=null) {
    votes = votes.split('#');
    for(var i=0;i<divs.length;i=i+1) {
      for(var j in votes) {
        if(divs[i].id == 'mod_tool_'+votes[j]) {
         divs[i].innerHTML="<div title='You have already rated this comment' class='comment_mod_voted'><img src='http://img.perezhilton.com/images/commentmod/voted.png'/></div>";
        }
      }
    }
  }
}

addLoadEvent(initPosse);
