var curVote = '';


function hideShowEl(elID,shown){
  var el0 = document.getElementById(elID);
  if (shown){
    if (el0) el0.style.display="block";  
  }else{
    if (el0) el0.style.display="none";
  }
}

function toggleWaitingArea (shown){
  hideShowEl('msgPanel',false);
  if (shown){
    $('#voteBtnUp').hide();
    $('#waitingPanel').show();
  }else {
    $('#voteBtnUp').show();
    $('#waitingPanel').hide();
  }
}

function isCookieSet(){
    var uName = Util.readCookie('user');
    //console.debug(uName);
    //console.debug(myVoteImg.curImgId);
    if (!uName){
      //alert('hello');
      Util.mySimpleDialog.show();
      return true;
    }    
  return false;
}
function onThumbUp(){
  //if (isCookieSet()) return;
  var val0 = document.getElementById('myHiddenVal');
  toggleWaitingArea(true);
  curVote = 'up';
  var sUrl = '/public/vote_up/' + val0.value;
  $.ajax({
   url: sUrl,
   success: function(t1){
      toggleWaitingArea(false);
      if (t1[0]){
        $("#thumbUp").fadeOut("quick",function(){
          $('#msgPanel').text(t1[1]+' votes now, Thank you!').show();
        });
      }else{
        showMsg(t1[1],'err');
      }
   },
   error : responseFailure,
   dataType : 'json'
  });
  return false;
};

function onFlagIt(){
  var val0 = document.getElementById('myHiddenVal');
  var sUrl = '/public/flag/' + val0.value;
  var reply = prompt("What's reason you don't think it's appropriate?", "")
  if(reply){
    $.ajax({
     url: sUrl+'?reason='+reply,
     success: function(t1){
        if (t1[0]){
          $('#flag_it').text('Got it! Thank you!');
        }else{
          showMsg(t1[1],'err');
        }
     },
     error : responseFailure,
     dataType : 'json'
    });
  }
  return false;
};

function showMsg(msg,type){
  var val0 = document.getElementById('msgPanel');
  val0.style.display="block";
  val0.innerHTML = msg;
  if (type == 'err'){
    val0.style.color='#FF0000';
  }else{
    val0.style.color='#F1F1F1';
  }

}
function responseFailure(o){
  curVote = '';
  toggleWaitingArea(false);
  showMsg('Error Occured while trying to Connect server, Please Check!','err');
};

function commentSuc(){
  //lazy here: need to use ajax handler to refresh the comment list part:
  document.location.reload();
}

function submitcomment(the_url,elId){
  var comment = $('#'+elId)
  var textVal = $.trim(comment.val());
  if (textVal == '') {
    alert('Please Provide your Comment before submit, thanks!','err');
    return;
  }
  $.ajax({
   type: "POST",
   url: the_url,
   data: 'content='+textVal,
   dataType: 'json',
   success: function(t1){
      toggleWaitingArea(false);
      if (t1[0]){
        window.location.reload();
      }else{
        showMsg(t1[1],'err');
      }
   },
   error : responseFailure
  });
  comment.val("");
}


