$(document).ready(function(){
    $('.greeting').greet({ mode: 'mtpro', indicator: cnStaticWebPath + 'images/indicator-2c2c2c.gif', loginText: 'Login' });

    // Sign-in auth tab switcher - auth_types is defined in the login page.
    $('#auth-options li').click(function(){
        authID = $(this).attr('id').replace("signin_option_", "");
        authOption = $("#signin_option_" + authID);
        if (authOption) {
            for (var i = 0; i < auth_types.length; i++) {
                var auth_type = auth_types[i];
                if (auth_type == authID) {
                    $('#signin_with_' + auth_type).show();
                    $('#signin_option_' + auth_type).addClass('selected');
                } else {
                    $('#signin_with_' + auth_type).hide();
                    $('#signin_option_' + auth_type).removeClass('selected');
                }
            }
        };
    });

    $('a[rel*=lightbox]').lightBox({
      imageLoading:cnStaticWebPath + 'images/lightbox-ico-loading.gif',
	  imageBtnPrev:cnStaticWebPath + 'images/lightbox-btn-prev.gif',
	  imageBtnNext:cnStaticWebPath + 'images/lightbox-btn-next.gif',
	  imageBtnClose:cnStaticWebPath + 'images/lightbox-btn-close.gif',
	  imageBlank:cnStaticWebPath + 'images/lightbox-blank.gif'
	  });
    if ($('#search input[name=query]').val() == "") {
      var searchBoxDefault = "Find community members, blogs, topics";
      $('#search input[name=query]').val(searchBoxDefault);
      $('#search input[name=query]').focus(function(){
	  $(this).addClass('focused');
	  if($(this).attr("value") == searchBoxDefault) $(this).attr("value", "");
	});
      $('#search input[name=query]').blur(function(){
	  $(this).removeClass('focused');
	  if($(this).attr("value") == "") $(this).attr("value", searchBoxDefault);
	});
    } else {
      $('#search input[name=query]').addClass('focused');
    };
    $('ul#profile-tabs li a').click(function() {
	var clicked = $(this).attr('href');
	if ($(this).parent().hasClass('active')) return false;
	$(this).parent().parent().find('li a').each( function() {
	    if ($(this).attr('href') == clicked) {
	      $(this).parent().addClass('active'); 
	      $( $(this).attr('href') ).show();
	    } else {
	      $(this).parent().removeClass('active');
	      $( $(this).attr('href') ).hide();
	    }
	  });
	return false;
      });
    $('#password-fields input').val('');
    
    var changeUserpic = $('#userpic-field .field-content #change-userpic');
    var cancelUserpic = $('#userpic-field .field-content #cancel-userpic');
    var removeUserpic = $('#userpic-field .field-content #remove-userpic');
    var changePassword = $('#profile a#change-password');
    // hide input change link present
    $(changeUserpic).siblings("input").hide();
    // onclick: hide link, img. show input
    $(changePassword).click(function() {
	var speed = 500;
	$(this).hide();
	$('#password-fields').show(speed);
      });
    if (changeUserpic.size()) {
      $(changeUserpic).click(function() {
	  var speed = 500;
	  $(this).hide(speed);
	  $(this).parent().siblings("img").hide(speed);
	  $(this).parent().siblings("input").show(speed);
	  cancelUserpic.show(speed);
	  removeUserpic.hide(speed);
	  $(this).parent().addClass('active');
	  return false;
	});
      $(cancelUserpic).click(function() {
	  var speed = 500;
	  $(this).hide(speed);
	  $(this).siblings("img").show(speed);
	  $(this).siblings("input").hide(speed);
	  removeUserpic.show(speed);
	  changeUserpic.show(speed);
	  $(this).parent().removeClass('active');
	  return false;
	});
    };
    if (removeUserpic.size()) {
      $(removeUserpic).click(function() {
	  var id = $('[name="id"]').val();
	  var token = $('[name="magic_token"]').val();
	  var postData = { __mode: 'remove_userpic', user_id: id, magic_token: token  };
	  $.post(mt.blog.community.script, postData,
		 function(data){
		   var speed = 500;
		   $(removeUserpic).hide(speed);
		   $(removeUserpic).parent().siblings("img").hide(speed);
		   $(removeUserpic).parent().siblings("input").show(speed);
		   $(changeUserpic).hide(speed);
		 }
		 );
	  return false;
	});
    };
  });

/* 
 * Auto Expanding Text Area (1.2.2)
 * by Chrys Bader (www.chrysbader.com)
 * chrysb@gmail.com
 * Copyright (c) 2008 Chrys Bader (www.chrysbader.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
(function(jQuery) {
  var self = null;
  jQuery.fn.autogrow = function(o)
    {
      return this.each(function() {
	  new jQuery.autogrow(this, o);
	});
    };
  jQuery.autogrow = function (e, o)
    {
      this.options  = o || {};
      this.dummy  = null;
      this.interval   = null;
      this.line_height  = this.options.lineHeight || parseInt(jQuery(e).css('line-height'));
      this.min_height  = this.options.minHeight || parseInt(jQuery(e).css('min-height'));
      this.max_height  = this.options.maxHeight || parseInt(jQuery(e).css('max-height'));;
      this.textarea  = jQuery(e);
      if(this.line_height == NaN)
	this.line_height = 0;
      // Only one textarea activated at a time, the one being used
      this.init();
    };
  jQuery.autogrow.fn = jQuery.autogrow.prototype = {
  autogrow: '1.2.2'
  };
  jQuery.autogrow.fn.extend = jQuery.autogrow.extend = jQuery.extend;
  jQuery.autogrow.fn.extend({
    init: function() {
	var self = this;
	this.textarea.css({overflow: 'hidden', display: 'block'});
        this.textarea.bind('focus', function() { self.startExpand() } ).bind('blur', function() { self.stopExpand() });
        this.checkExpand();
      },
    startExpand: function() {
	var self = this;
	this.interval = window.setInterval(function() {self.checkExpand()}, 400);
      },
    stopExpand: function() {
	clearInterval(this.interval);
      },
    checkExpand: function() {
        if (this.dummy == null) {
          this.dummy = jQuery('<div></div>');
          this.dummy.css({
                'font-size'  : this.textarea.css('font-size'),
                'font-family': this.textarea.css('font-family'),
                'width'      : this.textarea.css('width'),
                'padding'    : this.textarea.css('padding'),
                'line-height': this.line_height + 'px',
                'overflow-x' : 'hidden',
                'position'   : 'absolute',
                'top'        : 0,
                'left' : -9999
                }).appendTo('body');
        }
        var html = this.textarea.val().replace(/(<|>)/g, '');
        if ($.browser.msie) {
          html = html.replace(/\n/g, '<BR>new');
        } else {
          html = html.replace(/\n/g, '<br>new');
        }
        if (this.dummy.html() != html) {
          this.dummy.html(html);
          if (this.max_height > 0 && (this.dummy.height() + this.line_height > this.max_height)) {
            this.textarea.css('overflow-y', 'auto');
          } else {
            this.textarea.css('overflow-y', 'hidden');
            if (this.textarea.height() < this.dummy.height() + this.line_height || (this.dummy.height() < this.textarea.height())) {
              this.textarea.animate({height: (this.dummy.height() + this.line_height) + 'px'}, 100);
            }
          }
        }
      }
    });
})(jQuery);
