$(document).ready(function(){
	function rand(min,max){
		var argc=arguments.length;
		if(argc === 0){
			min = 0;max = 2147483647;
		}else if (argc === 1){
			throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
		};
	return Math.floor(Math.random() * (max - min + 1)) + min;};
	
	
	
	curImg = 0;
	imgC = 0;
	slides = new Array(0);
	$(".galleryWrap").append("<img src='img/loaders/"+rand(1,3)+".gif' class='loader'>");
	loadProj = function(proj){
		$(".bread").empty();
		curImg = 0;
		imgC = 0;
		slides = new Array(0);
		$(".arrLeft").attr("src", "img/bottom_left_light.gif");
		$(".arrRight").attr("src", "img/bottom_right.gif");
		$(".loader").attr("src",'img/loaders/'+rand(1,3)+'.gif').show();
		$(".scrollWrap").css({left:0});
		$(".scrollWrap").empty();
		$.get("proj.php", {proj:proj},function(e){
			name = e[0];
			colour = e[1];
			url = e[2];
			totalImageSize = e[3];
			coltype = e[4];
			lastImageWidth = e[5];
			len = e.length;
			imgCount = len - 6;
			imagePadding = imgCount * 20;
			imageNames = [];
			$(".colorTable img").attr("src", "img/"+coltype+".gif");
			$(".scrollPlane .url").css({color:colour}).empty().append("<a style='text-decoration:none; color:"+colour+"' href='http://"+url+"' target='_blank'>"+url+"</a>");
			spanWidth = $(".drag span").width();$(".drag").width( spanWidth + 78 );
			$(".scrollWrap").width( totalImageSize + imagePadding );
			
			loadImgs = function(d){
				if(d == imgCount){
					$.each(imageNames,function(i,el){
						$(".scrollWrap").append("<img src='projects/"+proj+"/"+el+"' style='display:none;margin-right:20px;'>");
						imgC++;
					});
					$(".loader").hide();
					$(".scrollWrap img").each(function(i){
						$(this).fadeIn();
						$(".bread").append("<a href='#' id='"+i+"' class='crumb'>"+(parseInt(i)+1)+"</a>");
						slides[i] = 20 + parseInt($(this).width());
						$(".bread a").eq(curImg).css({color:'black'})
					;});
					};
				};
				
				counterB=0;
				counter=1;
				
				$.each(e, function(i, el){if( i == '0'){ }else if( i == '1'){ }else if( i == '2'){ }else if( i == '3'){ }else if( i == '4'){ }else if( i == '5'){ }else{$(".scrollWrap").empty();imageNames[counterB] = el;$("<img />").attr("src", "projects/"+proj+"/"+el).load(function(i){loadImgs(counter);counter++;});counterB++;};});},'json');};
				
				
				init = function(){
					proj = 'jnhfinancial';
					$("#scrollWrap li").each(function(){
						$(this).attr("class","");
					});
					$("#scrollWrap li:eq(3)").attr("class","current").css("background","url(projects/"+proj+"/button.gif) no-repeat");$("#scrollWrap").attr({"class":proj}).css({left:"0px"});loadProj(proj);$(".arrRight").bind("click",function(){ scrollToNextImage(); });$(".arrLeft").bind("click",function(){ scrollToPrevImage();});};doArr = function(){if(curImg == (imgC - 1)){$(".arrRight").attr("src", "img/bottom_right_light.gif");}else{$(".arrRight").attr("src", "img/bottom_right.gif");};if(curImg == 0){$(".arrLeft").attr("src", "img/bottom_left_light.gif");}else{$(".arrLeft").attr("src", "img/bottom_left.gif");};};scrollToNextImage = function(){if(curImg == (imgC - 1)){curImg = (imgC - 1);}else{curImg++;};doArr();$(".bread a").each(function(){$(this).css({color:'#999'})});$(".bread a").eq(curImg).css({color:'black'});i = curImg;left = 0;if( i != 0 ){while( i !=  0){left = parseInt(slides[i - 1]) + parseInt(left);i--;if(i== - 100){break;};};};$(".scrollWrap").stop().animate({left : "-" + left});};scrollToPrevImage = function(){if(curImg == 0){curImg = 0; }else{curImg--;};doArr();$(".bread a").each(function(){$(this).css({color:'#999'});});$(".bread a").eq(curImg).css({color:'black'});i = curImg;left = 0;if( i != 0 ){while( i !=  0){left = parseInt(slides[i - 1]) + parseInt(left);i--;if(i== - 100){break;};};};$(".scrollWrap").stop().animate({left : "-" + left});};scrollToImage = function(x){if(x == 0){curImg = 0;}else{curImg = x;};$(".bread a").each(function(){$(this).css({color:'#999'});});$(".bread a").eq(x).css({color:'black'});i = curImg;left = 0;if( i != 0 ){while( i !=  0){left = parseInt(slides[i - 1]) + parseInt(left);i--;if(i== - 100){break;};};};$(".scrollWrap").stop().animate({left : "-" + left});};init();$("#scrollWrap li a").click(function(e){proj = $(this).attr("class");$("#scrollWrap li").each(function(){ $(this).attr("class",""); $(this).css("background","none");});$(this).parent("li").attr("class","current").css("background","url(projects/"+proj+"/button.gif) no-repeat");$("#scrollWrap").attr({"class":proj}).css({left:"0px"});loadProj(proj);return false;});$(".bread a").live("click",function(){scrollToImage($(this).attr("id"));doArr($(this).attr("id"));return false;});});