/*
	jQuery equalHeights plugin
*/
(function($){$.fn.equalHeights=function(){$(window).resize($.proxy(function(){var tallest=0;$(this).css('min-height',0);$(this).each(function(){if($(this).outerHeight()>tallest){tallest=$(this).outerHeight();}});$(this).each(function(){var padding=$(this).outerHeight()-$(this).height();var height=tallest-padding;$(this).css({'min-height':height});if($.browser.msie&&parseInt($.browser.version,10)==6)$(this).height(height);});},this)).triggerHandler('resize');return this;};})(jQuery);

jQuery.fn.extend({
	hrefHash: function(){
		return $(this).attr('href').substr($(this).attr('href').indexOf('#'));
	}
});

$(function() {
    $('.lightbox').fancybox();
})

var Engine = {	
	enhancements: {
		equalHeights: function() {
			$('.columns-3').each(function(){
				$('.col .content', this).equalHeights();
			});
			
			$('.sidebar-a .nav, .columns-4:first').equalHeights();
		},
		
		rotator: function(fade, timeout) {
			/*
				@param fade: fadeIn/fadeOut speed in miliseconds;
				@param timeout: pause between items in miliseconds;
			*/
			
			if(!$('#rotator').length) {
				return;
			}

			var rotator = $('#rotator-content'),
				rotatorControls = $('<ul id="rotator-controls" />'),
				controls = '',
				items = $('> li', rotator),
				interval = null;
				
			var rotate = function() {
				var next =  $('a.active', rotatorControls).parent().is(':last-child') ? 
					$('a:first', rotatorControls) : 
					$('.active', rotatorControls).parent().next().find('a');
				
					next.click();
				},
				resetInterval = function() {
					clearInterval(interval);
					interval = setInterval(function() {
						rotate();
					}, timeout);
				};
				
			items.each(function(index){
				this.id = 'rotator-item-' + index;
				controls += '<li><a href="#rotator-item-' + index + '">' + index + '</a></li>';
			});

			rotatorControls.append(controls).insertAfter(rotator);

			$(rotator)
				.find('> li').hide()
				.filter(':first').show();

			$('a:first', rotatorControls).addClass('active');

			$('a', rotatorControls).each(function(i){
				$(this).click(function(){
					var id = $(this).hrefHash();
					
					$('.active', rotatorControls).removeClass('active');
					$(this).addClass('active');
	
					items
						.filter(':visible').fadeOut(fade).end()
						.filter(':eq(' + i + ')').fadeIn(fade);
					
					resetInterval();
					return false;
				});
			});
			
			resetInterval();
		}
	},
	
	fixes: {
		firstChild: function() {
			if(!$.browser.msie || $.browser.version != '6.0') {
				return;
			}
			
			$('.table-a .category').each(function(){
				$('th:first-child', this).addClass('first');
			});
		},
		lastChild: function() {
			if(!$.browser.msie) {
				return;
			}
				
			$('.columns-3').each(function(){
				$('.col:last', this).addClass('last-col');
			});			
		},
		nthChild4n: function() {
			$('.producers').each(function(){
				$('li', this).each(function(){
					if($(this).index() % 4 == 3) {
						$(this).addClass('nth-4n');
					}
				});
			});
		},
		table: function() {
			if(!$.browser.msie) {
				return;
			}
		
			$('.table-a').each(function(){
				$('tr', this).each(function(){
					$('th:odd, td:even', this).addClass('odd');
				});
			});
		}
	}
};

$(function(){
	Engine.enhancements.equalHeights();
	Engine.enhancements.rotator(750, 5000);
	Engine.fixes.firstChild();
	Engine.fixes.lastChild();
	Engine.fixes.nthChild4n();
	Engine.fixes.table();
});
