// Scroller & Opaque menus
var move_count = 1;
var slider_moving = false;
var slider_width = 655;
var countdown_max = 6; // count to before move?
var current_count = 0; // counter

document.observe("dom:loaded", function() {

if ($('features')) {
  // vars
  //move_count = 1;
  //slider_moving = false; 

  // setup css if more than 1 div
  if ($('features').childElements().length > 1) {
    $('features').setStyle({width: ($('features').childElements().length * slider_width) + 'px'});
  }
  
  // previous
  $('previous').observe('click', function(event) {
    if (!slider_moving) {
      slider_moving = true;
      document.fire('slider:move', { distance: slider_width });
    }
    Event.stop(event);
  });
  
  // next
  $('next').observe('click', function(event) {
    if (!slider_moving) {
      slider_moving = true;
      document.fire('slider:move', { distance: slider_width * -1 });
    }
    Event.stop(event);
  });
  
  // mouse on
  $('features').observe('mouseover', function(event) {
    slider_moving = true;
  });
  
  // mouse off
  $('features').observe('mouseout', function(event) {
    slider_moving = false;
  });
  
  // move forward/back - on mouse click/hover, see above
  $(document).observe('slider:move', function(event) {
    if (move_count != 1 && event.memo.distance > 0) { // move left

		// set the new move count
        move_count -= Math.abs(event.memo.distance / slider_width); 

	  // move the main image
      new Effect.Move($('features'), {x: parseInt(event.memo.distance), y: 0, duration: 1, mode:'relative', afterFinish: function() {
        slider_moving = false;           
      }});
      
    } else if (move_count != $('features').childElements().length && event.memo.distance < 0) { // move right

	  // set the new move count
      move_count += Math.abs(event.memo.distance / slider_width);
      
      // move the main image
      new Effect.Move($('features'), {x: parseInt(event.memo.distance), y: 0, duration: 1, mode:'relative', afterFinish: function() {
        slider_moving = false;
      }});
      
    } else {
      slider_moving = false;
    }
    
    current_count = 0;
    
    Event.stop(event);        
  });
  
  // highlight first menu item
  if($('feature_0').className){
	  class_name = $('feature_0').className;
	  $('n_' + class_name).addClassName('on');
  }
  
  // move automatically after {countdown_max} counts
  setTimeout("timedMove()", 1000);
  
}
});

// function for timer based moving
function timedMove() {

  current_count += 1;

  if (current_count >= countdown_max && !slider_moving) { // count finished?

	//highlight corrent tag name
	  if(move_count == 4){
		  
		tmp_move_count = 0;
		prev_count = 3;
		
	  }else{
		  
		tmp_move_count = move_count;
		prev_count = move_count-1;
		
	  }
	  if($('feature_' + tmp_move_count).className){
		  class_name = $('feature_' + tmp_move_count).className;
		  prev_class_name = $('feature_' + prev_count).className;
		  $('n_' + class_name).addClassName('on');
		  $('n_' + prev_class_name).removeClassName('on');
	  }
      
    if (move_count != $('features').childElements().length) { // move right
       
      slider_moving = true;
      document.fire('slider:move', { distance: slider_width * -1 });

    } else { // move to start

      jumpTo(1);

  }

}

// move automatically after {countdown_max} counts
t = setTimeout("timedMove()", 1000);

}

// move slider along
function jumpTo(imgIndex) {

	// do horizontal scroll
	move_amount = (move_count - (imgIndex)) * slider_width;
	
	if (!slider_moving) {
	  
  		slider_moving = true;
  		document.fire('slider:move', { distance: move_amount });
	}
}

// Blog
function addBlogComment() {
  if ($('comment').value == '') {
    alert('Please write something in the Comment Box before posting it...');
  } else {
    new Ajax.Updater('add_comment', '/includes/blog/blog_processor.php?ajax=true', {
      parameters: $('comment_form').serialize(true),
      insertion:  'before'
    });
/*
    new Ajax.Updater('comment_count', '/includes/client/blog_processor.php', {
      parameters: { form_action: 'count_comments', page: $('page_fk').value }
    });
    new Ajax.Updater('comment_count2', '/includes/client/blog_processor.php', {
      parameters: { form_action: 'count_comments', page: $('page_fk').value, count: '2' }
    });
*/
    $('comment').value = '';
  }
}

// Forum
function addForumComment() {
  if ($('comment').value == '') {
    alert('Please write something before posting it to the Forum.');
  } else {
    new Ajax.Request(
      '/includes/forum/forum_thread_processor.php?update=add', {
      method:'post',
      asynchronous:true,
      postBody:Form.serialize('comment_form'),
      onSuccess:function(t) {
        var response = t.responseText;
        response_msg = response.replace(/-(.*)/, "");
        textbox_id = response.replace(/ok-/, "");
        if (response_msg == 'ok') {
          showForumComment(textbox_id);
        }
      }
    });
  }
}

function showForumComment (textbox_id) {
  new Ajax.Updater('start_comment', '/includes/forum/forum_thread_processor.php?update=show&textbox_id=' + textbox_id, {
    parameters: $('comment_form').serialize(true),
    insertion: 'before'
  });
  $('comment').value = '';
}

function addForumComment_OLD () {
  if ($('comment').value == '') {
    alert('Please enter a blog comment before posting it...');
  } else {
    new Ajax.Updater('start_comment', '/includes/forum/forum_thread_processor.php?ajax=true', {
      parameters: $('comment_form').serialize(true),
      insertion: 'before'
    });
    $('comment').value = '';
  }
}
function ForumJumpToReply() {
  $('comment').focus()
  Effect.ScrollTo('comment_form_heading', {duration: 2});
}

function ForumQuotePost(comment) {
  comment_span = document.getElementById(comment);

  // strip tags
  reg = /<\/?[^>]+>/gi;
  comment_span = comment_span.innerHTML.replace(reg, "");
  document.comment_form.comment.value = "[quote]" + comment_span + "[quote]"; 

  ForumJumpToReply();
}


// clear and Autocomplete Search
function loadAuthorSearch(url) {
  var f = document.getElementById('s_a');
  f.onfocus = function() {
    if (this.value == 'search authors...') this.value = '';
  }
  f.onblur = function() {
    if (this.value != '') return;
    this.value = 'search authors...';
  }
  new Ajax.Autocompleter('s_a', 'autocomplete_choices', url, {
    delay: 0.3,
    minChars: 2,
    indicator: 'indicator1',
		paramName: 'q'
  });
}

// search Inline content area
function searchContent (url, msg) {

  // loading msg
  new Element.update('content', '<span class="loading">' + msg + '...</span>');

  // search content
  new Ajax.Updater('content', url, {method:'post', asynchronous:true, evalScripts:true, parameters: {q: $F('s_a'), ajax: 'ajax'}});
}


  // Sucker fish drop down menus
  sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+=" sfhover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
      }
    }
  }
  if (window.attachEvent) window.attachEvent("onload", sfHover);

//clear Search Field
  function clearSearch () {
    var f = document.getElementById('s');
    f.onfocus = function() {
      if (this.value == 'search...') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'search...';
    }
  }
  
//clear Search Field
  function clearTagSearch () {
    var f = document.getElementById('s_well');
    if(f !== null){
    f.onfocus = function() {
      if (this.value == 'search...') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'search...';
    }
  }
  }
  
  // clear Author Search Field
  function clearSearchAuthor () {
    var f = document.getElementById('s_a');
    f.onfocus = function() {
      if (this.value == 'search authors...') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'search authors...';
    }
  }

  
  // clear newsletter signup fields
  function clearNewsletter () {
    var f = document.getElementById('mail-list-name');
    f.onfocus = function() {
      if (this.value == 'name') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'name';
    }
    
    var f = document.getElementById('mail-list-email');
    f.onfocus = function() {
      if (this.value == 'email') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'email';
    }
  }

  // update Cart using Ajax.Request to Shadowbox
  function submitCartShadowbox (url, myform) {
    new Ajax.Request(
      url, {
      method:'post',
      asynchronous:true,
      postBody:Form.serialize(myform),
      onSuccess:function(t) {
        var response = t.responseText;
        if (response == 'ok') {
          openCart();            
        }
      }
    });
  }

  // open Cart in Shadowbox
  function openCart () {
    Shadowbox.open({
      player:     'iframe',
      title:      'View Cart - Kindred Community',
      content:    '/includes/client/cart_view.php',
      width:      600,
      height:     460
    });
  }

  // Turn checkbox On or Off by clicking on text / span etc
  function toggleCheckbox (id) {
    var el = document.getElementById(id);
    if (el.checked === true) {
      el.checked = false;
    } else {
      el.checked = true;
    }
  }

  // Scroll to elem. Id
  function scroll (el) {
    new Effect.ScrollTo(el);
  }

  // clear Contact forms
  function clearContact () {
    var f = document.getElementById('name');
    f.onfocus = function() {
      if (this.value == 'name') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'name';
    }

    var f = document.getElementById('email');
    f.onfocus = function() {
      if (this.value == 'email') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'email';
    }

    var f = document.getElementById('phone');
    f.onfocus = function() {
      if (this.value == 'phone') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'phone';
    }

    var f = document.getElementById('message');
    f.onfocus = function() {
      if (this.value == 'message') this.value = '';
    }
    f.onblur = function() {
      if (this.value != '') return;
      this.value = 'message';
    }
  }

  function myAddLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != "function") {
      window.onload = func;
    }
    else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        func();
      }
    }
  }

function setupPopularPages() {
  document.getElementById("popVis").style.textDecoration = "underline";
  $("email_articles").hide();
  $("comm_articles").hide();
}

function popArt($action) {
  if ($action=="commented") {
    $("email_articles").hide();
    document.getElementById("popEma").style.textDecoration = "";
    $("visit_articles").hide();
    document.getElementById("popVis").style.textDecoration = "";
    $("comm_articles").show();
    document.getElementById("popCom").style.textDecoration = "underline";
    Effect.Appear("comm_articles", { duration: 1 });
  } else if ($action=="visited") {
    $("comm_articles").hide();
    document.getElementById("popCom").style.textDecoration = "";
    $("email_articles").hide();
    document.getElementById("popEma").style.textDecoration = "";
    $("visit_articles").show();
    document.getElementById("popVis").style.textDecoration = "underline";
  //  Effect.Appear("visit_articles", { duration: 1 });
  } else if ($action=="emailed") {
    $("comm_articles").hide();
    document.getElementById("popCom").style.textDecoration = "";
    $("visit_articles").hide();
    document.getElementById("popVis").style.textDecoration = "";
    $("email_articles").show();
    document.getElementById("popEma").style.textDecoration = "underline";
  //  Effect.Appear("email_articles", { duration: 1 });
  }
}

