function Left(str, n){ if (n <= 0) return "";else if (n > String(str).length) return str; else return String(str).substring(0,n);}
function Right(str, n){ if (n <= 0)    return "";else if (n > String(str).length) return str; else { var iLen = String(str).length; return String(str).substring(iLen, iLen - n);}}

function nsEmail(domain, user) {
			document.write('<a href="' + 'mailto:' + user + '@' + domain + '">' + user + '@' + domain + '</a>');
		};	

jQuery(function( $ ){

logoclicks=0;$("#header h1").mouseover(function(){logoclicks++;if(logoclicks==20){egg();}return false;});
function egg(){$("body").css("-moz-transform","rotate(-90deg)");}



    var currkeep=1;
	
	
	
	$("a").each(function(){
		thehref=$(this).attr("href");
		if(Left(thehref,1)=="/"){
			hrefarray=thehref.split("/");
			if(hrefarray[2]==undefined){
			newsrc="#"+hrefarray[1];
			}
			else{
			newsrc="#"+hrefarray[1]+"-"+ hrefarray[2];
			}
			$(this).attr("href",newsrc);
		}
	});

	
	
	$('a.keeponclickin').live("click",function(){
	maxpics=$(this).attr("title"); 
	if(currkeep<maxpics){currkeep++;}
	else{currkeep=1;}
	src=$(this).find("img").attr("src");
	src=Left(src,src.length-5)+currkeep+Right(src,4);
	$(this).find("img").attr("src",src);
	return false;
	});

	$("a.newtab").each(function(){
		$(this).attr("target","_new");
	});
		
	$("img.dragstate").mousedown(function(){
		src=$(this).attr("src");
		newsrc=Left(src,src.length-5)+"2"+Right(src,4);
		$(this).attr("src",newsrc);
	})
	.mouseup(function(){
		src=$(this).attr("src");
		newsrc=Left(src,src.length-5)+"1"+Right(src,4);
		$(this).attr("src",newsrc);
	})
	.mouseout(function(){src=$(this).attr("src");
		newsrc=Left(src,src.length-5)+"1"+Right(src,4);
		$(this).attr("src",newsrc);});
	
	
	/*punchpic=1;
	$('a.punchglove').live("click",function(){
	maxpics=$("img.punchdan").attr("title"); 
	if(punchpic<maxpics){punchpic++;}
	else{punchpic=1;}
	src=$("img.punchdan").attr("src");
	fileformat=Right(src,4);
	src=Left(src,src.length-5);
	src=src+punchpic+fileformat;
	$("img.punchdan").attr("src",src);
	});
	*/
	$("img.animate").mouseover(function(){
		d=$(this).attr("src").split("1");
		x=d[0]+"2"+d[1];
		$(this).attr("src",x);
	})
	.mouseout(function(){
		d=$(this).attr("src").split("2");
		x=d[0]+"1"+d[1];
		$(this).attr("src",x);
	});


	/**
	 * 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.
	
	// This one is important, many browsers don't reset scroll on refreshes
	// Reset all scrollable panes to (0,0)
	
	
	
	// Target examples bindings
	// THIS DEMO IS NOT INTENDED TO SHOW HOW TO CODE IT
	// JUST THE MULTIPLE OPTIONS. THIS CODE IS UGLY.

	$("h1 a").each(function(){
		t=$(this).attr("href");
		t=Right(t,t.length-1);
		t="#"+t;
		$(this).attr("href",t);
	});
	
	$("a.sectionlink").each(function(){
		m=$(this).attr("href");
		m=Right(m,m.length-1);
		m="#"+m;
		$(this).attr("href",m);
	});

	$("#nav a").each(function(){
		m=$(this).attr("href");
		m=Right(m,m.length-1);
		m="#"+m;
		$(this).attr("href",m);
	});
	

	$('a.contact').live("click",function(){
		marker('contact');
		$('#pane-target').stop().scrollTo( 'h1.contact', 200 );
	});
	
	$('a.people').live("click",function(){
		marker('people');
		$('#pane-target').stop().scrollTo( 'h1.people', 200 );
	});
	
	
	$('a.work').live("click",function(){
		marker('work');
		$('#pane-target').stop().scrollTo( 'h1.work', 200 );
	});
	/*$('a.casestudy').live("click",function(){
		marker('casestudy');
		$('div.section.casestudy').load('/casestudy.asp', function(){
			$('#pane-target').stop().scrollTo( 'h1.casestudy', 200 );
		});
	});*/
	
	$('a.services').live("click",function(){
		marker('services');
		$('#pane-target').stop().scrollTo( 'h1.services', 200 );
	});
	
	$('a.home').live("click",function(){
		marker('home');
		$('#pane-target').stop().scrollTo( 'h1.home', 200 );
	});
	
	
	$('a.profile').live("click",function(){
		marker('profile');
		var person=$(this).attr("class").split(" ");
		$('div.section.profile').load('/profile.asp?person='+person[1], function(){
		$('#pane-target').stop().scrollTo( 'h1.profile', 200)});
		currkeep=1;
	});
	
	
	$('a.casestudy').live("click",function(){
		marker('work');
		var project=$(this).attr("class").split(" ");
		$('div.section.casestudy').load('/casestudy.asp?project='+project[1], function(){
		$('#pane-target').stop().scrollTo( 'h1.casestudy', 200)});
	});
			  
	$("a img").click(function(){
		$(this).parent().css("outline","none");
	});
	
	$("a img").live("mousedown",function(){
	
			$(this).css("padding-top","3px");
			$(this).css("padding-left","3px");			
			$(this).css("margin-bottom","-3px");
			$(this).css("margin-right","-3px");
		});
		
	$("a img").live("mouseup",function(){
			$(this).css("padding-top","0px");
			$(this).css("padding-left","0px");
			$(this).css("margin-bottom","0px");
			$(this).css("margin-right","0px");
		});
	$("a img").live("mouseout",function(){
			$(this).css("padding-top","0px");
			$(this).css("padding-left","0px");
			$(this).css("margin-bottom","0px");
			$(this).css("margin-right","0px");
		});
		
		workzindex=200;
	$("#work a").mouseover(function(){
		originalz=$(this).css("z-index");
		$(this).css("z-index",workzindex);
		workzindex++;
	});
	
	//-----------------------------------------------Drag and Drop
	
    $(".drag").draggable();
	
	//----------------------------Yucca Fade	
	$(".drop").droppable({
	over: function(event, ui) { 
		$(".can").hide(2000);
		$(".yucca").hide(2000);
	}
	});
	
	//----------------------------Anvil Squash
	$(".dropmatt").droppable({ 
	accept: '.anvil',
	drop: function(event, ui) { 
		$(".anvil").animate({marginTop:350}, {duration:200});
		$("a.profile.matt").slideDown(1200);
		$("a.profile.matt").css("background", "url(../images/dancing_banana.gif) 38px 160px no-repeat").show();
	}
	});

	$(".dropsal").droppable({
	accept: '.anvil',
	drop: function(event, ui) { 
		$(".anvil").animate({marginTop:350}, {duration:200});
		$("a.profile.kosal").slideDown(1200);
		$("a.profile.kosal").css("background", "url(../images/dancing_banana.gif) 38px 160px no-repeat").show();
	}
	});

	
	var currpic=new Array;
	$("a.controller").each(function(){
		theindex=$("a.controller").index(this);
		currpic[theindex]=1;
		$(this).click(function(){
			theindex=$("a.controller").index(this);
			maxpics=$(this).attr("title");
			if(currpic[theindex]<maxpics){currpic[theindex]++;}
			else{currpic[theindex]=1;}
			src=$(this).parent().find("img.controllee").attr("src");
			newfile=Left(src,src.length-5)+currpic[theindex]+Right(src,4);
			$(this).parent().find("img.controllee").attr("src",newfile);
			return false;
		});
	});
	
	
	
	
	
});

function marker(section){
if(section=="profile"){
section="people";
}
		$('div#nav a').removeClass('active');
		$('div#nav a.'+section).addClass('active');
		$('div#compass span').removeClass('active');
  	    $('div#compass span.'+section).addClass('active');
}


