$(document).ready(function(){
	$('#nav a:first').addClass('first');
	if($('#nav a.sel').index() == 0) $('#nav a:first').addClass('selFirst');
	
	// screeny
	var randStart = Math.floor(Math.random()*$('#screen div').size());
	$('#screen').prepend('<p class="bgBlack"></p>');
	$('#screen div').eq(randStart).css('display','block');
	$('#screen div:eq('+randStart+') div').css('display','block');
	$('#screen p.scrNav a').eq(randStart).addClass('sel');
	
	// start rotate screens with 5s delay
	$('#screen p.scrNav').animate({marginBottom: '0px'},8000,function(){
		if(randStart == $('#screen div').size()-1) changeScreen(randStart, 0);
		else changeScreen(randStart, randStart+1);
	});
	$('#screen p.scrNav a').click(function(e){
		e.preventDefault();
		$('#screen p.scrNav').stop();
		changeScreen(($('#screen div:visible').index()-1),$(this).index());
	});
	
	$('a.lightbox').lightBox();
	
	// FAQ
	$('div.faq p.question').wrapInner('<a href="#" />');
	$('div.answer').css('display','none');
	$('div.faq p.question a').click(function(e){
		e.preventDefault();
		$(this).parent().parent().children('div.answer').slideToggle();
	});
	
	// lightbox link catch
	$('#ramy_covers a').click(function(e){
		e.preventDefault();
		callLightbox($(this).attr('href'));
	});
	$('a.textLightBox').click(function(e){
		e.preventDefault();
		callLightbox($(this).attr('href'));
	});
});
$(window).scroll(function(){
	//$('#lightbox_window').css({marginTop: (($(window).height()-$('#lightbox_window').height())/2)+$(window).scrollTop()+'px'});
});

function changeScreen(hide, show){
	$('#screen div').eq(hide).fadeOut(function(){
		$('#screen p.scrNav a').eq(hide).removeClass('sel');
		$('#screen p.scrNav a').eq(show).addClass('sel');
		$('#screen div').eq(show).fadeIn(function(){
			hide = show;
			if(++show == $('#screen div').size()) show = 0;
			$('#screen p.scrNav').animate({marginBottom: '0px'},8000,function(){changeScreen(hide, show);});
		});
	});
}

function callLightbox(href){
	// creating and positioning elements
	$('body').append('<div id="lightbox_bg"></div><div id="lightbox_window"></div><a href="#" id="lightbox_close"></a>');
	$('#lightbox_bg').css({width: $(window).width()+'px',height: $(window).height()+'px',opacity: '0'}).click(function(){
		$('#lightbox_close').fadeOut();
		$('#lightbox_window').fadeOut();
		$('#lightbox_bg').fadeOut(function(){
			$('div').remove('#lightbox_window');
			$('a').remove('#lightbox_close');
			$('div').remove('#lightbox_bg');
		});
	});
	// loading content
	$('#lightbox_window').css({marginLeft: (($(window).width()-$('#lightbox_window').width())/2)+'px',marginTop: (($(window).height()-$('#lightbox_window').height())/2)+$(window).scrollTop()+'px',opacity: '0'}).load(href,function(){
		//show lightbox
		$('#lightbox_window').animate({opacity: '1'},200,function(){lightboxActions();});
	})
	$('#lightbox_bg').animate({opacity: '0.85'},500).css('width',$(this).width()+21+'px');
	// close lightbox
	$('#lightbox_close').css({marginLeft: (($(window).width()+$('#lightbox_window').width())/2)+'px',marginTop: (($(window).height()-$('#lightbox_window').height())/2)-35+$(window).scrollTop()+'px',opacity: '1'}).click(function(e){
		e.preventDefault();
		$('#lightbox_close').fadeOut();
		$('#lightbox_window').fadeOut();
		$('#lightbox_bg').fadeOut(function(){
			$('div').remove('#lightbox_window');
			$('a').remove('#lightbox_close');
			$('div').remove('#lightbox_bg');
		});
	});
}

function lightboxActions(){
	$('p.sameWindow a').click(function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		$('#lightbox_window').fadeOut(200,function(){
			$('#lightbox_window').load(href,function(){
				$('#lightbox_window').fadeIn(200);
				lightboxActions();
			});
		});
	});
}
