// global javascript and jquery functions for Justin Barker.tv
// (c) Erik Gorton 2009 Ethical Design

$(document).ready(function() {
													 
	if(gCurrentPage == "about"){timeline();}
	
	if(gCurrentPage == "services"){slideshows_services();}
	
	if(gCurrentPage == "latest"){slideshows_latest();}
	
  $('.whois_activate').click(function(){
  	$('#whois_content_box').slideToggle({duration:500, easing:'easeInOutSine'});
		return false;
  });
	
	slideshows_footer();
	
	// Setup the Share button
	
	$('#footer_icons_box .share').hover(function(){ $('#footer_share_window').css('display','block'); return false; });
	
	$('#footer_share_window').hover(
      function (){ $('#footer_icons_box .share').css('background-position','0px -24px'); },
      function (){ 
				$(this).css('display','none');
				$('#footer_icons_box .share').css('background-position','0px 0px');
			}
    );
	
	$('#footer_share_window').click(function(){ $(this).css('display','none'); });

});

// ---------------------------------------------------------------------------------------------

function slideshows_footer(){
	$('.slider_footer_bubble').cycle({ 
			fx:      'scrollDown',
			easing:  'easeInOutBack',
			speed:    800, 
			timeout:  7000,
			next:   '.slider_footer_bubble',
			random:   1
	});
	
	$('.slider_footer_clients').cycle({ 
			fx:      'fade',
			easing:  'easeInSine',
			speed:    800, 
			timeout:  5000,
			next:   '.slider_footer_clients',
			random:   1
	});
}


function slideshows_services(){
	$('.slideshow_references').cycle({ 
			fx:      'scrollLeft',
			easing:  'easeInOutBack',
			speed:    1000, 
			timeout:  10000,
			next:   '.slideshow_references',
	});
}


function slideshows_latest(){
	$('.slideshow_flickr').cycle({ 
			fx:      'scrollLeft',
			easing:  'easeInOutBack',
			speed:    1000, 
			timeout:  10000,
			next:   '.slideshow_flickr',
			pause:    1
	});
}

// ---------------------------------------------------------------------------------------------

function timeline() {
	var theRuler = document.getElementById("timeline_ruler");
	var theWidthIncrement = 50;
	var theHeightIncrement = 15;
	var theHeightOffset = 245 -15; // +/-# = adjustment. this is the height of the main graphic we dont want to obscure.
	var theHeadingOffset = 36; // need to remove the heading height as it uses a negative margin and doesn't show up in normal equation.
	var theMaxHeight = 650;
	var theEntryWidth = 196 + 40; // +40 = the separation between whole columns.
	
	var theTotalWidth = 0 - theWidthIncrement;
	var theTotalHeight = theHeightOffset - theHeightIncrement;
	var theHeight = 0;
	var theTempHeight = 0;
	var theEntryNum = 0;
	while(1==1){
		theEntryNum++;
		var theEntry = document.getElementById("entry_" + theEntryNum);
		if (!theEntry) { theEntryNum--; break; }
		theRuler.innerHTML = theEntry.innerHTML;
		theHeight = theRuler.clientHeight;
		//alert(theEntryNum+" = "+theHeight);
		theTempHeight = theTotalHeight + theHeightIncrement + theHeight;
		if ( (theTempHeight + theHeadingOffset) > theMaxHeight) { 
			theTotalHeight = theHeightOffset + theHeight; theTotalWidth = theTotalWidth + theEntryWidth;
		} else {
			theTotalHeight = theTempHeight;
			theTotalWidth = theTotalWidth + theWidthIncrement;
		}
		theEntry.style.height = (theTotalHeight - theHeadingOffset) +'px';
		theEntry.style.left = theTotalWidth+'px';
	}
	//alert(theEntryNum+" entries");
}

function timelinePreview(theType, theURL, theWidth, theHeight){
	var thePage_width = 1000;
	var thePage_height = 650;
	var thePreview_mask = document.getElementById("timeline_preview_mask");
	var thePreview_box = document.getElementById("timeline_preview_box");
	var thePreview = document.getElementById("timeline_preview");
	if(theType == "pic"){
		thePreview.innerHTML = '<a href="#" onClick="timelineClose(); return false"><img src="'+theURL+'"/></a>';
	} else {
		theWidth = 569;
		theHeight = 320;
		if(theType == "Vimeo"){
			thePreview.innerHTML = '<object width="'+theWidth+'" height="'+theHeight+'"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+theURL+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+theURL+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="'+theWidth+'" height="'+theHeight+'"></embed></object>';
		} else {
			thePreview.innerHTML = '<object width="'+theWidth+'" height="'+theHeight+'"><param name="movie" value="http://www.youtube.com/v/'+theURL+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+theURL+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+theWidth+'" height="'+theHeight+'"></embed></object>';
		}
	}
	thePreview.style.width = theWidth+'px';
	thePreview.style.height = theHeight+'px';
	thePreview_box.style.left = parseInt( (thePage_width - (theWidth+22) )/2 )+'px'; 
	// 22 -- 11 (outer box border)*2
	thePreview_box.style.top = parseInt(  ( (thePage_height - (theHeight+22) )/2 ) *0.6  )+'px' ;
	thePreview_mask.style.display = 'block';
	thePreview_box.style.display = 'block';
	thePreview.style.display = 'block';
}

function timelineClose(){
	var thePreview_mask = document.getElementById("timeline_preview_mask");
	var thePreview_box = document.getElementById("timeline_preview_box");
	var thePreview = document.getElementById("timeline_preview");
	thePreview_mask.style.display = 'none';
	thePreview_box.style.display = 'none';
	thePreview.style.display = 'none';
	thePreview.innerHTML = "";
}

function timelineSlide(theDirection){
	var theWidth = 800;
	var theMove = '';
	var elem = document.getElementById("timeline_container");
	var theMax = $.scrollTo.max( elem, 'x');
	var theCurrent = $('#timeline_container').scrollLeft();//current scrollLeft position of the div's content
	var theMove = theMax - theCurrent;
	if(theDirection == 'present'){ 
		theDirection = '-=';
		if(theMove > theCurrent){ theWidth = theCurrent; }
	} else { 
		theDirection = '+=';
		if(theMove < theWidth){ theWidth = theMove; }
	}
	//alert(theWidth);
	theMove = theDirection+theWidth+'px';
	$('#timeline_container').scrollTo( {top:'0px',left:theMove}, 800, { easing:'easeInOutQuad' } );
}

// ---------------------------------------------------------------------------------------------


/*******
	***	Anchor Slider by Cedric Dugas   ***
	*** Http://www.position-absolute.com ***
	Never have an anchor jumping your content, slide it.
	Don't forget to put an id to your anchor !
*****/
		
$(document).ready(function() {
	$("a.anchorScroll").anchorAnimate()
});

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1000
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}


/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);