
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Cobweb <typo3@cobweb.ch>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ************************************************************** */

/**************************************************************
 * Prepares the load of content provided by typo3 on an foreign server.
 * @param  string url		Url of the page providing the content code (without slash at the end and no protocol instruction) 
 * @param  string ctype 	Content type (can be 'records' or 'lib')
 * @param  string cvar  	Content id (can be the uid of the tt_content loaded or the name of the library)
 * @param  string rtype 	Render type (by default '4653')
 * @param  string target 	Id of the DOM element that will be replaced by the loaded content code
 * @param  string callback 	JS function to call once the code is loaded
 *
 * @example c2s('www.mysite.com/fr','lib','cw_topbar');
 * @example c2s('www.mysite.com/fr','lib','cw_topbar',2235,'mytopbar','My_Function_To_Call');
 *
 * @return void
 ************************************************************** */

function c2s(url, ctype, cvar, rtype, target, callback, t3_jslib) {
	var currentUid;
	var cBack;
	// Url
	if (typeof url != "undefined" && url != null) url = (location.protocol=='https:'?'https://' + url :'http://' + url ) + "/";
	else return;
	// render type id
  	if (typeof rtype == "undefined" || rtype == null) rtype = "4653";

	// render jsLibType
  	if (typeof t3_jslib == "undefined" || t3_jslib == null) t3_jslib = "jquery";
	
	if (typeof target == 'undefined' || target == null) {
		currentUid = 'c2s_' + Math.floor(Math.random()*99999999999);
		// Outputting the container
		document.write("<div id='" + currentUid + "' class='c2s_loadingmsg'>loading ...</div>");
	} else {
		currentUid = target;
	}
	
	// Calback after container is updated
  	if (typeof callback != "undefined" && callback != null) {
		cBack = '&t3_cback=' + callback;
	}
	else {
		cBack = '';
	}
	
	c2sUid.push(currentUid);
   	//c2sUrl.push(url + "t3rtype/" + rtype + "/" + "t3ctype/" + ctype + "/" + "t3cvar/" + cvar + "/t3cuid/" + currentUid + "/" + cBack);

	c2sUrl.push(url + "?type=" + rtype 
				+ "&t3_ctype=" + ctype  
				+ "&t3_cvar=" + cvar 
				+ "&t3_cuid=" + currentUid
				+ "&t3_jslib=" + t3_jslib
				+ cBack);

}


/***************************************************************/
// Observer that lauches the load of every content 
// element once the DOM is loaded

jQuery(document).ready(function() {
	var currentUid = null;
	var currentUrl = null;

	// Reversing order of elements to generate
	c2sUid.reverse();
	c2sUrl.reverse();
	
	// Getting all contents and placing them in the DOM
	while(c2sUid.length > 0) {
		currentUid = c2sUid.pop();
		currentUrl = c2sUrl.pop();		
		var script = jQuery('<script></script>').attr({ type: 'text/javascript', src: currentUrl });		
		jQuery('#'+currentUid).append(script);	       	
	}
});

/***************************************************************/
// Declaring some global vars
var c2sUid = new Array();
var c2sUrl = new Array();
