var haveinner;
var insertdivb;

function _manage_club_status(clubSTATUSinput){
  op = "do=setCLUBstatus&club_status="+clubSTATUSinput;
  action = "forums_process";
  inner_div = "";
  ajaxType = "";
  ajaxMethod = "post";
  myForm = "";
  ajaxSendRequest(op,action,inner_div,ajaxType,ajaxMethod,myForm);

}

function _show_manage_forum(){
  var screen_width_val = screen.width;
  var screen_hight_val = screen.height;
  $('allen_lightbox').style.width = screen_width_val + 'px';
  $('allen_lightbox').style.height = screen_hight_val + 'px';
  $('allen_lightbox').style.display = "block";
  document.body.style.overflow="hidden";
  $('manage_forum').style.left = screen_width_val/2 + 'px';
  $('manage_forum').style.top = '250px';
  $('manage_forum').style.width = '200px';
  $('manage_forum').style.display = "block";


}

function _top_posts(topThredsID){

    var toplistsid = 'post_list_id_'+topThredsID;
    $('top_form_threads_id').value = topThredsID;
    $('top_form_do').value = 'top_process';
    //new Effect.Highlight(toplistsid ,{startcolor:'#ffffff', endcolor:'#999999'});
    //Effect.Fade(toplistsid);
    send('top_form');
  return false;
}


function _delete_posts(deletePostsID,deleteThredsID){  
   
  $('delete_form_posts_id').value = deletePostsID;
    var deletelistsid = 'post_list_id_'+deletePostsID;
  if(deleteThredsID != ''){
    $('delete_form_threads_id').value = deleteThredsID;
    $('delete_form_deletype').value = 'xxxxx';
  }else
    $('delete_form_deletype').value = 'posts';
     
    $('delete_form_do').value = 'delete_process';
    
    new Effect.Highlight(deletelistsid ,{startcolor:'#ffffff', endcolor:'#999999'});
    Effect.Fade(deletelistsid);
  send('delete_form');
  return false;
}

function edit_posts(postsID,postAuthor){
  var screen_width_val = screen.width;
  var screen_hight_val = screen.height;

  $('allen_lightbox').style.width = screen_width_val + 'px';
  $('allen_lightbox').style.height = screen_hight_val + 'px';
  $('allen_lightbox').style.display = "block";

  subjectlistid = 'subject_list_'+postsID;
  
  messagelistid = 'message_list_'+postsID;


  postsSubject = $(subjectlistid).innerHTML;
  inner_message_Txt = $(messagelistid).innerHTML;


  document.body.style.overflow="hidden";
  $('post_threads_type').innerHTML ="編輯";
  hide_div('threads_article'); 
  $('posts_threads').style.x = screen_width_val/2-350;
  $('posts_threads').setAttribute("TOP","0");
  $('posts_threads').style.zIndex = 2;
  $('posts_threads').style.backgroundColor = "#FFFFFF";
  $('posts_threads').style.display = "block";
  $('posts_id').value = postsID ;
  $('form_author').value = postAuthor ;
  $('form_subject').value = postsSubject ;
  $('form_message').value = inner_message_Txt ;
  return false;

}

function posts_posts(){

  var screen_width_val = screen.width;
  var screen_hight_val = screen.height;

  $('allen_lightbox').style.width = screen_width_val + 'px';
  $('allen_lightbox').style.height = screen_hight_val + 'px';
  $('allen_lightbox').style.display = "block";
  $('threads_article').style.display = "block";
  document.body.style.overflow="hidden";
  $('post_threads_type').innerHTML ="回應";

  $('posts_threads').style.x = screen_width_val/2-350;
  $('posts_threads').setAttribute("TOP","0");
  $('posts_threads').style.zIndex = 2;
  $('posts_threads').style.backgroundColor = "#FFFFFF";
  $('posts_threads').style.display = "block";

  $('posts_id').value = '' ;
  $('form_subject').value = '' ;
  $('form_message').value = '' ;

  

  return false;
}




function posts_threads(){

  var screen_width_val = screen.width;
  var screen_hight_val = screen.height;
  
  $('allen_lightbox').style.width = screen_width_val + 'px';  
  $('allen_lightbox').style.height = screen_hight_val + 'px';
  $('allen_lightbox').style.display = "block";

  document.body.style.overflow="hidden";

  $('posts_threads').style.x = screen_width_val/2-350;
  $('posts_threads').setAttribute("TOP","0");
  $('posts_threads').style.zIndex = 2;
  $('posts_threads').style.backgroundColor = "#FFFFFF";
  $('posts_threads').style.display = "block";

  window.location.href="#";

  return false;
}

function addElement(add_divs) {
  var ni = $('exDiv');
  var newdiv = document.createElement('div');
  var divIdName = 'my'+add_divs_id+'Div';
  newdiv.setAttribute('id',divIdName);
  ni.appendChild(newdiv);
}

function addThread(texts,add_divs_id){
  var a = document.createElement("div");
  var divIdName = 'my'+add_divs_id+'Div';
  a.setAttribute('id',divIdName);
  a.setAttribute('width','900px');
  a.innerHTML = texts;

  //var b = document.createElement("div");
  //b.innerHTML="This is row 2, cell 12";
  if(haveinner!=null){
    var b = document.getElementById("xd");
    var parentDiv = b.parentNode;
    parentDiv.insertBefore(a,b);
  }else{

    $('xd').appendChild(a);
  }
  insertdivb = divIdName; 
  haveinner = 1;
  return false;
}

function hide_div(hide_divs){
  
  var hide_dive_array = hide_divs.split(",");
  var ni = $('exDiv');
  document.body.style.overflow='';
  /*
  alert(hide_dive_array.length);
  alert(hide_dive_array[0]);
  alert(hide_dive_array[1]);
  */

  
  for ($i = 0;$i<hide_dive_array.length;$i++){
    var tmp_hide_div = hide_dive_array[$i];
    
    Element.hide(tmp_hide_div);
/* 
    new Effect.Highlight(tmp_hide_div ,{startcolor:'#ffffff', endcolor:'#999999'});
    Effect.Fade(tmp_hide_div);

    newdiv = document.createElement('div');
    newdiv.setAttribute('id',tmp_hide_div);
    ni.appendChild(newdiv);
*/
  

  }
}

  function send(send_typ){
    $('send_form_button').disabled=true;
    if (send_typ == 'default')
        op = 'op=changeid&id=';
    else if (send_typ == 'getFormTable'){
         alert('x');

    }else if (send_typ == 'myPostThreadsForm'){
         op = '';
         action = 'forums_process';
         inner_div = '';
         ajaxType = '';
         ajaxMethod = 'post';
         myForm = "myPostThreadsForm";
    }else if(send_typ == 'delete_form'){
         op = '';
         action = 'forums_process';
         inner_div = '';
         ajaxType = '';
         ajaxMethod = 'post';
         myForm = "delete_form";


    }else if(send_typ == 'top_form'){
         op = '';
         action = 'forums_process';
         inner_div = '';
         ajaxType = '';
         ajaxMethod = 'post';
         myForm = "top_form";

    }else{
        op = 'op=check&id='+$('idcheck').value;
        ajaxSendRequest(op,'id.change.php','content_id');
        op = 'op=changeid&id='+$('idcheck').value;
    }
     ajaxSendRequest(op,action,inner_div,ajaxType,ajaxMethod,myForm);
  }
 
  function ajaxSendRequest(ops,actions,inner_divs,ajaxTypes,ajaxMethods,myForms) {
    Element.show('loading');
    /*
    var actions = action;
    var inner_divs = inner_div;
    var ajaxTypes = ajaxType;
    var myForms = myForm;
    */
    //alert(phpfile+'\r\n'+inner_divs);
    if(ops)
      var params = ops;
    else{
      var params = Form.serialize($(myForms));
    }




    if(ajaxType != 'update'){
          var myAjax = new Ajax.Request(
              actions,{
                method:ajaxMethods, 
                asynchronous:true, 
                parameters:params, onComplete: showAJAXResponse
              }
     );
     }else if (ajaxType == 'update'){
        var myAjax = new Ajax.Updater(
              inner_div, 
              actions,{
                  method:ajaxMethod,
                  parameters:params,
                  onFailure:function(){
                  Element.hide('loading');
                  },onComplete: showAJAXResponse
            });

     }
    
    
    var screen_width_val = screen.width;
    var screen_hight_val = screen.height;
    //$('allen_lightbox').style.width = screen_width_val + 'px';  
    //$('allen_lightbox').style.height = screen_hight_val + 'px'; 
    //$('content_id').style.x = screen_width_val-960;  
    $('loading').style.x = screen_width_val-960;  

    return false;
    }

    function showAJAXResponse(originalRequest){
        Element.show('loading');
        var return_messsage = originalRequest.responseText
        return_messsage = return_messsage.parseJSON();
        if (return_messsage[0] == 'success'){
            if(return_messsage[3]!= 'updateClubstatus'){
            $('myPostThreadsForm').disabled=false;
            Form.reset('myPostThreadsForm');
            hide_div('allen_lightbox,posts_threads');
            var article = return_messsage[2];
            }else{

            hide_div('allen_lightbox,manage_forum');
            }
            //alert(originalRequest.responseText);
            if(return_messsage[3] == "new_posts"){
                alert(return_messsage[1]);
            
               setTimeout(location.href=article.link,1500);

            }
            if(return_messsage[3] == "updateClubstatus"){
                alert(return_messsage[1]);
               setTimeout(location.href="forum_manage",1500);

            }


            if(return_messsage[3] == "delete_threads"){
                alert(return_messsage[1]);
               setTimeout(location.href="forum_index",1500);

            }
            if(return_messsage[3] == "new_threads"){
               alert(return_messsage[1]);
               setTimeout(location.href="forum_index",1500);


            }



      //location.href=article.link; 
        if (article!=null) {
            for (i = 0; article[i]!=null; i++) {
                var temp_delete_listid = "threads_list_"+article[i]._id;
                text = "";
                temptextid = "";
                text = _process_innerhtml_text(return_messsage[3],article[i]);
                temptextid = article[i]._id; 
                addThread(text,temptextid);
                
            } 

        }

        }
        

//        alert(return_messsage[1]);
        Element.hide('loading');
        $('send_form_button').disabled=false;
    return true;
    }

    function _process_innerhtml_text(process_type,article){
       switch (process_type){
          case 'new_threads':
                 text +="<div id = \"threads_list_"+article._id+"\">";
                text +="<table width = \"100%\"><tr class=\"altrow\">";
                text +="<td width=\"5%\"  class=\"gary_12\"></td>";
                text +="<td width=\"38%\" align=\"left\"  class=\"gary_12\"><a href=\"forum_post&threads_id="+article._id+"\">"+article._subject+"</a></td>";
                text +="<td width=\"13%\"align=\"center\"  class=\"green_12\">";

                if(article._blog_id){
                  text +="<a href=\"http://blog.yam.com/"+article._blog_id+"\">"+article._author+"</a>";
                }else{
                  text +=article._author;
                }

                text +="</td>";
                text +="<td  width=\"6%\" align=\"center\"  class=\"main_text_12\">"+article._views+"</td>";
                text +="<td  width=\"6%\" align=\"center\"  class=\"main_text_12\">"+article._replies+"</td>";
                text +="<td   width=\"13%\" align=\"center\" class=\"green_12\"><a href=\"#\">"+article._lastposter+"</a></td>";
                text +="<td align=\"center\"  class=\"main_text_11\">"+article._parsetime+"</td>";
                text +="</tr>";

                text +="</table>";

                text +="</div>";

            

          break;

          case 'new_posts':
               /*
<div id = "post_list_id_<{$post->_id}>">
<table width = "100%">
<tr>
<td width="108" align="center" valign="top" class="line">
<div class="dis" style="margin:10px;">
<p>
<{if $post->_blog_id}>
<a href="http://blog.webs-tv.net/<{$post->_blog_id}>/" target ="_blank"><img src="<{$post->get_pic()}>"></a>
<{else}>
<a href="#" ><img src="<{$post->get_pic()}>"></a>
<{/if}>
</p>
<h5>
<{if $post->_blog_id}>
<a href="http://blog.webs-tv.net/<{$post->_blog_id}>/" target = "_blank"><{$post->_Nickname}></a>
<{else}>
<{$post->_Nickname}>
<{/if}>
</h5>
</div>
<div style="clear:both;"><a href="#"><!--img src="i/bt_5.gif" width="88" height="27" border="0" /--></a></div>
</td>
<td align="center" class="dot_line_left line">

<table width="98%" cellspacing="0" cellpadding="0" style="margin-top:8px;">

<tr>
<td width="19"><img src="../i/media_8.gif" width="19" height="23" /></td>
<td align="left" bgcolor="#EDEDED" class="blue_13"><a href="#"><{$post->_subject}></a></td>
<td width="45" align="right" bgcolor="#EDEDED" class="title_gary">
&lt;<{ $pager->_pageNum*20+$smarty.foreach.posts_loop.iteration-20}>F&gt;
</td>
<td width="7" align="right"><img src="../i/media_8b.gif" width="7" height="23" /></td>
</tr>
</table>
<!-- preword -->
<div class="prew">

<{$post->_message}>


</div>
<!-- /preword -->
<!-- postdate -->
<div class="r black_12">
<{if $userdata.username !=""}>
  <{if $post->_blog_id == $userdata.username}>
    〔<a href="#" onclick = "edit_posts('<{$post->_id}>','<{$post->_Nickname}>','<{$post->_subject}>','<{$post->_message}>');">編輯</a>〕
  <{/if}>
  <{if $smarty.session.status == 3}>
〔<a href="#"  onclick = "if(confirm('確定要置頂嗎'))_top_posts('<{if $post->_first == 1}><{$threadsdata.0->_id}><{/if}>');">置頂</a>〕
  <{/if}>
  <{if $post->_blog_id == $userdata.username}>
    〔<a href="#" onclick = "if(confirm('確定要delete嗎'))_delete_posts('<{$post->_id}>','<{if $post->_first == 1}><{$threadsdata.0->_id}><{/if}>');" >刪除</a>〕

  <{/if}>
<{/if}>

<{if $post->_first == 1}>
〔<a href="#"  onclick="posts_posts();">回覆此篇</a>〕
<{/if}>


</div>
<div class="r black_12">發表時間：<{$post->_replies_time|date_format:"%Y/%m/%d %H:%M:%S"}></div>
<!-- /postdate -->
</td>
</tr>
</table>
</div>


               */ 

          break;
      }


      return text;

    }

    
    function resetthisForm(restarForm){
        Form.reset(restarForm);
        return false;

    }


