var NovaCMS_slider_timer = 0;
var NovaCMS_slider_status = 0;
var NovaCMS_slider_current = 1;
var NovaCMS_slider_timeOn = 5000;
var NovaCMS_slider_timeFadeIn = 200;
var NovaCMS_slider_timeFadeOut = 200;

function NovaCMS_slider(id)
{
	NovaCMS_slider_status = 1;
	NovaCMS_slider_timer = setTimeout(function(){NovaCMS_slider_worker(id)}, NovaCMS_slider_timeOn);
}

function NovaCMS_slider_stop()
{
	if(NovaCMS_slider_timer)
	{
		clearTimeout(NovaCMS_slider_timer);
		NovaCMS_slider_timer = 0;
	}
}

function NovaCMS_slider_worker(id)
{
	NovaCMS_slider_stop();
	
	if(NovaCMS_slider_status != 1)
		return;
		
	var prev = NovaCMS_slider_current;
		
	NovaCMS_slider_current++;
		
	var total = $("#"+id+" > div").length;
	
	if(NovaCMS_slider_current > total)
		NovaCMS_slider_current = 1;
		
	$("#tabdiv"+prev).fadeOut(NovaCMS_slider_timeFadeOut, function(){
		$("#numbers a").removeClass('current');
		$("#slide-link-"+NovaCMS_slider_current).addClass('current');		
		$("#tabdiv"+NovaCMS_slider_current).fadeIn(NovaCMS_slider_timeFadeOut);
	});	
	
	NovaCMS_slider_timer = setTimeout(function(){NovaCMS_slider_worker(id)}, NovaCMS_slider_timeOn);
}

function NovaCMS_slider_change(id, image)
{
	NovaCMS_slider_stop();
	
	NovaCMS_slider_status = 2;
	
	if(NovaCMS_slider_current == id)
		return;
	
	$("#tabdiv"+NovaCMS_slider_current).stop(true, true).fadeOut(NovaCMS_slider_timeFadeOut, function(){
		$("#numbers a").removeClass('current');
		$("#slide-link-"+NovaCMS_slider_current).addClass('current');
		$("#tabdiv"+image).stop(true, true).fadeIn(NovaCMS_slider_timeFadeOut);
	});	

	NovaCMS_slider_current = image;
}

function NovaCMS_slider_continue(id)
{
	if(NovaCMS_slider_status == 2)
	{
		NovaCMS_slider(id);
	}
}


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;
var intro_state = 0;
function intro_animate_start()
{
	if(intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
	
	if(intro_state == 0)
		intro_timer = setTimeout("intro_animate()", 5000);
}

function intro_animate_stop()
{
	if(intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
}

function intro_change(id)
{
	intro_animate_stop();
	intro_state = 1;
	
	if(intro_current == id)
		return;
	
	intro_current = id - 1;
	
	if(gblTimer)
	{
		clearTimeout(gblTimer);
		gblTimer = 0;
	}
	
	intro_animate();
	//pitch_switch(intro_current, intro_min, intro_max);
	//$("#tabdiv"+intro_current).css('opacity', 1);
}

function intro_release()
{
	if(intro_state == 1)
	{
		intro_state = 0;
		intro_animate_start();
	}
}

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;
var gblTimer = 0;

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);
		glbTimer = 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 : 200;
	m = (m) ? m : 10;
	
	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;}}
	);
}

