$(function()
{
	// syntax highlighting
	prettyPrint();
	
	// footer sackboy zavadzia, ked nie je sidebar dostatocne vysoky
	if ($('#container').height() < 1250) $('#sackboy').hide();
	
	// pri kategoriach zobrazime ze rss sa tyka len tejto kategorie
	$('#subscribe').find('.rss').hover(function() { $(this).find('span').show(); },	function() { $(this).find('span').hide(); });
	
	// review ratings
	var review = $('.review-rating');
	if (review.length > 0)
	{
		var text = review.text().replace(',', '.').split('/'),
			rating = $.trim(text[0]),
			max = $.trim(text[1]),
			half = (rating.toString().indexOf('.') > - 1) ? true : false,
			star = '',
			html = '';
		
		for (var i = 0, floored = Math.floor(rating); i < max; i++)
		{
			if (i < floored) star = 'full_star';
			else if (half) { star = 'half_star'; half = false; }
			else star = 'no_star';
			
			html += '<img src="http://brm.sk/wp-content/themes/brm2/pics/'+star+'.png" alt="'+star+'">';
		}
		
		review.html( html ).attr('title', rating+'/'+max);
	}
	
	// skakajuca sipka nad "povedz mi o tom viac" linkom
	triangle_down();
	
	// otvaranie linkov z postov v novych oknach
	$(".post .content p a, .wp-caption a").attr("target", "_blank");
	
	// da pred kazde <pre> v poste obrazok s ujom
	$(".post pre:not(.noheader)").before('<div class="pre-header">&nbsp;</div>')
	$(".post pre").wrap('<div class="pre-wrapper"></div>');
	
	// od-anti-spam-uje mailove adresy
	$("span.mail, div.mail").each(function()
	{
		var m = $(this).text().replace(" zavinac ", "@").replace(" bodka ", ".");
		$(this).html('<a href="mailto:'+m+'">'+m+'</a>');
	});
});

function triangle_down()
{
	$('.more-triangle').animate({ top: -3 }, 250).delay(7500).animate({ top: -7 }, 100, null, function() { triangle_down() });
}


/**
	ACHIEVEMENTS
*/
var achievements = {
	bnd: "Thinking beyond boundaries"
};

var achievements_done = {};

function setAchievement(id)
{
	if (achievements_done[id]) return;
	
	achievements_done[id] = true;
	
	var newelm = $('<div class="achievement"><span class="close">x</span><img src="http://8.brm.sk/a/?t=Achievement%20unlocked!&d='+encodeURIComponent(achievements[id])+'"></div>');
	$(newelm).find(".close").click(function()
	{
		$(this).parent().fadeOut(800, function() { $(this).remove(); });
		return false;
	});
	$("body").append(newelm);
	$(newelm).fadeIn('slow');
}

/**
       _     _            _ _            _
  ___ | |__ | | __ _  ___(_) | ___   _  | |
 / _ \| '_ \| |/ _` |/ __| | |/ / | | | | |
| (_) | |_) | | (_| | (__| |   <| |_| | |_|
 \___/|_.__/|_|\__,_|\___|_|_|\_\\__, | |_|
                                 |___/
*/

var mousepos = { x: 0, y: 0 };
var isdragged = false;
var screensize = 81403;


function clearSelection()
{
	if(document.selection && document.selection.empty)
	{
		document.selection.empty();
	}
	else if(window.getSelection)
	{
		var sel = window.getSelection();
		if(sel && sel.removeAllRanges) sel.removeAllRanges();
		//sel.collapse();
	}
}


function updateClouds(mousedelta)
{
	$(".cloud.drag").each(function()
	{
		var newposx = parseInt(this.style.marginLeft) + mousedelta.x;
		var newposy = parseInt(this.style.top) + mousedelta.y;

		if (newposy < -20) newposy = -20;
		if (newposy > 100) newposy = 100;

		this.style.marginLeft = newposx+"px";
		this.style.top = newposy+"px";

		var r = $("#"+this.id+"r").get(0);
		r.style.top = newposy+50+"px";
		r.style.marginLeft = newposx+8+"px";
		r.style.height = 114-newposy+"px";
	
		var pos = r.style.backgroundPosition;
		if (!pos) pos = "0px 0px";
		pos = pos.split(" ");
		r.style.backgroundPosition = "0px "+(parseInt(pos[1])-mousedelta.y)+"px";

		var rr = $("#"+this.id+"rr").get(0);
		rr.style.marginLeft = (newposx-10+8)+"px";

		if (newposx > screensize) //zaciname od polovice screenu, kedze v style je left: 50%, cize treba prescrollovat 1.5 obrazovky ;)
		{
			setAchievement("bnd");
		}
	});
}


function moverain(obj)
{
	var pos = obj.style.backgroundPosition;
	if (!pos) pos = "0px 0px";
	
	pos = pos.split(" ");
	obj.style.backgroundPosition = "0px "+(parseInt(pos[1])+5)+"px";
	
	refl = $(obj).next().get(0);
	if (!refl.frame) refl.frame = 0;
	refl.frame++;
	
	if (refl.frame % 3 != 1) return;
	pos = refl.style.backgroundPosition;
	if (!pos) pos = "0px 0px";

	pos = pos.split(" ");
	refl.style.backgroundPosition = "0px "+(parseInt(pos[1])+14)+"px";
}


function updateMouse(ev)
{
	var mousedelta = { x: (ev.pageX - mousepos.x), y: (ev.pageY - mousepos.y) };

	mousepos.x = ev.pageX;
	mousepos.y = ev.pageY;

	if (isdragged)
	{
		clearSelection();
		updateClouds(mousedelta);
	}
}


$(function()
{
	//$("body").prepend('<div id="dbg" style="position: fixed; top: 2px; right: 2px; width: 100px; min-height: 16px; background: #fff; box-shadow: 0px 1px 5px #000;"><!-- --></div>');
	
	$('.rain, .rain_refl').css('opacity', 0).css('display', 'block');
	screensize = window.innerWidth;
	
	$(document).mousemove(updateMouse);
	$(document).scroll(updateMouse);
	$(document).mouseup(function()
	{
		if (isdragged)
		{
			$(".drag").removeClass("drag").removeClass("hover");
			$(".rain, .rain_refl").stop(true).animate({ "opacity" : 0 }, 1000, function() { if (this.raintimer) clearInterval(this.raintimer); this.raintimer = false; });
			isdragged = false;
		}
	});
	
	$(".cloud").mouseover(function()
	{
		$(this).addClass("hover");
	})
	.mouseout(function()
	{
		$(this).removeClass("hover");
	})
	.mousedown(function()
	{
		$(this).addClass("drag");
		isdragged = true;
		
		updateClouds({x:0, y:0});
		
		var rain = $("#"+this.id+"r").get(0);
		
		if (rain.raintimer)
		{
		  clearInterval(rain.raintimer);
		  rain.raintimer = false;
		}
		
		rain.raintimer = setInterval(function()
		{
		  moverain(rain);
		}, 1000.0 / 60.0);
		
		$(rain).stop().animate({ "opacity" : 0.7 }, 400);
		$(rain).next().stop().animate({ "opacity" : 0.7 }, 400);
	});
});


/**
	MISC.
*/

$(function()
{
	try //IE hack - aby nevybuchol ked zacne prsat :p
	{
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
});
