var currentTag = -1;
var akt;
var m = $(".ref").length+3;
var kt = "#all";
$(document).ready(function(){

	currentTag = $("#socialmedia");

	$("#tags a").click(function(){
		var s = $(this).attr("href").split(":")[1];
		currentTag.hide();
		$("#tags a.current").removeClass("current");
		$(this).addClass("current");
		currentTag = $("#"+s);
		currentTag.show();
		
	})


	$(".close").click(closePopup);

	$.fn.fancyzoom.defaultsOptions.imgDir='images/';
	$('a.zoom').fancyzoom(); 

	$(document).keydown(function(e){
		if($(".popup").length){
			if(e.keyCode == 39){ //right
var j = $(akt).prevAll(".ref").length+1+$(akt).nextUntil(".showing").length;
				if(j<m){
					popupize($("#ref"+j), '1');
				}
			} else
			if(e.keyCode == 37){ //left
var j = $(akt).prevAll(".ref").length-1-$(akt).prevUntil(".showing").length;
				if(j>-1){
					popupize($("#ref"+j), '1');
				}
			} else
			if(e.keyCode == 27){ //esc
closePopup();			}
		}
	})

	$(".ref").click(function(){
		popupize($(this), '0');
	})

$(".reftags>a").click(function(){
	closePopup();
	kt = $(this);
	$(".reftags>a.actual").removeClass("actual");
	$(this).addClass("actual");	
	
	var s = $(this).attr("href").replace("#","");

	if(s == "all"){
		movize($(".ref"));
	} else {
		filterize($(".ref"),s);
	}

	return false;
})
/* TODO: reftags, 
	$("a").click(function(){
		if(!$(this).hasClass("zoom") && !$(this).hasClass("inner") ){
		window.open($(this).attr("href"));
		return false;
		}
	});
*/


})

$(window).load(function(){
	var r = $(".ref").removeClass("m2");
	absolutize(r);fadeize(r);
	$("#refs").animate({height:(Math.ceil(r.length/5)*165)},300);
})

function thankyou(){
	alert("Köszönjük leveled!");
}

function shrinkize($items){
	$items.each(function(){
		//console.log( $(this).text().length ); 
		if($(this).text().length > 20){
			$(this).css("font-size","9px!important")
		}
	})
}

function absolutize($items){
	var w = 165, r = 5;
	$items.each(function(i){
		var column = i % r,
			row = Math.floor( i / r);
		$(this)
			.show(0)
			.css("position","absolute")
			.css("top",row*w+"px")
			.css("left",column*w+"px");
	})
}

function fadeize($items){
	var delay = 50, timer = 400;
	$items.each(function(i){
		var $t = $(this).addClass("showing");
		setTimeout(function(){
			$t.animate({opacity:1},timer)
		},i*delay)
	});
}

function movize($items){
	var w = 165, r = 5;
	var delay = 50, timer = 400;
	$items.each(function(i){
		var column = i % r,
			row = Math.floor( i / r);
		var $t = $(this).show(timer/2).addClass("showing");	
		setTimeout(function(){
			$t.animate({
				"top":row*w+"px",
				"left":column*w+"px",
				"opacity":1
				}, timer);		
		},i*delay)
	})
	$("#refs").animate({height:(Math.ceil($items.length/5)*165)},300);
	
}

function valaszt($items){
$items.addClass("showing");
}

function filterize($items,clss){
	var f = [], nf = [];
	$items.each(function(){
		if($(this).hasClass(clss)){
			f.push(this);	
		} else {
			nf.push(this);
		}
	});
	//$(nf).hide();
	hideize($(nf));
	movize($(f));
}

function hideize($items){
	var delay = 50, timer = 200;
	$items.each(function(i){
		var $t = $(this).removeClass("showing");	
		setTimeout(function(){
			$t.animate({
				"top":"-=165",
				"opacity":0
				}, timer/2, function(){$t.hide(0);});		
		},i*delay)

	})
}

//TODO: alsó sor, filter

function popupize($item, $o){
	var p = $(".popup");
	if(p.length || $o=='0'){
		closePopup();
$(".reftags>a.actual").removeClass("actual");
$(kt).addClass("actual");
	}
	if(!p.length || $o=='1'){
akt = $item;
	$item.addClass("popup");
	var l = $item.prevAll(".ref.showing").length;
	$item.attr("rel",l);
	var $cb = $item;
	var i = l % 5;
	var r = Math.floor(l / 5);
		r = Math.max(r - 1, 0)*165;
	$(".reftags>a.actual").removeClass("actual");
	var elem = $item.attr("class").substr(9).split(" ");
	var u=0;
	if(elem.length==2){$("#all").addClass("actual");}
	for(u=0; u<elem.length; u++){
		if(elem[u]=="showing" || elem[u]=="popup" || elem[u]==""){}
		else{$("#"+elem[u]).addClass("actual");}
	}
		$item.find(".desc").show(0);
		$item
			.animate({
				height: 300,
				width:630,
				top:r+82,
				left:82			
			},400,function(){
				var hh = $cb.offset().top + 630;
				var bb = $(window).scrollTop() + $(window).height();
			
				var margin = 40;
			
				var top = $cb.offset().top;
				var wtop = $(window).scrollTop();
				
				if(top < wtop){
					$("html,body").animate({scrollTop: top - margin},500);
				}
			
				var bottom = top + 300;
				var wbottom = wtop + $(window).height();
				
				if(bottom > wbottom){
					$("html,body").animate({scrollTop: bottom - $(window).height()+ margin} ,500);
					
				}
			/*
				if(hh > bb){
					$("html,body").animate({scrollTop: $cb.offset().top - 80},500);
				}
			*/
				//console.log($item.offset());
			})
		$(".border")
			.css("height",$item.css("height"))
			.css("width",$item.css("width"))
			.css("top",$item.css("top"))
			.css("left",$item.css("left"))
			.css("display","block")
			.animate({
				height:320,
				width:650,
				top: r+72,
				left:72
				},400);
		$(".close").animate({
			top: r+72,
			left:72+630,
			opacity:1
		},100)
}
}

function closePopup(){
	var $item = $(".popup"), l = parseInt($item.attr("rel"));
	var i = l % 5;
	var r = Math.floor(l / 5);	
	
	$item.find(".desc").hide(0);
			
	$item
	.animate({
		height:135,
		width:135,
		top: r*165,
		left: i*165
		},150,function(){$item.removeClass("popup")});	
			
	$(".border")
	.hide(0);
	$(".close").animate({
		opacity:0
	},100)
	/*
	.animate({
		height:135,
		width:135,
		top: r*165,
		left: i*165
		},150,function(){$(".border").hide(0)});
	*/
	
}



