/**
 * Java script wrapper used by external sites wishing to make a photoshow
 * @author nick
 * Copyright (c) Simple Star, Inc. 2007
 */

/**
 * First ensure that we have a namespace  
 */
if (typeof PhotoShow == "undefined") {
	PhotoShow={};
}

if (typeof PhotoShow.widgets == "undefined") {
	PhotoShow.widgets={};
}

/**
* Creates or extends a URL argument list by adding the passed argument.
* Appends &iArgName=urlencode(iValue);
*/
PhotoShow.widgets.addToUrlArgs = function(iExistingArgs, iArgName, iValue) {
	return iExistingArgs+'&'+iArgName+"="+encodeURIComponent(iValue);
};

/**
 * generate query string from iDevKey and psConfig
 * @param {Object} iDevKey
 * @param {Object} psConfig
 */
PhotoShow.widgets.getQueryString = function(iDevKey, psConfig) {
	var urlArgs = "?developerKey=" + iDevKey;
	for (var arg in psConfig) {
		urlArgs = PhotoShow.widgets.addToUrlArgs(urlArgs, arg, psConfig[arg]);
	}
	return urlArgs;
};

/**
 * Function which will launch a window in which the user can create a new PhotoShow.
 * iDevKey is a unique Developer Key assigned by Simple Star to each partner.
 * Other arguments are passed in via a psConfig object. Currently this includes:
 * 
 * sessionKey: a session key or similar authorization context. PhotoShow treats this as opaque and transient. 
 *             It will hand it back to you as part of the post callback (and any other calls to your servers 
 *             during the authoring session). (optional)
 * userId:     a unique user id (optional, but highly recommended). PhotoShow treats this as opaque and permanent. 
 *             It will use it to group together shows created by the same user. 
 * @param {Object} iDevKey
 * @param {Object} psConfig
 */
PhotoShow.widgets.launchMakePhotoShow = function(iDevKey, psConfig) {
	// window dimensions
	var top=(screen.height-550)/2;
    var left=(screen.width-600)/2;
	
	// construct widget url
	var widgetUrl = "http://www.photoshow.com/widgets/landing" + PhotoShow.widgets.getQueryString(iDevKey, psConfig);
	
	// open the widget window
	window.open(widgetUrl, "Create", "width=600, height=550, left=" + left + ",top=" + top);
};

/**
 * Function which will embed an IFRAME into a page in which the user can create a new PhotoShow.
 * iDevKey is a unique Developer Key assigned by Simple Star to each partner.
 * Other arguments are passed in via a psConfig object. Currently this includes:
 * 
 * sessionKey: a session key or similar authorization context. PhotoShow treats this as opaque and transient. 
 *             It will hand it back to you as part of the post callback (and any other calls to your servers 
 *             during the authoring session). (optional)
 * userId:     a unique user id (optional, but highly recommended). PhotoShow treats this as opaque and permanent. 
 *             It will use it to group together shows created by the same user. 
 * @param {Object} iDevKey
 * @param {Object} psConfig
 */
PhotoShow.widgets.embedMakePhotoShow = function(iDevKey, psConfig) {
	
	// construct widget url
	var widgetUrl = "http://www.photoshow.com/widgets/addphotos" + PhotoShow.widgets.getQueryString(iDevKey, psConfig);
	var iFrameHtml = "<iframe src='" + widgetUrl + "' id='psWidget' name='psWidget' width='600' height='550' frameborder='0' vspace='0' hspace='0' marginwidth='0' marginheight='0' scrolling='no'></iframe>"
	
	// write widget
	if( psConfig != null && psConfig.divId != null ) {
		// get the div where we write the iFrame
		var widgetDiv = document.getElementById(psConfig.divId);
		widgetDiv.innerHTML = iFrameHtml;

	} else {
		var documentBody = document.body;
		documentBody.innerHTML = documentBody.innerHTML + iFrameHtml;
	}

}; 