var global_num=0;
var global_old_num=0;

var done = 0;
var num2 = 1;
var curOpacity = 100;
var name = new Array();
var price = new Array();
var size = new Array();
var loadedPage = 0;

var check_loaded_ti;
var image_cache = new Image;

var auto_interval='';
var waiting_for_auto_restart='';
var first_auto = true;


// auto-play the Newsplayer images
function auto_player() {
  if (first_auto) {
    auto_interval = window.setInterval("nextImage()",6000);
  }
  else {    auto_interval = window.setInterval("nextImage()",6000);
  }

}

function nextImage() {	var newnum = global_num+1;
	if (newnum>5) {		newnum = 1;
	}  changeImage(newnum);
  // restart the interval - so that the first change happens quickly but the next ones take longer
  if (first_auto) {  	first_auto = false;
    window.clearInterval(auto_interval);
    auto_interval = window.setInterval("nextImage()",6000);
  }
}
// stop the auto-player for 15 seconds - this is when you click a button and want to read the text
function clear_auto() {	window.clearInterval(auto_interval);
  window.clearTimeout(waiting_for_auto_restart);
  waiting_for_auto_restart = window.setTimeout(restart_auto,15000);
}


function restart_auto() {  window.clearTimeout(waiting_for_auto_restart);
  first_auto = false;
  nextImage();
  auto_player();
}

// stop the auto-player for 2 minutes - this is when you have clicked on the Full Article and you're reading it
function stop_auto() {	window.clearInterval(auto_interval);
  window.clearTimeout(waiting_for_auto_restart);
  waiting_for_auto_restart = window.setTimeout(restart_auto,120000);
}


// Extra bits for Mozilla
if (document.all) {
  isMoz = 0;
} else {
  isMoz = 1;
}

// If this is Mozilla, use our Load function
if (isMoz) {
  Image.prototype.readyState = "0";
  Image.prototype.__load__ = Image.prototype.load;
  Image.prototype.load = _Image_load;
}

//This is called when it starts loading
function _Image_load(strURL) {
    //change the readyState
    this.readyState = 1;

    //call the original load method
    this.__load__(strURL);
}

// This is called when it's finished loading
function _Image_onload() {
    //change the readyState
    this.readyState = 4;
}


function pageLoaded() {  loadedPage = 1;
}

function blank() {}

function changeImage(num, first) {
  if (!first) {  	first = false;
  }

  if (loadedPage==1) {    window.interval = setInterval("", 1);    window.clearInterval(interval);    done=0;    global_old_num = global_num;
    global_num=num;

    if (first==true) {
      returnObjById('main_1').src = 'images/newsplayer/'+img_prefix+np_content[global_num][5]+'.jpg?' + Math.floor(Math.random()*1001);
//      returnObjById('mainlink').href = './full_article.php?id=' + np_content[global_num][5];
//      returnObjById('newsplayer_text_container').innerHTML = "<span class='title'>" + np_content[global_num][2] + "</span><br />" + np_content[global_num][3];
      returnObjById('newsplayer_whiteangle').innerHTML = "<a href='" + np_content[global_num][4] + "'>" + np_content[global_num][0] + "</a>";
    }
    else {    	changeOpac(0,'main_'+global_num);
    }

    // update all buttons to make this one the selected one
    for (z=1; z<=5; z++) {
      obj=returnObjById("button_"+z);
      if (obj) {
        if (z!=global_num) {
          obj.className = "button";
        }
        else {        	obj.className = "button_selected";
        }
      }
    }

  }
}

function opacity_func(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    // make it visible
    returnObjById(id).style.display='';

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            window.setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            window.setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = returnObjById(id).style;
    var oldobject = returnObjById('main_'+global_old_num).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    curOpacity = opacity;
    if (opacity==0 && done==0) {
      // start showing a new image - set it's Z-index to be on top of the others
      object.zIndex = '100';
      oldobject.zIndex='50'; // need old image to be behind the new one, but still on top of the others
      // Set Z-index of TEXT for this new one to be on top of the others
      returnObjById('text_' + global_old_num).style.display = 'none';
      returnObjById('text_' + global_num).style.display = '';

      image_cache = null;      image_cache = new Image;
      image_cache.src = 'images/newsplayer/'+img_prefix+np_content[global_num][5]+'.jpg';
      if (isMoz) {
        image_cache.addEventListener("load", _Image_onload, false);
      }
      opacity_func('main_'+global_num, 0, 100, 400);
      //check_loaded_ti = window.setInterval("check_loaded()",200);
//      returnObjById('mainlink').href = './full_article.php?id=' + np_content[global_num][5];
//      returnObjById('newsplayer_text_container').innerHTML = "<span class='title'>" + np_content[global_num][2] + "</span><br />" + np_content[global_num][3];
      returnObjById('newsplayer_whiteangle').innerHTML = "<a href='" + np_content[global_num][4] + "'>" + np_content[global_num][0] + "</a>";

      done=1;
    }

    // hide the old one again and set z-index to the back
    if (opacity==100 && done==1) {      oldobject.zIndex='0';
      oldobject.opacity = (0);
      oldobject.MozOpacity = (0);
      oldobject.KhtmlOpacity = (0);
      oldobject.filter = "alpha(opacity=0)";
    }

}

function check_loaded() {
  var cachepic_obj = image_cache;
  if ( (cachepic_obj.readyState != "complete") && (cachepic_obj.readyState != 4) ){
  }
  else {//    returnObjById('main_'+global_num).src = 'images/newsplayer/'+img_prefix+np_content[global_num][5]+'.jpg'
    window.clearInterval(check_loaded_ti);
    opacity_func('main_'+global_num, 0, 100, 400);
  }
}
