// JavaScript Document

var currentScreen = 0;
var numScreens = 0;

function showLogo(){
	$('h1.fade a').animate({'opacity':1},1000).addClass('ready');
}

function showNav(){
	$('p.nav.fade a').animate({'margin-top':0, 'opacity':1, 'color':'#d8bf80'},1000).addClass('ready');	
}

function showLink(){
	$('#rmwc-link.fade').fadeTo(1000,0.7).addClass('ready');
}

function showFooter() {
	$('#footer.fade').fadeTo(1000,1);	
}

function updateNextPrev(){
	if(currentScreen==0){
		$('.control a.prev').css('display','none');	
	}else{
		$('.control a.prev').css('display','block');
	}
	
	if(currentScreen<numScreens-1){
		$('.control a.next').css('display','block');	
	}else{
		$('.control a.next').css('display','none');
	}
}

$(document).ready(function(){
	$('h1.fade a').css('opacity',0);
	
	$('#footer.fade').css('opacity',0);
	$('#rmwc-link.fade').css('opacity',0);
	$('#rmwc-link')
		.mouseover(function(e){
			if($(this).hasClass('ready')){
				$(this).animate({
					opacity:1			
				},400);
			}
		}).mouseout(function(e){
			if($(this).hasClass('ready')){
				$(this).animate({
					opacity:0.7			
				},1000);
			}
		});	
		
	$('p.nav.fade a').css({'opacity':0,'color':'#ffffff'});
	$('p.nav:not(.fade, .blog) a').css('margin-top',0);
	$('p.nav a')
	.mouseover(function(e){
		if($(this).hasClass('ready')){
			$(this).animate({
				color:'#ffffff'			
			},300);
		}
	}).mouseout(function(e){
		if($(this).hasClass('ready')){
			$(this).animate({
				color:'#e1e1e1'			
			},1000);
		}
	});
	
	$('#intro').css({
		'background':'#000000 url(assets/images/ui/intro-placeholder.jpg) center center no-repeat',
		'opacity':0
	}).flash({
		src:'assets/swf/intro.swf',
		height:365,
		width:940,
		wmode:'opaque',
		bgcolor:'#000000',
		expressInstall:false
	},{
		update:false	
	}).fadeTo(700,1);
	
	// gallery
	$('#gallery ul:not(.news, .pc)').css('width',$('#gallery ul:not(.news, .pc) li').length*204);
	$('#gallery ul.news').css('width',$('#gallery ul.news li').length*408);
	$('#gallery ul.pc').css('width',$('#gallery ul.pc li').length*408);
	if($('#gallery ul:not(.news, .pc) li').length > 4){
		numScreens = Math.ceil($('#gallery ul:not(.news, .pc) li').length / 4);
		updateNextPrev();
		$('.control').show();	
	}
	if($('#gallery ul.news li').length > 2){
		numScreens = Math.ceil($('#gallery ul.news li').length / 2);
		updateNextPrev();
		$('.control').show();	
	}
	if($('#gallery ul.pc li').length > 2){
		numScreens = Math.ceil($('#gallery ul.pc li').length / 2);
		updateNextPrev();
		$('.control').show();	
	}
	if($('#press-gallery div.container div.page').length > 1){
		numScreens = Math.ceil($('#press-gallery div.container div.page').length);
		updateNextPrev();
		$('.control').show();
	}
	
	$('#gallery ul:not(.news, .pc) li a').mouseenter(function(e){$(this).fadeTo(1000,1)}).mouseleave(function(e){$(this).fadeTo(1000,0.55)}).css('opacity',0).each(function(index){
		$(this).delay(index*500).animate({'opacity':1},1000).animate({'opacity':0.55},1000);	 
	});
	$('#gallery ul.news li').each(function(index){
		$(this).css('opacity',0);
		$(this).delay(index*500).animate({'opacity':1},1000);	 
	});
	$('#gallery ul.pc li').each(function(index){
		$(this).css('opacity',0);
		$(this).delay(index*500).animate({'opacity':1},1000);	 
	});
	$('#gallery ul li span.divider').css('opacity',0).each(function(index){
		$(this).delay(index*500).animate({'opacity':1},1000);	 
	});
	
	$('#press-gallery div.container div.page a').css('opacity',0.55);
	$('#press-gallery div.container div.page a').mouseover(function(e){
		$(this).animate({'opacity':1}, 1000);															
	}).mouseout(function(e){
		$(this).animate({'opacity':0.55}, 1000);
	});
	
	$('.control:not(.press) a').css('opacity',0.6).mouseover(function(e){ $(this).animate({opacity:1}, 500)}).mouseout(function(e){ $(this).animate({opacity:0.6},500);});
	
	$('.control:not(.press) a.next').click(function(e){
		e.preventDefault();
		currentScreen++;
		$('#gallery ul').animate({'margin-left':-816*currentScreen},1000,'easeOutExpo');
		updateNextPrev();
	});
	$('.control:not(.press) a.prev').click(function(e){
		e.preventDefault();
		currentScreen--;
		$('#gallery ul').animate({'margin-left':-816*currentScreen},1000, 'easeOutExpo');
		updateNextPrev();
	});
	
	$('.control.press a.next').click(function(e){
		e.preventDefault();
		currentScreen++;
		$('#press-gallery div.container').animate({'margin-left':18-340*currentScreen},1000,'easeOutExpo');
		updateNextPrev();
	});
	$('.control.press a.prev').click(function(e){
		e.preventDefault();
		currentScreen--;
		$('#press-gallery div.container').animate({'margin-left':18-340*currentScreen},1000, 'easeOutExpo');
		updateNextPrev();
	});
	
	$('a.subnav').css('opacity',0.7).mouseover(function(e){$(this).fadeTo(1000,1)}).mouseout(function(e){$(this).fadeTo(1000,0.7)});
	
	$('p.thumbnails a:first').addClass('selected');
	$('p.thumbnails a:not(.selected)').find('span').css('opacity',0);
	$('p.thumbnails a').mouseover(function(e){
		if(!$(this).hasClass('selected')){
			$(this).find('span').fadeTo(1000, 1);	
		}
	}).mouseout(function(e){
		if(!$(this).hasClass('selected')){
			$(this).find('span').fadeTo(700, 0);	
		}
	}).click(function(e){
		e.preventDefault();
		if(!$(this).hasClass('selected')){
			$('p.thumbnails a').removeClass('selected');
			$(this).addClass('selected');
			$('p.thumbnails a').trigger('mouseout');
			$('#detail-image').find('img').attr('src',$(this).attr('href'));
		}
	});
	
	$('form.login')
		.find('#username').val('Username').focus(function(e){
			if($(this).val() == 'Username'){
				$(this).val('');	
			}
		}).blur(function(e){
			if($(this).val() == ''){
				$(this).val('Username');	
			}
		});
	$('form.login').find('#password').val('Password').focus(function(e){
			if($(this).val() == 'Password'){
				$(this).val('');	
			}
		}).blur(function(e){
			if($(this).val() == ''){
				$(this).val('Password');	
			}
		});
	
	$('form.signup')
		.find('#email').val('Email address').focus(function(e){
			if($(this).val() == 'Email address'){
				$(this).val('');	
			}
		}).blur(function(e){
			if($(this).val() == ''){
				$(this).val('Email address');	
			}
		});
	
	$('h2.contact').css('opacity',0).animate({opacity:1},1000);
	$('.inset.contact').find('.info.fleft').css('opacity',0).animate({opacity:1},1000);
	$('.inset.contact').find('.address.fleft').css('opacity',0).delay(500).animate({opacity:1},1000);
	
	$('#gallery.about').find('img.callout').css('opacity',0).animate({opacity:1},1000);
	$('#gallery.about').find('p.description').css('opacity',0).delay(500).animate({opacity:1},1000);
	
	$('div.entry-content').each(function(index){
		$(this).find('a img').eq(0).hide();
	});
	// homepage intro animation
	setTimeout(showLogo,3000);
	setTimeout(showNav,3500);
	setTimeout(showLink,4000);
	setTimeout(showFooter,4500);
	
});
