if ( ! window.nl ) {
	window.nl = {} ;
} ;
if ( ! window.nl.ing ) {
	window.nl.ing = {} ;
} ;

window.nl.ing.Brandbox = function(){
	this.initialize();
}

window.nl.ing.Brandbox.prototype = {
	initialize: function(){
		var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
		if( playerVersion.major >= 8 ){
			this.showSite();
		} else {
			this.showPluginMessage();
		}
	}, 
	showSite: function(){
		//var path = location.href ;
		var path = "" ;
		//path = path.split( '/' ) ;
		//path.splice( path.length-1, 1 );
		//path = path.join( '/' ) + '/';
		
		var freqCapPos = this.getFreqCapPos( window.freqCapId, Number(window.freqCapDays), Number(window.freqCapViews) );
		// basic variables used to control brandbox
		var flashvars = {
			path:path,
			btOnOff:window.btOnOff,
			omnitureOnOff:window.omnitureOnOff,
			startIndex:freqCapPos,
			brandboxLoop:window.brandboxLoop,
			pageName:window.omniturePageName,
			koersenXmlUrl:window.koersenXmlUrl,
			brandboxXmlUrl:window.brandboxXmlUrl,
			omnitureAccount:window.omnitureAccount,
			omniturePageName:window.omniturePageName,
			btXMLUrl:window.btXMLUrl,
			brandboxVideoFile:window.brandboxVideoFile,
			brandboxVideoSkinFile:window.brandboxVideoSkinFile
		};
		var params = {
		  menu: "false",
		  allowScriptAccess: "always"
		};
		var attributes = {
		  id: "brandbox_swf",
		  name: "brandbox_swf"
		};
		swfobject.embedSWF( window.brandboxSwfFile, "brandbox", 940, 245, "8.0.0", false, flashvars, params, attributes );
	},
	showPluginMessage: function(){
		// show div containing message or show animated alternative gif
		var noflash = document.getElementById( "noflash" );
		noflash["className"] = "";
	},
	getFreqCapPos: function( freqCapId, freqCapCookieDays, freqCapCookieViews ){
		var freqCap = getCookie( "brandbox"+freqCapId );
		if( freqCap == null ){
			setCookie( "brandbox"+freqCapId, 1, Number( freqCapCookieDays ) );
			$counter = 1;
		} else {
			setCookie( "brandbox"+freqCapId, Number(freqCap)+1, Number( freqCapCookieDays ) );
			$counter = Number(freqCap)+1;
		}
		if( $counter <= Number( freqCapCookieViews ) ){
			return 0;
		} else {
			return ( $counter %  2 ) + 1; // 1 or 2
		}
	}
};

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function launchBrandBox(){
	new window.nl.ing.Brandbox();
}function Brandbox() {
//dummy function for backward compatibility
}
