google.load("jquery", "1.4");
google.setOnLoadCallback(function() {
	core();
});

function hanleRandomDivs()
{
	$(".random").each(function() {
		var nthChild = Math.floor(Math.random() * $(this).children().size() + 1);

		$(this).find(":nth-child(" + nthChild + ")").show();
	});
}

function core()
{
	custom();

	hanleRandomDivs();
	handleImageHrefs();
	
	// News collapse
	$("#frontnews li").click(function() {
		if ($(this).find('.slidedown').css('display') == 'none') {
			$(this).siblings().find('.slidedown').slideUp('fast');
			$(this).find('.slidedown').slideDown();
		} else {
			$(this).find('.slidedown').slideUp('fast');
		}
	});
	
}

function custom()
{
	$("#jsddm li:last").addClass('last');
	
	$(".inner").each(function(i) {
		$(this).hide();
		$(this).css('visibility', 'visible');
		var item = $(this);

		$(this).find('img').not(".overlay img").load(function() {
			$(item).delay(i * 300).hide().fadeIn();
			$(item).parents('div').find('.loading').remove();
		}).attr('src', $(this).find('img').attr('rel'));

	});
}



function handleImageHrefs()
{
	$('.imageItem').find('img').each(function(i) {
		if($(this).attr('href'))
		{
			if($(this).attr('target') != '')
			{
				$(this).wrap('<a href="' + $(this).attr('href') + '" target="' + $(this).attr('target') + '" />')
			}
			else
			{
				$(this).wrap('<a href="' + $(this).attr('href') + '" />')
			}
		}

	});
}
