/*
might improve this script soon
created by zenverse.net
*/
var cal_postimagenum; //no. of post images
var cal_flydelay = 40;
var cal_nextrounddelay = 8000;
var cal_tohide = 1;
var cal_toshow = 2;
var cal_groupid = 'cal_img';
//var cal_spanid = 'cal_text';
var cal_running = false;

//#########################

function cal_gallery_run(rerun) {
if (rerun) {
cal_running = false;
}

if (!cal_running) {
setTimeout("cal_hideit('"+cal_tohide+"','"+cal_toshow+"');",cal_flydelay);
}
cal_running = true;

var zstart;
var zend;
//prepare for next round
switch (cal_postimagenum) {
  case 1:
  zstart = 1;
  zend = 1;
break;
  case 2:
  zstart = cal_toshow;
  zend = cal_tohide;
break;
  default:
  // start default
  zstart = cal_toshow;
  zend = cal_toshow+1;
  if (zend > cal_postimagenum) {
  zend = 1;
  }
  // end default
break;
} //end switch
//alert('hiding '+cal_tohide+', showing '+cal_toshow);
cal_tohide = zstart;
cal_toshow = zend;
}

//#########################

var cal_displacement = 55;
var cal_hide_pos = 0;
var cal_show_pos = 250;

function cal_hideit(id,toshow) {
document.getElementById(cal_groupid+id).style.position = 'relative';

cal_hide_pos = cal_hide_pos+cal_displacement;
document.getElementById(cal_groupid+id).style.top = '-'+cal_hide_pos+'px';

if (cal_hide_pos < 230) {
setTimeout("cal_hideit('"+id+"','"+toshow+"')",cal_flydelay);
} else {
cal_hide_pos = 0;
setTimeout("cal_showit('"+toshow+"','"+id+"')",cal_flydelay);
document.getElementById(cal_groupid+id).style.display = 'none';

document.getElementById(cal_groupid+toshow).style.position = 'relative';
document.getElementById(cal_groupid+toshow).style.top = cal_show_pos+'px';
document.getElementById(cal_groupid+toshow).style.display = 'block';
}
}

//#########################

function cal_showit(id,tohide) {
document.getElementById(cal_groupid+id).style.position = 'relative';

cal_show_pos = cal_show_pos-40;
document.getElementById(cal_groupid+id).style.top = cal_show_pos+'px';

if (cal_show_pos > 0) {
setTimeout("cal_showit('"+id+"','"+tohide+"')",cal_flydelay);
} else {
document.getElementById(cal_groupid+id).style.top = '0px';
cal_show_pos = 250;
setTimeout("cal_gallery_run(true);",cal_nextrounddelay);
}
}


//#########################

function cal_getnumofimages() {

var stopat = 1;
var tryx;
for ( var i=1; i<30; i++ ) {
  try {
    tryx = document.getElementById(cal_groupid+i).innerHTML;
  } catch (e) {
      stopat = i;
      break;
  }
}
cal_postimagenum = stopat-1;

if (cal_postimagenum == 1) {
cal_toshow = 1;
} else {
cal_toshow = 2;
}


if (cal_postimagenum) {

if (cal_postimagenum > 1) {
for (var x=2;x<=cal_postimagenum;x++) {
document.getElementById(cal_groupid+x).style.display = 'none';
//document.getElementById(cal_spanid+x).style.display = 'none';
document.getElementById(cal_groupid+x).style.visibility = 'visible';
//document.getElementById(cal_spanid+x).style.visibility = 'visible';
}
}

setTimeout("cal_gallery_run();",cal_nextrounddelay);
}
}


//#########################

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(cal_getnumofimages);