var imgcount = 0;

$(function() {
	if($.browser.opera) { 
		$.support.opacity = true; 
	}
	$('.sidebar a').click(function() {
		if ($(this).parent().parent().attr('id') == 'video-audio') {
			//$(this).parent().siblings('.gallery').children('.cycle').cycle(Number($(this).children('img').attr('id').replace('gallery-','')));
			var player = $('#n0')[0];
			var href = $(this).attr('href');
			player.sendEvent('STOP');
			player.sendEvent('LOAD', href);
			player.sendEvent('PLAY');
			if (!$.browser.msie) {
				$(this).parent().children('a').children('img.active').removeClass('active').animate({'opacity':.5},1000);
				$(this).children('img').addClass('active').animate({'opacity':1},1000);
			}
		} else {
			$(this).parent().siblings('.gallery').children('.cycle').cycle(Number($(this).children('img').attr('id').replace('gallery-','')));
		}
		return false;
	});
	
	if (!$.browser.msie) {
		
		$('.pill:first').css('margin-left','20px');

		$('.sidebar img').not('.active').css('opacity',0.5);
		
		$('.sidebar img').hover(
			function(){
				$(this).css({'opacity':1});
			},
			function(){
				if (!$(this).hasClass('active')) $(this).css({'opacity':0.5});
			}
		);
	
	}
	
	$('.cycle').each(function() {
		var self = this;
		$(this).cycle({
			before: function(cur, next, opts) {
				var imgs = $.makeArray($(cur).parent().parent().siblings('.sidebar').children('a'));
				if (imgcount == 0) {
					imgcount++;
				} else if (!$.browser.msie) {
					$(cur).parent().parent().siblings('.sidebar').children('a').children('img.active').removeClass('active').animate({'opacity':.5},1000);
					$(imgs[opts.nextSlide]).children('img').addClass('active').animate({'opacity':1},1000);
				}
				$(next).css('marginLeft',(625 - $(next).outerWidth()) / 2);
			},
			timeout: 6000
		}).cycle('pause');
	});
	
	$('a[rel=external]').attr('target','_blank');
});