(function($) {
	$.fn.extend({
		slider: function() {
			return this.each(function() {

				var $holder = $(this);
				var holderWidth = parseInt($holder.find('> div').outerWidth());

				// our navigation and controls should be hidden from non javascript users, therefore lets show them
				$holder.find('.navigation').show();
				$holder.find('.controls').show();

				// loop through items and set the left property
				var i = 0;
				$holder.find('.item').each(function() {
					$this = $(this);
					var left = i * $this.outerWidth();
					$this.css({'position':'absolute', 'left':left + 'px'});
					i++;
				});

				// add the active class to the first item as it is used as a pointer for the current item
				$holder.find('.home-slider-wrap div.item:first-child').addClass('active');
				$holder.find('.navigation a:first-child').addClass('active');
				
				$holder.find('.previous').hover(function() {
					if ($holder.find('.home-slider-wrap div.active').index() == 0) {
						$(this).css('cursor', 'default');
					} else {
						$(this).addClass('hover').css('cursor', 'pointer');
					}
				}, function() {
					$(this).removeClass('hover');
				});
				$holder.find('.next').hover(function() {
					if (($holder.find('.home-slider-wrap div.active').index() + 1) * 4 >= $holder.find('.item').length) {
						$(this).css('cursor', 'default');
					} else {
						$(this).addClass('hover').css('cursor', 'pointer');
					}
				}, function() {
					$(this).removeClass('hover');
				});
				$holder.find('.previous').click(function() {
					if ($holder.find('.home-slider-wrap div.active').index() > 0) {
						animateSlide('left');
					}
					return false;
				});
				$holder.find('.next').click(function() {
					if (($holder.find('.home-slider-wrap div.active').index() + 1) * 4 < $holder.find('.item').length) {
						animateSlide('right');
					}
					return false;
				});
				function animateSlide(direction) {
					$holder.find('.item').each(function() {
						if (direction == 'left') {
							var newLeft = parseInt($(this).css('left')) + holderWidth;
						} else if (direction == 'right') {
							var newLeft = parseInt($(this).css('left')) - holderWidth;
							var newActiveItem = $holder.find('.home-slider-wrap div.active').index() + 1;
						}
						$(this).stop().animate({'left':newLeft + 'px'}, 400, 'swing');
					});
					if (direction == 'left') {
						var newActiveItem = $holder.find('.home-slider-wrap div.active').index();
					} else if (direction == 'right') {
						var newActiveItem = $holder.find('.home-slider-wrap div.active').index() + 2;
					}
					$holder.find('.home-slider-wrap div.item').removeClass('active');
					$holder.find('.home-slider-wrap div.item:nth-child(' + newActiveItem + ')').addClass('active');
				}
			});
		}
	});
})(jQuery);


/*--- clear inputs ---*/
function clearInputs(){
	$('.search_area input:text, .search_area input:password, .search_area textarea').each(function(){
		var _el = $(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}
/*--- treatment gallery ---*/
function treatmentGall(){
	var fade_speed = 400; //in ms
	var slide_speed = 600; //in ms
	
	var _btn = $('#treatment_nav a');
	var box_hold = $('#treatment_box');
	var nav_hold = $('#treatment_info div.nav_hold');
	var info_hold = $('#treatment_info div.info_hold');
	if(_btn.length && box_hold.length && nav_hold.length && info_hold.length){
		var _loader = $('<div class="loader">Loading...</div>');
		_loader.css({display:'none', opacity: 0});
		box_hold.append(_loader);
		var ajax_f = false;
		var a_box = (_btn.index(_btn.filter('.active:eq(0)')) != -1)?(_btn.index(_btn.filter('.active:eq(0)'))):(0);
		//_btn.removeClass('active').eq(a_box).addClass('active');
		
		$.ajax({
			url: 'index/get-most-popular/category',
			dataType: 'json',
			success: function(_list){
				_btn.eq(a_box).data('list_url', _list);
				_btn.eq(a_box).data('_box', $('<div class="list_hold"></div>'));
				_btn.eq(a_box).data('nav_hold', $('<ul><li><a href="#" class="previous">previous</a></li><li><a href="#" class="next">next</a></li></ul>'));
				//_btn.eq(a_box).data('_box').addClass('active');
				box_hold.append(_btn.eq(a_box).data('_box'));
				nav_hold.append(_btn.eq(a_box).data('nav_hold'));
				initGall(a_box);
			}
		});
		_btn.click(function(){
			//changeGall(_btn.index(this));
			//return false;
		});
	}
	function changeGall(ind_box){
		if(ajax_f && a_box != ind_box){
			ajax_f = false;
			_btn.eq(a_box).removeClass('active');
			_btn.eq(ind_box).addClass('active');
			
			if(_btn.eq(ind_box).data('_box')){
				_btn.eq(a_box).data('nav_hold').hide();
				_btn.eq(a_box).data('_box').stop().removeClass('active').animate({opacity: 0}, fade_speed, function(){ $(this).css('display', 'none');});
				
				_btn.eq(ind_box).data('nav_hold').show();
				_btn.eq(ind_box).data('_box').stop().css('display','block').addClass('active').animate({opacity: 1}, fade_speed);
				
				ajax_f = true;
				a_box = ind_box;
			}
			else{
				_loader.stop().css('display','block').animate({opacity: 1}, 150);
				_btn.eq(a_box).data('nav_hold').hide();
				_btn.eq(a_box).data('_box').stop().removeClass('active').animate({opacity: 0}, fade_speed, function(){ $(this).css('display', 'none');});
				$.ajax({
					url: _btn.eq(ind_box).attr('href'),
					dataType: 'json',
					success: function(_list){
						_btn.eq(ind_box).data('list_url', _list);
						_btn.eq(ind_box).data('_box', $('<div class="list_hold"></div>'));
						_btn.eq(ind_box).data('nav_hold', $('<ul><li><a href="#" class="previous">previous</a></li><li><a href="#" class="next">next</a></li></ul>'));
						_btn.eq(ind_box).data('_box').css('opacity', 0);
						_btn.eq(ind_box).data('nav_hold').hide();
						box_hold.append(_btn.eq(ind_box).data('_box'));
						nav_hold.append(_btn.eq(ind_box).data('nav_hold'));
						initGall(ind_box);
						_btn.eq(ind_box).data('nav_hold').show();
						_btn.eq(ind_box).data('_box').stop().css('display','block').addClass('active').animate({opacity: 1}, fade_speed);
						
						a_box = ind_box;
						_loader.stop().animate({opacity: 0}, 150, function(){ $(this).css('display','none');});
					}
				});
			}
		}
	}
	
	function initGall(_i){
		var list_hold = $('<ul></ul>');
		var _list = [];
		_btn.eq(_i).data('_box').append(list_hold);
		var btn_prev = _btn.eq(_i).data('nav_hold').find('a.previous');
		var btn_next = _btn.eq(_i).data('nav_hold').find('a.next');
		var _a = 0;
		var _step = list_hold.parent().width();
		
		$.ajax({
			url: _btn.eq(_i).data('list_url')[_a],
			dataType: 'html',
			success: function(_html){
				_list[_a] = $('<li>'+_html+'</li>');
				list_hold.append(_list[_a]);
				initInfo(_list[_a]);
				ajax_f = true;
			}
		});
		
		if(_btn.eq(_i).data('list_url').length > 1){
			btn_prev.addClass('prev_disable');
		}
		else{
			btn_prev.addClass('prev_disable');
			btn_next.addClass('next_disable');
		}
		btn_prev.click(function(){
			if(!$(this).hasClass('prev_disable')) changeEl(_a - 1);
			return false;
		});
		btn_next.click(function(){
			if(!$(this).hasClass('next_disable')) changeEl(_a + 1);
			return false;
		});
		
		function changeEl(_ind){
			if(ajax_f && _ind != _a){
				ajax_f = false;
				if(_ind == 0) btn_prev.addClass('prev_disable');
				else btn_prev.removeClass('prev_disable');
				if(_ind == _btn.eq(_i).data('list_url').length - 1) btn_next.addClass('next_disable');
				else btn_next.removeClass('next_disable');
				list_hold.stop().animate({left:-_ind*_step}, slide_speed);
				if(_list[_ind]){
					_a = _ind;
					ajax_f = true;
				}
				else{
					_loader.stop().css('display','block').animate({opacity: 1}, 150);
					$.ajax({
						url: _btn.eq(_i).data('list_url')[_ind],
						dataType: 'html',
						success: function(_html){
							_list[_ind] = $('<li>'+_html+'</li>');
							list_hold.append(_list[_ind]);
							initInfo(_list[_ind]);
							_loader.stop().animate({opacity: 0}, 150, function(){ $(this).css('display','none');});
							_a = _ind;
							ajax_f = true;
						}
					});
				}
			}
		}
	}
	function initInfo(el_hold){
		var _el = el_hold.find('ul li:has("div.info_el")');
		_el.mouseenter(function(){
			info_hold.children().hide();
			if($(this).data('info')){
				$(this).data('info').show();
			}
			else{
				$(this).data('info', $($(this).find('div.info_el').html()).appendTo(info_hold));
				$(this).data('info').show();
			}
		}).mouseleave(function(){
			$(this).data('info').hide();
		});
	}
}
$(document).ready(function($) {

	clearInputs();
	treatmentGall();

	$('.tooltip').livequery(function() {
        $(this).tooltip({
            track      : true,
            delay      : 0,
            showURL    : false,
            fixPNG     : true,
            extraClass : 'prompt'
        });
    });

    jQuery.fn.form_error = function(id, groupId, errMsg) {
        $('#' + id).after('<div class="field_err ' + groupId + '"><div class="field_err_icon"></div>' + errMsg + '</div>');
    }

    jQuery.fn.show_loading = function(obj) {
        obj.after('<img align="absmiddle" class="loading" src="images/loading.gif" style="display: inline; margin-left: 5px">');
    }

    jQuery.fn.hide_loading = function() {
        $('.loading').remove();
    }

    jQuery.fn.showPrompt = function(text) {
        jQuery.facybox(text, 'bolder');
        $('#facybox').center().fadeIn(500);
        setTimeout($.facybox.close, 1500);
    }

	// set all external links to target blank
	$('a').each(function() {
		if ($(this).attr('href')) {
			var start = $(this).attr('href').indexOf('://');
			if (start != -1) {
				var parts = $(this).attr('href').split('://');
				var end = parts[1].indexOf('/') != -1 ? parts[1].indexOf('/') : parts[1].length;
				if (parts[1].substr(0, end) != document.domain) {
					$(this).attr('target', '_blank');
				}
			}
		}
	});

	// bookmark page
	$('a.bookmark').click(function() {
		var url = document.URL;
		var pageTitle = $('title').text();
		if (window.sidebar) {
			window.sidebar.addPanel(pageTitle, url, '');
		} else if (window.external) {
			window.external.AddFavorite(url, pageTitle);
		} else if (window.opera && window.print) {
			var elem = document.createElement('a');
			elem.setAttribute('href' ,url);
			elem.setAttribute('title', pageTitle);
			elem.setAttribute('rel', 'sidebar');
			elem.click();
		}
		return false;
	});

	// judges pagination switcher
	$('div.judges-group').hide();
	$('div.judges-group').first().show();
	$('a.judge-pagination-link').click(function() {
		var pageNumber = $(this).attr('id').replace('judge-page-link-', '');
		$('div.judges-group').hide();
		$('#judges-group-' + pageNumber).fadeIn();
		return false;
	});

	// blue border and link for judges
	$('div.judges div.judge').hover(function() {
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
	});
	$('div.judges div.judge').click(function() {
		var href = $(this).find('a.more-info').attr('href');
		window.location = href;
	});

	// fix for ie 6
	$('.treatment_box li li:last-child').css('padding-right', 0);
	
	// slider
	$('div.home-slider div.item').hover(function() {
		$(this).addClass('hover');
		var id = $(this).attr('id').replace('item-', '');
		$('#item-description-' + id).css('display', 'block');
	}, function() {
		$(this).removeClass('hover');
		$('.item-description').css('display', 'none');
	});

	// slide our home page images
	if ($('div.home-slider').length > 0) {
		$('div.home-slider').slider();
	}

	// create the flash banner in the footer
	var flashvars = {};
    flashvars.targetURL = 'http://www.thelittlemule.com';
    var params = {};
    var attributes = {};
	swfobject.embedSWF('flashes/LM-Anno.swf', 'footer-advertising', '728', '90', '9.0.0', false, flashvars, params, attributes);
});
