// JavaScript Document
// written by Alain Lechenet - all rights reserved
PLAYER_WIDTH=340; // width of the video player
PLAYER_HEIGHT=335; // height of the video player
PLAYER_DRAG_MIN=20; // defines the low bound of the dragable area
PLAYER_DRAG_MAX=270; // defines the high bound of the dragable area
var mouseX, mouseY, mouseXX, mouseYY; // X & Y position of the mouse

function winWidth(){ // return the width of the window
	if(window.innerWidth) return window.innerWidth; // netscape
	else if(document.body && document.body.offsetWidth) return document.body.offsetWidth; // IE
	else return 700; // other navigators 
}

function winHeight(){ // return the height of the window
	if(window.innerHeight) return window.innerHeight; // netscape
	else if(document.body.offsetHeight) return document.body.offsetHeight; // IE
	else return 500; // other navigators
}

function openPlayer(vid){ // open the flash player playing the "vid" video (display it in fact!)
	var s=vid.match(/([\w\d\.-\/]+\/)([\w\d-\/]+\.flv)/);
// s[1]=medias folder file path / s[2]= flv file name
	var to = new SWFObject(s[1]+"stonewashPlayer.swf", "videoClip", PLAYER_WIDTH, PLAYER_HEIGHT, "6", "#ffffff");
	to.addParam("quality", "high");
	to.addParam("wmode", "transparent");
	to.addParam("allowScriptAccess", "samedomain");
	to.addParam("loop", "false");
	to.addVariable("videoFile", s[2]);
	to.write("videoFlash");
	$("videoFlash").style.visibility="visible";
// the player is displayed as a floating draggable window - handling for drag event
	Event.observe($("videoFlash"), "mousedown", startDrag);
}

function startDrag(event){
	Event.stop(event);
	mouseX=Event.pointerX(event); // X and Y position of the mouse pointer 
	mouseY=Event.pointerY(event);
// a draggable area in the player is defined : from the PLAYER_DRAG_MIN px to the PLAYER_DRAG_MAX px of the height of the player
	if(((mouseY-playerTop)>=PLAYER_DRAG_MIN) && ((mouseY-playerTop)<=PLAYER_DRAG_MAX)){
		Event.observe($("videoFlash"), "mousemove", dragPlayer);
		Event.observe($("videoFlash"), "mouseup", stopDrag);
	}
}

function dragPlayer(event){ // l = Left position, t = Top position
	Event.stop(event);
	mouseXX=Event.pointerX(event);
	mouseYY=Event.pointerY(event);
// the next statement verifies that the player stays inside the window
	if((playerLeft+mouseXX-mouseX)>=0 && (playerLeft+PLAYER_WIDTH+mouseXX-mouseX)<=winWidth()){
		playerLeft+=mouseXX-mouseX;
		mouseX=mouseXX;
	}
	if((playerTop+mouseYY-mouseY)>=0 && (playerTop+PLAYER_HEIGHT+mouseYY-mouseY)<=winHeight()){
		playerTop+=mouseYY-mouseY;
		mouseY=mouseYY;
	}
	$("videoFlash").style.left=playerLeft+"px";
	$("videoFlash").style.top=playerTop+"px";
}

function stopDrag(event){ // when user release the left click after dragging the player
	Event.stop(event);
	Event.stopObserving($("videoFlash"), "mousemove", dragPlayer);
	Event.stopObserving($("videoFlash"), "mouseup", stopDrag);
}

function closePlayer(){ // close the player (hide it in fact!)
	$("videoFlash").style.visibility="hidden";
	$("videoFlash").update("<p>a flash player problem occured</p>");
	Event.stopObserving($("videoFlash"), "mousedown", startDrag);
}
