// JavaScript Document

var recAct = 1;
var recCount = 1;

function scrollPrev(){
		/*if(recAct > 3){
			recLeft = 3;
		}
		else{
			recLeft = recAct - 1;
		}*/
		recAct = recAct - 3;
		scrollInit();
		amount = ((recAct-1)*330);
		$("#recContent").animate( { left: -amount+"px" } );
		
}
	
function scrollNext(){
		/*recLeft = (recCount - recAct - 2);
		if(recLeft >=3){
			recLeft = 3 ;
		}*/
		recAct = recAct + 3;
		scrollInit();
		amount = ((recAct-1)*330);	
		$("#recContent").animate( { left: -amount+ "px" } );
		
}

function scrollTo(page){
                console.log('page'+page);
		recAct = (page-1)*3 + 1;
		scrollInit();
		amount = ((recAct-1)*330);	
		$("#recContent").animate( { left: -amount+ "px" } );
		
}

function scrollInit(){
	projHide(openProj);
	if(recCount>3 && (recAct+3) <= recCount){
		$('#recNext').show();
	}
	else{
		$('#recNext').hide();	
	}
	if(recAct>1){
		$('#recPrev').show();
	}
	else{
		$('#recPrev').hide();			
	}
	
	pageCount = Math.ceil(recCount/3);	
	pageActive = Math.ceil((recAct+1)/3);
	$("#recnums").html(pageActive+"/"+pageCount);
	
}


var hideTimeout;
var showTimeout;
var openProj;

function projShow(id){
	if(openProj && openProj != id){
		projHide(openProj);
	}
	$("#projLnk"+id).fadeIn();
	$("#projClient"+id).fadeOut();
	openProj = id;
	
}

function projHide(id){
	$("#projLnk"+id).fadeOut();
	$("#projClient"+id).fadeIn();
	openProj = '';
}

var imgAct = 1;
var imgCount = 1;

function imagePrev(){
        if((imgAct) > 1){
		imgAct--;
        }
        else{
              imgAct=imgCount;  
        }
        amount = (imgAct-1)*630;
	$("#photoList").animate( { left: -amount+"px" } );	
        imgInit();
}
	
function imageNext(){
	if((imgAct+1) <= imgCount){
            imgAct++;
	}
        else{
            imgAct=1;
        }
        amount = (imgAct-1)*630;	
	$("#photoList").animate( { left: -amount+ "px" } );
        imgInit();
}


function imgInit(){
	if(imgCount > 1){
            
                if((imgAct+1) <= imgCount){
			$('#imgNext').show();
		}
		else{
			$('#imgNext').hide();	
		}
		if(imgAct>1){
			$('#imgPrev').show();
		}
		else{
			$('#imgPrev').hide();			
		}
            
            
                $("#imgNum").addClass('extramargin').html(imgAct+"/"+imgCount);
		$('#imgNum').show();
		
	}
        //copyright
	if($("#photo"+imgAct).attr("alt")!=""){
		$("#copyright").html('&copy; ' + $("#photo"+imgAct).attr("alt"));
	}
	else{
		$("#copyright").html("");
	}
}


$(document).ready(function(){
    
     $("#references #subnav a").click(function(){
            $("#references #subnav li").removeClass("active"); 
            $(this).parent('li').addClass("active");   
            $(".category.active").not($(this.hash)).slideUp().removeClass("active");
            $(this.hash).not(".category.active").slideDown().addClass("active");
            return false;
    });
    
    
    
    if($('#photoList img').size() > 1){
        imgCount = $('#photoList img').size();
        
        $('#photoList').attr('style','width:'+(imgCount*650)+'px;');
        
        $('.imgNav').removeClass('visuallyhidden');
        $('#imgNext, #photoList img').click(function(){
            imageNext();
            return false;
        })
        $('#imgPrev').click(function(){
            imagePrev();
            return false;
        })
    }
    if($('#photoList').size() >0){
        imgInit();
    }

    $('.projnav').hover(function(){
        console.log($(this).attr('id')+'txt');
        $('#'+$(this).attr('id')+'txt').toggleClass('visuallyhidden');
    })
    
    if($('#recContent .column').size() > 3){
         $('.controls').removeClass('visuallyhidden');
        recCount=$('#recContent .column').size();
        
        $('#recNext').click(function(){
            scrollNext();
            return false;
        })
        $('#recPrev').click(function(){
            scrollPrev();
            return false;
        })
        
        scrollInit();
    } 
    
    //scrollable areas home & info
    $('.scrollarea').jScrollPane({verticalGutter: 10});
    
    
    if(window.location.hash){
        var hash = window.location.hash.substring(1);
         console.log(hash);
        //is er een project?
        if($('#projClient'+hash).size()>0){
            console.log('found architect');
            itemsperpage = 63;
            gotopage = Math.floor($('#projClient'+hash).data('count')/itemsperpage);
            console.log(gotopage);
            if(gotopage>0)scrollTo(gotopage+1);
            projShow(hash);
        }
        //of een categorie
        if($('#cat'+hash).size()>0){
            console.log(hash);
            $('#cat'+hash+ ' a').click();
        }
    }
})

