
var subPageActive = false;

function showContent(url) {
	$('#popupContent').fadeOut(function(){
		$('#leftContentInner2').load(url+'&ajax=1', {}, function(){
			$('#loading').hide();
			$("#leftContentInner2").slideDown('fast');
			$("#contentClose").show();
			subPageActive = true;
			addLinkEvents();
		});
	});
}

function addLinkEvents() {
	$('a.ajax').unbind();
	$('a.ajax').bind("click", function(){
		$('#loading').show();
		if (subPageActive)
		{
			$("#leftContentInner2").hide();
			$("#contentClose").hide();
			showContent($(this).attr("href"));
		} else {
			showContent($(this).attr("href"));
		}
		return false;
	});

}

function addEvents() {

	$('#logout').unbind();
	$('#logout').click(function(){
		$('#rightContentInner').load('?logout=1&right=1&ajax=1', function(){addEvents();});
		if ($('#leftContentInner2 .arr4, #leftContentInner2 .arr5').size()) closeContent();
		return false;
	});

	$('#loginSection input[type=submit]').unbind();
	$('#loginSection input[type=submit]').bind("click", function(){
		$('#rightContentInner').load('?right=1&ajax=1', {
				authEnterUser: $('#loginSection input[name=authEnterUser]').val(),
				authEnterPass: $('#loginSection input[name=authEnterPass]').val()
			}, function(){
				addEvents();
			});
		return false;
	});

	addLinkEvents();

}



function timeUpdate() {
	to = setTimeout("timeUpdate()", 1000);
	if (subPageActive) return;

	time = 1275116400000 - ((new Date()).getTime()) + diff;

	if (time < 0)
	{
		$('#counterDay span').text('0');
		$('#counterHour span').text('00');
		$('#counterMin span').text('00');
		$('#counterSec span').text('00');
		clearTimeout(to);
		return;
	}

	days = (time - (time % 86400000)) / 86400000;
	time = time - (days * 86400000);
	hours = (time - (time % 3600000)) / 3600000;
	time = time - (hours * 3600000);
	mins = (time - (time % 60000)) / 60000;
	time = time - (mins * 60000);
	secs = (time - (time % 1000)) / 1000;

	$('#counterDay span').text(days+'/');
	$('#counterHour span').text((hours < 10 ? "0" + hours : hours)+'/');
	$('#counterMin span').text((mins < 10 ? "0" + mins : mins)+'/');
	$('#counterSec span').text(secs < 10 ? "0" + secs : secs);

}

function zeroFill(num, len) {
	ret = '' + num;
	while (ret.length < len)
	{
		ret = '0' + ret;
	}
	return ret;
}

var prevBg = 0;
var prevBgGiu = 0;
var bgTimer;
function switchBackground() {
	bgTimer = setTimeout("switchBackground()", 10000);
	var preload = new Image();
	preload.onload = function(){
		$('#noFlashBG').animate({opacity: 0}, 1000, function(){
			$('#noFlashBG').css('backgroundImage', 'url('+preload.src+')');
			$('#noFlashBG').animate({opacity: 1}, 1000);
		});
	};
	var src;
	if (Math.random() < 0.6) {
		nextGiu = (prevBgGiu = (prevBgGiu + Math.floor(Math.random()*8))%12) + 1;
		src = 'giu_' + zeroFill(nextGiu, 2);
	} else {
		next = (prevBg = (prevBg + Math.floor(Math.random()*111))%112) + 1;
		src = zeroFill(next, 3);
	}
	preload.src = '/backgrounds/' + src + '.jpg';
}

function closeContent(immediately) {
	subPageActive = false;
	if (immediately)
	{
		$("#leftContentInner2").hide();
	} else {
		$("#leftContentInner2").slideUp('fast');
	}
	$("#contentClose").hide();
	return false;
}

$(document).ready(function(){
	subPageActive = $("#leftContentInner2").css("display") != "none";

	$('#contentClose, #toHome').bind("click", function(){
		$(this).blur();
		closeContent();
		return false;
	});

	addEvents();
	switchBackground();
	timeUpdate();
	$('#noFlashBG').width(($('#container').width()/2)+80);

	$('#right h2 span.decor, #counter .decor').each(function(){
		$(this).width($(this).parent().width() - $(this).siblings('.text').width() - 10);
	});

	$('#popupOpen').click(function(){
		closeContent(true);
		$('#popupContent').css('left', (($('#container').width()/2)-475)+'px').fadeIn();
		return false;
	});

	$('#popupContent a.close').click(function(){
		$(this).blur();
		$('#popupContent').fadeOut();
		return false;
	});

	$('#video a.close, #video a.back').click(function(){
		$(this).blur();
		switchBackground();
		$('#video').fadeOut();
		$('#videoContent').html('');
		return false;
	});

	$('#videoList a.close').click(function(){
		$(this).blur();
		$('#videoList').slideUp();
		$('#videoContent').html('');
		return false;
	});

	$('.showVideo').click(function(){
		$(this).blur();
		clearTimeout(bgTimer);
		vid = $(this).attr('href').substr(31);
		$('#videoContent').html('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+vid+'&hl=en_US&fs=1&rel=0&color1=0x000000&color2=0x000000"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+vid+'&hl=en_US&fs=1&rel=0&color1=0x000000&color2=0x000000" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>');
		$('#video').css('left', (($('#container').width()/2)-495)+'px').fadeIn();
		$('#noFlashBG').stop().animate({opacity: 0}, 1000, function(){
			$('#noFlashBG').animate({opacity: 0}, 1000);
		});
		return false;
	});

	$('#counter h3').click(function(){
		$('#videoList').slideToggle();
		$('#videoList .decor').each(function(){
			$(this).width($(this).parent().width() - $(this).siblings('.text').width() - 10);
		});

		return false;
	});


});

