$(document).ready(function() 
{
	if(screen.height < 1000)
	{
		$('html').css('margin-top', '-180px');
		$('html').css('margin-left', '-40px');
		$('html').css('font-size', '80%');
		$('#slideshow').css('margin-top', '-90px');
		$('ul#scrollcontainer').css('min-height', '690px');
		$('ul#scrollcontainer').css('min-height', '690px');
		$('div#moveup').css('margin-left', '-50px');
		$('div#movedown').css('margin-left', '-50px');
		$('div#moveup').css('margin-top', '200px');
		$('div#movedown').css('margin-top', '200px');
		$('span.note').css('padding-top', '5px');
	}
	
	if($('ul#scrollcontainer').length > 0)
	{
		if(jQuery.browser.msie)
		{
			$('ul#scrollcontainer').css('height', getViewportHeight()-24);
		}
		else
		{
			$('ul#scrollcontainer').css('height', getViewportHeight()-70);
		}
				
		
		checkUpDown();
		
		$('ul#scrollcontainer li.item').each(function()
		{
			$(this).mouseenter(function()
			{
				if(!visible(this) && !jQuery.browser.msie)
				{		
					$(this).block(
					{ 
						overlayCSS: { backgroundColor: '#ccc', cursor: 'pointer' },
						message: null
					});
				}
				else if(!visible(this) && jQuery.browser.msie)
				{
					$(this).addClass("ieactive");
				}
			});
				
			$(this).mouseleave(function()
			{
				if(!jQuery.browser.msie)
				{
					$(this).unblock();
				}
				else
				{
					$(this).removeClass("ieactive");
				}
			});
			
			$(this).click(function()
			{
				$(this).unblock();
				
				if(!visible(this))
				{	
					if($(this).offset().top > 0)
					{
						if(screen.height < 1000)
						{
							$('ul#scrollcontainer').scrollTo( {top: '+=626px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
						}
						else
						{
							$('ul#scrollcontainer').scrollTo( {top: '+=640px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
						}
					}
					else
					{
						if(screen.height < 1000)
						{
							$('ul#scrollcontainer').scrollTo( {top: '-=626px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
						}
						else
						{
							$('ul#scrollcontainer').scrollTo( {top: '-=640px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
						}
					}
				}
			});			
		});
		
		$('ul#scrollcontainer img.next').each(function()
		{
			$(this).click(function()
			{
				$('ul#scrollcontainer').scrollTo( {top: '0px', left: '+=822px'}, 2000, {axis:'x'} );
			});
		});
		
		$('ul#scrollcontainer li.main_scroll:first').css('padding-top', '120px');
		$('ul#scrollcontainer li.main_scroll:last').css('padding-bottom', '85px');
		
		$('#moveup').click(function () 
		{ 
			if(screen.height < 1000)
			{
				$('ul#scrollcontainer').scrollTo( {top: '-=626px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
			}
			else
			{
				$('ul#scrollcontainer').scrollTo( {top: '-=640px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
			}
	    });
	
		$('#movedown').click(function () 
		{ 
			if(screen.height < 1000)
			{
				$('ul#scrollcontainer').scrollTo( {top: '+=626px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
			}
			else
			{
				$('ul#scrollcontainer').scrollTo( {top: '+=640px', left: '0px'}, 2000, {axis:'xy', onAfter:checkUpDown} );
			}
		});
		
		$('li.main_scroll').each(function()
		{
			$('ul.fade_navigation li a').each(function()
			{
				this.onclick = function()
				{
					$('ul#scrollcontainer').scrollTo( this.hash, 2000, {axis:'x'} );
					return false;
				}
			});
		});
		
	}
	
	if($('#slideshow').length > 0)
	{
		$(function() 
		{
		    setInterval( "slideSwitch()", 5000 );
		});

	}
	
	$('#navitems a').each(function()
	{
		if(this.id == 'projects')
		{
			$(this).click(function()
			{
				if($('#subnav').css("display") == 'block')
				{
					$('#subnav').hide("slow");
				}
				else
				{
					$('#subnav').show("slow");
				}
				return false;
			});
		}
	});
});

//Show / hide the up and down arrow button
function checkUpDown()
{
	var iScrollTop 		= $('ul#scrollcontainer').scrollTop();
	var iInnerHeight	= $('ul#scrollcontainer').innerHeight();
	var iScrollTotal	= document.getElementById('scrollcontainer').scrollHeight;

	//Move down button
	if((iScrollTop + iInnerHeight) >= iScrollTotal)
	{
		$('#movedown').hide("slow");
	}
	else
	{
		$('#movedown').show("slow");
	}

	if(iScrollTop < 100)
	{
		$('#moveup').hide("slow");
	}
	else
	{
		$('#moveup').show("slow");
	}
}

function visible( elm )
{
	var offset 			= $(elm).offset().top;
	var height 			= $(elm).height();
	var viewportheight 	= getViewportHeight();
	var visible			= (offset < 0) || ((offset + height) > viewportheight);
	
	return !visible;
}

function getViewportHeight() 
{
    var height = window.innerHeight; // Safari, Opera
    var mode = document.compatMode;

    if ( (mode || !$.support.boxModel) ) { // IE, Gecko
        height = (mode == 'CSS1Compat') ?
        document.documentElement.clientHeight : // Standards
        document.body.clientHeight; // Quirks
    }

    return height;
}


function slideSwitch() 
{
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
