var volover = false;
var metadata = false;
var xmoovlocation = ""
var nonxmoovlocation = ""
var vidlocation = ""
var xmoov =  false;
var xmoovoffset = 0;
var videourl;
var tryingfor = 0;
var time = 0;
var rawseconds = 0;
var rolling = true;
var flashVideoPlayer;
var pixelswaiting = false;
var duration = 1;
var bytestotal = 1;
var durationformat = "0:00";
var fullwidth = 400;
var fakeout = false;
var barwidth = fullwidth - xmoovoffset;
var videoplaying = false;
var videopaused = false;

function makeVideo(xlocfp, nxlocfp, vlocfp){
		xmoov = false;
		xmoovlocation = xlocfp;
		nonxmoovlocation = nxlocfp;
		vidlocation = vlocfp;

	    
		initialize();
		videoplaying = false;
		videopaused = false;
	    currentvideo = 0;
		flashVideoPlayer.setInfoXmoov( xmoovlocation );	
	    flashVideoPlayer.setInfoFLV( vidlocation );	

		changeObjectVisibility('v_pause', 'hidden');
		
		//callFlashPlayVideoByURL(nonxmoovlocation + vidlocation);
}

function makeVideo2(xlocfp, nxlocfp, vlocfp, wfp, hfp){
		xmoov = false;
		xmoovlocation = xlocfp;
		nonxmoovlocation = nxlocfp;
		vidlocation = vlocfp;

	    
		initialize();
		videoplaying = false;
		videopaused = false;
	    currentvideo = 0;
		flashVideoPlayer.setInfoXmoov( xmoovlocation );	
	    flashVideoPlayer.setInfoFLV( vidlocation );	
		flashVideoPlayer.setWidthFLV( wfp );
		flashVideoPlayer.setHeightFLV( hfp );	

		changeObjectVisibility('v_pause', 'hidden');
		
		//callFlashPlayVideoByURL(nonxmoovlocation + vidlocation);
}
	
				  
function wereReady(){
			currentvideo = 0;
			if(!xmoov){
			updatevideourl(nonxmoovlocation + vidlocation);
			}
			else{
			createvideourlxmoov(0);	
			}
   			
			$("#trackbar").click(
									 function(e){
										 	//this defines that track bars properties as a button. 
								
											  var pixelsleft = e.pageX - $(this).offset().left;
											  updatexmoov(pixelsleft);
											  if(metadata){
												//if the metadata is here we can invoke duration and go ahead with telling it to play  
     										    seekTo((pixelsleft/fullwidth)*duration);
										
											  }
											  else{
												//if the metadata has not arrived:
												//store the position of the pixels, store the xmoovoffset (will be referenced to find your desired time) and launch the video at an arbitrary time (0) so that we can get some metadata!)
												pixelswaiting = true;	
												updatexmoov(pixelsleft);
												callFlashPlayVideoByTime(0);
											  }
											 
											  }
									);	


        //handling and tracking interaction with the volume bar:
		$("#volumetrack").mousedown(function(){volover = true});
		$("#volumetrack").mouseup(function(){volover = false});
		$("#volumetrack").mouseleave(function(){volover = false});
		$("#volumetrack").mousemove(
									function(k){
												if(volover){
												volumeit(parseInt(k.pageX - $(this).offset().left));
												}
											}
									);
		
		
   	
	
		$("#volumetrack").click(
									function(k){
												volumeit(parseInt(k.pageX - $(this).offset().left));
											}
									);
		
		
		$("#v_play").click(
									function(){
												callFlashPlayPauseVideo();
											}
									);
		$("#v_pause").click(
									function(){
												callFlashPlayPauseVideo();
											}
									);
		
		
		
   		} // end we're ready
	
	

  

    function initialize() {
        /* Check whether the browser is IE. If so, flashVideoPlayer is
window.videoPlayer. Otherwise, it's document.videoPlayer. The
videoPlayer is the ID assigned to the <object> and <embed> tags. */
        var isIE = navigator.appName.indexOf("Microsoft") != -1;
        flashVideoPlayer = (isIE) ? window['videoPlayer'] : document['videoPlayer'];
		
	
			
    }
	
	function getStyleObject(objectId) {
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		return getObjNN4(document,objectId);
	} else {
		return false;
	}
} 
	

	
function changeObjectVisibility(objectId, newVisibility) 
//left over bits of code from  a long time ago. Should be switched with jquery?	
	{
    	var styleObject = getStyleObject(objectId);
    	if(styleObject) {
			styleObject.visibility = newVisibility;
			return true;
    	} 
		else{
				return false;
    	}
	} 
	
function changevPlaylist(objectId, state) 
    {
    	var styleObject = getStyleObject(objectId);
    	if(styleObject) {

			if(state == '1'){

				styleObject.color='#ffffaa';
				return true;
			}
			else{
				styleObject.color = '#ffffff';
				return true;
			}
    	} 
		else{
				return false;
    	}
	} 
	
function video_step(dir){
	
		currentvideo = currentvideo + dir;
		if (currentvideo >= vidPlayList.length){
		
		currentvideo = currentvideo - vidPlayList.length;
		
		}
		if (currentvideo < 0){
		
		currentvideo = currentvideo + vidPlayList.length;
		} 
		
		callFlashPlayVideoByID(currentvideo);
	
	}
	

	
	function callVidFromPage(timevari){
		//this function is called by hrefs on the main page. Should be used as cue points with seconds. 
		var pixelsleft = (timevari/duration)*fullwidth;
	    updatexmoov(pixelsleft);
		seekTo(timevari);
	}

	function callFlashPlayVideoByURL(vidurl){
		//this will be modified for the addition of a playlist. Right now it is useless...
		  flashVideoPlayer.playVideo(vidurl);

	}
	
	function callFlashPlayVideoByTime(time){
		  //our main root to the swf. Throw the video a time call... must use xmoov streaming.
		  if(xmoov){
		  flashVideoPlayer.playVideoXmoov(time);
		  }
		  else{
			  
			if(metadata){  
			flashVideoPlayer.seek(time); 
			}
			else{
				 flashVideoPlayer.playVideo(nonxmoovlocation + vidlocation);
			}
			
		  }
			
	}
	
	function callFlashPlayVideoByID(vidID) {
	    //playlist only...
		currentvideo = vidID;
		nowplaying(currentvideo);
		updatevideourl(vidPlayList[vidID].url);
        callFlashPlayVideoByURL(videourl);
		changeObjectVisibility('v_pause', 'visible');
		for(j = 0; j < vidPlayList.length; j++)
		{
			if(j == currentvideo){
				changevPlaylist('videoplaylist_item_' + j, '1');
				
			}
			if(j != currentvideo){
				changevPlaylist('videoplaylist_item_' + j, '2');
			}
		
		
		}
    }
	
	function nowplaying(idp){
		//also for playlist use.
		//document.getElementById('nowplayingp').innerHTML = "Now Playing: <span style='color:#ffff99'>" + vidPlayList[idp].name + "</span>";
	}
	
	
	function volumeit(pixvol){
	 	//volume control for speaking to flash
		$("#volumestatus").css('width', pixvol+"px");
		callFlashVolume(parseInt(pixvol*2));		
		
	}
	function callFlashVolume(vol){
		
		flashVideoPlayer.volumeChange(vol);	
		
	}
	
	
	
    function callFlashPlayPauseVideo() {
		//toggle states from play pause button
		
		
		if(videoplaying == true){
			videopaused = !videopaused;
			flashVideoPlayer.pauseResume();
			if(videopaused == true)
			{
				changeObjectVisibility('v_pause', 'hidden'); 
				changeObjectVisibility('v_play', 'visible'); 

			}
			else{changeObjectVisibility('v_pause', 'visible');
 			}

		}
		if(videoplaying == false){
			 $('#update').html('&nbsp;&nbsp;buffering...');
			if(!xmoov){
			callFlashPlayVideoByTime(rawseconds);
			}
			else{
			callFlashPlayVideoByTime(rawseconds);
			}
			videopaused = false;
			videoplaying = true;
			changeObjectVisibility('v_pause', 'visible'); 
			//problem here? ^^^
		}
    }
	
function updatexmoov(pixlft){
	//keeps track of left-offset based on where we are loading from in the file. This is based in pixels. 
	if(xmoov){
	xmoovoffset = pixlft;
	barwidth = fullwidth - xmoovoffset;
	}
}

function createvideourlxmoov(time){
	//this creates a url using the xmoov stream php script and passing the needed variables
	//does it even work this way now?
	
	
	
	var tempurl =(xmoovlocation + "?file=" + vidlocation + "&position=" + parseInt((time/duration)*bytestotal) + "&bw=mid");

    updatevideourl(tempurl);
}

function updatevideourl(temptemp){
	videourl = temptemp;
}
	
function updateStatus(message) {
		
		//recieves messages from the flash and helps out double checking the state of the buttons and whatnot.
		if(message == "  NetStream.Play.Start"){videoplaying = true;changeObjectVisibility('v_pause', 'visible');changeObjectVisibility('v_play', 'hidden');}
		if(message == "  NetStream.Play.Stop"){videoplaying = false; changeObjectVisibility('v_pause', 'hidden');changeObjectVisibility('v_pause', 'visible');}

}
	
function metameta(message){
	//this function recieves the metadata. Basically this play is waiting for meta data before it does much of anything. 
	metadata = true;// <--- anounces the arrival of the metadata.
	
	var mindur;
	duration = parseInt(message); //global property duration. Referenced by many
	
	  if((message%60)<10){//This turns the duration into xx:xx form to display on the player
			  mindur = "0" + parseInt(message%60);
	  }
	  else{
		      mindur = parseInt(message%60);
	  }
	
	  durationformat = parseInt(message/60)+":"+(mindur);
	
	
	
	//for two kinds of cueing requests, metameta checks to see if we are waiting for metadata. If we were waiting it will complete these requests. 
	if(pixelswaiting){
		//this one checks for 'pixels waiting' that will be set by clicking on the trackbar before metadata is available. 
		//update xmoov has already been called. Infact we will rely on that to get the offset to determine the time.
		tryingfor = (xmoovoffset/fullwidth)*duration;
		seekTo(tryingfor);
	}
	
	if(tryingfor > 0){
		//this is checking to see if any specific times were requested before metadata came through. trying for is in seconds.
		updatexmoov((tryingfor/duration)*fullwidth);
		seekTo(tryingfor);
	}
	 
	
 }
 
 	
	
function timetime(message){
	//this is called from flash. The seconds are already in xx:xx format
	 $('#timetime').html(message + "/" + durationformat);	
 }

function bufferbuffer(part, total){
	 //this function is called over and over with the amount of bytes loaded and the total
	 bytestotal = total;
	 if(!metadata){
		 //until we hear from the flash about its metadata, we don't touch the buffer bar (it would be meaningless without the duration information...
		  $('#bufferbuffer').css("width", 0);
	 }
	 else{
	 //when metadata is available, we can update it comfortably
	 $('#bufferbuffer').css("width", (parseInt((part/total)*barwidth)));
	 $('#bufferbuffer').css("left", xmoovoffset);
	 $('#update').html('');
	 }
}

function alertalert(mes){
	 //this is used for recieving messages from flash and sending them to javascript alert... 
	 //commented out for production use...
	 
	 alert(mes);
}
 
function playplay(message){
  if (metadata && !($('#playhead').is('.ui-draggable-dragging')))
    {
	$('#playhead').css("left", ((message/duration)*fullwidth));
	}
}


function seekTo(time){ 
	
	//if(!metadata){
	if(!xmoov){
		flashVideoPlayer.seek(time);
	}
	else{
	
	tryingfor = time;
	//}
    var leftboundary = parseInt($('#bufferbuffer').css("left").slice(0, -2));
    var rightboundary = (parseInt($('#bufferbuffer').css("left").slice(0, -2)) + parseInt($('#bufferbuffer').css("width").slice(0, -2)));
		  
		  //alert(leftboundary + "<-->" + rightboundary);
		  //alert("seek to " + time);
		  //if((parseInt($('#playhead').css("left").slice(0, -2)) >= leftboundary) && (parseInt($('#playhead').css("left").slice(0, -2)) <= rightboundary)){
			  
			//  flashVideoPlayer.seek((time));
			 // alert("seek to" + time);
		  //}
		  //else{
		  if((tryingfor > 0) && metadata){
		  createvideourlxmoov(parseInt(time));
		  callFlashPlayVideoByTime(time);
		  }
		  else{
			callFlashPlayVideoByTime(0);  
		  }
		
		  }
	
	
	}
	

   

