function toggle(div) 
{
	var elem = document.getElementById(div);
    if (elem.style.display=='') {elem.style.display='none'; return;}
    elem.style.display='';
}

function pitch_mouse_over(id, min, max)
{
	intro_animate_stop();
	pitch_switch(id, min, max);
}

function pitch_mouse_out()
{
	intro_animate_start();
}

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('tabdiv' + id).style.display = 'block';
	intro_current = id;
}

var intro_timer = 0;
var intro_current = 1;
var intro_last = 1;
var intro_min = 0;
var intro_max = 0;
function intro_animate_start()
{
	if(intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
	
	intro_timer = setTimeout("intro_animate()", 3000);
}

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))
		{
			//pitch_switch(intro_current, intro_min, intro_max);
			photoShufflerFade();
			//intro_animate_start();
			return;
		}
		intro_current++;
	}
}

var faderTime = 0;

var gblPauseSeconds = 3;
var gblFadeSeconds = .85;
var gblRotations = 1;

// End Customization section

var gblDeckSize;
var gblOpacity = 100;
var gblOnDeck = 0;
var gblStartImg;
var gblImageRotations;

function photoShufflerFade()
{
	//document.getElementById('pitch').style.background = 'url(' + document.getElementById('tabdivimg'+intro_last).src + ') -1px 0px';
	//document.getElementById('backimage').src = document.getElementById('tabdivimg'+intro_last).src;
	
	var theimg = document.getElementById('tabdiv'+intro_current);
	my_setOpacity(theimg, 0);
	pitch_switch(intro_current, intro_min, intro_max);
	
	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
	var fadeDelta = 100 / (30 * gblFadeSeconds);

	// fade top out to reveal bottom image
	if (gblOpacity < 2*fadeDelta ) 
	{
		gblOpacity = 100;
		// stop the rotation if we're done
		if (gblImageRotations < 1) return;
		//photoShufflerShuffle();
		// pause before next fade
		intro_last = intro_current;
		intro_animate_start();
	}
	else
	{
		gblOpacity -= fadeDelta;
		my_setOpacity(theimg,gblOpacity);
		setTimeout("photoShufflerFade()",30);  // 1/30th of a second
	}
}

function my_setOpacity(obj, opacity) 
{
  opacity = (opacity == 100)?99.999:opacity;
	opacity = 100 - opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

scrollStep=4

timerLeft=""
timerRight=""
timerTop=""
timerBottom=""

function toLeft(id){
  document.getElementById(id).scrollLeft=0
}

function scrollDivLeft(id){
  clearTimeout(timerRight)
  document.getElementById(id).scrollLeft+=scrollStep
  timerRight=setTimeout("scrollDivLeft('"+id+"')",10)
}

function scrollDivRight(id){
  clearTimeout(timerLeft)
  document.getElementById(id).scrollLeft-=scrollStep
  timerLeft=setTimeout("scrollDivRight('"+id+"')",10)
}

function toRight(id){
  document.getElementById(id).scrollLeft=document.getElementById(id).scrollHeight
}

function toTop(id){
  document.getElementById(id).scrollTop=0
}

function scrollDivTop(id){
  clearTimeout(timerBottom)
  document.getElementById(id).scrollTop+=scrollStep
  timerBottom=setTimeout("scrollDivTop('"+id+"')",10)
}

function scrollDivBottom(id){
  clearTimeout(timerTop)
  document.getElementById(id).scrollTop-=scrollStep
  timerTop=setTimeout("scrollDivBottom('"+id+"')",10)
}

function toBottom(id){
  document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight
}

function stopMe(){
  clearTimeout(timerRight)
  clearTimeout(timerLeft)
  clearTimeout(timerTop)
  clearTimeout(timerBottom)
}

var gallery_action = false;
function gallery_move(id, cnt, dir, w, m)
{
	if(gallery_action)
		return;
		
	w = (w) ? w : 169;
	m = (m) ? m : 0;
	
	var curr = document.getElementById("gallery-"+id+"-holder").style.left;
	curr = parseFloat(curr);

	if(isNaN(curr))
		curr = 0;
	if(dir > 0)
	{
		if(curr >= 0)
			return;
	}
	else
	{
		if(curr + cnt * (w + m*2) - (300 / w) * (w + m*2) <= 0)
			return;
	}

	gallery_action = true;
	var offset = w+m;

	if(dir < 0)
		dir = "-";
	else
		dir = "+";
		
	$("#gallery-"+id+"-holder").animate(
		{left : dir+"="+offset+"px"},
		{queue:true, duration:100, complete: function() {gallery_action = false;}}
	);
}

function check_upit()
{
	var pass = true;

	if(check_field('name')) pass = false;	
	if(check_field('address')) pass = false;
	if(check_field('phone')) pass = false;
	if(check_field('email')) pass = false;
	if(check_field('q1')) pass = false;
	if(check_field('q2')) pass = false;
	if(check_field('q3')) pass = false;
	if(check_field('q4')) pass = false;
	if(check_field('q5')) pass = false;
	if(check_field('q6')) pass = false;
	if(check_field('q7')) pass = false;
	if(check_field('q8')) pass = false;
	
	if(!pass)
		$("#form-error").fadeIn(500);
	
	return pass;
}

function check_field(id)
{
	pass = true;
	if($("#"+id).val() == '')
	{
		pass = false;
		$("#"+id).css('border', '1px solid #4e1647');
	}
	else
		$("#"+id).css('border', '1px solid #ccc');
	return pass;
}

var zoomLevel = 1;
function plan(id)
{
	var l = $("#translations").val().split("|");

	if($('#'+id+"-"+zoomLevel).css('display') == 'none')
	{
		$("#plan-container"+"-"+zoomLevel+" > img").each(function(){
			if($(this).css('display') == 'inline' && $(this).attr('id') != 'pulice-'+zoomLevel && $(this).attr('id') != 'pplan-'+zoomLevel && $(this).attr('id') != 'plights-'+zoomLevel && $(this).attr('id') != 'pzone-'+zoomLevel)
				if(jQuery.browser.msie)
					$(this).hide();
				else
					$(this).fadeOut(500);
				$("#i"+$(this).attr('id')).removeClass('current');
		});
		
		if(jQuery.browser.msie)
			$('#'+id+"-"+zoomLevel).show();
		else
			$('#'+id+"-"+zoomLevel).fadeIn(500);
			
		$('#i'+id+"-"+zoomLevel).addClass('current');
		
		if(id == 'pulice')
			$('#apulice').html(l[1]).addClass('current');
		else if(id == 'pzone')
			$('#apzone').html(l[3]).addClass('current');
		else if(id == 'pgatovi')
			$('#apgatovi').html(l[5]).addClass('current');
	}
	else
	{
		if(jQuery.browser.msie)
			$('#'+id+"-"+zoomLevel).hide();
		else
			$('#'+id+"-"+zoomLevel).fadeOut(500);
		
		$('#i'+id+"-"+zoomLevel).removeClass('current');
		
		if(id == 'pulice')
			$('#apulice').html(l[0]).removeClass('current');
		else if(id == 'pzone')
			$('#apzone').html(l[2]).removeClass('current');
		else if(id == 'pgatovi')
			$('#apgatovi').html(l[4]).removeClass('current');
	}
}

var lightsTimer = 0;
function plan_lights()
{
	if(lightsTimer) 
	{
		clearTimeout(lightsTimer);
		lightsTimer = 0;
	}
	
	if($('#plights'+"-"+zoomLevel).css('display') == 'none')
		$("#plights"+"-"+zoomLevel).fadeIn(1000);
	else
		$("#plights"+"-"+zoomLevel).fadeOut(1000);
	
	lightsTimer = setTimeout("plan_lights()", 5000);
}

var planTimer = 0;
function showTooltip(id)
{
	if(planTimer) 
	{
		clearTimeout(planTimer);
		planTimer = 0;
	}
	
	planTimer = setTimeout(function(){displayTooltip(id);}, 500);	
}

function displayTooltip(id)
{
	if(planTimer) 
	{
		clearTimeout(planTimer);
		planTimer = 0;
	}

	$("#t"+id).fadeIn(500);
}

function hideTooltip(id)
{
	if(planTimer) 
	{
		clearTimeout(planTimer);
		planTimer = 0;
	}
		
	$("#t"+id).fadeOut(200);
}

function zoomPlan()
{
	zoomLevel = (zoomLevel == 1) ? 2 : 1;
		
	if(zoomLevel == 1)
	{
		//$("#plan-container-2").fadeOut(500, function(){ $("#plan-container").fadeIn(200); });
		$("#plan-container-2").hide();
		$("#plan-container-1").show();
		$("#zoom-in").hide();
		$("#zoom-out").show();
	}
	else if(zoomLevel == 2)
	{
		//$("#plan-container").fadeOut(500, function(){ $("#plan-container-2").fadeIn(200); });
		$("#plan-container-1").hide();
		$("#plan-container-2").show();
		$("#zoom-out").hide();
		$("#zoom-in").show();
	}
}

var planMouseDown = false;
var planMouseX = 0;
var planMouseY = 0;
function plan_move(e)
{
	e.preventDefault();
	if(planMouseDown)
	{
		var x = e.pageX - planMouseX;
		var y = e.pageY - planMouseY;

		var currX = parseInt($("#plan-container-2").css('left'));
		var currY = parseInt($("#plan-container-2").css('top'));
		
		x = currX + x;
		y = currY + y;
		
		if(x < -1184 + 950) x = -1184 + 950;
		if(x > 0) x = 0;
		if(y < -748 + 600) y = -748 + 600;
		if(y > 0) y = 0;
		
		$("#plan-container-2").css('top', y);
		$("#plan-container-2").css('left', x);
	}
}

function plan_mousedown(e)
{
	e.preventDefault();
	planMouseDown = true;
	planMouseX = e.pageX;
	planMouseY = e.pageY;
}

function plan_mouseup(e)
{
	e.preventDefault();
	planMouseDown = false;
}

