var w,h,hh,c,cid,cc,rc,e,cords=[],order="r",current=[],ptop=92,bot=59,b=72,bb=b+1,op=0.08,view,animation=0;

$(".menu .line2").hide();
$(".menu .line1 a").each(function(i){
	$(".menu .line1 a:eq("+i+")").click(function(){
		$(".menu .line1 a").removeClass('hover');
		$(".menu .line1 a:eq("+i+")").addClass('hover');
		$(".menu .line1").animate({'top':'4px','opacity':0.5},200);
		$(".menu .line2").fadeOut('slow').hide();
		$(".menu .line2:eq("+i+")").fadeIn('slow').show();
	});
	$(".menu .line2:eq("+i+") a").each(function(j){
		$(".menu .line2:eq("+i+") a:eq("+j+")").click(function(){
			$(".menu .line2 a").removeClass('hover');
			$(".menu .line2:eq("+i+") a:eq("+j+")").addClass('hover');
			order=$(".menu .line2:eq("+i+") a:eq("+j+")").attr('id').split("-")[1];
			refillmatrix(order);
		});
	});
});

function addit(ar){for(iii in ar) ar[iii]['i']=iii;return ar;}
var imagesarray=addit(getimgarray());

function sortarray(arr){
	function randorder(){return(Math.round(Math.random())-0.5);}
	return arr.sort(randorder);
}

function getimg(choice){
	if(c==current.length){current=sortarray(imagesarray);c=0;}
	var it=current[c]['i'];
	switch(choice){
		case('thumb_gray'):call='images/'+current[it]['year']+'/'+current[it]['folder']+'/thumbnail_gray_'+imagesarray[it]['file'];break;
		case('thumb'):call='images/'+current[it]['year']+'/'+current[it]['folder']+'/thumbnail_'+current[it]['file'];break;
		default:call=current[it][choice];break;
	}
	return call;
}

function topleft(){
	var ee=0,tmpe=[0,0,0,0,0];
	for(y=0;y<rc;y++){
		for(x=0;x<cc;x++){
			if(x>=0&&x<=((cc/2)-5)&&y>=((rc/2)-3)&&y<(rc/2)){view[0]['e'][tmpe[0]]=ee;tmpe[0]++;}
			if(x>=((cc/2)-4)&&x<=((cc/2)+5)&&y>=0&&y<(rc/2)){view[1]['e'][tmpe[1]]=ee;tmpe[1]++;}
			if(x>=((cc/2)-4)&&x<=((cc/2)+5)&&y>=(rc/2)&&y<rc){view[2]['e'][tmpe[2]]=ee;tmpe[2]++;}
			if(x>=0&&x<=((cc/2)-5)&&y==(rc/2)+2){view[3]['e'][tmpe[3]]=ee;tmpe[3]++;}
			if(x>=((cc/2)+6)&&x<cc&&y==(rc/2)-3){view[4]['e'][tmpe[4]]=ee;tmpe[4]++;}
			var v=Math.floor(w/bb);
	 		var l=(w/2)-((cc/2)*bb)+(x*bb);
			var v=Math.floor(h/bb);
			var t=(hh/2)-((rc/2)*bb)+(y*bb);
			cords[ee]={'x':x,'y':y,'t':t,'l':l};
			ee++;
		}
	}
}

function matrix(){
	for(var x=0;x<e;x++) $(".matrix").append('<div class="brick" style="top:'+cords[x]['t']+'px;left:'+cords[x]['l']+'px;"></div>');
	$('<div class="tip"><p>&nbsp;</p></div>').appendTo(".matrix").css({'opacity':0});
	c=0;
	$(".brick").each(function(xyz){
		it=getimg('i');
		var lid=xyz+'-'+it+'-'+getimg('id');
		cords[xyz]['id']=lid;
		$(this).html('<img src="'+getimg('thumb_gray')+'" alt="'+getimg('name')+'"/>').css({'opacity':op}).attr('id',lid).attr('class',((getimg('new')==1)?'new'+getimg('year').substring(4,getimg('year').length)+' ':'')+'brick year'+getimg('year'));
		c++;
	});
}

function refillmatrix(o){
	order=o;
	$(".brick").animate({opacity:op},100).removeClass('bricklight');
	var arr={'n':'.newi','nw':'.newweb','2009w':'.year2009web','2010':'.year2010i','2009':'.year2009i','2008':'.year2008i','2007':'.year2007i'}
	$(arr[o]).addClass('bricklight').animate({opacity:0.3},200);
}

function getc(obj){var o=obj.position();return [o.top,o.left];}

function createview(){
	var tmp=[];
	var bmx=cords[0]['l']+(cc*bb)/2;
	var bmy=cords[0]['t']+(rc*bb)/2;
	var sc=[
			{'l':bmx-(7*bb),'t':(bmy-(3*bb)),'w':bb*3+1,'h':bb*3+1,'n':'info'},
			{'l':bmx-(4*bb),'t':bmy-(3*bb),'w':bb*10+1,'h':bb*6+1,'n':'main'},
			{'l':bmx+(6*bb),'t':bmy-(3*bb),'w':bb+1,'h':bb+1,'n':'close'},
			{'l':bmx-(5*bb),'t':bmy+(2*bb),'w':bb+1,'h':bb+1,'n':'zoom'}
	];
	for(var x=0;x<sc.length;x++){
		$(".matrix").append('<div class="view v'+sc[x]['n']+'"><span>&nbsp;</span></div>');
		tmp[x]='.v'+sc[x]['n'];
		$(tmp[x]).css({'opacity':0,'left':sc[x]['l']+'px','top':sc[x]['t']+'px','width':sc[x]['w']+'px','height':sc[x]['h']+'px'});
	}
	$(".vclose span").html('<a href="javascript:;" class="ctc"></a>');
	$(".vzoom span").html('<a href="javascript:;" class="ctz"></a>');
	return tmp.join(',');
}

function sitemaker(){
	$('#jquery-lightbox').remove();
	$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	$('embed, object, select').css({ 'visibility' : 'visible' });
	$(".vclose span a").unbind('click');
	view=[{'e':[]},{'e':[]},{'e':[]},{'e':[]},{'e':[]}];
	animation=0;
	w=mwh()[0];
	h=mwh()[1];
	hh=h-ptop-bot;
	c=0;
	cid=0;
	cc=Math.ceil((w/2)/bb)*2;
	rc=Math.ceil((hh/2)/bb)*2;
	e=cc*rc;
	cords=[{'t':0,'l':0,'x':0,'y':0}];

	$(".matrix").empty().css({"width":w+"px","height":hh+"px","top":ptop+"px"});

	$(".footer").css({"top":(h-bot)});
	$(".footer").hover(function(){$(this).stop().animate({"top":(h-230)},{duration:400,easing:'easeOutBack'});},function(){$(this).stop().animate({"top":(h-bot)},{duration:500});});

	topleft();
	matrix();

var tmpimg,tmpimg2,tmpop;
$(".brick").hover(function(){
	tmpthis=$(this);
	tmpimg=tmpthis.children("img").attr('src');
	tmpthis.stop().animate({'opacity':1},400).children("img").attr('src',tmpimg.replace(/_gray/,""));
	var tc=getc(tmpthis);
	tc[0]=(tc[0]<b)?tc[0]+(2*bb):tc[0];
	tc[1]=(tc[1]>(b*(cc-4)))?tc[1]-(3*bb):tc[1];
	var title=$(this).children("img").attr('alt');
	if(!$(".vmain").hasClass('opened')) $('.tip').css({'top':(tc[0]-b)+"px",'left':(tc[1]+bb+1)+"px"}).stop().animate({'opacity':1},500).children('p').html(title).css({'margin-top':(36-($('.tip p').height()/2))});
},function(){
	tmpimg2=tmpimg;
	tmpop=(tmpthis.hasClass('bricklight'))?0.3:op;
	tmpthis.stop().animate({'opacity':tmpop},600).children("img").attr('src',tmpimg2);
	if(!$(".vmain").hasClass('opened')) $('.tip').stop().animate({'opacity':0},200);
});

var theview=createview();
var info="";
var m=[['left','-=',(3*bb)],['top','-=',(3*bb)],['top','+=',(3*bb)],['left','-=',bb],['left','+=',bb]];

$(".brick").click(function(){
  $(".matrix").append('<div class="overthetop">&nbsp;</div>');
  var faketime=1;
  var info=getimgarray()[$(this).attr('id').replace('#','').split("-")[1]];
  if(!$(".vmain").hasClass('opened')){
	faketime=2000;
	$(this).animate({'opacity':op},100).children("img").attr('src',tmpimg);
	$('.tip').animate({'opacity':0},200);
	$('.view').animate({'opacity':'1'},1000,function(){animation=1;});
	var timer=setInterval(function(){
		if(!$(".brick").is(":animated")&&animation==1){
			clearInterval(timer);
			for(var si in m){
				var viewelem=[];
				for(var cxx=0;cxx<view[si]['e'].length;cxx++) viewelem[cxx]='#'+cords[view[si]['e'][cxx]]['id'];
				if(m[si][0]=="left") $(viewelem.join(",")).animate({'left':m[si][1]+m[si][2]+"px"},500);else $(viewelem.join(",")).animate({'top':m[si][1]+m[si][2]+"px"},500);
			}
		}
	},200);
  }
	var fake=0;
	$("#fake").animate({opacity:1},faketime,function(){fake=1;});
	var timer2=setInterval(function(){
		if(!$("#fake").is(":animated")&&fake==1){
			clearInterval(timer2);
			$(".vinfo span").html("<h2>"+info['name']+"</h2><p>"+info['year'].substring(0,4)+"</p>");
			$.ajax({url:'images/'+info['year']+'/'+info['folder']+'/description.txt',dataType:"text",async:true,success:function(d){$(".vinfo span").append(d);}});
			$(".vinfo span").fadeIn('fast');
			var zooms="";
			if(info['zoom']>1) for(var xx=2;xx<=info['zoom'];xx++) zooms+="<a href=\"images/"+info['year']+"/"+info['folder']+"/zoom"+xx+"_"+info['file']+"\" title=\""+info['name']+"\">&nbsp;</a>";
			zooms=(zooms=="")?"":"<div>"+zooms+"</div>";
			$(".vmain span").html('<a href="images/'+info['year']+'/'+info['folder']+'/zoom1_'+info['file']+'" title="'+info['name']+'"><img src="images/'+info['year']+'/'+info['folder']+'/standard_'+info['file']+'" alt=""/></a>'+zooms);
			$(".vmain span a").lightBox();
			$(".vclose span a").html('<img src="gfx/close.png" alt=""/>');
			$(".vzoom span a").html('<img src="gfx/zoom_icon.png" alt=""/>');
			$(".vclose span img,.vzoom span img,.vmain span img").fadeIn('fast');
			$(".view").css({"z-index":5000});
			$(".overthetop").remove();
			$(".vmain").addClass('opened');
		}
	},200);
});


$(".vclose span a").click(function(){
	$(".matrix").append('<div class="overthetop">&nbsp;</div>');
    $("#tooltip").remove();
	$(".view").animate({opacity:0},500,function(){$(".view").css({'z-index':1});$(".vclose span a,.vzoom span a,.vmain span,.vinfo span").empty();});
	for(var si in m){
		var viewelem2=[];
		for(var cxy=0;cxy<view[si]['e'].length;cxy++) viewelem2[cxy]='#'+cords[view[si]['e'][cxy]]['id'];
		if(m[si][0]=="left") $(viewelem2.join(",")).animate({'left':((m[si][1]=="-=")?"+=":"-=")+m[si][2]+"px"},500,function(){$(".overthetop").remove();$(".vmain").removeClass('opened');});else $(viewelem2.join(",")).animate({'top':((m[si][1]=="-=")?"+=":"-=")+m[si][2]+"px"},500,function(){$(".overthetop").remove();$(".vmain").removeClass('opened');});
	}
});

$(".vzoom span a").click(function(){
	$(".vmain span a:eq(0)").trigger('click');
});

$(".vclose span a").hover(function(){
	$(this).children("img").attr('src','gfx/close_button.gif');
},function(){
	$(this).children("img").attr('src','gfx/close.png');
});

$(".vmain span").hover(function(){$("body").append("<p id=\"tooltip\"><img src=\"gfx/ctz.gif\" alt=\"\"></p>");});
$(".vclose span").hover(function(){$("body").append("<p id=\"tooltip\"><img src=\"gfx/ctc.gif\" alt=\"\"></p>");});
$(".vmain span,.vclose span").hover(function(e){
	$("#tooltip").css("top",(e.pageY-10)+"px").css("left",(e.pageX+20)+"px").fadeIn("fast");
},function(){$("#tooltip").remove();});	
$(".vmain span,.vclose span").mousemove(function(e){$("#tooltip").css("top",(e.pageY-10)+"px").css("left",(e.pageX+20)+"px");
});		

}

function showwork(work){
	var idend='div.brick[id*="'+work+'"]:eq(0)';
	$(idend).addClass('fromnews').trigger('click');
}

$(".news,.lin,.bio").scrollTo(0,0);
function email(){var a='aspizak';var b='gmail.com';var c='Email from spizak.com';document.location.href='mail'+'to'+':'+a+'@'+b+'?subject='+c;}

