/*
 * Copyright (c) contentmetrics GmbH, 2008
 * THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
 * APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
 * HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
 * IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
 * ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
 *
 * Autor: Frank Räther, 04-03-2008
 * Code: JavaScript Library zum Tracken von generischen Events in Flash Applikationen
 * Kunde: KarstadtQuelle Versicherung
 * Version: V5, last changed 14.07.2008, F. Räther
 */

/*
 * The function captureFlashEvents should be called with appropriate values from out a flash app whenever
 * button clicks a/o player events shall be tracked down to e given tracking system. Current implementation supports
 * omniture sitecatalyst versions H.x. Support of other tracking tools can be adopted by changing the generic code
 * w/o changing anything within ActionScript of the Flash app's.
 * This Function should be called with the ActionScript function getUrl(...):
 * ...
 *	getUrl("javascript:captureFlashEvents(....)");
 * ...
 * The Script needs to be available in the current surrounding HTML page itself and must apply to the Flash
 * sandbox model.
 *
 * Parameters:
 * FlashName		-		Name of Flash File as String
 * FrameName		-		Name of a Frame if applicable, else empty String (!)
 * Sekunde		-		Time since start for Media as String, format should be something like '00:00'
 * EventBezeichnung 	-		Name of the Event as String
 * UseCookie		-		whether data should be stored in a cookie for the next page to
 *							submit data to the underlying tracking system as Boolean
 */
function captureFlashEvents(FlashName, FrameName, Sekunde, EventBezeichnung, UseCookie){
	if(typeof(cm_captureflash)=='function')cm_captureflash(FlashName, FrameName, Sekunde, EventBezeichnung, UseCookie);
}

/*
 * Configuration Settings
 * A List of names, determing the following transitions:
 * Omniture Object Name as String
 * Variable for FlashName
 * Variable for FrameName
 * Variable for Sekunde
 * Variable for EventBezeichnung
 * Event for counting EventBezeichnung
 * Name of the Cookie
 * Example: var cm_tfvs="s,eVar8,eVar9,eVar10,eVar11,event9,cm_captureflash";
 *  config:              s_object, file,  scene, time,  action, event,  cookiename
 * an empty cookiename will switch use of cookies off, capturing every single call,
 * regardless of Parameter 'UseCookie'
 */
var cm_tfvs="s,eVar8,eVar9,eVar10,eVar11,event9,cm_captureflash";

function cm_captureflash(flashfile,flashscene,flashtime,flashaction,usecookie){
	var debug=window.location.search.indexOf('cm_debug=true')>-1;
	if(debug)alert('cm_captureflash cm_tfvs='+cm_tfvs);
	var tfvs=cm_tfvs.split(',');
	var s_object=tfvs[0]+'.';
	if(s_object=='.')s_object='';
	var evarflashfile=tfvs[1];
	var evarflashscene=tfvs[2];
	var evarflashtime=tfvs[3];
	var evarflashaction=tfvs[4];
	var eventflash=tfvs[5];
	var cookiename=tfvs[6];
	var ex=s_object+evarflashfile+'="'+flashfile+'";';
		ex+=s_object+evarflashscene+'="'+flashscene+'";';
		ex+=s_object+evarflashtime+'="'+flashtime+'";';
		ex+=s_object+evarflashaction+'="'+flashaction+'";';
	if(flashaction){
		ex+='if(!'+s_object+'events){'+s_object+'events="'+eventflash+'";}';
		ex+='else if('+s_object+'events.indexOf("'+eventflash+'")==-1){'+s_object+'events+=","+"'+eventflash+'";}';
		if(debug)alert('cm_captureflash '+ex);
	}
	if(usecookie && cookiename){
		var cookieval=ex;
		ex=s_object+ "c_w('" +cookiename+ "','" +cookieval+ "',0);" ;
	} else {
		ex+=s_object+"t();";
	}
	if(debug)alert('cm_captureflash '+ex);
	eval(ex);
}
var cm_flashfromcookiecaptured=false;
function cm_captureflashfromcookie(){
	if(!cm_flashfromcookiecaptured){
		var debug=window.location.search.indexOf('cm_debug=true')>-1;
		var tfvs=cm_tfvs.split(',');
		var s_object=tfvs[0]+'.';
		var cookiename=tfvs[6];
		var cookieval="";
		ex="cookieval=" +s_object+ "c_r('" +cookiename+ "');";
		if(debug)alert('cm_captureflashfromcookie '+ex);
		eval(ex);
		if(debug)alert('cm_captureflashfromcookie '+cookieval);
		if(cookieval)eval(cookieval);
		cookieval="";
		ex=s_object+ "c_w('" +cookiename+ "','" +cookieval+ "',0);" ;
		if(debug)alert('cm_captureflashfromcookie '+ex);
		cm_flashfromcookiecaptured=true;
	}
}
