/*
	Please refers to documentation here : http://api.adsme.com/api/js/rss/docs/adsme.js/index.html
*/
var _adsmeServerURL = "http://api.joliprint.com";
var _adsmeBtnImgRoot = "/api/img/paidcontent.org/";
var _adsmeBtnImg = "adsme_btn_default.png";
var _adsmeBtnLabel = "Print (PDF)";
var useMiva = true;
var adsmeButtons = new Array(
	{ id:"DEFAULT", url:"adsme_btn_default.png" },
	{ id:"SMALL_RED", url:"adsme_btn_small_red.jpg" }
);
/**
* AdsmeConfig constructor (mandatory)
* @constructor
*/

function AdsmeConfig(){
	/**@private*/
	this.feedUrl = null;
	/**@private*/
	this.serverUrl = _adsmeServerURL;
	//this.serverUrl = (options != null && options.serverUrl != null) ? options.serverUrl : _adsmeServerURL;
	/**@private*/
	this.adsmeBtnImg = "DEFAULT";
	/**@private*/
	this.adsmeBtnLabel = _adsmeBtnLabel;
	/**@private*/
	this.labelBefore = false;
}

/**
* Set the Feed URL (mandatory).
* @param {String} feedUrl the URL of your Atom Feed. Adsme will connect to this URL
*/
AdsmeConfig.prototype.setFeedUrl = function ( feedUrl ){
	this.feedUrl = feedUrl;
}

/**
* Set the global images buttons to display
* @param {String} btn the button name (DEFAULT / SMALL_RED / etc ...)
*/
AdsmeConfig.prototype.setButton = function ( btn ){
	this.adsmeBtnImg = btn;
}

/**
* Set the global label of the buttons and their positions
* @param {String} label the label text to display
* @param {Boolean} before the position of the label (true=> before / false => after). Default value is after (false)
*/
AdsmeConfig.prototype.setLabel = function ( label, before ){
	if (label == null) var label = "";
	if (before == null) var before = false;
	this.adsmeBtnLabel = label;
	this.labelBefore = before;
}
/**
* Get the URL of the image button
* @private
*/
AdsmeConfig.prototype.getButtonUrl = function ( btn ){
	if (btn == null) var btn = "DEFAULT";
	btn = btn.toUpperCase();
	var url = null;
	for( var i = 0 ; i < adsmeButtons.length; i++ ){
		if (adsmeButtons[i].id == btn){
			url = adsmeButtons[i].url;
			continue;
		}
	}
	if (url == null) url = adsmeButtons[0].url;
	url = this.serverUrl + _adsmeBtnImgRoot + url;
	return url;
}

/**
* Displays the Adsme button on your website
* @param {Object} config your AdsmeConfig
* @param {String} entryID the entryId (used to define the final URL to call on your website)
* @param {String} entryURL the entryUrl (used to retrieve the entry in your feed)
* @param {Object} options used to define specific options to this button (image / label text / label position / PDF behaviour)
* 
* options is a set of key/value pairs. All options are optionnal :
* btnImg (used to defined a different image for the button)<br/>
* btnLabel, used to define a different label text for the button<br/>
* labelBefore, used to define a different position for the label<br/>
* disposition, used to define the Content Disposition of the generated document. Accepted values : inline|attachment (default : inline)
* feedEntry, used to define the feed entry you want to print form the feed if your feed contains more than 1 entry.
* <br/>
* Examples : <br/>
*<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{ btnImg:'DEFAULT', btnLabel:'Print now !'}); // uses the image DEFAULT with label "Print now ! "<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{ btnImg:'SMALL_RED', btnLabel:'Print with Adsme !'}); // uses the image SMALL_RED with label "Print with Adsme !"<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{ btnLabel:'Print with Adsme !'}); // uses the default global image with label "Print with Adsme !"<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{ btnLabel:'Print', labelBefore:true}); // uses the default global image with label "Print" positionned before the image<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{btnImg:'SMALL_RED'}); // used the image SMALL_RED with global label text after the image<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}',{labelBefore:true}); // uses the default image and label with text before the image<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}', {disposition:'attachment'}); // uses the default values and download the PDF (the PDF won't be shown in the browser)<br/>
* adsme_sb(adsmeConfig,{entry_id}, '{url_title_path=site/index}'); // uses the default values<br/>
*/
//function adsme_sb(config,entryID,entryURL,options){
/*
function adsme_sb(feedUrl, feedEntry, options){
	if (options == null) var options = {};
	if (feedEntry != null) options.feedEntry = feedEntry;
	
	adsme_sb_direct(feedUrl, options);
}
*/
//function adsme_sb_direct(feedUrl, options){
function adsme_sb(feedUrl, options){

	var btnImg = config.adsmeBtnImg;
	if (options != null && options.btnImg != null) btnImg = options.btnImg;
	var btnImgUrl = config.getButtonUrl( btnImg );
	
	var btnLabel = config.adsmeBtnLabel;
	if (options != null && options.btnLabel != null) btnLabel = options.btnLabel;
	
	var feedEntry = null;
	if (options != null && options.feedEntry != null ) feedEntry = options.feedEntry;
	
	//var au = config.serverUrl + "/api/rss/?url=" + feedUrl + "&psrc=" + encodeURIComponent( feedEntry );
	//var au = config.serverUrl + "/api/rss/?url=" + feedUrl;
	var au = config.serverUrl + "/api/rest/rss/print?url=" + feedUrl;
	if (feedEntry != null ) au += "&psrc=" + encodeURIComponent( feedEntry );
	au += "&btn=" + encodeURIComponent( btnImg ) + "&label=" + encodeURIComponent( escape( btnLabel ) );
	
	if (useMiva == true){
		au = config.serverUrl + "/api/adservices/get.jspz?redirect=" + encodeURIComponent(au);
	}
	
	var disposition = null;
	if (options != null && options.disposition != null) disposition = options.disposition;
	
	if ( disposition != null){
		au += "&disposition=" + disposition;
	}

	var labelBefore = config.labelBefore;
	if (options !=null && options.labelBefore != null) labelBefore = options.labelBefore;
	if (options !=null && options.labelBefore != null) labelBefore = options.labelBefore;
	var label = "<a href='javascript:adsme_pr( \"" + au + "\" )'>" + btnLabel + "</a>";
	if ( btnLabel != null && btnLabel.length > 0 && labelBefore == true ) document.write( label + "&nbsp;" );
	document.write( "<a href='javascript:adsme_pr(\"" + au + "\")'><img border='0' src='" + btnImgUrl + "?entryURL=" + encodeURIComponent(feedEntry) + "' alt='" + escape( btnLabel ) + "' /></a>" );
	if ( btnLabel != null && btnLabel.length > 0 && labelBefore != true ) document.write( "&nbsp;" + label );
}
/**
* Calls the Adsme print process
* @private
*/
function adsme_pr(au){
	var w = window.open( au );
}

var config= new AdsmeConfig();