jQuery.fn.placeholder = function(_options){

  var _options = jQuery.extend({

    className: "placeholded"

  },_options);

  

  return this.each(function(){

    var _hold = $(this);

    var _text = this.defaultValue;

    var _form = _hold.parents('form').eq(0);

    if(!_text)

      return;

    _hold.addClass(_options.className);

    _form.submit(function(){

      if(_hold.hasClass(_options.className))

        _hold.val("");

    });

    _hold.focus(function(){

      if(_hold.hasClass(_options.className))

      {

        _hold.val("");

        _hold.removeClass(_options.className);

      }

    });

    _hold.blur(function(){

      if(_hold.val() == "")

      {

        _hold.val(_text);

        _hold.addClass(_options.className);

      }

    });   

  });

}



jQuery.fn.fadingImages = function(){

  return this.each(function(){

    var _hold = $(this);

    var _el = $(this).find('li');

    var _count = _el.index(_el.filter(':last')) + 1;

    var _active = 0;

    var _timeout = 7000;

    var _speed = 500;

    

    for (var i=0; i < _count; i++)

    {

      _el.eq(i).css('z-index', _count - i);

    }

    

    function changeImage()

    {

      _active++;

      if(_active == _count)

      {

        _active = 0;

        _el.eq(_active).fadeIn(_speed, function(){

          _el.not(':first').fadeIn(1);

        });

      }

      _el.eq(_active - 1).fadeOut(_speed);

      setTimeout(changeImage, _timeout);

    }

    

    setTimeout(changeImage, _timeout);

  });

}



jQuery.fn.customSlider = function(){



  return this.each(function(){

    var _hold = $(this);

    var _wrap = _hold.find('#slides');

    var _el = _hold.find('#slides > li')

                   .css('float', 'left');

    var _count = _el.index(_el.filter(':last')) + 1;

    var _active = 0;

    var _btn = $('#tabSet > li');

    var _arrow = $(document.createElement('span')).addClass('arrow');

    var _fullWidth = 1;

    

    for (var i=0; i < _count; i++)

    {

      _fullWidth += _el.eq(i).outerWidth();

    }

    

    _wrap.css('width', _fullWidth);

    

    _btn.removeClass('selected');

    _btn.eq(_active).addClass('selected').append(_arrow);

    

    _btn.bind('mouseover mouseenter', function(){

      if(!$(this).hasClass('selected'))

      {

        _btn.removeClass('selected');

        _active = _btn.index($(this).addClass('selected').append(_arrow));

        var _current = _el.eq(_active);

        _wrap.stop().animate({ left: -_current.position().left }, 600);

      }

    });

  });

}



jQuery.fn.topLocation = function()

{

  return $(this).each(function(){

    var hold = $(this);

    var selectors = hold.find('.selector a');

    var selects = hold.find('form select');

    selectors.bind('click', function(e){

      e.preventDefault();

      e.stopPropagation();

      var position = $(this).prevAll('a').length;

      selectors.removeClass('active').eq(position).addClass('active');

      selects.removeClass('active').eq(position).addClass('active');

    });

    selects.bind('change', function(e){

      if($(this).val())

      {

        document.location = $(this).val();

      }

    });

  });

}



$(document).ready(function(){

  $('.placeholder').placeholder();

  $('#slider').customSlider();

  $('#expertFaces').fadingImages();

  $('#topLocation').topLocation();
	
	$('#compare-form').compareClinics();
	
	$('#compare-form').attr('action', '/cmail.php');

});


