 var screen_name = null;
var first_name = null;
var last_name = null;
var comment = null;
var user = null;
var time = null;
var url = null;
var signature = null;
var avatar = null;
var loggedin = null;
var page = null;
var comment_id = null;

function ajax_request(url, data, func)
{
    var myAjax = new Ajax.Request
    (
        url,
        {method: 'post', parameters: data, onComplete: func}
    );
}

function init()
{
   if($('login') != null)
   {
      Event.observe($('login'),'submit',login, false);
      loggedin = false;
   }
   if($('logout') != null)
   {
      Event.observe($('logout'),'click',logout, false);
      loggedin = true;
   }
   if($('try_again') != null)
   {
      Event.observe($('try_again'),'click',try_again, false);
   }
   if($('comment_text') != null)
   {
      update_comment_preview();
      Event.observe($('comment_text'),'keyup',update_comment_preview, false);
   }
   if($('contents') != null && $('comment-preview') != null)
   {
      update_thread_preview();
      Event.observe($('contents'),'keyup',update_thread_preview, false);
   }   
   if($('add_pr_fields') != null)
   {
      update_pr_headline();
      update_pr_preview();
      Event.observe($('pr_headline'),'keyup',update_pr_headline, false);
      Event.observe($('pr_contents'),'keyup',update_pr_preview, false);
      Event.observe($('pr_preview'),'click',pr_preview_warn, false);
      Event.observe($('pr_edit'),'click',pr_preview_edit, false);
      Event.observe($('add_pr_form'),'submit',pr_submit_warn,false);
   }   
   if($('leavecomment') != null)
   {
      Event.observe($('leavecomment'),'submit',leave_comment, false);
   }
   if($('threadreply') != null)
   {
      Event.observe($('threadreply'),'submit',thread_reply, false);
   }
   if($('edit_profile') != null)
   {
      Event.observe($('edit_profile'),'click',show_profile_editor, false);
   }
   /*
   if($('save_profile') != null)
   {
      Event.observe($('edit_profile'),'submit',hide_profile_editor, false);
   }
   */
   if($('password_fields') != null)
   {
      $('password_fields').style.display = "none";
      Event.observe($('password_title'),'click',toggle_password_fields, false);
      $('password_title').style.cursor = 'pointer';
   }
   if($('upload_control') != null &&
      $('create_control') != null)
   {
         $('upload_accord').style.display = ($('upload_accord').className!="display"?"none":"block");
         $('create_accord').style.display = ($('create_accord').className!="display"?"none":"block");
         
         $('upload_control').style.cursor = $('create_control').style.cursor = 'pointer';
         Event.observe($('upload_control'),'click',upload_control_toggle,false);
         Event.observe($('create_control'),'click',create_control_toggle,false);
   }   

   allNodes = document.getElementsByClassName("external");
   for(i = 0; i < allNodes.length; i++)
   {
      allNodes[i].onclick = function(){window.open(this.href);return false;};
   }
   
   allNodes = document.getElementsByClassName("quote");   
   for(i = 0; i < allNodes.length; i++)
   {
      allNodes[i].onclick = function(){
      var id = this.href.replace(/^.*?(\d+)\/#commentform$/,'$1');
      $('comment_text').value += '<blockquote><strong>'+$('a'+id).innerHTML+" said:</strong>\n" +
                                  $('p'+id).innerHTML.replace(/\n/g,'').replace(/<br\s*?\/?\s*?>/mg,"\n")+"</blockquote>\n";
      location.href='#commentform';
      $('comment_text').focus();
      update_comment_preview();
      return false;
      };
   }
   
   allNodes = document.getElementsByClassName("confirm");
   for(i = 0; i < allNodes.length; i++)
   {
      allNodes[i].onclick = function(){return confirm("Are You Sure You Want To Do That?");};
   }
}

function upload_control_toggle()
{
   $('upload_accord').style.display = ($('upload_accord').style.display == "none" ? "block" : "none" );
}

function create_control_toggle()
{
   $('create_accord').style.display = ($('create_accord').style.display == "none" ? "block" : "none" );
}

function toggle_password_fields()
{
   $('password_fields').style.display = ($('password_fields').style.display == 'none') ? 'block' : 'none';
}

function requires_login()
{
   return !(new RegExp("http:\/\/"+location.host+"\/(contact|about|legal|join)").test(location.href));
}

function show_article_fields()
{
   new Rico.Effect.FadeTo('article_fields', 0, 1, 1);
   $('article_fields').style.display='block';
   $('feed_fields').style.display='none';
   $('resource_fields').style.display='none';
   new Rico.Effect.FadeTo('article_fields', 1, 1000, 20);
}
function show_feed_fields()
{
   new Rico.Effect.FadeTo('feed_fields', 0, 1, 1);
   $('article_fields').style.display='none';
   $('feed_fields').style.display='block';
   $('resource_fields').style.display='none';
   new Rico.Effect.FadeTo('feed_fields', 1, 1000, 20);
}
function show_resource_fields()
{
   new Rico.Effect.FadeTo('resource_fields', 0, 1, 1);
   $('article_fields').style.display='none';
   $('feed_fields').style.display='none';
   $('resource_fields').style.display='block';
   new Rico.Effect.FadeTo('resource_fields', 1, 1000, 20);
}

function show_profile_editor(e)
{
   Event.stop(e);
   $('data_display').style.display='none';
   $('data_editor').style.display='block';
   new Rico.Effect.FadeTo('data_editor', 0, 1, 1);
   new Rico.Effect.FadeTo('data_editor', 1, 500, 50);
}

function thread_reply(e)
{
   Event.stop(e);
   ajax_request(document.location.href.replace(/#.*$/mg,''),Form.serialize('threadreply')+'&method=ajax', thread_handler);
}

function leave_comment(e)
{
   Event.stop(e);
   ajax_request(document.location.href.replace(/#.*$/mg,''),Form.serialize('leavecomment')+'&method=ajax', comment_handler);
}

function comment_handler(originalRequest)
{
   if(originalRequest.responseText != 'failure')
   {
      comment_id = getTagContents('comment_id',originalRequest.responseText);
      comment = getTagContents('comment',originalRequest.responseText);
      user = getTagContents('user',originalRequest.responseText);
      screen_name = getTagContents('screen_name',originalRequest.responseText);
      avatar = getTagContents('avatar',originalRequest.responseText);
      time = getTagContents('time',originalRequest.responseText);

      new Rico.Effect.FadeTo('comment-preview', 0, 500, 10, {complete:switch_comment});
      $('comment_text').value = '';
   }
}

function thread_handler(originalRequest)
{
   if(originalRequest.responseText != 'failure')
   {
      comment_id = getTagContents('comment_id',originalRequest.responseText);
      comment = getTagContents('comment',originalRequest.responseText);
      user = getTagContents('user',originalRequest.responseText);
      screen_name = getTagContents('screen_name',originalRequest.responseText);
      avatar = getTagContents('avatar',originalRequest.responseText);
      signature = getTagContents('signature',originalRequest.responseText);
      time = getTagContents('time',originalRequest.responseText);

      new Rico.Effect.FadeTo('comment-preview', 0, 500, 10, {complete:switch_thread});
      $('comment_text').value = '';
   }
}

function getTagContents(tag, string)
{
   return string.replace(new RegExp("[\\s\\S]*<"+tag+">([\\s\\S]*?)<\\/"+tag+">[\\s\\S]*", "mg"), '$1');
}

function switch_thread()
{
   $('comment-preview').innerHTML = '';
   new Rico.Effect.FadeTo('comment-preview', 1, 1, 1);

   $('thread_entries').innerHTML = $('thread_entries').innerHTML +
   '<div class="thread_entry" id="t'+comment_id+'">'+
   '<div class="timestamp">'+time+' -- <a href="'+location.href.replace(/\/?#.*$/mg,'')+'/quote/'+comment_id+'/#commentform" class="thread-admin-action quote">Quote This Post</a> <a href="/forums/edit/'+comment_id+'/" class="thread-admin-action">Edit</a> <a href="/forums/delete/'+comment_id+'/" class="thread-admin-action">Delete</a></div>'+
   '<div class="meta">'+
   '<a href="/members/'+screen_name+'/"><img src="/images/avatars/'+avatar+'" alt="'+user+'" /></a>'+
   '<a href="/members/'+screen_name+'/" id="a'+comment_id+'">'+user+'</a>'+
   '</div>'+
   '<div class="thread_entry_content">'+
   '<span id="p'+comment_id+'">'+comment+'</span>'+
   ((signature != '')?('<div class="signature">'+signature+'</div>'):'') +
   '</div>' +
   '</div>';
   
   init();
}

function switch_comment()
{
   $('comment-preview').innerHTML = '';
   new Rico.Effect.FadeTo('comment-preview', 1, 1, 1);

   $('thecomments').innerHTML = $('thecomments').innerHTML +
   '<li id="t'+comment_id+'" class="thecomment">' +
   '<span>' +
   '<a href="/members/'+screen_name+'/"><img src="/images/avatars/'+avatar+'" /></a>' +
   '<a href="/members/'+screen_name+'/" id="a'+comment_id+'">'+user+'</a>'+
   time +
   '</span>'+
   '<div id="p'+comment_id+'">'+comment+'</div>' +
   '<p><a href="'+location.href.replace(/\/?#.*$/mg,'')+'/quote/'+comment_id+'/#commentform" class="quote">Quote This Post</a> <a href="/articles/edit/'+comment_id+'/">Edit</a> <a href="/articles/delete/'+comment_id+'/">Delete</a></p>'+
   '</li>';
   
   init();
}

function login(e)
{
   Event.stop(e);
   ajax_request('http://'+location.host+'/login/ajaxLogin/',Form.serialize('login'), login_handler);
}

function login_handler(originalRequest)
{
    if(/oisv_cookie=/.test(document.cookie))
    {
       first_name = getTagContents('first_name',originalRequest.responseText);
       last_name = getTagContents('last_name',originalRequest.responseText);
       screen_name = getTagContents('screen_name',originalRequest.responseText);

       new Rico.Effect.FadeTo('credentials', 0, 500, 10, {complete:switch_login} );
    }
    else
    {
       new Rico.Effect.FadeTo('credentials', 0, 500, 10, {complete:switch_failed} );
    }
}

function switch_login()
{
   $('credentials').innerHTML = 'Logged in as: '+first_name+' '+last_name+'<br />' +
   '<a href="/members/'+screen_name+'/">Profile</a> | ' +
   '<a id="logout" href="/login/logout/">Logout</a>';

   new Rico.Effect.FadeTo('credentials', 1, 1000, 20);
   if($('tuglogged') != null)
   {
      $('tug').style.display='none';
      new Rico.Effect.FadeTo('tuglogged', 0, 1, 1);
      $('tuglogged').style.display='block';
      $('tuglogged').innerHTML = '<p>Hi '+first_name+', You are now logged in</p>';
      new Rico.Effect.FadeTo('tuglogged', 1, 1000, 20);
   }
   else if(requires_login())
   {
      $('container').innerHTML = page;
      page = null;
   }
   init();
}

function switch_failed()
{
   $('credentials').innerHTML = 'Invalid Username/Password Combination. ' +
   '<a id="try_again" href="/login/">Try Again</a>?<br /><a id="forgot" href="/login/forgotPassword/">Lost your Password?</a>';

   new Rico.Effect.FadeTo('credentials', 1, 1000, 20);
   init();
}

function switch_logout()
{
   $('credentials').innerHTML = '<form id="login" method="post" action="">' +
   '<fieldset>' +
   '<label>Email Address:<input type="text" id="email" name="email" class="io" onfocus="this.className=\'focused\'" onblur="this.className=\'io\'" /></label>'+
   '<label>Password:<input type="password" id="password" name="password" class="io" onfocus="this.className=\'focused\'" onblur="this.className=\'io\'" /></label>'+
   '<input type="image" src="/images/login-button.png" class="bt" />'
   '</fieldset>' +
   '</form>';

   new Rico.Effect.FadeTo('credentials', 1, 1000, 20);
   if(loggedin == true)
   {
      if($('tuglogged') != null)
      {
         loggedin = false;
         new Rico.Effect.FadeTo('tuglogged', 0, 1, 1);
         $('tuglogged').style.display='none';
         $('tuglogged').innerHTML = '<p>You are now logged out ... Goodbye.</p>';
         $('tuglogged').style.display='block';
         new Rico.Effect.FadeTo('tuglogged', 1, 1000, 20);
      }
      else if(requires_login())
      {
         $('container').style.display = 'none';
         page = $('container').innerHTML;
         $('container').innerHTML = '<div id="tug" style="display:block;margin:20px 40px;">'+
                                    'You are now logged out ... Goodbye.'
                                    '</div>';
         new Rico.Effect.FadeTo('container', 0, 1, 1);    
         $('container').style.display = 'block';    
         new Rico.Effect.FadeTo('container', 1, 1000, 20);
      }
   }
   init();
}

function logout(e)
{
   Event.stop(e);
   ajax_request('http://'+location.host+'/login/logout/',null,logout_handler)
}

function try_again(e)
{
   Event.stop(e);
   new Rico.Effect.FadeTo('credentials', 0, 500, 10, {complete:switch_logout} );
}

function logout_handler(originalRequest)
{
   new Rico.Effect.FadeTo('credentials', 0, 500, 10, {complete:switch_logout} );
}

function update_comment_preview()
{
   $('comment-preview').innerHTML = '<p>'+$F('comment_text').replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}

function update_thread_preview()
{
   $('comment-preview').innerHTML = '<p>'+$F('contents').replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}

function update_pr_preview()
{
   $('pr_preview_contents').innerHTML = '<p>'+$F('pr_contents').replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}

function update_pr_headline()
{
   $('pr_preview_headline').innerHTML = '<p>'+$F('pr_headline').replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}

function pr_preview_warn(e)
{
   Event.stop(e);

   new Rico.Effect.FadeTo('add_pr_fields', 0, 500, 10,{complete:hide_pr_fields});
   $('pr_preview_area').style.display='block';
   new Rico.Effect.FadeTo('pr_preview_area', 1, 1000, 20);
}

function pr_submit_warn(e)
{
   if(!confirm('Please Proof Read your Release\n\nYou cannot edit it once it is submitted'))
   {
      Event.stop(e)
   }
}

function pr_preview_edit(e)
{
   Event.stop(e);
   new Rico.Effect.FadeTo('pr_preview_area', 0, 500, 10);
   $('pr_preview_area').style.display='none';
   $('add_pr_fields').style.display='block';
   new Rico.Effect.FadeTo('add_pr_fields', 1, 1000, 20);
}
function hide_pr_fields()
{
   $('add_pr_fields').style.display='none';
}


Event.observe(window, 'load', init, false);