

/*
	Remove the Iframe's subdomain and set document.domain
	
	This works with a few assumptions:
		1. The iframe's domain is subdomain.domain.x
		2. The surrounding page's domain is domain.x or subdomain2.domain.x
		3. The surrounding page contains the JavaScript line 
			document.domain = 'domain.x';
	
	That line is necessary in combination with the following line to indicate 
	to the browser that the iframe is allowed to access the surrounding page's 
	Omniture object.
*/
try{
	document.domain = document.domain.replace(/^.*?(nivea)/i, '$1');
	// e.g. document.domain = 'domain.x';
} catch(e){};





/*
	Omniture placeholder script for iframe-based developments
	
	Usage:
	In the Iframe page, adapt and include the following code:

	<script src="http://www.yourniveadomain.tld/js/nivea_omniture_iframe.js"></script>
	<script>
		Omniture.trackSubPageView('/subpageview/path');
	</script>	

	To enable the debugging mode and output information to the browser's console,
	add the follwing line to the tracking code:
	(Until the parent's Omniture object is available)

	<script src="http://yourniveadomain.tld/js/nivea_omniture_iframe.js"></script>
	<script>
		Omniture.debug();
		Omniture.trackSubPageView('/subpageview/path');
	</script>		
	
*/
var Omniture = (function(){


	// Basic options
	var delay		= 250,			// Check for parent's Omniture every 250ms..
		tries		= 40,			// ..and repeat 40 times
		queue		= [],			// Tracking calls are stored here until parent's Omniture is ready
		debugging	= false,		
		failed		= false;
	
	
	
	// These are the methods that are set up as placeholder
	// methods of the Omniture API object
	var methods = [
		"startVideo", "endVideo", "reset",  "track", 
		"trackNOPV", "trackEvent", "trackCustomEvent", "trackLink", 
		"trackExternalLink", "trackDownloadLink", 
		"trackSubPageView", "setProduct", "setErrorPage"
	];
	
	
	// This method is also exposed to the Omniture object and enables debugging
	var debug = function(){
		debugging = true;
		log("Debugging enabled");
	}
	
	
	
	
	// Tracking calls that are initiated before the surrounding page's
	// Omniture API has finished are enqueued for later
	var enqueue = function(name, args){
		if(failed){
			log("Ignored Omniture." + name + "('" + args.join("', '") + "')");
		}
		else{
			queue.push({
				name: 	name,
				args: 	args
			});
			log("Queued Omniture." + name + "('" + args.join("', '") + "')");
		}
	}
	
	
	// Once the surrounding page's Omniture API is set up, all queued
	// tracking calls are flushed and sent to the API
	var flush = function(){
		var length = queue.length;
		for(var i = 0; i < length; i++){
			var method = queue[i];
			if(Omniture[method.name]){
				Omniture[method.name].apply(Omniture, method.args);
				log("Executed Omniture." + method.name + "('" + method.args.join("', '") + "')");
			}
		}
		
		log("Finished flushing early tracking calls (" + queue.length + ")");
		queue = [];
	}
	
	
	// Access to browser log if debugging is enabled
	var log = function(string){
		debugging && console && console.log && console.log("Omniture Iframe API: " + string);
	}
	

	// Wait for surrounding page to set up the Omniture API
	// and replace the placeholder object with it.
	var init = function(){
		
		var counter = 1;
		
		(function(){
		
			if(parent.location == location){
				log('Quit accessing parent because this is not an iframe');
				failed = true;
				return false;
			}
		
			if(counter++ > tries){
				log('Parent\'s Omniture not ready after ' + (Math.round(tries*delay/100)/10) + ' seconds');
				log(queue.length + ' queued and all future tracking calls are ignored');
				failed = true;
				return false;
			}
		
			try {
				if(parent.Omniture){
					
					log('Parent\'s Omniture ready and placeholder replaced');
					Omniture = parent.Omniture;
					
					// Enabling debugging after the replacement has happened would otherwise 
					// result in a error because the parent's Omniture has no debug() method.
					if(Omniture.debug == undefined){
						Omniture.debug = function(){};
					}
					
					// If possible, the parent's SWFAddress is imported into the iframe as well.
					if(parent.SWFAddress){
						log('Parent\'s SWFAddress ready');
						SWFAddress = parent.SWFAddress;
					}
					
					flush();
					
					log('Debug logging will stop at this point.');
					
					return
				}
			} catch(e){};
		
			window.setTimeout(arguments.callee, delay);		
		}());
		
		// Make sure init() is only called once!
		self.init = function(){};
	}

		
	// This object will be extended with placeholder methods
	// and exposed via the Omniture variable
	// (Until the parent's Omniture is ready)
	var self = {
		debug: 	debug,
		init: 	init
	}
	
	
	// Set up placeholder tracking functions
	for(var i = 0; i < methods.length; i++){
		self[ methods[i] ] = (function(method){
			return function(){
				enqueue(method, Array.prototype.slice.call(arguments, 0));
			}
		}(methods[i]));
	}
	

	// Turn on debug mode if parent window has debug parameter
	if (parent.location.href.match(/apidebug=(1|true)/)) {
		debug();
	}

	
	// The placeholder functions are exposed as 
	// methods of the Omniture object
	return self;
	
}());


/* 
	This prevents SWFAddress inside Flash from failing
	until this has been replaced by the parent's SWFAddress object
*/
var SWFAddress = null;



Omniture.init();



