var iCurrentVideo = -1;
var bFlashFinished = false;
var oWMPContainer = document.getElementById('wmpContainer');
var oWMPlayer = null;
var oRPContainer = document.getElementById('rpContainer');
var oRealPlayer = null;
var oQTContainer = document.getElementById('qtContainer');
var oQTPlayer = null;
var oFlashContainer = document.getElementById('flashContainer');
var oFlashPlayer = null;
var oPrevVideo = document.getElementById('prevVideo');
var oNextVideo = document.getElementById('nextVideo');

function initContainer(oEl) {
  oEl.style.display = 'none';
  oEl.innerHTML = '';
}


	// some variables to save
	var currentPosition;
	var currentVolume;
	var currentItem;

	// these functions are caught by the JavascriptView object of the player.
	function sendEvent(typ,prm) { thisMovie("mpl").sendEvent(typ,prm); };
	function getUpdate(typ,pr1,pr2,pid) {
	  if (typ == "state")
	  {
	   if (pr1 == 3)  
	   { 
	    bFlashFinished = true;
	   }
	  } 
	};

	// These functions are caught by the feeder object of the player.
	function loadFile(obj) { thisMovie("mpl").loadFile(obj); };
	function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }
	function removeItem(idx) { thisMovie("mpl").removeItem(idx); }
	function getItemData(idx) {
		var obj = thisMovie("mpl").itemData(idx);
		var nodes = "";
		for(var i in obj) { 
			nodes += "<li>"+i+": "+obj[i]+"</li>"; 
		}
		document.getElementById("data").innerHTML = nodes;
	};

	// This is a javascript handler for the player and is always needed.
	function thisMovie(movieName) {
	    if(navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	};




function loadVideo(iVideo) {
  initContainer(oQTContainer);
  oQTPlayer = null;
  initContainer(oRPContainer);
  oRealPlayer = null;
  initContainer(oFlashContainer);
  oFlashPlayer = null;
  initContainer(oWMPContainer);
  oWMPlayer = null;
  var oVideo = aVideos[iVideo];
  try {
    if (oVideo.type == 'mov') {
      oQTContainer.innerHTML = '<embed id="qt" type="video/quicktime" src="' + oVideo.url +
       '" width="320" height="256" autoplay="true" loop="false" controller="true" scale="tofit"></embed>';
      oQTContainer.style.display = 'block';
      oQTPlayer = document.getElementById('qt');
    } else if (oVideo.type == 'rm') {
      oRPContainer.innerHTML = '<embed id="rp" type="audio/x-pn-realaudio-plugin" src="' + oVideo.url +
       '" width="320" height="266" autostart="true" loop="false"></embed>';
      oRPContainer.style.display = 'block';
      oRealPlayer = document.getElementById('rp');
    } else if (oVideo.type == 'flvx') {
      oFlashContainer.innerHTML = '<embed id="mpl" src="&autostart=true&enablejs=true&javascriptid=mpl" \
       loop="false" menu="false" quality="high" scale="noscale" salign="lt" \
       width="320" height="260" bgcolor="#000000" \
       allowScriptAccess="sameDomain" type="application/x-shockwave-flash" \
       pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
      oFlashContainer.style.display = 'block';
      oFlashPlayer = document.getElementById('flash');
 
    } else if (oVideo.type == 'flv') {
      oFlashContainer.innerHTML = '<embed id=mpl src="http://www.pcplats.com/player/mediaplayer.swf?file=http://www.pcplats.com/url.php?wimpy=' +  oVideo.url +  '/wimpy.xml&height=260&width=320&enablejs=true&javascriptid=mpl&autostart=true" width="320" height="260" allowfullscreen="true" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> ';
      oFlashContainer.style.display = 'block';
      oFlashPlayer = document.getElementById('mpl');
 
    } else if (oVideo.type == 'swf') {
      oFlashContainer.innerHTML = '<embed id="flash" type="application/x-shockwave-flash" src="' + oVideo.url +
       '" width="320" height="256" play="true" loop="false" quality="high" bgcolor="#000000"></embed>';
      oFlashContainer.style.display = 'block';
      oFlashPlayer = document.getElementById('flash');
    } else if (oVideo.type == 'mp3') {
      oFlashContainer.innerHTML = '<embed id=mpl src="http://www.pcplats.com/player/mediaplayer.swf?file=http://www.pcplats.com/url.php?wimpy=' +  oVideo.url +  '/wimpy.xml&height=260&width=320&enablejs=true&javascriptid=mpl&autostart=true&showeq=true" width="320" height="260" allowfullscreen="false" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> ';
      oFlashContainer.style.display = 'block';
      oFlashPlayer = document.getElementById('mpl');
  	} else {
      oWMPContainer.innerHTML = '<embed id="wmp" type="application/x-mplayer2" src="' + oVideo.url +
       '" autostart="1" width="320" height="285"></embed>';
      oWMPContainer.style.display = 'block';
      oWMPlayer = document.getElementById('wmp');
  	}
  } catch (oException) {}
	

	
	if (!bLoop) {
	  if (iCurrentVideo <= 0) {
	    oPrevVideo.style.display = 'none';
	  } else {
	    oPrevVideo.style.display = '';
	  }
	  if (iCurrentVideo >= aVideos.length - 1) {
	    oNextVideo.style.display = 'none';
	  } else {
	    oNextVideo.style.display = '';
	  }
	}
	
	
	if (typeof resizeToContent == 'function') {
  	setTimeout(function() {
      resizeToContent();
    }, 0);
  }
}

function loadPrevVideo() {
  iCurrentVideo--;
  if (iCurrentVideo < 0) {
    if (bLoop) {
      iCurrentVideo = aVideos.length - 1;
    } else {
      iCurrentVideo++;
      return;
    }
  }
	setTimeout(function() {
    loadVideo(iCurrentVideo);
  }, 0);
}

function loadNextVideo() {
  iCurrentVideo++;
  if (iCurrentVideo >= aVideos.length) {
    if (bLoop) {
      iCurrentVideo = 0;
    } else {
      iCurrentVideo--;
      return;
    }
  }
	setTimeout(function() {
    loadVideo(iCurrentVideo);
  }, 0);
}

if (aVideos.length) {
  document.getElementById('playerContainer').style.display = 'block';
  loadNextVideo();
  setInterval(function() {
    //alert(oFlashPlayer.IsPlaying());
    try {
      if (oWMPlayer && typeof oWMPlayer.playState != 'undefined') {
        // WMP
        if (oWMPlayer.playState == 0 || oWMPlayer.playState == 10) {
          loadNextVideo();
          return;
        }
      } else if (oQTPlayer && typeof oQTPlayer.GetRate != 'undefined' &&
       typeof oQTPlayer.GetTime != 'undefined' && typeof oQTPlayer.GetEndTime != 'undefined') {
        // Quicktime
        if (oQTPlayer.GetRate() == 0 && oQTPlayer.GetTime() == oQTPlayer.GetEndTime()) {
          loadNextVideo();
          return;
        }
      } else if (oRealPlayer && typeof oRealPlayer.GetPlayState != 'undefined') {
        // Realplayer
        if (oRealPlayer.GetPlayState() == 0) {
          if (oRealPlayer.started) {
            loadNextVideo();
            return;
          }
        } else if (!oRealPlayer.started) {
          oRealPlayer.started = true;
        }
      } else if (oFlashPlayer) {
        // Flash
        if (bFlashFinished) {
          if (oFlashPlayer.started) {
            bFlashFinished = false;
            loadNextVideo();
            return;
          }
        } else if (!oFlashPlayer.started) {
          oFlashPlayer.started = true;
        }
      }
    } catch (oException) {}
  }, 10);
} else {
  document.getElementById('errorContainer').style.display = 'block';
}
