/* 
Forma-ab 
*/

$('document').ready(function(){
	
	/* Sākumlapa */
	var fpProjects = $(".fp-project");
	var num;
	var selected;
	if(fpProjects.length > 4){
		//randomā jāizvēlas projekti no fpProjects
		$(fpProjects).each(function(){ 
			$(this).attr("class", "fp-project-hidden"); 
		});
		for(var i=0; i<=3; i++){
			selected = false;
			while(selected == false){
				num = Math.floor(Math.random()*(fpProjects.length));
				if($(fpProjects[num]).attr('class') != "fp-project-visible"){
					selected = true;
					$(fpProjects[num]).attr('class', "fp-project-visible");
				}
			}
		}
	}
	$(fpProjects).each(function(){
		var img = $(this).find('img');
		var imgSrc = img.attr('src');
		var overImgSrc =  imgSrc.split('.jpg')[0] + '_bw.jpg';// + imgSrc.split('.')[1];
		
		$(this).mouseover(function(){
			img.attr('src', overImgSrc);
		});
		$(this).mouseout(function(){
			img.attr('src', imgSrc);
		});
	});
	/***********************************/
	
	// navigācija - rādīt/paslēpt apakškategoriju
	var navi 	  = $("#navigation .main-p"),
		hide_this = $("#navigation ul.p").hide(),
		loc = location.href,
		link = hide_this.find("a:first").attr("href"),
		link2 = hide_this.find("a:last").attr("href");
	
	if(loc == link || loc == link2){
		hide_this.show();
	}
	
	$(navi).toggle(function(){
		hide_this.show();
		if(loc == link){
			return false;
		}else{
			window.location = link;
		}
	}, function(){
		hide_this.hide();
	});
	
	//kārtošana pēc projektu gadiem, rādam tikai pirmo gadu
	//var proj = $(".pr-list-item");
	var d = new Date();
	var year = d.getFullYear();
	
	for(var j=1992; j<=year; j++){
		var projy = $(".pr-list-item .verticaltxt:contains("+j+")");
		$(projy).each(function(i){
			if(i!=0){
				$(this).hide();
			}
		});
	}
	// kārtošana pēc tipa, rādam tikai pirmo tipu
	var proj = $(".pr-list-item");
	var curr_type = '';
	
	$(proj).each(function(){
		//vertikālā teksta paslēpšana. rāda tikai pirmajam no kreisās puses
		var vTxt = $(this).find("div.smaller-verticaltxt"),
			type = $(this).find("span[title=type]").html();
		if(curr_type == type){
			vTxt.hide();
		}else{
			curr_type = type;
		}
		//================================================

		// teksta samazināšana atkarībā no bildes augstuma
		var cont = $(this), 
			img  = cont.find("a").find("img"),
			txtCont = cont.find('p'),
			h3		= cont.find("a").find("H3"),
			txtH = 430 -((img.height() // visa konteinera augstums - bildes augstums
				   + h3.height() + parseInt(h3.css("margin-top")) + parseInt(h3.css("margin-bottom")) // h3 apakšas margins un augšas
				   + 10  // P top un bottom margin pa 5px
				   ) ), // platums no apakšas
			
			txt  = txtCont.html();//.substr(0, txtH + 90) + '...';

			txtCont.html(txt);
		/*		
		console.log(parseInt(h3.css("margin-bottom")));
		//console.log(txt);
		console.log('cont size: ' + cont.height());
		console.log('img size: ' + img.height());
		console.log('h3 size: ' + (h3.height() + parseInt(h3.css("margin-top")) + parseInt(h3.css("margin-bottom"))) );
		console.log('p margins :' + 10);
		console.log('p size: ' + txtH);
		console.log(txtCont);
		console.log(cont);		*/		
		txtCont.css({"height" : txtH, "overflow" : "hidden" });

	});

	



	
});

