var slideURLs = new Array();
var slideCaptions = new Array();
var slideNum = 0;
var oldSlide = -1;
var searchAuthors = new Array();

function smtd_startFckEditor(id) {
	var oFCKeditor = new FCKeditor( id ) ;
	oFCKeditor.BasePath = '/fckeditor/' ;
	oFCKeditor.Width = 700 ;
	oFCKeditor.Height = 600 ;
	oFCKeditor.ToolbarSet = 'SMTD' ;
	oFCKeditor.ReplaceTextarea() ;
}

function searchAuthor(aname) {
  var i;
  var count = 0;
  var resultpane = document.getElementById('search_auth_result');
  resultpane.innerHTML = '';
  if (aname=='') {
    resultpane.innerHTML = 'Cho bi&#7871;t t&#234;n mu&#7889;n t&#236;m';
    return false;
  }
  aname = aname.toLowerCase();
  for (i=0;i<searchAuthors.length;i++) {
    var authorName = searchAuthors[i][0];
    var authorId = searchAuthors[i][1];
    var found = false;
    
    if (aname.length == 1) {
      found = authorName.indexOf(aname) == 0;
    } else if (aname.length <3) {
      found = authorName.indexOf(aname) == 0 || authorName.indexOf(' '+aname)!=-1;
    } else {
      found = (authorName.indexOf(aname)!=-1);
    }
    if (found) {
      count++;
      resultpane.innerHTML += '<div class="author_entry">'+document.getElementById('authorentry_'+authorId).innerHTML+'</div>';
    }
  }
  if (count == 0) {
    resultpane.innerHTML = 'Kh&#244;ng t&#236;m th&#7845;y &quot;'+aname+'&quot;';
  }
  return false;
}

function searchAuthorInSelect(sel,aname) {
  var i,j;
  var found = false;
  if (!sel) {
    alert(sel.name);
    return;
  }
  if (aname=='') {
    alert('Cho biet ten tac gia!');
    return false;
  }
  aname = aname.toLowerCase();
  for (j=0;j<sel.options.length;j++) {
    sel.options[j].style.backgroundColor='white';
  }
  for (i=0;i<searchAuthors.length;i++) {
    var authorName = searchAuthors[i][0];
    var authorId = searchAuthors[i][1];
    var just_found;
    
    if (aname.length == 1) {
      just_found = authorName.indexOf(aname) == 0;
    } else if (aname.length <3) {
      just_found = authorName.indexOf(aname) == 0 || authorName.indexOf(' '+aname)!=-1;
    } else {
      just_found = (authorName.indexOf(aname)!=-1);
    }
    if (just_found) {
      for (j=0;j<sel.options.length;j++) {
        if (parseInt(sel.options[j].value) ==  authorId) {
          if (!found) sel.selectedIndex = j;
          sel.options[j].style.backgroundColor='yellow';
        }
      }
      found = true;
    }
  }
  if (!found) {
    alert('Khong tim thay "'+aname+'"');
  }
  return false;
}


function checkDiscussionSubmit(f) {
  if (f.body.value.length<5) {
    alert("Cho biet noi dung!");
    f.body.focus();
    return false;
  }
  f.B.disabled = true;
  return true;
}

function startSmtd() {
  if (document.getElementById('slide')) doSlideShow();
}

function doSlideShow() {
  slideShowPhoto(slideNum);
  slideNum += 1;
  if (!slideURLs[slideNum]) slideNum = 0;
  setTimeout('doSlideShow()',5000);
}

function slideShowPhoto(num) {
  var slideshow = document.getElementById('slide');
  var oldThumb = document.getElementById('albumthumb'+oldSlide);
  var newThumb = document.getElementById('albumthumb'+num);
  
  if (oldSlide == num) return false;
  
  slideshow.innerHTML = '<img src="'+slideURLs[num]+'"><div class="caption">'+slideCaptions[num]+'</div>';
  slideNum = num;
  if (oldThumb) {
    oldThumb.className = 'normal';
  }
  if (newThumb) {
    newThumb.className = 'selected';
  }
  oldSlide = num;
  return false;
}

function closePhoto() {
	var picframeborder=document.getElementById('picframeborder');
	var picframe = document.getElementById('picframe');
        picframeborder.innerHTML = '';
	picframeborder.style.display='none';
        return false;
}

function enlargePhoto_cb(request) {
  var picframeborder= document.getElementById('picframeborder');
  picframeborder.innerHTML = request.responseText;
}

function enlargePhoto(photoid) {
    var screenWidth = parseInt(document.body.clientWidth);
    var screenHeight = parseInt(document.body.clientHeight);
    var picframeborder= document.getElementById('picframeborder');
    var width = 500;
    var height = 500;

    picframeborder.style.width=width+'px';
    picframeborder.style.height=height+'px';
    picframeborder.style.left=Math.round((screenWidth-width)/2)+'px';
    picframeborder.style.top=Math.round((screenHeight-height)/2+parseInt(document.body.scrollTop))+'px';
    picframeborder.style.display = 'block';
    picframeborder.innerHTML = 'Please wait ...';
    return sendRequest('index.php','c=photo&p='+photoid,enlargePhoto_cb);
}

function getHtmlRequest() {
  var request = false;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
  return request;
}

function sendRequest(url,query,callback_func) {
  request = getHtmlRequest();
  if (!request) return true;
  request.open("POST", url, true);
  request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  request.setRequestHeader("Content-length", query.length);
  request.setRequestHeader("Connection", "close");

  request.onreadystatechange =
    function () {
      if (request.readyState==4 && request.status==200) {
        callback_func(request);
      }
    };
  request.send(query);
  return false;
}

function confirmDelete(url) {
    window.name='main';
    if (confirm('Delete?')) window.open(url,'main');
    else return;
}

function addEvent(obj, evType, fn)
{
  if (obj.addEventListener)
  {
    obj.addEventListener(evType, fn, false);
    return true;
  }
  else if (obj.attachEvent)
  {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
  else
  {
    return false;
  }
}


