var vplayerstate = 0;

function listenvplaystate(obj) {

  if(obj['newstate'] == "PLAYING") {
    vplayerstate = 1;
  } else {
    vplayerstate = 0;
  }
}

function playerReady(obj) {
  var vplayerobjt = document.getElementById("ply");
  vplayerobjt.addModelListener("STATE","listenvplaystate");
}


function medianavTab(tabname, marker_to) {
  var vplayerobj = document.getElementById("ply");
  var videoobj = document.getElementById("mpnvideo");
  var a360obj = document.getElementById("mpn360");
  var zoomobj = document.getElementById("mpnzoom");
  var marker = document.getElementById("mpnmarker");
  var vpobj = document.getElementById("videoplayer");
  var ivobj = document.getElementById("imageviewer");
  var rotobj = document.getElementById("viewer360");
  
  var class_video_off = "video_link_off";
  var class_video_on = "video_link_on";
  var class_zoom_off = "zoom_link_off";
  var class_zoom_on = "zoom_link_on";
  var class_a360_off = "a360_link_off";
  var class_a360_on = "a360_link_on";
  
  if(tabname == "zoom") {
    if(videoobj != null)
	{
		videoobj.className = videoobj.className.replace(class_video_on,class_video_off);
	    videoobj.style.zIndex = 1;
	}	
	
	if(zoomobj != null)	
	{
    	zoomobj.className = zoomobj.className.replace(class_zoom_off,class_zoom_on);
	    zoomobj.style.zIndex = 3;
    }

	if(a360obj != null)
	{
		a360obj.className = a360obj.className.replace(class_a360_on,class_a360_off);
		a360obj.style.zIndex = 2;
	}	
	
	marker.className = "media_nav_"+marker_to;
    
	if(vpobj != null)
    	vpobj.style.visibility = "hidden";
    if(ivobj != null)
		ivobj.style.visibility = "visible";
    if(rotobj != null)
		rotobj.style.visibility = "hidden";

    if(vplayerstate == 1) vplayerobj.sendEvent("PLAY");
  } else if(tabname == "360") {
    if(videoobj != null)
	{
		videoobj.className = videoobj.className.replace(class_video_on,class_video_off);
	    videoobj.style.zIndex = 4;
	}
	
	if(zoomobj != null)	
	{
    	zoomobj.className = zoomobj.className.replace(class_zoom_on,class_zoom_off);
	    zoomobj.style.zIndex = 5;
	}
	
	if(a360obj != null)
	{
		a360obj.className = a360obj.className.replace(class_a360_off,class_a360_on);
	    a360obj.style.zIndex = 6;
	}
	
	marker.className = "media_nav_"+marker_to;
    
	if(vpobj != null)
		vpobj.style.visibility = "hidden";
    if(ivobj != null)
		ivobj.style.visibility = "hidden";
    if(rotobj != null)
		rotobj.style.visibility = "visible";

    if(vplayerstate == 1) vplayerobj.sendEvent("PLAY");
  } else if(tabname == "video") {
    if(videoobj != null)
	{
		videoobj.className = videoobj.className.replace(class_video_off,class_video_on);
	    videoobj.style.zIndex = 9;
	}
	
	if(zoomobj != null)	
	{
    	zoomobj.className = zoomobj.className.replace(class_zoom_on,class_zoom_off);
	    zoomobj.style.zIndex = 8;
	}
	
	if(a360obj != null)
	{
		a360obj.className = a360obj.className.replace(class_a360_on,class_a360_off);
		a360obj.style.zIndex = 7;
	}
	
	marker.className = "media_nav_"+marker_to;

	if(vpobj != null)
    	vpobj.style.visibility = "visible";
    if(ivobj != null)
		ivobj.style.visibility = "hidden";
    if(rotobj != null)
		rotobj.style.visibility = "hidden";
  }
  marker.style.zIndex = 1;
}

function additionalVideos(display)
{
	var videoScroller = document.getElementById("videothumbs_visiblearea");
	
	if(videoScroller != null)
		videoScroller.style.display = display;
}

function additional360(display)
{
	var videoScroller = document.getElementById("rot360thumbs_visiblearea");
	
	if(videoScroller != null)
		videoScroller.style.display = display;
}

function playVideo(id)
{
	var scrollingArea = document.getElementById('videothumbs_slider');
	var selectedVideo = document.getElementById('video_thumb_'+id);
	var currentVideo = null;
//	var playlists = getPlaylists();
	nowPlaying = id;
	
	for(i = 0; i < 5; i++)
	{
		currentVideo = document.getElementById('video_thumb_'+i);
		
		if(currentVideo != null)
			currentVideo.src = currentVideo.src.replace('/on/','/off/');
	}
	
	selectedVideo.src = selectedVideo.src.replace('/off/','/on/');
	player.sendEvent('LOAD',{type:"playlist",file:"images/media-player/playlists/"+playlists[id],title:"Someone else's Cool Video"});
	
//	player.sendEvent('ITEM',id);
//	player.sendEvent('STOP');
}

function nextVideo(playlists)
{
	var currentVideo = document.getElementById('video_'+nowPlaying);
	var nextVideo = document.getElementById('video_'+(nowPlaying+1));
	var scrollingArea = document.getElementById('videothumbs_slider');
	
	if(currentVideo != null && nextVideo != null)
	{
		nowPlaying++;
		
		if(nowPlaying > lastVisible)
		{
			firstVisible++;
			lastVisible++;
			scrollMargin = scrollMargin - 111;
			
			scrollingArea.style.marginLeft = scrollMargin+"px";
		}
		
		playVideo(nowPlaying,playlists);
	}
}

function previousVideo(playlists)
{
	var currentVideo = document.getElementById('video_'+nowPlaying);
	var previousVideo = document.getElementById('video_'+(nowPlaying-1));
	var scrollingArea = document.getElementById('videothumbs_slider');
	
	if(currentVideo != null && previousVideo != null)
	{
		nowPlaying--;
		
		if(nowPlaying < firstVisible)
		{
			firstVisible--;
			lastVisible--;
			scrollMargin = scrollMargin + 111;
			
			scrollingArea.style.marginLeft = scrollMargin+"px";
		}
		
		playVideo(nowPlaying,playlists);
	}
}

function warrantynavTab(tabname) {
  var warrantyobj = document.getElementById("wnwarranty");
  var serviceobj = document.getElementById("wnservice");
  var partsobj = document.getElementById("wnparts");
  var marker = document.getElementById("wnmarker");
  var warobj = document.getElementById("wnwarrantyc");
  var servobj = document.getElementById("wnservicec");
  var ptobj = document.getElementById("wnpartsc");
  
  if(tabname == "warranty") {
    warrantyobj.className = "wn_warranty_link_on";
    serviceobj.className = "wn_service_link_off";
    partsobj.className = "wn_parts_link_off";
    warobj.style.display = "block";
    servobj.style.display = "none";
    ptobj.style.display = "none";
    marker.className = "wn_warranty_link_marker";
  } else if(tabname == "service") {
    warrantyobj.className = "wn_warranty_link_off";
    serviceobj.className = "wn_service_link_on";
    partsobj.className = "wn_parts_link_off";
    warobj.style.display = "none";
    servobj.style.display = "block";
    ptobj.style.display = "none";
    marker.className = "wn_service_link_marker";  
  } else {
    warrantyobj.className = "wn_warranty_link_off";
    serviceobj.className = "wn_service_link_off";
    partsobj.className = "wn_parts_link_on";
    warobj.style.display = "none";
    servobj.style.display = "none";
    ptobj.style.display = "block";
    marker.className = "wn_parts_link_marker"; 
  }
  marker.style.zIndex = 1;
}  

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

function load360(zid,source,imgtitle, thumb_id) {
    try {
    getFlashMovie(zid).loadSequence(source,imgtitle);
    } catch (zerror) {
      alert(zerror);
    }

	var scrollingArea = document.getElementById('rot360thumbs_slider');
	var selectedVideo = document.getElementById('rot360_thumb_'+thumb_id);
	var currentVideo = null;
	nowPlaying = thumb_id;
	
	for(i = 0; i < 4; i++)
	{
		currentVideo = document.getElementById('rot360_thumb_'+i);
		
		if(currentVideo != null)
			currentVideo.src = currentVideo.src.replace('/on/','/off/');
	}
	
	selectedVideo.src = selectedVideo.src.replace('/off/','/on/');
}

function loadTrackingPixel(strURL){
	if(strURL != ""){
		document.getElementById("ifTrackingPixel").src = strURL;
	}
}
