pengag.createIfNotDefined(["pengag", "BuchDemoGlobal"], new Array());
pengag.createIfNotDefined(["pengag", "BuchDemoPacks"], new Array());
pengag.createIfNotDefined(["pengag", "v10"],{});



//if (typeof(pengag.v10) == "undefined")
//{
//	pengag.v10 = {};
//}

pengag.v10.CBuchDemoPackage = function(homePath)
{	
	var my = this;
	
	this.basePath = homePath;
	
	//Variables for direct access to the modules
	this.loader = null;
	this.adSourceProvider = null;
	this.cube = null;
	this.videoPlayer = null;
	this.controls = null;
	this.generator = null;
	
	//temporary
	this.flvUrl;
	this.products;
	
	//Loads the YouTubePlayer
	this.loadVideo = function()
	{
		//pengag.log("LOADING PLAYER");
		var module = my.loader.createModule();
		module.moduleReady = my.loadProvider;
		//module.moduleReady = my.connect;
		my.videoPlayer = module;
		module.loadModule("ag.peng.video.FLVPlayer", 1, 0);
	}
	
	this.loadProvider = function()
	{
		var module = my.loader.createModule();
		module.moduleReady = my.loadCube;
		my.adSourceProvider = module;
		module.loadModule("ag.peng.advertisements.sources.manual.ManualSourceProvider", 2, 0);
		//module.loadModule("ag.peng.advertisements.sources.produktvorschlag.ProduktvorschlagSourceProvider", 2, 0);
	}
	
	this.loadCube = function()
	{
		var module = my.loader.createModule();
		module.moduleReady = my.loadControls;
		my.cube = module;
		module.loadModule("ag.peng.advertisements.displays.Cube", 2, 0, "right");
	}
	
	this.loadControls = function()
	{
		var module = my.loader.createModule();
		module.moduleReady = my.loadGenerator;
		my.controls = module;
		module.loadModule("ag.peng.video.Controls", 2, 0, "bottom");
	}
	
	this.loadGenerator = function()
	{
		var module = my.loader.createModule();
		module.moduleReady = my.connect;
		my.generator = module;
		module.loadModule("ag.peng.core.StandardEmbedCodeGenerator", 2, 0);
	}
	
	//Connects VideoPlayer, Source and Display
	this.connect = function()
	{
		my.cube.callFunction("connect", my.adSourceProvider.pengId);
		//my.adSourceProvider.callFunction("connect", my.videoPlayer.pengId);
		my.controls.callFunction("connect", my.videoPlayer.pengId);
		my.generator.callFunction("connect", my.videoPlayer.pengId);
		my.internalLoadComplete();
	}	
	
	this.internalLoadComplete = function()
	{
		pengag.info("[BuchDemoPackage] internal load complete");
		//my.orderSourceProvider.callFunction('addSource', my.productId);
		my.videoPlayer.callFunction('loadVideo', my.flvUrl);
		pengag.info("[BuchDemoPackage] adding products");
		pengag.log(my.products);
		for(var i = 0; i < my.products.length; i++)
		{
			pengag.info("Loading product:");
			pengag.log(my.products[i]);
			my.adSourceProvider.callFunction('addSourceAsObject', my.products[i]);
		}
		
		my.adSourceProvider.callFunction('load');
		my.loadComplete();
	}
	
	this.loadComplete = function ()
	{
		
	}
}

pengag.v10.CBuchDemoPackage.targetContext = "core20";
pengag.v10.CBuchDemoPackage.targetPrefix = "v2.0";
pengag.v10.CBuchDemoPackage.targetSuffix = "-2.0";

/**
 * Loads the PengLoader.swf form the swfUrl into the HTML Element with the specified ID. Aditionally loads
 * the YouTubePlayer and the Recommender Module. You can access the loader, player and EpagesSourceProvider through the
 * returned object. They are accessible when the loadComplete function of this object is called.
 */
pengag.v10.CBuchDemoPackage.loadBuchDemoPackage = function (id, width, height, version, basePath, skinName, alternative, flvUrl, products, loadComplete)
{
	var loader = new pengag.core20.CPengLoader(basePath);
	var pack = new pengag.v10.CBuchDemoPackage(basePath);
	pack.loader = loader;
	pack.flvUrl = flvUrl;
	if((typeof loadComplete)!="undefined")
	{
		pack.loadComplete = loadComplete;
	}
	
	//set products
	
	pack.products = products;
	
	//create versionInfo
	//TODO: set product descirptors or wait until productid is used
	var packageInfoTmp =
	[
		"package", "BuchDemoPackage",
		"majorVersion", "1",
		"minorVersion", "0",
		"flvUrl", flvUrl,
		"products", products
		
	]
	var versionInfo = 
	{
		basePath      : basePath,
		versionPrefix : pengag.core20.versionPrefix,
		versionSuffix : pengag.core20.versionSuffix,
		width         : width,
		height		  : height,
		skinName	  : skinName,
		alternative	  : alternative,
		packageInfo   : packageInfoTmp
	}
	pengag.log("VERSION INFO");
	pengag.log(versionInfo);
	loader.versionInfoObject = versionInfo;
	
	pengag.BuchDemoPacks.push(pack);
	pengag.BuchDemoGlobal.push(loader);
	pengag.info("buchDemoGlobal: ");
	pengag.log(pengag.BuchDemoGlobal);
	pengag.log(pengag.BuchDemoGlobal[0]);
	loader.clientObject = "pengag.BuchDemoGlobal[" + (pengag.BuchDemoGlobal.length - 1) + "]";
	loader.loaderReady = pack.loadVideo;
	//pengag.log("initPeng");
	loader.initPeng(id, width, height, version, basePath + pengag.core20.versionPrefix + skinName + ".swf");
	
	return pack;
}

pengag.v10.CBuchDemoPackage.loadFromPengConfig = function (configElement)
{
	var spaceId = configElement.id;
	var alternative = $('p', configElement).html();
	var width = $('div.width', configElement).text();
	var height = $('div.height', configElement).text();
	var skinName = $('div.skinName', configElement).text();
	var flvUrl = $('div.flvUrl', configElement).text();
	
	//parse products
	
	var productsDiv = $('div.products', configElement);
	var products = new Array();
	$('div',productsDiv).each(
		function (i) 
		{
			var items = $(this).text().split(",");
			var product = 
			{
				pictureURL : items[0],
				title : items[1],
				shortDescription : items[2],
				longDescription : items[3],
				shopName : items[4],
				price : items[5],
				shippingString : items[6],
				currency : items[7],
				buyString : items[8],
				buyInstructionType : items[9],
				buyInstructions : items[10]
			}
			products.push(product);
	    });
	
	//products parsed
	
	var homePath;
	var homePathDiv = $('div.homePath', configElement).text();
	if(homePathDiv == "")
	{
		homePath = pengag.basePath;
	}
	else
	{
		homePath = homePathDiv;
	}
	
	pengag.log("USING PATH " + homePath);
	
	if(spaceId != "" && width != "" && height != "")
	{
		pengag.v10.CBuchDemoPackage.loadBuchDemoPackage(
			spaceId,
			width,
			height,
			"10.0.0",
			homePath,
			skinName,
			alternative,
			flvUrl,
			products);
		pengag.log("PARSED");
	}
	else
	{
		pengag.log("NOT PARSED");
	}
}