// JavaScript Document
function doMail(name, company, domain) {
      locationstring = 'mai' + 'lto:' + name + '@' + company + '.' + domain;
      window.location.replace(locationstring);
   }
  
$(document).ready(function() {
	// fancy zoom
	if ($("body#home").length < 1){
		setupZoom();
	}
	
	// slider page
	// make image viewer super wide (# of images x mask width)
	$("#official .slideShow ul").css("width",$("#official .slideShow ul").children().size() * $("#official .slideShow").width());
	
	// make vimeo videos play inline
	$("#thevision ul li a").click(function(){
		var $li = $(this).parents('li');
		var $ul = $(this).parents('ul');
		var num = $ul.children().index($li);
		
		var title = $("#thevision ul li:eq("+num+") a").attr("title");
		var desc = $("#thevision ul li:eq("+num+") .thedesc").html();
		var clip = $("#thevision ul li:eq("+num+") .thedesc").attr("id");
		var clip = clip.replace("desc-","");
		
		makeVimeo(title, desc, clip);
		
		return false;
	});
	
	// make the gallery scroller
	$(".subnav #next").click(function(){
		nextImageSet();
		return false;
	});
	$(".subnav #prev").click(function(){
		prevImageSet();
		return false;
	});
	
	
	// make the discog links play something
	$("#thesound ol li a").click(function(){
										  
		var themp3 = $(this).attr("href");
		var themp3 = themp3.replace("_preview/","_mp3/");
		var theid = themp3.substr(5);
			
		if ($(this).hasClass("active")){
			soundManager.stopAll();
			soundManager.destroySound(theid);
			$(this).removeClass("active");
		} else {
			var $li = $(this).parents('li');
			var $ul = $(this).parents('ol');
			var num = $ul.children().index($li);
			$("#thesound ol li a").removeClass("active");
			$(this).addClass("active");
			
			soundManager.stopAll();
			
			soundManager.createSound({
				id: theid,
				url: themp3,
				volume: 60,
				autoPlay: true,
				onfinish:function() {
					playnextsound(num);
				}
			 });	
			
		}
	
		return false;
	});
	
	// make the discog links play something
	$("#atmosphere a").click(function(){
									  
		var themp3 = $(this).attr("href");
		var themp3 = themp3.replace("_preview/","_mp3/");
		var theid = themp3.substr(5);
		
			
		if ($(this).hasClass("active")){
			soundManager.stopAll();
			soundManager.destroySound(theid);
			$(this).removeClass("active");
		} else {
			
			$(this).addClass("active");
	
			soundManager.stopAll();
			
		  	soundManager.createSound({
				id: theid,
				url: themp3,
				volume: 70,
				autoPlay: true
			  });
		}
		
		return false;
	});
	
	
});
function loopSound(soundID) {
	/*
	window.setTimeout(function() {
		soundManager.play(soundID,{onfinish:function(){loopSound(soundID);}});
		},1);
	}
	soundManager.createSound('foo','/some/path/to/an.mp3')
	// (or soundManager.createSound( { id: 'foo', url: '/some/path...'} );
	loopSound('foo');
	*/
}


soundManager.onload = function() {
	
	
	/*soundManager.defaultOptions({
		//onfinish: playnextsound					
	});*/
	$("#atmosphere a").click();
	
}




function playnextsound(curSnd){
	var nxtSound = curSnd + 1;
	var totals = $("#thesound ol li").length -1;
	if (nxtSound > totals){
		nxtSound = 0;
	}
	$("#thesound ol li a").removeClass("active");
	soundManager.stopAll();
	
	$("#thesound ol li a").eq(nxtSound).click();
	
}






var page = 0;

function nextImageSet(){
	var totalPages = Math.ceil($("#official .slideShow ul li").size() / 2);
	
	var width = $("#official .slideShow").width();
	var margin = $("#official .slideShow ul").css("marginLeft");
	var margin = margin.replace("px","");
	
	var nextPage = page + 1;
	
	if (nextPage == totalPages){
		nextPage = 0;
	} 
	
	var dest = (width*nextPage)*-1;
	
	$("#official .slideShow ul").animate({ 
        marginLeft: dest
      }, 700 );
	
	page = nextPage;
}
function prevImageSet(){
	var totalPages = Math.ceil($("#official .slideShow ul li").size() / 2);
	
	var width = $("#official .slideShow").width();
	var margin = $("#official .slideShow ul").css("marginLeft");
	var margin = margin.replace("px","");
	
	var prevPage = page - 1;
	
	
	if (prevPage <= 0){
		prevPage = 0;
	} 
	
	var dest = (width*prevPage) * -1;
	
	$("#official .slideShow ul").animate({ 
        marginLeft: dest
      }, 700 );
	
	page = prevPage;
}
function makeVimeo(title, desc, clip){
	var theHTML = "";
	theHTML = '<object width="420" height="343">';
	theHTML += '		<param name="allowfullscreen" value="true" />';
	theHTML += '		<param name="allowscriptaccess" value="always" />';
	theHTML += '		<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+clip+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=017e78&amp;fullscreen=1" />';
	theHTML += '		<embed src="http://vimeo.com/moogaloop.swf?clip_id='+clip+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=017e78&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="420" height="343"></embed>';
	theHTML += '</object>';

	
	$("#thevideo").html(theHTML);
	$("#thevision .desc h3").html(title);
	$("#thevision .desc p").html(desc);
}