$(document).ready(function()
{
	$('.nav_hover').hover(function()
	{
		$(this).children('ul').show('slow');
	},function(){$('.subnav').hide();});
	
	if($('#index_slides').length)
		$("#index_slides").cycle({fx:'fade',background:'transparent'});
	
	$('.icon').hover(function(){
		alttext = $(this).children('a').children('img').attr('alt');
		$(this).children('.shadow').text(alttext);
	},function(){
		$(this).children('.shadow').text('');
	});
	
	$('.scope-content').scroll_me();
	i=1;
	$('.scope-content').each(function(){
		if(i>1)
			$(this).hide();
		i++;
	});
	j=1;
	$('.scope-image').each(function(){
		margin = (500-$(this).height())/2;
		$(this).css({'margin-top':margin+'px'});
		if(j>1)
			$(this).hide();
		else{
			href =$(this).attr('href')
			if(href)
				$('#overlay_controls').append('<div id="before_button" href="'+href+'">before</div>');
		}	
		j++;
	});
	
	if($('#before_button').length){
		$('#before_button').fancybox({
				'overlayShow'	: true,
				'overlayOpacity'	: 0.0,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'padding' : 0
			});
	}
	
	if(i<3){
		$('#next').css({'visibility':'hidden'});
		$('#last').css({'visibility':'hidden'});
	}
	
	if($('.scope_link').length){
		$('.scope_link:first').addClass('selected');
		$('.scope_link:first').parent().addClass('selected');
		current_scope = $('.scope_link:.selected').attr('href').substring(1);
	}
	function show_scope(scope){
		$('.scope_link').removeClass('selected');
		$('.scope_link').parent().removeClass('selected');
		$('#scope_'+scope).addClass('selected');
		$('#scope_'+scope).children('a').addClass('selected');
		
		if($('#scope-'+scope).length){
			$('.scope-content').hide();
			$('#scope-'+scope).show();
		}
		if($('#scope-image-'+scope).length){
			$('.scope-image').hide();
			$('#scope-image-'+scope).show();
			margin = (500-$('#scope-image-'+scope).height())/2;
			$('#scope-image-'+scope).css({'margin-top':margin+'px'});
			if(href = $('#scope-image-'+scope).attr('href')){
				if($('#before_button').length)
					$('#before_button').attr('href',href);
				else
					$('#overlay_controls').append('<div id="before_button" href="'+href+'">before</div>');
			}else
				$('#before_button').remove();
				
			if($('#before_button').length){
				$('#before_button').fancybox({
						'overlayShow'	: true,
						'overlayOpacity'	: 0.0,
						'transitionIn'	: 'elastic',
						'transitionOut'	: 'elastic',
						'padding' : 0
					});
			}
		}
		if($('#scope_'+scope).attr('class')=='first selected')
			$('#last').css({'visibility':'hidden'});
		else
			$('#last').css({'visibility':'visible'});
			
		if($('.last').length){
			if($('#scope_'+scope).attr('class')=='last selected')
				$('#next').css({'visibility':'hidden'});
			else
				$('#next').css({'visibility':'visible'});
		}
	}
	$('.scope_link').click(function(){
		current_scope = $(this).attr('href').substring(1);
		show_scope(current_scope);
	});
	
	$('#last').css({'visibility':'hidden'});
	$('#next').click(function(){
		current_scope = $('li#scope_'+current_scope).next().children('a').attr('href').substring(1);
		if($('li#scope_'+current_scope).attr('class')=='last')
			$('#next').css({'visibility':'hidden'});
		
		$('#last').css({'visibility':'visible'});
		show_scope(current_scope);
	});
	$('#last').click(function(){
		current_scope = $('li#scope_'+current_scope).prev().children('a').attr('href').substring(1);
		if($('li#scope_'+current_scope).attr('class')=='first')
			$('#last').css({'visibility':'hidden'});
		
		$('#next').css({'visibility':'visible'});
		show_scope(current_scope);
	});
	
	num_icons = $("#bottom_icons li").length;
	new_width = num_icons*108;
	num_pages = Math.ceil(num_icons/8);
	left = 1;
	
	current_shown_port = Math.floor($('#bottom_icons').children('.current_scope').index()/8);
	if(current_shown_port>0){
		left = current_shown_port+1;
		$('#bottom_icons').css({'margin-left':'-'+(864*current_shown_port)});
	}else
		$("#bottom_left").css({'visibility':'hidden'});
	if(left>=num_pages){
		left=num_pages;
		$('#bottom_right').css({'visibility':'hidden'});
	}
		
	$("#bottom_icons").css({'width':new_width+"px"});
	$("#bottom_right").click(function(){
		$("#bottom_left").css({'visibility':'visible'});
		if(left<(num_pages))
			$('#bottom_icons').animate({'margin-left':'-=864',speed:'slow'});
		left = left+1;
		if(left>=(num_pages)){
			left = num_pages;
			$("#bottom_right").css({'visibility':'hidden'});
		}
	});
	$("#bottom_left").click(function(){
		$("#bottom_right").css({'visibility':'visible'});
		if(left>1)
			$('#bottom_icons').animate({'margin-left':'+=864',speed:'slow'});
		left = left-1;
		if(left<2){
			left = 1;
			$("#bottom_left").css({'visibility':'hidden'});
		}
	});
	
	$('#scroll_up').hide();
	num_ricons = $("#right_icons li").length;
	num_rows = num_ricons/4;
	new_height = num_ricons*120;
	if(num_ricons<17)
		$('#scroll_down').hide();
	from_top = 0;
	//console.log(from_top);
	$("#right_icons").css({'height':new_height+"px"});
	$("#scroll_down").click(function(){
		//console.log('hi'+num_rows+' '+from_top);
		if(from_top<(num_rows-4))
			$('#right_icons').animate({'margin-top':'-=120'});
		from_top++;
		if(from_top>=(num_rows-4)){
			from_top = num_rows-4;
			$('#scroll_down').hide();
		}
		if(from_top>0)
			$('#scroll_up').show();
	});
	$("#scroll_up").click(function(){
		if(from_top>0)
			$('#right_icons').animate({'margin-top':'+=120'});
		from_top--;
		if(from_top<1){
			from_top = 0;
			$('#scroll_up').hide();
			$('#scroll_down').show();
		}
		
	});
});
