var ProductForum = Class.create({
  baseUrl: "/",
  newThreadDialog:null,
  postThreadDialog:null,
  activeThreadId:0,
  newThreadId:0,
  
  initialize: function() {
  },
  
  init: function() {
  this.bindLinks();
  
  if ($('productforum_newthread_form'))
  {
    this.newThreadDialog=$('productforum_newthread_form');
    new Dialog.Box('productforum_newthread_form',{reset_form:false});
  }

  if ($('productforum_postthread_form'))
  {
    this.postThreadDialog=$('productforum_postthread_form');
    new Dialog.Box('productforum_postthread_form',{reset_form:false});
  }
  
  //$$('#productforum_postthread_form form').observe('submit'
  
  },
  bindLinks: function(){

  if ($('productforum_postthread_post_button'))
  {
    $('productforum_postthread_post_button').stopObserving();
    $('productforum_postthread_post_button').observe('click',productForum.sendPost);
  }
  
  if ($('productforum_postthread_cancel_button'))
  {
    $('productforum_postthread_cancel_button').stopObserving();
    $('productforum_postthread_cancel_button').observe('click',function(e){productForum.postThreadDialog.hide()});
  }

  if ($('productforum_newthread_post_button'))
  {
    $('productforum_newthread_post_button').stopObserving();
    $('productforum_newthread_post_button').observe('click',productForum.sendPost);
  }
  
  if ($('productforum_newthread_cancel_button'))
  {
    $('productforum_newthread_cancel_button').stopObserving();
    $('productforum_newthread_cancel_button').observe('click',function(e){productForum.newThreadDialog.hide()});
  }

  if ($('productforum_newthread'))
  {
    $('productforum_newthread').stopObserving();
    $('productforum_newthread').observe('click',function(e){productForum.newThread(e)});
  }

  $$('a.productforum-view-thread').each(function(el){
    Event.stopObserving(el);
  });
  $$('a.productforum-view-thread').each(function(el){
    Event.observe(el,'click',function(e){productForum.loadThread(e)});
  });

  
  $$('a.productforum-post-reply').each(function(el){
    Event.stopObserving(el);
  });
  $$('a.productforum-post-reply').each(function(el){
    Event.observe(el,'click',function(e){productForum.postThread(e)});
  });

  //$('productforum_newthread').observe('click',productForum.newThread);
  
  },
  loadThread:function (event) {
  Event.stop(event);
  var id=this.getParamFromUrl(event.element().href,'thread_id');
  this.loadThreadById(id);
  },
  loadThreadById:function (id) {
  $$('.productforum-thread-container').each(function(el){el.hide()});
  $('productforum_thread_container_'+id).update("<img src='"+SKIN_URL+"images/productforum/loading.gif'/>");
  $('productforum_thread_container_'+id).show();
  new Ajax.Request(productForum.baseUrl+'productforum/product/thread/thread_id/'+id+'/',
  {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseText;
      //$$('.productforum-thread-container').each(function(el){el.hide()});
      $('productforum_thread_container_'+id).update(response);
      //$('productforum_thread_container_'+id).show();
      productForum.bindLinks();
    },
    onFailure: function()
    { 
      $('productforum_thread_container_'+id).update("Error loading thread");
    }
  });
  },
  loadForum:function () {
  var productId=$$("#productforum_postthread_form input[name=product_id]")[0].value;
  new Ajax.Request(productForum.baseUrl+'productforum/product/forum/id/'+productId+'/thread_id/'+productForum.newThreadId,
  {
    method:'get',
    onSuccess: function(transport){
      var response = transport.responseText;
      //$$('.productforum-thread-container').each(function(el){el.hide()});
      $('productforum').replace(response);
      //$('productforum_thread_container_'+id).show();
      productForum.init();
    },
    onFailure: function()
    { 
    }
  });
  },
  sendPost: function(event) {
  Event.stop(event);
  productForum.postThreadDialog.hide();
  productForum.newThreadDialog.hide();
  var params=[];
  if (productForum.activeThreadId>0)
  {
   params=$$('#productforum_postthread_form form')[0].serialize(true);
  }
  else
  {
   params=$$('#productforum_newthread_form form')[0].serialize(true);
  }
  
  new Ajax.Request(productForum.baseUrl+'productforum/product/post/thread_id/'+productForum.activeThreadId+'/',
  {
    parameters: params,
    method:'post',
    onSuccess: function(transport){
      var response = transport.responseText;
      //$$('.productforum-thread-container').each(function(el){el.hide()});
      if (productForum.activeThreadId>0)
      {
        $('productforum_thread_container_'+productForum.activeThreadId).update(response);
        productForum.bindLinks();
      }
      else
      {
        productForum.newThreadId=response;
        productForum.loadForum();
      }
      
      //$('productforum_thread_container_'+id).show();
    },
    onFailure: function()
    { 
      alert('Error posting message!');
    }
  });
  
  },
  postThread:function (event) {
  Event.stop(event);
  var id=event.element().rel;
  productForum.activeThreadId=id;
  $$("#productforum_postthread_form input[name=thread_id]")[0].value=id;
  $$("#productforum_postthread_form textarea")[0].value='';
  this.postThreadDialog.show();
  },
  newThread:function (event) {
  Event.stop(event);
  var id=event.element().rel;
  productForum.activeThreadId=0;
  $$("#productforum_newthread_form input[name=title]")[0].value='';
  $$("#productforum_newthread_form textarea")[0].value='';
  this.newThreadDialog.show();
  },
  getParamFromUrl: function(url,param){
   var re=new RegExp("/"+param+"/(.*?)/");
   var m=url.match(re);
   if (m.length==2) return m[1];
   return null;
  }
  
});

var productForum = new ProductForum();

document.observe('dom:loaded', function(){productForum.init()});
//window.observe('load', function(){productForum.initialize()});
//windows.setInterval("productForum.initialize()",5000);

