$(document).ready(function()
{
	initLoginBox();
	initGallery();
	initPopup(".popup", ".popup-opener");
	initSlider();
	
	$(".columns > ul").makeacolumnlists({cols: 2, colWidth: 0, equalHeight: 'ul', startN: 1});
	initNavigation();
	
	if($(".sub-nav").length > 0)
	{
		jQuery(".sub-nav").accordion({
			alwaysOpen: true,
			header: 'a'
		});
	}
	if($(".table-2").length > 0)
	{
		$(".table-2").tablesorter({sortList:[[2,0]]});
	}
});
function initNavigation()
{
	$('#nav li').hover(function()
	{
		$(this).addClass('hover');
		if ($.browser.msie && $.browser.version < 7)
		{
			hideSelectBoxes(this.getElementsByTagName("ul")[0]);
		}
	},
	function()
	{
		$(this).removeClass('hover');
		setTimeout(function(){Cufon.refresh('#nav > li > a')}, 200);
		if ($.browser.msie && $.browser.version < 7)
		{
			showSelectBoxes(this.getElementsByTagName("ul")[0]);
		}
	});
}
function initLoginBox()
{
	$(".login-btn").click(function()
	{
		$("#header .login-form").toggle();
		return false;
	});
}
function initGallery()
{
	var buttons = $(".gallery-nav li a");
	var content = $(".gallery-items-holder li");
	$(content).css({"display": "none", "opacity": 0});
	$(buttons).each(function(i, el)
	{
		if($(this).hasClass("active"))
		{
			$(content).eq(i).css({"display": "block", "opacity": 1});
		}
		this.onclick = function()
		{
			$(content).css({"display": "none", "opacity": 0}).eq(i).animate({"opacity": 1}).show();
			$(buttons).removeClass("active")
			$(el).addClass("active")
			return false;
		}
	});
}
function initPopup(_box, _opener)
{
	$(_box).hide();
	$(_opener).click(function()
	{
		var popup = $("#" + this.rel);
		
		if(!$(this).hasClass("opened"))
		{
			$(_box).hide();
			$(_opener).removeClass("opened");
			$(popup).css({"display": "block", "top": findPosY(this) + 10, "left": findPosX(this) + 145 + $(this).width()});
			$(this).addClass("opened");
		}
		else
		{
			$(popup).hide();
			$(this).removeClass("opened");
		}
		return false;
	});
}
function findPosY(obj)
{
	var posTop = 0;
	while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
	return posTop;
}
function findPosX(obj)
{
	var posLeft = 0;
	while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
	return posLeft;
}
function initSlider(){
	var _holder = $('.slider')
	var _opener = _holder.find('a.opener');
	
	_opener.each(function(){
		$(this).toggle(function(){
			$(this).parents('.slider').find('.slide').slideUp();
			$(this).addClass('close');
			$(this).removeClass('open');
		},function(){
			$(this).parents('.slider').find('.slide').slideDown();
			$(this).addClass('open');
			$(this).removeClass('close');
		});
	});
};
