var httpstatus = 0;
var req;

function emoticon(text) {
      text = " " + text + " ";
      var txtarea = this.document.editmsg.text;
      if (txtarea.createTextRange) {
            // var caretPos = txtarea.createRange();
            // caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
            txtarea.value += text;
      } else {
            var selLength = txtarea.textLength;
            var selStart = txtarea.selectionStart;
            var selEnd = txtarea.selectionEnd;
            if ( (selEnd === 1) || (selEnd === 2) ) {
                  selEnd = selLength;
            }

            var s1 = (txtarea.value).substring(0, selStart);
            var s2 = (txtarea.value).substring(selStart, selEnd);
            var s3 = (txtarea.value).substring(selEnd, selLength);
            txtarea.value = s1 + text + s2 + s3;
      }
      txtarea.focus();
}

function addupload() {
      var i;
      var text;
      var div;
      for (i = totalupload; i < maxupload; i++) {
      // if (totalupload < maxupload) {
            text = this.document.getElementById("uploadslots");
            text.innerHTML = text.innerHTML + "<input name=\"userfile[]\" size=\"60\" type=\"file\" /><br />";
            totalupload++;
      }
      if (totalupload === maxupload) {
            div = this.document.getElementById("addslot");
            div.style.display = 'none';
      }
}

function getpic(obj) {
      id = obj.attr('id').substr(obj.attr('id').indexOf("_") + 1);
      picid = obj.attr('id').substr(obj.attr('id').indexOf("-") + 1);
      
      $("#picback_" + id).css("background", "url(" + baseurl + "images/main/icons/wait.gif) no-repeat");

      $.ajax({
            type: 'GET',
            url: baseurl + 'forum/file',
            data: { id: picid },
            dataType: 'text',
            async: true,
            success: function(data) {
                  obj.unbind("click");
                  obj.attr("href", baseurl + "gallery/picture/" + picid);               
                                    
                  $("#pic_" + id).one("load", function() {
                        $("#picback_" + id).css("background", "none");                  
                        forum_checkpicwidth($(this), 700, 700);                  
                  });
                  
                  $("#pic_" + id).attr("src", data);                  
            },
            error: function(request, status, error) {
                  alert("error : " + request + status + error);
            }
      });    

      return true;
}

function confirmmsgdelete(url, msg) {
      if (this.window.confirm(msg)) {
            this.window.location.href = url;
      }
}

function confirmfiledelete(url, msg) {
      if (this.window.confirm(msg)) {
            this.window.location.href = url;
      }
}

function expandblock(id) {
      div = this.document.getElementById(id);
      if (div.style.height != 'auto') {
            div.style.height = 'auto';
      } else {
            div.style.height = '10px';            
      }
}

function expandevalmsg(id) {
      div = this.document.getElementById(id);
      if (!div.style.visibility || div.style.visibility == 'hidden') {
            div.style.visibility = 'visible';
            div.style.display = 'block';
      } else {
            div.style.visibility = 'hidden';
            div.style.display = 'none';
      }
}

function expandmsg(id) {
      div = this.document.getElementById(id);
      //if (div.style.height != 'auto') {
            div.style.height = 'auto';
      //} else {
            //div.style.height = '30px';            
      //}      

      img = this.document.getElementById('arrow' + id);
      img.style.visibility = 'hidden';
      img.style.display = 'none';
}

function forum_checkpicwidth(obj, maxwidth, maxheight) {              
      if (obj.width() > obj.height()) {
            if (obj.width() > maxwidth) {
                  obj.width(maxwidth);
            } else if (obj.height() > maxheight) {
                  obj.height(maxheight);
            }
      } else {
            if (obj.height() > maxheight) {
                  obj.height(maxheight);
            } else if (obj.width() > maxwidth) {
                  obj.width(maxwidth);
            }    
      }      
}

$(document).ready(function() {
      $("a[id*=picanchor]").bind("click", function() {
            getpic($(this));
      });      
});

