	/* Doubleclick tag constants */
var DoubleClickSettings = {
	// init path string constants
	
	//doubleclick image id
	id: 'doubleclick',
	
	//base url for doublelick hits
	url: 'http://ad.doubleclick.net/activity;',
	
	//source for tag, specific to sagatiba
	src: '1946933',
	
	//type tag
	type: 'sagat873',
	
	
	//these objects create links between doubleclick tags and real paths
	//as the controller (and google analytics) see them
	cats : [{
		// path and corresponding doubleclick category
		
		//root
		path: '/',
		
		//cat tag for root
		cat: 'sagat237'
	},
	
	{
		//sagas fall 08 section
		path: '/tv/fall_08',
		
		//corresponding cat tag for sagas fall 08
		cat: 'sagas417'
	},
	
	{
		
		// ...
		path: '/blog',
		cat: 'blogi105'
	},
	
	{
		path: '/events',
		cat: 'event408'
	},
	
	{
		path: '/drinks',
		cat: 'mixol055'
	},
	
	{
		path: '/venues/105',
		cat: 'locat231'
	},
	
	{
		path: '/spirit/sagatiba_pura',
		cat: 'spiri816'
	}]
};

var wordpress_root = 'wp';

/* Application code */
var RDApplication = Class.create({
	initialize: function(){
		this.loaded();
	},
	loaded: function(){
		if($('adland')){
			window.location = '/#/'+controller.getHash();
		}
		
		$$("a[rel=external]").each(function (node, i){
			node.observe('click', function(){
				node.target = "_blank";
			});
		});
		
		if($('ageform')){
			region_default = $F('region');
			document.observe('click', function(event){
				if(event.target.id == 'yes'){
					region = 'en-us';
					if($F('region') != region_default) region = $F('region');
					
					event.target.href += '&region=' + region + '&root='+window.location.pathname + '';
				}
			});
			
			
			this.centerElement($('ageform'));
			$('yes').href += '&content='+ controller.getHash();
		} else if ($('agecheck')) {
			
		
		
		} else {
			if($('body')){
				this.centerElement($('wrap'));
				this.sendGA(controller.getHash());
				document.observe('receiver:hashChange', function(event){
					event.stop();
					$('flash_content').receiver(controller.getHash());
					//var title_array = controller.pathArray().without('en-us', 'en', 'pt').invoke('capitalize').invoke('replace','_',' ').reverse();
					var title = controller.pathArray().without('en-us', 'en', 'pt').invoke('capitalize').invoke('replace','_',' ').reverse().join(' ‹ ');
					
					document.title = (controller.pathArray().length >= 1 && controller.pathArray().first().length > 0) ? title + ' ‹ Sagatiba Pura Cachaça' : 'Home ‹ Sagatiba Pura Cachaça';

					this.sendGA(controller.getHash());
				}.bind(this));	
				
			}
		}
	},
	sendGA: function(path){
		this.doubleClickTracking('/'+path);
		pageTracker._trackPageview('/'+path);
	},
	centerElement: function(el){
		Event.observe(window, 'resize', util.center.curry(el));
		util.center(el);
	},
	forwardAd: function(){
		//alert(controller.getHash());
	},
	
	doubleClickTracking: function(path){
		
		// short reference to settings
		var d = DoubleClickSettings;
		
		//random number
		var a = 10000000000000 * Math.random() + '';

		
		// if we are at a trackable page
					// this function searches the cats array and finds the cat tag
					// that matches the current path
		var cat = d.cats.find(function(c) { if(c.path == path) return c})

		if(cat){
			//builds image url
			
			var src = d.url + 'src=' + d.src + ';type=' + d.type + ';cat=' + cat.cat + ';ord=' + a + '?';

			if($(d.id)) {
				//image exists, change src
				$(d.id).src = src;
			} else {
				//no image, embed it
				$(document.body).insert('<img id="'+d.id+'" src="' + src + '" alt="Floodlight" />');
			}
			
			//make sure image is hidden and the correct size
			$(d.id).setStyle({
				width: '1px',
				height: '1px',
				border: '0',
				display: 'none'
			});
		}
	}
});

document.observe('dom:loaded', function(){
	controller = new RDController('');
		if($('flash_content')){
			
			var title = controller.pathArray().invoke('capitalize').invoke('replace','_',' ').reverse().join(' ‹ ');
			document.title = (controller.pathArray().length > 1) ? title + ' ‹ Sagatiba Pura Cachaça' : 'Sagatiba Pura Cachaça';
			
			var region = document.body.lang;
			var xmlRoot = '';//window.location.protocol + '//' + window.location.hostname + window.location.pathname + 'wp/' + region + '/';
			var flvRoot = '/';//window.location.pathname;
			var configFile = 'assets_intl/xml/' + document.body.lang + '/config/config.xml';//window.location.protocol + '//' + window.location.hostname + window.location.pathname + 'assets_intl/xml/' + document.body.lang + '/config/config.xml';
			
			swfobject.embedSWF('assets_intl/swf/sagatiba_loader_international.swf', 'flash_content', '968', '605', '8', false, {initialState: controller.getHash(), region: region, xmlRoot: xmlRoot, flvRoot: flvRoot, configFile: configFile }, { allowfullscreen: 'true', wmode: 'normal', allowScriptAccess: 'always', scale: 'noScale' }, {id: 'flash_content', name: 'flash_content'});
			sagatiba = new RDApplication();
		
		} else {
			
			// age check
			if($('agecheck')){
				var region = document.body.lang;
				var xmlRoot = '';//window.location.protocol + '//' + window.location.hostname + window.location.pathname + 'wp/' + region + '/';
	            var flvRoot = '/';//window.location.pathname;
	            var configFile = 'assets_intl/xml/' + document.body.lang + '/config/config.xml';//window.location.protocol + '//' + window.location.hostname + window.location.pathname + 'assets_intl/xml/' + document.body.lang + '/config/config.xml';
				var url = 'assets_intl/swf/ageVerification.swf?IPCountry=' + __lingua;
								
				swfobject.embedSWF(url, 'agecheck', '100%', '100%', '8', false, { region: "", configFile: "assets_intl/xml/common/ageConfig.xml" }, { allowfullscreen: 'true', wmode: 'normal', allowScriptAccess: 'always', scale: 'noScale' }, {id: 'agecheck', name: 'agecheck'});
				
			}
			sagatiba = new RDApplication();
		}
});