$(document).ready(function(){
	// add hover
	$("a").focusin(function(){ $(this).blur(); });
	$("li:last-child").addClass("last");
	$("li:first-child").addClass("first");
	$("li").stdhover().stdactive();
	
	$("li").each(function(){
        if ($(this).find("li").length > 0) $(this).addClass("haschild")
    });
	
	//top menu

	
	//gallery-top
	$(".gallery-top .ngg-gallery-thumbnail-box a").hover(function(){
		if (! $(this).hasClass("active"))
			$(this).animate({borderColor: '#daf373'});
	}, function(){
		if (! $(this).hasClass("active"))
			$(this).animate({borderColor: '#fff'});
	});
	
	// XHTML1.1 restricts target="_blank". Here is the fix (<a class="_blank">...</a>)
	$('a._blank').click(function(){
		window.open(this.href);
		return false;
	});
	
	// Page sliding effect
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	})
	$.fn.doTOPGALLERY = function(){
		//$(":animated").stop(true, true);
		$(".gallery-top .overlay a.active").removeClass("active").animate({borderColor: '#fff'});;
		$(this).addClass("active").animate({borderColor: '#daf373'});
		var href = $(this).attr("href");
		transition(href);
	};
	$(".gallery-top .overlay a").mouseover(function(){$(this).doTOPGALLERY()}).click(function(){return false});
	
	$(".gallery-indent").stdhover();
	var topgallery = function(){
		if (!$(".gallery-indent").hasClass("hover")) {
			$(".gallery-top .overlay a:eq(" + Math.floor(Math.random() * $(".gallery-top .overlay a").length) + ")").doTOPGALLERY();
		}
		setTimeout(topgallery, 4000);
	}
	topgallery();	
	
	// Preload screenshots
	$(".overlay a").each(function(){
		var href = $(this).attr("href")
		new Image().src = href
	})
	
	// Overlay for screenshots
	$(".page-content .overlay a").fancybox({
		'overlayShow'	: 'false',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'hideOnContentClick' : 'true',
		'titlePosition'	: 'none'
	})
	
	// menu
	$("#menu-top-menu > li").hover(function(){
		$(this).find("ul").fadeIn(300)
	}, function(){
		$(this).find("ul").fadeOut(300)
	})
});

function transition(href) {
	$(".gallery-top .pictures :animated").stop(true, true);
	$(".gallery-top .pic-1").css("background", "url('" + href + "') repeat-x scroll left top transparent");
	$(".gallery-top .pic-2").fadeOut(500, function(){
		$(this).css("background", "url('" + href + "') repeat-x scroll left top transparent").show();
		$(".gallery-top .pic-1").css("background", "transparent");
	});
}

$.fn.stdhover = function() {
	$(this).hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	return this;
}
$.fn.stdactive = function() {
	$(this).focusin(function(){
		$(this).addClass("active");
	})
	$(this).focusout(function(){
		$(this).removeClass("active");
	});
	return true;
}
