
/* Popup Player : Open video in popup ------------------------- */

// Hack IE
/*
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	var loc = "../../";
} else {
	var loc = "";
}
function openVideoPopup(pageName, windowName, w, h) {
	// La popup "video.php" se retaille elle-même à 320 x 280
	var xPos = (window.screen.availWidth/2) - 160;
	var yPos = 120;
	var config = 'width='+w+', height='+h+', left='+xPos+', top='+yPos+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no';
	var w = window.open(loc+pageName, windowName, config=config);
}

*/


/**
 * videoPlayer.js
 *
 * @author Partikule
 * @copyright Copyright © 2006, Partikule, All rights reserved.
 *
 * This file is to be included on all you pages that include the videoPlayer.
 */
function VideoPlayer() {

	// Get document base directory path
	baseDir = document.location.href;
	if (baseDir.indexOf('?') != -1)
		baseDir = baseDir.substring(0, baseDir.indexOf('?'));

	baseDir = baseDir.substring(0, baseDir.lastIndexOf('/') + 1);
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
		this.documentBasePath = baseDir+"/../../";
	}
	else {
	    this.documentBasePath = baseDir
	}
};

VideoPlayer.prototype.getScriptPath = function() {
	var elements = document.getElementsByTagName('script');
	var baseDir = this.documentBasePath;

	for (var i=0; i<elements.length; i++) {
		if (elements[i].src && (elements[i].src.indexOf("videoPlayer.js") != -1 )) {
			var src = elements[i].src;
			src = this.convertRelativeToAbsoluteURL(baseDir, src.substring(0, src.lastIndexOf('/')));
			return src;
		}
	}
	return null;
};


/**
 * Opens a videoPlayer popup.
 */
VideoPlayer.prototype.openPopup = function(flvName) {

	// flvName (without extension)
	/*
	var flvNameElements = flvName.split("/");
	flvName = flvNameElements[flvNameElements.length-1];
	if ((pos = flvName.indexOf('.flv')) != -1) {
		flvName = flvName.substring(0,pos);
	}
	*/

	// Popup URL
	var url = this.getScriptPath() + "/videoPlayerPopup.php?a=b";
	if (typeof(flvName) != "undefined") {
		url += "&flvName=" + flvName;
	}
	alert(flvName);

	// Popup window name (video name)
	if ((pos = flvName.indexOf('~')) != -1) {
		var windowName = flvName.substring(0,pos);
	}

	// Popup
	var width =		320;
	var height =	280;

	var xPos = (window.screen.availWidth/2) - 160;
	var yPos = 120;
	var config = 'width='+width+', height='+height+', left='+xPos+', top='+yPos+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no';
	var w = window.open(url, windowName, config=config);

	if (w == null) {
		alert('Sorry, but we have noticed that your popup-blocker has disabled a window that provides application functionality. You will need to disable popup blocking on this site in order to fully utilize this tool.');
		return;
	}

	try {
		w.focus();
	} catch (e) {
	}

};
/**
 * Parses a URL in to its diffrent components.
 */
VideoPlayer.prototype.parseURL = function(url_str) {
	var urlParts = new Array();

	if (url_str) {
		var pos, lastPos;

		// Parse protocol part
		pos = url_str.indexOf('://');
		if (pos != -1) {
			urlParts['protocol'] = url_str.substring(0, pos);
			lastPos = pos + 3;
		}

		// Find port or path start
		for (var i=lastPos; i<url_str.length; i++) {
			var chr = url_str.charAt(i);

			if (chr == ':')
				break;

			if (chr == '/')
				break;
		}
		pos = i;

		// Get host
		urlParts['host'] = url_str.substring(lastPos, pos);

		// Get port
		lastPos = pos;
		if (url_str.charAt(pos) == ':') {
			pos = url_str.indexOf('/', lastPos);
			urlParts['port'] = url_str.substring(lastPos+1, pos);
		}

		// Get path
		lastPos = pos;
		pos = url_str.indexOf('?', lastPos);

		if (pos == -1)
			pos = url_str.indexOf('#', lastPos);

		if (pos == -1)
			pos = url_str.length;

		urlParts['path'] = url_str.substring(lastPos, pos);

		// Get query
		lastPos = pos;
		if (url_str.charAt(pos) == '?') {
			pos = url_str.indexOf('#');
			pos = (pos == -1) ? url_str.length : pos;
			urlParts['query'] = url_str.substring(lastPos+1, pos);
		}

		// Get anchor
		lastPos = pos;
		if (url_str.charAt(pos) == '#') {
			pos = url_str.length;
			urlParts['anchor'] = url_str.substring(lastPos+1, pos);
		}

		return urlParts;
	}

	return {path : '', port : '', host : '', query : '', anchor : ''};
};

VideoPlayer.prototype.convertRelativeToAbsoluteURL = function(base_url, relative_url) {
	var baseURL = this.parseURL(base_url);
	var relURL = this.parseURL(relative_url);

	if (relative_url == "" || relative_url.charAt(0) == '/' || relative_url.indexOf('://') != -1 || relative_url.indexOf('mailto:') != -1 || relative_url.indexOf('javascript:') != -1 || relative_url.replace(/[ \t\r\n\+]|%20/, '', 'g').charAt(0) == "#")
		return relative_url;

	// Split parts
	baseURLParts = baseURL['path'].split('/');
	relURLParts = relURL['path'].split('/');

	// Remove empty chunks
	var newBaseURLParts = new Array();
	for (var i=baseURLParts.length-1; i>=0; i--) {
		if (baseURLParts[i].length == 0)
			continue;

		newBaseURLParts[newBaseURLParts.length] = baseURLParts[i];
	}
	baseURLParts = newBaseURLParts.reverse();

	// Merge relURLParts chunks
	var newRelURLParts = new Array();
	var numBack = 0;
	for (var i=relURLParts.length-1; i>=0; i--) {
		if (relURLParts[i].length == 0 || relURLParts[i] == ".")
			continue;

		if (relURLParts[i] == '..') {
			numBack++;
			continue;
		}

		if (numBack > 0) {
			numBack--;
			continue;
		}

		newRelURLParts[newRelURLParts.length] = relURLParts[i];
	}

	relURLParts = newRelURLParts.reverse();

	// Remove end from absolute path
	var len = baseURLParts.length-numBack;
	var absPath = (len <= 0 ? "" : "/") + baseURLParts.slice(0, len).join('/') + "/" + relURLParts.join('/');
	var start = "", end = "";

	// Build start part
	if (baseURL['protocol'])
		start += baseURL['protocol'] + "://";

	if (baseURL['host'])
		start += baseURL['host'];

	if (baseURL['port'])
		start += ":" + baseURL['port'];

	// Build end part
	if (relURL['query'])
		end += "?" + relURL['query'];

	if (relURL['anchor'])
		end += "#" + relURL['anchor'];

	// Re-add trailing slash if it's removed
	if (relative_url.charAt(relative_url.length-1) == "/")
		end += "/";

	return start + absPath + end;
};

// Global instance
var videoPlayer = new VideoPlayer();


