var $j = jQuery;

function sameHeight(c, scope){
	if (scope){
		$j(scope).each(function(){
			var set = $j(this).find(c);
			sameHeight(set);
		});
	}
	else {
		var h = 0;
		$j(c).each(function(){
			var th = $j(this).height()
			h = (th > h)? th : h;
		});
		$j(c).height(h);	
	}
}

jQuery.fn.toggleDefaultVal = function(){
	jQuery(this).each(function(){
		jQuery(this).bind({
			'focus': function(){
				if (jQuery.trim(jQuery(this).val()) == this.defaultValue) jQuery(this).val('');
			},
			'blur': function(){
				if (jQuery.trim(jQuery(this).val()) == '') jQuery(this).val(this.defaultValue);
			}
		});
	});
}

$j(function(){
	$j('#siteSearchTxt').toggleDefaultVal();
});

jQuery.fn.categorymenu = function(){
	return $j(this).each(function(){
		var element = $j(this);
		var lis = element.children('li:has(ul)');
		lis.addClass('expands');
		lis.each(function(){
			var li = $j(this);
			li.children('a').bind('click', function(event){
				if (li.is('.active')) return;
				event.preventDefault();
				lis.filter('.active').children('.category-dropdown').slideUp(null, function(){
					$j(this).closest('.active').removeClass('active');
				});
				li.addClass('active').children('.category-dropdown').slideDown();
			});
		});
	});
};

$j(function(){
	$j('#siteCategories>ul').categorymenu();
});


jQuery.fn.latestNews = function(options){
	if (typeof(options) == 'undefined') var options = {};
	options = $j.extend({
		delay: 5000,
		speed: 1000
	}, options);
	return $j(this).each(function(){
		var element = $j(this);
		var items = element.children();
		items.wrapAll('<div class="latest-news-track" />');
		var track = items.parent();
		element.append('<div class="latest-news-grad" />');
		var elementHeight = 0;
		items.each(function(){
			var h = $j(this).height();
			if (h > elementHeight) elementHeight = h;
		});
		elementHeight += 10;
		element.height(elementHeight);
		var controls;
		var currentItem = 0;
		var animating = false;
		var paused = false;
		var goto = function(index){
			if (animating) return;
			if (index == currentItem) return;
			animating = true;
			var pos = items.eq(index).position().top;
			track.animate({'top': (-pos) + 'px'}, {duration: options.speed, complete: function(){
				animating = false;
			}});
			if (controls)
			controls.eq(currentItem).removeClass('active');
			controls.eq(index).addClass('active');
			currentItem = index;
		};
		
		var timer;
		var next = function(){
			var n = ((currentItem + 1) < items.length)? currentItem + 1 : 0;
			goto(n);
		};
		((options.wrapper)? $j(options.wrapper) : element).bind({
			'mouseenter': function(){
				if (paused) return;
				paused = true;
				clearInterval(timer);
			},
			'mouseleave': function(){
				if (!paused) return;
				paused = false;
				timer = setInterval(next, options.delay);
			}
		});
		timer = setInterval(next, options.delay);
		if (options.controls){
			var control = $j('<ul class="latest-new-controls" />');
			items.each(function(index){
				var el = $j('<li>' + index + '</li>');
				el.bind('click', function(){goto(index)});
				control.append(el);
			});
			controls = control.children();
			controls.eq(0).addClass('active');
			$j(options.controls).append(control);
		}
	});
};

$j(function(){
	$j('#siteNewsItems').latestNews({
		controls: '#siteLatestNewsTop',
		wrapper: '#siteLatestNews'
	});					
});
