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 = 3500;
    var _speed = 750;
    
    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(){
	  if($.fn.slideshow)
  {
    $('#slideshow').slideshow({
      timeout: 5000,
      fadetime: 1000,
      type: 'sequence'
    });
  }
  $('.placeholder').placeholder();
  $('#slider').customSlider();
  $('#expertFaces').fadingImages();
  $('#topLocation').topLocation();
	
	$('#cast-vote').click(function () {
		$('.section.poll .bg').slideUp('slow',
		function () {
				$('.section.poll .bg').load('votes.php').slideDown('slow');
		});
		
		$.post('poll.php', {poll: $('#latestPoll input[@name=poll]:checked').val()});
		return false;
	});
	
	$('#contact').attr('action', 'contact2.php');
	$('#contact.abroad').attr('action', 'cosmetic-dentistry-abroad-form.php');
	
	$('.guideListing').replaceWith(function () {
		var urlObject = $(this).children('.url');
    if (typeof urlObject != 'undefined') {
			var url = urlObject.html();
			var classes= $(this).attr('class');
			urlObject.remove();
			return $('<a href="' + url.replace(/^\s*/g, '').replace(/\s*/g, '') + '" rel="nofollow" target="_blank" class="' + classes + '">' + $(this).html() + '</a>');
		}
	});
	
	$('#askAnExpert').attr('action', 'expert/index.php/frontend/ask');
	
	$('#reviewForm').attr('action', 'http://www.cosmeticdentistryguide.co.uk/ci_dentists/profile/comments.html');
});

