/**
 * JS Library for Video Player Module
 *
 * This file contains all of the needed JavaScript functions
 * required by the video player module. There are 2 methods
 * for loading an embedded video player: AJAX (preferred), and
 * IFRAME. The AJAX method will allow cleaner integration, but
 * the IFRAME method will work on older browsers.
 *
 * @author Harold Asbridge
 * @package Video
 * @version 1.0
 */

/**
 * XMLHttpRequest global object
 */
var req;

/**
 * Video container element Id. Default is 'video_player'. This should be
 * a DIV or a SPAN, since it's innerHTML property will be used.
 */
var videoPlayerId = 'video_player';

/**
 * Video to display
 */
var videoId;

/**
 * Initiate remote request to shared/video.display.php, using XMLHttpRequest
 * @param {integer} videoId
 * @param {integer} width
 * @param {integer} height
 * @param {bool} autoplay
 */
function loadVideoPlayer(videoId, width, height, autoplay,template)
{
	height = height + 20;
	var url = BASE_HREF + '/shared/video.display.php?id=' + videoId + '&width=' + width + '&height=' + height + '&autoplay=' + autoplay + '&template='+template;

	// branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = updateVideoContent;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = updateVideoContent;
            req.open("GET", url, true);
            req.send();
        }
    }
    
    //hide category description
    if(document.getElementById('cat_desc'))
        document.getElementById('cat_desc').style.display = "none";
}

/**
 * Pop up a new window, with an embedded video player.
 * @param {integer} videoId
 * @param {integer} width
 * @param {integer} height
 * @param {bool} autoplay
 */
function popupVideoPlayer(videoId, width, height, autoplay)
{
	height = height + 20;
	var url = BASE_HREF + '/shared/video.display.php?id=' + videoId + '&width=' + width + '&height=' + height + '&autoplay=' + autoplay;
	var newWin = window.open(url, '', 'toolbar=0, menubar=0, statusbar=0, width=' + width + ', height=' + height);
	newWin.document.style.padding = '0';
	newWin.document.style.margin = '0';
}

/**
 * Pop up a new window, with an embedded video player in fullscreen.
 * @param {integer} videoId
 * @param {bool} autoplay
 */
function popupVideoFullScreen(videoId, autoplay)
{
    
    //Stop the current video playing
    loadVideoPlayer(videoId, 320, 180, "false", "");
    
    //Get window size
	//winW = screen.width;
	//winH = screen.height;
	
	winW = 720;
	winH = 405;

	var url = BASE_HREF + '/shared/video.display.php?id=' + videoId + '&width=' + winW + '&height=' + winH + '&autoplay=' + autoplay;
	var newWin = window.open(url, '', 'toolbar=0, menubar=0, statusbar=0, width=' + winW + ', height=' + winH);
}

/**
 * Update video player content on-the-fly, after receiving answer from XMLHttpRequest
 *
 */
function updateVideoContent()
{
    // only if req shows "complete"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
        {
            // Update video player
            var videoElement;
            if (videoElement = document.getElementById(videoPlayerId))
            {
            	videoElement.innerHTML = req.responseText;
            	//videoElement.innerHTML += "<a href=\"javascript:popupVideoFullScreen(" + videoId + ", false);\">Enlarge</a>";
            }
            else
            {
            	alert('Could not locate "video_player" element');
            }
        }
        else
        {
        	// Error
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

