(function($){
	$.fn.rotator = function(){
		return this.each(function(){
			element = $(this);
			fadeTime = 1000;
			stayTime = 5000;

			$(element).children('li:gt(0)').hide();
			count = $(element).children('li').size()-1;

			current = 0;

			rotate = 1;

			function rotateit(){
				if (rotate==1){
					next = current+1;
					if (current>=count){
						next = 0;
					}

					$('a.thumb').removeAttr('id');
					$('a[href="'+next+'"]')
						.attr('id','current-thumb');

					$(element)
						.children('li:eq('+current+')')
						.css('z-index','50')
						.animate({
							opacity: 1
						}, fadeTime, function(){
							$(this).hide();});

					$(element)
						.children('li:eq('+next+')')
						.css('z-index','100')
						.fadeIn(fadeTime);

					if (current>=count){
						current = 0;
					} else {
						current = current+1;
					}
				}
			};

			$('.thumb')
				.live('click',function(){
					id = $(this).attr('href');
					rotate = 0;

					$(element)
						.children('li:eq('+current+')')
						.css('z-index','50')
						.animate({
							opacity: 1
						}, fadeTime, function(){
							$(this).hide();});

					$(element)
						.children('li:eq('+id+')')
						.css('z-index','100')
						.fadeIn(fadeTime);

					$('a.thumb').removeAttr('id');
					$(this).attr('id','current-thumb');

					current = id;
					return false;
				})

			setInterval(rotateit,stayTime);
		})
	}
})(jQuery);

$(document).ready(function(){
	$('.rotator').rotator();

    $('ul#nav li')
        .hover(
            function(){
                $(this).children('ul').show();
            },
            function(){
                $(this).children('ul').hide();
            }
        )

	$('.over').lightBox();

	$('#search_listings')
		.keyup(
			function(){
				search = $(this).val();
				$('#listings li').hide();
				$('#listings li:contains("'+search+'")').show();
			}
		)
		.focus(
			function(){
				if ($(this).val() == '( Start typing to search. i.e. 773 )'){
					$(this).val('');
				}
			}
		)
		.blur(
			function(){
				if ($(this).val() == ''){
					$(this).val('( Start typing to search. i.e. 773 )');
				}
			}
		)

	$('#side_search input')
		.keyup(
			function(){
				search = $(this).val();

				if (search == ''){
					$('#loader').hide();
					$('#info').fadeIn();
				} else {
					$.ajax({
						url: "ajax/search_listings.php",
						data: {val:search},
						success: function(data){
							$('#loader').html(data).fadeIn();
							$('#info').hide();
						}
					})
				}
			}
		)
		.focus(
			function(){
				if ($(this).val() == ''){
					$('#side_search #info').fadeIn();
				} else {
					search = $(this).val();
					$.ajax({
						url: "ajax/search_listings.php",
						data: {val:search},
						success: function(data){
							$('#loader').html(data).fadeIn();
							$('#info').hide();
						}
					})
					$('#loader').fadeIn();
				}
			}
		)
		.blur(
			function(){
				if ($(this).val() == ''){
					$('#side_search #info').fadeOut();
				} else {
					$('#loader').fadeOut();
				}
			}
		)
});
