// Macromedia functions for rollovers
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

// Reads a parameter from the paren't window's URL
function gup(name,def)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.parent.location.href );
  if( results == null )
    return def;
  else
    return results[1];
}

// Some global variables
var pos = 0, episodeHeight = 72, slideDistance = 1, currentSelection = null, scrollTimer = null;

// Moves the chooser up an episode
function scrollUp() {
  stopScrolling();
  scrollTimer = setTimeout('continueScrolling(+9999)',500);
  scrollEpisode(Math.floor((pos+episodeHeight)/episodeHeight)*episodeHeight);
}

// Moves the chooser down an episode
function scrollDown() {
  stopScrolling();
  scrollTimer = setTimeout('continueScrolling(-9999)',500);
  scrollEpisode(Math.ceil((pos-episodeHeight)/episodeHeight)*episodeHeight);
}

// Slides the chooser logarithmically to the destinaton.
function scrollEpisode(dest) {
  if (move(dest, Math.floor((dest - pos) / 6))) {
	setTimeout('scrollEpisode('+dest+')',10);
  }
}

// Slides the chooser linearly to the destination.
function continueScrolling(dest) {
  if (move(dest, dest > pos ? slideDistance : -slideDistance)) {
	if (scrollTimer != null) scrollTimer = setTimeout('continueScrolling('+dest+')',10);
  }
}

// Cancels the auto-scroll.
function stopScrolling() {
  if (scrollTimer != null) {
    clearTimeout(scrollTimer);
	scrollTimer = null;
  }
}

// Moves the chooser towards the dest position by the given offset amount
function move(dest,off) {
  if (dest < -282) dest = -282;
  else if (dest > 0) dest = 0;
  if (off > 0 && pos+off <= dest || off < 0 && pos+off >= dest) {
    pos += off;
	document.getElementById('selector_images').style.top = pos + 'px';
	return true;
  }
  
  pos = dest;
  document.getElementById('selector_images').style.top = dest + 'px';
  return false;
}

// Reads a parameter from the window's URL
function gup(name,def)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return def;
  else
    return unescape(results[1]);
}

// Switches to a different episode
function swap(episode) {
    var outer = gup('outer');
	if (outer.match(/episode=/))
  	  window.parent.location = outer.replace(/episode=[^&#]*/,'episode='+episode);
	else if (outer.match(/\?/))
	  window.parent.location = outer + '&episode='+episode;
	else
	  window.parent.location = outer + '?episode='+episode;
}

//Shows a specific episode
function show(episode, index_file) {
  if (currentSelection)  currentSelection.oSrc = currentSelection.src = currentSelection.src.replace(/_c1.*$/,'_c1.png');
  currentSelection = document.getElementById(episode);
  currentSelection.oSrc = currentSelection.src = currentSelection.src.replace(/_c1.*$/,'_c1_f2.png');
  move(-currentSelection.parentNode.parentNode.parentNode.offsetTop);
  var episodeloc = document.location.href;
	episodeloc = episodeloc.replace(/\/en\//,"/");
	episodeloc = episodeloc.replace(/\/cn\//,"/");
	episodeloc = episodeloc.replace(/\/sp\//,"/");
	episodeloc = episodeloc.replace(/\/flash1\.html.*$/,"/../"+episode+"/"+index_file);
  document.getElementById('content').src = episodeloc;
}


