/*  SuperTag, version 1.0
 *  (c) 2009 Eelco van Kuik / Conversion Company Web Performance
 *
 *  SuperTag is proprietary software.
 *
 */

/***  
 *  Supertag
 *	
 *	type: object
 *  arguments: <none>
 *
 *	Masterclass that contains link to all other objects, web analytics packages and methods
 *
 */

function SuperTag () {
	
	this._p = new Array();
	this.domain=window.location.hostname;
	this.downloadExtensions="exe,zip,wav,mp3,mov,mpg,avi,wmv,doc,pdf,xls,csv";
	this.domains="";
	this.debug = false;
	
	//Associative array with query parameters and values	
	var qparams = false;
	
	//Reference to Web Analytics packages
	this.packages = {};
	
	//Contains the logline of Supertag
	this._log = '';
	
	/***
	 *  
	 *	_initialize()
	 *	
	 *	type: method
	 *  arguments: <none>
	 *
	 *	Initializer / constructor of Supertag. Usually resides at the top of all pages and contains all file inclusions.
	 *
	 */
	
	this._initialize = function () { 
		
	}

	/***
	 *  
	 *	submit()
	 *	
	 *	type: method
	 *  arguments: <none>
	 *
	 *	Sends data to all data packages. Is usually called at the bottom of HTML pages
	 *
	 */
	
	this.submit = function() {
		//Checks if all libraries are loaded
		if ( !this.isLoaded() ) return;
		//Identifies the pages
		this.identifier();
		//This function is included in an extra file
		this.controller();
		//Wrap it up for this page
		this.destructor();
	}
	
	/***
	 *  
	 *	isLoaded()
	 *	
	 *	type: method
	 *  arguments: <none>
	 *
	 *	Checks if all libraries are loaded
	 *
	 */
	 
	this.attempts = 0;
	this.isLoaded = function() {
		
		//Check if everything of the packages is loaded
		for ( pkg in this.packages ) {
			if ( !this.packages[ pkg ].isLoaded() ) {
				if ( this.attempts < 4 ) {
					setTimeout( '_supertag.submit()', 100 );
				}
				this.attempts++;
				return false;
			}
		}
		
		this.attempts = 0;
		return true;
	
	}
	
	/***
	 *  
	 *	controller()
	 *	
	 *	type: method
	 *  arguments: <none>
	 *
	 *	empty function for if somebody forgets it...
	 *
	 */
	
	this.controller = function() { }

	/***
	 *  
	 *	identifier()
	 *	
	 *	type: method
	 *  arguments: <none>
	 *
	 *	empty function for if somebody forgets it...
	 *
	 */
	
	this.identifier = function() { }
	
	/***
	 *  
	 *	include()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	1) URL to a JavaScript library to include, 
	 * 				2) String presentatie van de functie of object
	 * 				3) Type waaraan de String presentatie moet voldoen om te zien of de library niet reeds is ingeladen
	 *
	 *	Include functie voor externe JavaScript files
	 *
	 */
	
	this.include = function ( aHttpUrl, aSslUrl ) {
		
		var aSync = false;
		if ( typeof arguments[2] != 'undefined' ) {
			aSync = true;
		}
		
	    var aScriptTag = document.createElement('script');
	    aScriptTag.type = 'text/javascript';
	    aScriptTag.async = aSync;
    	aScriptTag.src = ('https:' == document.location.protocol ? aSslUrl : aHttpUrl );
	    var anotherScriptTag = document.getElementsByTagName('script')[0];
	    anotherScriptTag.parentNode.insertBefore( aScriptTag, anotherScriptTag );
		
	}

	/***
	 *  
	 *	write()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	1) a string with content
	 *
	 *	Writes the content to the Supertag frame with ID "supertag_iframe" if present otherwise returns false;
	 *
	 */
	
	this.write = function ( aString ) {
		/*
		var aIframe = this.getIframe();
		
		if ( aIframe ) {
			aIframe.document.write( aString );
			return true;
		} else {
			return false;
			//document.write( aString );
		}
		*/
	}
	
	/***
	 *  
	 *	_destructor()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 
	 *
	 *	Closes up supertag
	 *
	 */
	
	this.destructor = function () {
		/*
		var aIframe = this.getIframe();
		if ( this._iFrameIsOpen && aIframe ) {
			aIframe.document.close( );
			this._iFrameIsOpen = false;
		}
		*/
	}
	
	/***
	 *  
	 *	getEventLevel()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	1) Level number
	 *
	 *	Gets the event level and return ::empty:: if empty.
	 *  When the _supertag.event is set to i.e.: order/internet/step1, then 
	 *	level 0 = order, level 1 = internet, level 2 = step1
	 *
	 */
	
	this.getEventLevel = function ( aLevel ) {
		
		if ( typeof this.event != 'undefined' ) {
			var events = _supertag.event.split( '/' );
			if ( typeof events[ aLevel ] != 'undefined' ) {
				return events[ aLevel ];
			} else {
				return '::empty::';
			}
		}
		
		return '::empty::';
	}
	
	
	/***
	 *  
	 *	isTestDomain()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	<none>
	 *
	 *	Matches the current domain to the list of strings in the _supertag.testDomains property 
	 *
	 */
	this.testDomains = new Array( 'projects.local', 'localhost', '127.0.0.1', '192.168.', 'paul.local', 'tamtam.nl' );
	this.isTestDomain = function ( ) {
		
		//If no test domains are declared
		if ( typeof this.testDomains != 'object' ) { return false; }
		
		//From harddisk
		if ( this.domain.length == 0 ) { return true; }
		2
		for ( i = 0; i < this.testDomains.length; i++ ) {
			if ( this.domain.indexOf( this.testDomains[i] ) >= 0 ) {
				return true
			}
		}
		
		//Not in the list
		return false;
	}
	
	/***
	 *  
	 *	isIgnoreDomain()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	<none>
	 *
	 *	Matches the current domain to the list of strings in the _supertag.ignoreDomains property 
	 *
	 */
	this.denyDomains;
	this.isDenyDomain = function ( ) {
		
		//If no test domains are declared
		if ( typeof this.denyDomains != 'object' ) { return false; }
		
		for ( i = 0; i < this.denyDomains.length; i++ ) {
			if ( this.domain.indexOf( this.denyDomains[i] ) >= 0 ) {
				return true;
			}
		}
		
		//Not in the list
		return false;
	}
	
	/***
	 *  
	 *	isAllowDomain()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 	<none>
	 *
	 *	Matches the current domain to the list of strings in the _supertag.allowDomains property 
	 *
	 */
	this.allowDomains;
	this.isAllowDomain = function ( ) {
		
		//If no test domains are declared
		if ( typeof this.allowDomains != 'object' ) { return true; }
		
		for ( i = 0; i < this.allowDomains.length; i++ ) {
			if ( this.domain.indexOf( this.allowDomains[i] ) >= 0 ) {
				return true;
			}
		}
		
		//Not in the list
		return false;
	}
	
	/***
	 *  
	 *	getQueryParams()
	 *	
	 *	type: method
	 *	permissions: private
	 *  arguments: <none>
	 *
	 *	Populates the query parameter property list. Is executed only once.
	 *
	 */

	var getQueryParams = function ( ) {
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		var qsParm = {};
		for (var i=0; i<parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				var key = parms[i].substring(0,pos).toLowerCase();
				var val = parms[i].substring(pos+1).toLowerCase();
				qsParm[key] = val;
			}
		}
		
		qparams = qsParm;
	}
	
	/***
	 *  
	 *	getQueryParams()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 1) query parameter name
	 *
	 *	Returns the value of the query parameter
	 *
	 */
	 
	 this.getQueryParam = function ( aParamName ) {
		if ( typeof qparams == 'boolean' ) {
			getQueryParams();		
		}
		
		return qparams[ aParamName ];
	}
	
	/***
	 *  
	 *	getPagename()
	 *	
	 *	type: method
	 *	permissions: public
	 *  arguments: 
	 *
	 *	Returns the pageName, is overridden by _supertag.pageName
	 *
	 */
	 
	 this.getPagename = function ( ) {
		
		var aPagename = '';
		if ( typeof _supertag.pageName != 'undefined' ) {
			aPagename = new String( _supertag.pageName );
		} else {
			aPagename = window.location.pathname;
			aPagename = aPagename.replace( new RegExp("/", "g"),':');
			aPagename = aPagename.replace( /^:/, '' );
			aPagename = aPagename.replace( /:$/, '' );			
		}
		
		return aPagename;
	}
	
};


var _supertag = new SuperTag();


/**
 *
 * Yahoo Web Analytics plugin
 *
 * Reference to the object is: YWATracker 
 *
 * Public methods and variables:
 *
 *	account - contains the account number
 *
 * Versie: v0.2
 */ 

function SuperTagPlugin_YWA ( ) {
	
	var lib_location = 'http://d.yimg.com/mi/ywa.js';
	var ssl_location = 'https://s.yimg.com/mi/ywa.js';
	
	//Initialize
	_supertag.include( lib_location, ssl_location, false );
	
	this.isLoaded = function () {
		return typeof (window.YWA) != 'undefined';
	}
	
	this.submit = function () {
		
		if ( _supertag.isTestDomain() || _supertag.isAllowDomain() ) {
			window.YWATracker.submit();
		}
	}
	
	this.submit_action = function () {
		
		if ( _supertag.isTestDomain() || _supertag.isAllowDomain() ) {
			window.YWATracker.submit_action();
		}
	}
	
}

/* Example include in supertag config file: 

_supertag.packages.YWA = new SuperTagPlugin_YWA();

//And put in the top of the controller function the following line:

window.YWATracker = window.YWA.getTracker('10001585703000');

/********* End of Yahoo Web Analytics plugin *********************************************/

//Yahoo Web Analytics
_supertag.packages.YWA = new SuperTagPlugin_YWA();
_supertag.packages.YWA.account='1000751328942';


_supertag.controller = function () {
	
	window.YWATracker = YWA.getTracker( _supertag.packages.YWA.account );
	
	_supertag.allowDomains= ['.juridischloket.nl'];
	
	//BEPAAL EIGEN DOMEINEN
	_supertag.domains = '*.juridischloket.nl';
	YWATracker.setDomains( _supertag.domains );
	
	//PAGE NAME
	var leadingAndTrailing = new RegExp( "^\/|\/$", "g" );
	_supertag.pad = _supertag.pad.replace( leadingAndTrailing, '' );
	
	if ( !_supertag.pad ) {
		_supertag.pad = 'home';
	}
	
	YWATracker.setDocumentName( _supertag.pad );
	
	//Splitst het pad op basis van de / en stuurt een array terug
	var hierarchy = _supertag.pad.split( '/' );
	
	//vraag_en_antwoord/werk/arbeidscontract/hoe-lang-mag-uw-proeftijd-zijn
	
	//CONTENT HIERARCHIE
	//Yahoo WA
	if ( hierarchy[0] ) YWATracker.setDocumentGroup( hierarchy[0] ); //vraag_en_antwoord
	if ( hierarchy[1] ) YWATracker.setCF( 7, hierarchy[1] ); //werk
	if ( hierarchy[2] ) YWATracker.setCF( 8, hierarchy[2] ); //werk/arbeidscontract
	if ( hierarchy[3] ) YWATracker.setCF( 9, hierarchy[3] ); //werk/arbeidscontract/hoe-lang-mag-uw-proeftijd-zijn
	if ( hierarchy[4] ) YWATracker.setCF( 10, hierarchy[4] ); //werk/arbeidscontract/hoe-lang-mag-uw-proeftijd-zijn
		
	var submitAction = false;
	
	switch ( _supertag.getEventLevel( 0 ) ) {
		case "vraag_en_antwoord":
		
			switch ( _supertag.getEventLevel( 1 ) ) {
				case "vraag":
					YWATracker.setAction('06');
					
					//Vraag ID
					YWATracker.setSKU( _supertag.antwoord );
				break;
				case "subvraag":
					YWATracker.setAction('07');
			
					//Vraag ID
					YWATracker.setSKU( _supertag.antwoord );
					
					submitAction = true;
				break;
				case "vraag_stap":
					YWATracker.setAction('12');
			
					//Vraag ID
					YWATracker.setSKU( _supertag.antwoord );
					
					submitAction = true;
				break;
				case "vraag_situatie":
					YWATracker.setAction('13');
			
					//Vraag ID
					YWATracker.setSKU( _supertag.antwoord );
					
					submitAction = true;
				break;
				case "geholpen_ja":
					YWATracker.setAction('14');			
				break;
				case "geholpen_ja_verzonden":
					YWATracker.setAction('15');
				break;
				case "geholpen_nee":
					YWATracker.setAction('16');			
				break;
				case "geholpen_nee_verzonden":
					YWATracker.setAction('17');
				break;
				case "printen":
					YWATracker.setAction('08');		
				break;
			}			
		break;
		case "error":
			YWATracker.setDocumentName( 'Error ' + _supertag.error );
			YWATracker.setDocumentGroup( 'error' );
		break;
		case "kanaalsturing":		
			switch ( _supertag.getEventLevel( 1 ) ) {
				case "mail1":
					YWATracker.setAction('09');
				break;
				case "mail2":
					YWATracker.setAction('10');
				break;
				case "chat":
					YWATracker.setAction('03');
				break;
				case "loket":
					YWATracker.setAction('04');
				break;
				case "telefoon":
					YWATracker.setAction('05');
				break;
			}
		break;
		case "video":		
			switch ( _supertag.getEventLevel( 1 ) ) {
				case "start":
					YWATracker.setAction('18');
					submitAction = true;					
				break;
			}
		break;
		case "zoeken":
		
			YWATracker.setAction('INTERNAL_SEARCH');
			YWATracker.setISK( _supertag.getQueryParam( 'k' ) ); 
			YWATracker.setISR( _supertag.oss_r );
			
		break;		
	}
	
	//Check of de pagina als action of als page view moet worden verzonden
	if ( submitAction ) {
		_supertag.packages.YWA.submit_action();
	} else {
		_supertag.packages.YWA.submit();	
	}

}
