$(function(){
	if ($('#wallpaper').length)
	{
		var loc = 'http://www.znanje.hr/spasitelj';
		$('#wallpaper').click(function(){ window.location = loc; });
		$("#content, #footer, #header").click(function(e){ e.stopPropagation(); });
	}
});

/* pitch functions */
var intro_timer = 0;

function pitch_switch(id, min, max){
	for (i = min; i <= max; i++) {
		if (document.getElementById('tabdiv' + i) != null)
			document.getElementById('tabdiv' + i).style.display = 'none';		
		document.getElementById('ball' + i).className = 'ball';		
	}
	document.getElementById('tabdiv' + id).style.display = 'block';
	document.getElementById('ball' + id).className = 'ball lit';	
	intro_current = id;
}

function intro_animate_start(){
	if (intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
	intro_timer = setTimeout("intro_animate()", 6000);
}

function intro_animate_stop(){
	if (intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
}

function intro_animate(){
	gblImageRotations = gblDeckSize * (gblRotations+1);
	intro_current++;
	for(i=intro_min; i<intro_max; i++)
	{
		if(intro_current >intro_max)
			intro_current = intro_min;
		
		if(document.getElementById('tabdiv'+intro_current))
		{
			photoShufflerFade();
			return;
		}
		intro_current++;
	}
}

var faderTime = 0;
var gblPauseSeconds = 3;
var gblFadeSeconds = .85;
var gblRotations = 1;
var gblDeckSize;
var gblOpacity = 100;
var gblOnDeck = 0;
var gblStartImg;
var gblImageRotations;

function photoShufflerFade(){
	var theimg = document.getElementById('tabdiv'+intro_current);
	my_setOpacity(theimg, 0);
	pitch_switch(intro_current, intro_min, intro_max);
	var fadeDelta = 100 / (30 * gblFadeSeconds);
	if (gblOpacity < 2*fadeDelta ) 
	{
		gblOpacity = 100;
		if (gblImageRotations < 1) return;
		intro_last = intro_current;
		intro_animate_start();
	}
	else
	{
		gblOpacity -= fadeDelta;
		my_setOpacity(theimg,gblOpacity);
		setTimeout("photoShufflerFade()",30);
	}
}

function my_setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	opacity = 100 - opacity;
	obj.style.filter = "alpha(opacity:"+opacity+")";
	obj.style.KHTMLOpacity = opacity/100;
	obj.style.MozOpacity = opacity/100;
	obj.style.opacity = opacity/100;
}


/* recommendation functions */
var recintro_timer = 0;

function recpitch_switch(id, min, max) {
	for (i = min; i <= max; i++) {
		if (document.getElementById('rectabdiv' + i) != null)
			document.getElementById('rectabdiv' + i).style.display = 'none';		
		document.getElementById('recball' + i).className = 'ball';		
	}
	document.getElementById('rectabdiv' + id).style.display = 'block';
	document.getElementById('recball' + id).className = 'ball lit';	
	recintro_current = id;
}

function recintro_animate_start(){
	if (recintro_timer) 
	{
		clearTimeout(recintro_timer);
		recintro_timer = 0;
	}
	recintro_timer = setTimeout("recintro_animate()", 6000);
}

function recintro_animate_stop(){
	if (recintro_timer) 
	{
		clearTimeout(recintro_timer);
		recintro_timer = 0;
	}
}

function recintro_animate(){
	gblImageRotations = gblDeckSize * (gblRotations+1);
	recintro_current++;
	for(i=recintro_min; i<recintro_max; i++)
	{
		if(recintro_current >recintro_max)
			recintro_current = recintro_min;
		
		if(document.getElementById('rectabdiv'+recintro_current))
		{
			recphotoShufflerFade();
			return;
		}
		recintro_current++;
	}
}

function recphotoShufflerFade(){
	var theimg = document.getElementById('rectabdiv'+recintro_current);
	my_setOpacity(theimg, 0);
	recpitch_switch(recintro_current, recintro_min, recintro_max);
	var fadeDelta = 100 / (30 * gblFadeSeconds);
	if (gblOpacity < 2*fadeDelta ) 
	{
		gblOpacity = 100;
		if (gblImageRotations < 1) return;
		recintro_last = recintro_current;
		recintro_animate_start();
	}
	else
	{
		gblOpacity -= fadeDelta;
		my_setOpacity(theimg,gblOpacity);
		setTimeout("recphotoShufflerFade()",30);
	}
}

/* shelf functions */
function shelfpick(id){
	$("#shelf" + id).animate({
	marginTop: "-6px"
	}, {
	duration: 160,
	specialEasing: {
		width: 'linear',
		height: 'easeOutBounce'
	},
	complete: function() {	
	}
	});
}
function shelfdrop(id){
	$("#shelf" + id).animate({
	marginTop: "0px"
	}, {
	duration: 160,
	specialEasing: {
		width: 'linear',
		height: 'easeOutBounce'
	},
	complete: function() {	
	}
	});
}

function tabs(id, n)
{
	for (x = 1; x <= n; x++)
	{
		document.getElementById('tab' + x).className = '';
		document.getElementById('tabdiv' + x).style.display = 'none';
	}
	
	document.getElementById('tab' + id).className = 'current';
	document.getElementById('tabdiv' + id).style.display = 'block';
}
