
jQuery.noConflict();
jQuery(document).ready(function($)
{
	// iPad
	var isiPad = navigator.userAgent.match(/iPad/i);
	
	if(isiPad)
	{
		window.onscroll = function() 
		{
			var top = (window.pageYOffset + window.innerHeight - $('#footer').height()) + 'px';
			$('#footer').css('top', top);
		}
	}

	// HOME
	var div = $("#welcome");
		
	if (div.size() > 0)
	{
		div.css({bottom: "-"+div.css('height')});
		setTimeout(onTimeout, 3000);
	}
	
	function onTimeout()
	{
		div.stop().animate({bottom:"0"}, {duration:2000, easing:"easeOutExpo"});
		$('#welcome .close').click(function() 
		{ 
			var bottom = div.height(); // div.height() - 50;
			div.stop().animate({bottom:"-"+bottom+"px", opacity:"0.8"}, {duration:2000, easing:"easeOutExpo"}); 
		});
	}
	
	// scrollable
	// http://www.kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
	// adjust height of scrollable divs based on window height
	
	// height( Math.floor($(window).height()*0.65) )
	var $scrollable = $('.scrollable')
		.height( Math.floor($(window).height()*0.65) )
		.jScrollPane(
		{
			// scrollbarWidth	: 9, 
			// scrollbarMargin	: 10, 
			// dragMinHeight	: 60,
			// wheelSpeed		: 20,
			// showArrows		: false, 
			// animateTo		: true,
			// animateInterval	: 500,
			// animateStep		: 3,
			// reinitialiseOnImageLoad: true
			verticalDragMinHeight: 60,
			verticalGutter: 9,
			animateScroll: true,
			mouseWheelSpeed: 20
		}),
		api = $scrollable.data('jsp');

	// $('#scroll-test').height( Math.floor($(window).height()*0.65) );
	$(window).resize(function() 
	{
		// console.log($('#scroll-test'));
		// $('#scroll-test').height( Math.floor($(window).height()*0.65) );
		$scrollable.height( Math.floor($(window).height()*0.65) )
		api.reinitialise();
	});
	
	$('#subnav a')
		.css( {backgroundPosition: "0 0"} )
		.mouseover(function()
		{
			$(this).stop().animate({backgroundPosition:"(0 -130px)"}, {duration:100});
		})
		.mouseout(function()
		{
			$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:800})
		});
	
	// ----------------------------------------------------- related
	
	$('.related a')
		.css( {backgroundPosition: "235px 50%"} )
		.mouseover(function()
		{
			$(this).stop().animate({backgroundPosition:"(240px 50%)"}, {duration:100});
		})
		.mouseout(function()
		{
			$(this).stop().animate({backgroundPosition:"(235px 50%)"}, {duration:300})
		});
		
	
	$('a.bio').fancybox(
	{
		'padding'				: 0,
		'overlayOpacity'		: 0.8,
		'overlayColor'			: '#FFFFFF',
		'frameWidth'			: 600,
		'frameHeight'			: 250,
		'hideOnContentClick'	: false,
		'callbackOnShow'		: function() { $('.headshots div').cycle({timeout: 2500}); }, // http://jquery.malsup.com/cycle/lite/
		'callbackOnClose'		: function() { $('.headshots div').cycle('destroy'); }
	});
	
	$("a.popup").fancybox(
	{
		'padding'				: 0,
		'overlayOpacity'		: 0.8,
		'overlayColor'			: '#FFFFFF',
		'frameWidth'			: 720,
		'frameHeight'			: 550,
		'hideOnContentClick'	: true
	});
	
	$('a.project').fancybox(
	{
		'padding'				: 0,
		'overlayOpacity'		: 0.8,
		'overlayColor'			: '#FFFFFF',
		'frameWidth'			: 225,
		'frameHeight'			: 285,
		'hideOnContentClick'	: false
	});
	
	// ----------------------------------------------------- grid
	
	// $('ul.grid li').each(function() { $(this).css({opacity:0.6}); });
	
	$('ul.grid li')
		.mouseover(function()
		{
			var a = $(this).find('a.view');
			a.html(a.attr('rel'));
			a.stop().animate({height:"30px"}, {duration:100});
		})
		.mouseout(function()
		{
			var a = $(this).find('a.view');
			a.html('');
			a.stop().animate({height:"0"}, {duration:800, easing:"easeOutExpo"});
		}
	);
	
	// ----------------------------------------------------- process
	
	$('#process-nav a, #process-slider .slide a.next').click(function()
	{
		var step = Number($(this).attr('rel'));
		$('#process-slider-main').stop().animate({left:((step-1) * -650) + "px"}, {duration:500});
		
		$('#process-nav li').each(function() { $(this).removeClass('current_page_item'); });
		$('#process-nav li.step'+step).addClass('current_page_item');
	});
	
	// ----------------------------------------------------- downloads
	
	$('table.downloads tr')
		.mouseover(function() { $(this).css('backgroundColor', '#f1f1f1'); })
		.mouseout(function() { $(this).css('backgroundColor', '#ffffff'); });
	
	
	$('#downloads-nav a').click(function()
	{
		$('#downloads-nav a').removeClass('selected');
		$('table.downloads').css('display', 'none');
		
		var target = '#downloads-'+$(this).attr('rel');
		
		$(this).addClass('selected');
		$('#downloads-breadcrumb').text($(this).text());
		$(target).stop().css({display: 'block'});
	});
	
	
//	$('a.download').click(function() 
//	{
//		var values = $(this).attr('rel').split('|');
//		onDownload(values[0], values[1], values[2]);
//	});
	
	
	function onDownload(hash, id, section)
	{    
//	   var elemIF = document.createElement("iframe"); 
//	   elemIF.src = "/wp-content/plugins/hybrid/downloader.php?hash="+hash+"&action=download"; 
//	   elemIF.style.display = "none"; 
//	   document.body.appendChild(elemIF); 
		
		$('#iframe').attr('src', "/wp-content/plugins/hybrid/downloader.php?hash="+hash+"&action=download");
//		setTimeout(doDownload, 1000);
//		
//		function doDownload()
//		{
//			$.get('/wp-content/plugins/hybrid/downloader.php', { hash: hash, id: id, section: section, action: "change" }, function(data) {
//				$('#file-'+id+'-'+section).find('a').attr('rel', data);
//			});
//		}
	}
	
});
