// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery(function( $ ){
	 
	$.localScroll();
	
	$('#contact_form').localScroll();

	
	$('#container').serialScroll({
		target:'#screen',
		items:'#thecontent li.page', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		axis:'x',// The default is 'y' scroll on both ways
		navigation:'#navigation a',
		duration:400,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		
		onBefore:function( e, elem, $pane, $items, pos ){
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
		}
	});
	
	$(".trigger").click(function() {
		type = $(this).attr("rel");
	
		$(".page:visible").fadeOut(500);
		$("#port-"+type).fadeIn(500);
	});
	
	$(".trigger2").click(function() {
		type = $(this).attr("rel");
	
		$(".page2:visible").fadeOut(500);
		$("#stuff-"+type).fadeIn(500);
	});
	
	$(".trigger3").click(function() {
		type = $(this).attr("rel");
	
		$(".page3:visible").fadeOut(500);
		$("#who-"+type).fadeIn(500);
	});
	$(".trigger4").click(function() {
		type = $(this).attr("rel");
	
		$(".page4:visible").fadeOut(500);
		$("#think-"+type).fadeIn(500);
	});
	
	/*
	$("ul.services").tabs("div.portfolio > div", { 
    /* tabs configuration goes here  
 
    // another property 
    effect: 'fade', 
 
    // ... the rest of the configuration properties 
	});*/


});

jQuery(function( $ ){
	/**
	 * Demo binding and preparation, no need to read this part
	 */
	//borrowed from jQuery easing plugin
	//http://gsgd.co.uk/sandbox/jquery.easing.php
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	// back links
	$('a.back').click(function(){
		$(this).parents('div.pane').scrollTo( 0, 800, { queue:true } );
		$(this).parents('div.section').find('span.message').text( this.title );
		return false;
	});
	//just for the example, to stop the click on the links.
	$('ul.links').click(function(e){
		e.preventDefault();
		var link = e.target;
		link.blur();
		if( link.title )
			$(this).parent().find('span.message').text(link.title);
	});
	
	// This one is important, many browsers don't reset scroll on refreshes
	// Reset all scrollable panes to (0,0)
	$('div.pane').scrollTo( 0 );
	// Reset the screen to (0,0)
	$.scrollTo( 0 );
	
/*
	// TOC, shows how to scroll the whole window
	$('#container a').click(function(){//$.scrollTo works EXACTLY the same way, but scrolls the whole screen
		$.scrollTo( this.hash, 1000);
		$(this.hash).find('span.message').text( this.title );
		return false;
	});
*/
	
	
});

$(document).ready(function() { 
    var options = { 
        target:        '#contact_form',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#myForm').ajaxForm(options); 
}); 

 

$(document).ready(function(){	
		$("#port-identity-screen").easySlider({
			controlsBefore:	'<p id="controls2">',
			controlsAfter:	'</p>',	
			speed: 200,
			prevId: 'prevBtn2',
			nextId: 'nextBtn2'	
		});
		$("#port-web-screen").easySlider({
			controlsBefore:	'<p id="controls">',
			controlsAfter:	'</p>',
			speed: 200,
			auto: false,
			continuous: true
		});
		$(".who-we-are").easySlider({
			controlsBefore:	'<p id="controls3">',
			controlsAfter:	'</p>',
			prevId: 'prevBtn3',
			nextId: 'nextBtn3'	
		});
		//$("#how-we-think").easySlider({
		//	controlsBefore:	'<p id="controls4">',
		//	controlsAfter:	'</p>',
		//	prevId: 'prevBtn4',
		//	nextId: 'nextBtn4'	
		//});
		$("#work-with-us").easySlider({
			controlsBefore:	'<p id="controls7">',
			controlsAfter:	'</p>',
			prevId: 'prevBtn7',
			nextId: 'nextBtn7'	
		});
		$("#port-print-screen").easySlider({
			controlsBefore:	'<p id="controls5">',
			controlsAfter:	'</p>',	
			speed: 200,
			prevId: 'prevBtn5',
			nextId: 'nextBtn5'	
		});
		$("#port-display-screen").easySlider({
			controlsBefore:	'<p id="controls6">',
			controlsAfter:	'</p>',	
			speed: 200,
			prevId: 'prevBtn6',
			nextId: 'nextBtn6'	
		});
		$("#port-health-screen").easySlider({
			controlsBefore:	'<p id="controls8">',
			controlsAfter:	'</p>',	
			speed: 200,
			prevId: 'prevBtn8',
			nextId: 'nextBtn8'	
		});

		$("#introductions").easySlider({
			controlsBefore:	'<p id="introControl">',
			controlsAfter:	'</p>',	
			prevId: 'prevIntro',
			nextId: 'nextIntro'	
		});
		$("#content").easySlider({
			controlsBefore:	'<p id="controls">',
			controlsAfter:	'</p>',
			auto: false,
			speed: 	400,
			continuous: true,
			prevId: 'prevBtn',
			nextId: 'nextBtn'	
		});
	});	


// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  {} 

$(document).ready(function() {
	$.featureList(
		$("#tabs li a"),
		$("#output li"), {
		}
	);
});
$(function(){
		$('.slides').slides({
			preload: true,
			generateNextPrev: true
		});
});
$(function(){
    $('img.lazy').jail({
        offset:160
    }).asynchImageLoader({
        event: "focus",
/*         placeholder : '../images/loading.gif' */
    });
});
		/* On Twitter JSON Response
		 * 
		 * This method is called by Twitter when the .json request is fully loaded. Go to:
		 * http://twitter.com/statuses/user_timeline/pixelsavvy.json?suppress_response_codes=true&amp;callback=onResponse&amp;count=2 
		 * to see how a callback works.
		 *
		 *	This function could be re-jingled to return the generated HTML without adding it to the page.
		 *
		 * @param	tweets		A list of tweets and their associated data
		 *
		 */
		function onTwitterJsonResponse(tweets) {

			var i = 0;
			var output = '';

			// Loop through the tweets and append the generated html to our output string.
			for (var i in tweets)
			{
				output += ('<div class="tweet">\
								<p class="tweet-content">' + replaceTwitterNameWithHTMLLinks(replaceURLWithHTMLLinks(tweets[i].text)) + '</p>\
								<p class="tweet-meta">\
									<time datetime="' + (new Date(tweets[i].created_at)).toString() + '">\
										<a href="http://twitter.com/' + tweets[i].user.screen_name + '/status/' + tweets[i].id + '">' + relativeTime(tweets[i].created_at) + '</a>\
									</time>\
								</p>\
							</div>');
			}

			// Grab the twitterUpdates element and insert our generated HTML into it
			// This could be replaced with $('#twitterUpdates') if you're using jQuery
			document.getElementById('twitterUpdates').innerHTML = output;
		
		}


		/* Relative Time
		 * 
		 * Takes a JS Date object and returns a human readable timestamp.
		 *
		 * @param	dateValue		A list of tweets and their associated data
		 *
		 */
		function relativeTime(dateValue) {
		
			var values		= dateValue.split(" ");
			dateValue		= values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		
			var parsed_date	= Date.parse(dateValue);
			var relative_to	= (arguments.length > 1) ? arguments[1] : new Date();
			var delta		= parseInt((relative_to.getTime() - parsed_date) / 1000);
		
			delta = delta + (relative_to.getTimezoneOffset() * 60);

			if (delta < 60)
			{
				return 'less than a minute ago';
			} 
			else if (delta < 120)
			{
				return 'about a minute ago';
			}
			else if (delta < (60 * 60))
			{
				return (parseInt(delta / 60)).toString() + ' minutes ago';
			}
			else if (delta < (120 * 60))
			{
				return 'about an hour ago';
			}
			else if (delta < (24 * 60 * 60))
			{
				return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
			}
			else if (delta < (48 * 60 * 60))
			{
				return '1 day ago';
			}
			else
			{
				return (parseInt(delta / 86400)).toString() + ' days ago';
			}
		}


		/* Replace URL With HTML Links
		 * 
		 * Accepts a string and converts any URLs into actual hyperlinks.
		 *
		 * @param	text		A string containing HTML links
		 *
		 */
		function replaceURLWithHTMLLinks(text) {
		  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		  return text.replace(exp,"<a href='$1'>$1</a>"); 
		}


		/* Replace Twitter Name With HTML Links
		 * 
		 * Accepts a string and converts any @Name into twitter account links.
		 *
		 * @param	text		A string containing HTML links
		 *
		 */
		function replaceTwitterNameWithHTMLLinks(text) {
		  var exp = /(@[A-Z0-9_]+)/ig;
		  return text.replace(exp,"<a href='http://twitter.com/$1'>$1</a>"); 
		}
