function tabs(id, n, m) {
	for (var x = n; x <= m; 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';
}

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) 
{
	//alert(id+" "+min+" "+max);
	for (i=min;i<=max;i++) {
		if(document.getElementById('tab' + i) != null)
			document.getElementById('tab' + i).className = '';
		if(document.getElementById('tabdiv' + i) != null)
			document.getElementById('tabdiv' + i).style.display = 'none';		
	}
	
	document.getElementById('tab' + id).className = 'current';
	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 = 1;
var intro_max = 4;
function intro_animate_start()
{
	if(intro_timer) 
	{
		clearTimeout(intro_timer);
		intro_timer = 0;
	}
	
	intro_timer = setTimeout("intro_animate()", 5000);
}

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('tab'+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;
}

function archive(dir)
{
	var p = document.getElementById('archive_page').value;
	if(dir == 'up')
		p--;
	else
		p++;
	var c = document.getElementById('archive_cat').value;
	
	new Ajax.Request('newsarhive.php',
	{
		method:'post',
		parameters:{ page:p, cat:c },
		onSuccess: function(transport)
		{
			var response = ajax_retrieve_messages(transport.responseText);
			//new Effect.DropOut('archive_div', {duration: 0.3});
			new Effect.Fade('archive_div', {from: 1, to: 0.01, duration: 0.2, 
			afterFinish: function() {
				document.getElementById('archive_page').value = p;
				document.getElementById('months').innerHTML = response[0];
				document.getElementById('archive_div_inner').innerHTML = response[1];
			}
			});
			
			new Effect.Fade('archive_div', {from: 0, to: 0.99, duration: 0.2, queue: 'end'});
			//new Effect.SlideDown('archive_div', {duration: 3.0, queue: 'end'});
		},
		onFailure: function()
		{
			alert('Something went wrong...') 
		}
	});
}

function ajax_retrieve_messages(str)
{
	var i = 0;
	var counter = 0;
	var msgs = [];
	
	counter = str.indexOf('[<ajax_message>', counter)
	while(counter >= 0)
	{
		var start_m = counter;
		var end_m = str.indexOf('</ajax_message>]', counter);
		var message = str.substring(start_m + 15, end_m);
		msgs[i] = message;
		i++;
		counter = str.indexOf('[<ajax_message>', end_m)
	}
	
	return msgs;
}