/*******************************************************************************
 * 
 * QlipsoPlayer.js
 * 
 *******************************************************************************
 * @copyright 	(c) 2010 qlipso 
 * 				All Rights Reserved.
 ******************************************************************************/
 
// -----------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------

var DEFAULT_URL     = "http://www.qlispo.com/share/QlipsoFrame.aspx?origin=qlipso" 
var DEFAULT_ACCOUNT = "qlipso.com";
var DEFAULT_WIDTH   = 900;
var DEFAULT_HEIGHT  = 640;

// -----------------------------------------------------------------------------
// Construction
// -----------------------------------------------------------------------------

/**
 * Creates a new QlipsoPlayer instance.
 */
function QlipsoPlayer()
{
    this.containerID = null;
    this.width = 0;
    this.height = 0;
    
	this.overrideFormURL = DEFAULT_URL;
	this.overrideDomain = DEFAULT_ACCOUNT;
	
	// If given, holds the details of an external user (which may include also shadow registration)
	this.userData = null;
	
	// If given, holds the initial media to be played when application is loaded
	this.initialMedia = null;
};

// -----------------------------------------------------------------------------
// Methods
// -----------------------------------------------------------------------------

/**
 * Loads the QlipsoClient application into a frame inside the defined
 * container element in the page.
 */
QlipsoPlayer.prototype.draw = function()
{
    var container = document.getElementById(this.containerID);
    if (!container) return;
    
	// Gather parameters for running Qlipso Client application
	//
	var params = {};
	var queryStrParams = this.parseQueryString();

    params['width'] = this.width;
    params['height'] = this.height;
	
	// if external user login - use its details
	if (this.userData) {
		params['userData'] = this.userData;
	}
	// otherwise, see whether automatic guest login
	else {
		if (queryStrParams.agl) params['agl'] = queryStrParams.agl;
		else if (this.agl)		params['agl'] = this.agl;
	}

	if (this.initialMedia)      params['initialMedia'] = this.initialMedia;

    if (this.overrideDomain)    params['overrideDomain'] = this.overrideDomain;

	if (queryStrParams.rid)     params['rid'] = queryStrParams.rid;
	else if (this.rid)          params['rid'] = this.rid;

    // Generate Frame to load the Qlipso Client application
    //
    var buf  = '';
    buf += '<iframe   name="qlipsoFrame"'+
                    ' allowtransparency="true"'+
                    ' frameBorder="no"'+
                    ' style="width:' + this.width + 'px;height:' + this.height + 'px;">';
    buf += '</iframe>';
	
	// generate the form for calling the Qlipso Client application    
	//
    buf += '<form id="form" method="POST" action="'+this.overrideFormURL+'" target="qlipsoFrame">';
    for (var p in params)
        buf +=   '<input type="hidden" name="'+p+'" value="'+params[p]+'"/>';
    buf += '</form>';
	
    // create the frame+form and submit the form to initiate application
    //
    container.innerHTML = buf;

    var form = document.getElementById('form');
    if (form) form.submit();
}

/**
 */
QlipsoPlayer.prototype.parseQueryString = function()
{
	var queryStr = location.search;

	if (queryStr.charAt(0) == "?")
		queryStr = queryStr.substring(1);
		
	var arrParts = queryStr.split("&");

    var params = {};
	for (var i=0,len=arrParts.length; i < len; i++) {
	    var eqpos = arrParts[i].indexOf('=');
	    if (eqpos > 0) {
	        var paramName = arrParts[i].substring(0, eqpos);
	        var paramValue = arrParts[i].substr(eqpos + 1);
	        
		    params[ paramName ] = paramValue;
		}
	}
	return params;
}
