var step = -970;
$(function() {
	if($.browser.opera) { 
		$.support.opacity = true;
		$('body').css('overflow','hidden');
	}
	$.get(
			'/ajax/',
			{ajax:1},
			function(data) {
				$('#content').append(data);
				$('.sidebar a').click(function() {
					if (!$(this).children('img').hasClass('active')) {
						if ($(this).parent().parent().attr('rel') == 'video-audio') {
							var href = $(this).attr('href');
							var player = $('#n0')[0];
							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 if (!$.browser.msie) {
							$(this).parent().siblings('.gallery').children('.cycle').cycle(Number($(this).children('img').attr('id').replace('gallery-','')));
						} else {
							if (parseInt($.browser.version) < 7) {
								var fadeimg = $(this).parent().siblings('.gallery').children('.cycle').children('.cycle-item').children('img.active');
								var allimgs = $.makeArray($(this).parent().siblings('.gallery').children('.cycle').children('.cycle-item'));
								var idx = Number($(this).children('img').attr('id').replace('gallery-',''));
								fadeimg.removeClass('active').parent().fadeOut('normal', function() {
									$(allimgs[idx]).fadeIn('slow').children('img').addClass('active');
								});
								$(this).parent().children('a').children('img.active').removeClass('active');
								$(this).children('img').addClass('active');
							//	alert('gallery-'+String(idx));
							} else { 
								var fadeimg = $(this).parent().siblings('.gallery').children('.cycle').children('.current');
								var allimgs = $.makeArray($(this).parent().siblings('.gallery').children('.cycle').children('.cycle-item'));
								var idx = Number($(this).children('img').attr('id').replace('gallery-',''));
								fadeimg.removeClass('current').fadeOut('normal', function() {
									$(allimgs[idx]).fadeIn('slow').addClass('current');
								});
								$(this).parent().children('a').children('img.active').removeClass('active');
								$(this).children('img').addClass('active');
							}
						}
					}
					return false;
				});
				
				//$('.cycle-item').not('.active').hide();
				
				
				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).attr('rel', 'false');
						$(this).cycle({
							before: function(cur, next, opts) {
								var imgs = $.makeArray($(cur).parent().parent().siblings('.sidebar').children('a'));
								if ($(cur).parent().attr('rel') == 'false') {
									$(cur).parent().attr('rel', 'true');
								} 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');
					});
			
				
				}
				
			
				$('#wrapper').css({overflow:'hidden',width:'970px',height:'406px',clear:'both',padding:0,margin:'0 0 20px'});
				if ($.browser.msie) $('p.description').css('marginTop', '0');
				$('#content').css({margin:0,width:'10000px'});
				$('.sub-content').css({float:'left',width:'970px',height:'400px',margin:0,clear:'none'});
				$('.sub-content').each(function() { $(this).attr('rel',$(this).attr('id')); $(this).attr('id',''); });
				$('#nav a').each(function() {
					if ($(this).attr('href') != '/') {
						$(this).attr('href', '#'+$(this).attr('href').replace('/','').replace('/',''))
						$(this).attr('id','p'+step);
						step -= 970;
					} else {
						$(this).attr('id','p0');
						$(this).attr('href', '#home');
					}
				}).click(function() {
					if (!$(this).hasClass('current')) {
						$('#nav a.current').removeClass('current');
						$(this).addClass('current');
						var href = $(this).attr('href');
						var margin = $(this).attr('id').replace('p','');
						if (margin != 'undefined' && margin != null && margin.length > 0) {
							if ($.browser.msie) $('.pill p').fadeOut('fast');
							if ($.browser.opera) $('#n0').fadeOut('fast');
							$('#content').animate({'marginLeft':margin+'px'},1000,null,function() {
								if ($.browser.msie && href == '#home') $('.pill p').fadeIn('fast');
								if ($.browser.opera && href == '#video-audio') $('#n0').fadeIn('fast');
							});
						}
					}
				});
				$('#header a').click(function() {
					$('#nav a[href=#home]').click();
					return false;
				});
			
				if (document.location.hash) {
					var hash = document.location.hash;
					hash = hash.match(/#(.*)/)[1];
					$('#nav a[href=#'+hash+']').click();
				}
				
				if ($.browser.msie) {
					
					pngfix();
					
					$('.cycle').each(function() {
					
						var count = 0;
					
						$(this).children('.cycle-item').each(function() {
							
							if (count > 0) $(this).hide();
							
							count++;
							
						});
					
					});
					
				}
				
				$('a[rel=external]').attr('target','_blank');
				
			},
			'html'
	);
});

