//setup the namespace
if (typeof cn == "undefined" || !cn) {
	var cn = {};
}

/**
 * Finds all imagemap areas, preloads their images, and sets up their rollover observers
 */
Event.observe(window, 'load', function() {
	$$('map.hoverable area').each(function(area) {
		var img = $$('img[usemap="#'+area.up('map').id+'"]')[0];
			
		var imageObj = new Image(); //preload
		imageObj.src = area.style.backgroundImage.replace(/"/g,"").replace(/url\(|\)$/ig, "");
		
		var swap = function() {
			var temp = img.src;
			img.src = area.style.backgroundImage.replace(/"/g,"").replace(/url\(|\)$/ig, "");
			area.style.backgroundImage = 'url(' + temp + ')';
		};
		
		area.observe('mouseover', swap);
		area.observe('mouseout', swap);
	});
});

/**
 * Employer Preview: Forms the URL and trigger the modal open
 */
cn.showEmployerPreview = function(bossFirstname, firstname, lastname, message) {
	var url = siteurl + "send/previewemployer?bossFirstname=" + escape(bossFirstname) + "&firstname=" + escape(firstname) + "&lastname=" + escape(lastname) + "&message=" + escape(message);
	cn.openPreview(url);
};

/**
 * Friend Preview: Forms the URL and trigger the modal open
 */
cn.showFriendPreview = function(firstname, message, tourKey, city) {
	var url = siteurl + "send/previewfriend?firstname=" + escape(firstname) + "&message=" + escape(message) + "&tourKey=" + escape(tourKey) + "&city=" + escape(city);
	cn.openPreview(url);
};

/**
 * Opens a popup modal to show an email preview page in an iframe
 */
cn.openPreview = function(url) {
	this.divId = 'preview';
	this.iframeId = 'previewIframe';
	var winHeight = 585;
	var winWidth = 630;
	
	var html = '';	
	html += '<div id="' + this.divId + '" style="display:none;">';
	html += '	<iframe src="' + url + '" id="' + this.iframeId + '" marginheight="25" marginwidth="25" frameborder="0" scrolling="auto"></iframe>';
	html += '	<div style="height:35px; cursor:pointer;" onclick="$(\'' + this.divId + '\').hide().remove();" title="Close"></div>';
	html += '</div>';
	$('container').insert({ before:html });
	
	var top = (document.viewport.getDimensions().height/2) - (winHeight/2);
	var left = (document.viewport.getDimensions().width/2) - (winWidth/2);
	$(this.divId).setStyle({
		height: winHeight + 'px',
		width: winWidth + 'px',
		top: (top>0 ? top : 0) + 'px',
		left: (left>0 ? left : 0) + 'px',
		background: 'transparent url(/images/bg/email_preview.png) 0 0 no-repeat',
		position: 'absolute',
		zIndex: '99',
		display: 'block',
		lineHeight: '1em',
		padding: '60px 14px 0px 14px'
	});
	$(this.iframeId).setStyle({
		width: (winWidth - 28) + 'px',
		height: (winHeight - 60 - 46) + 'px'
	});
	if(typeof(DD_belatedPNG)!="undefined") DD_belatedPNG.fix('#' + this.divId);
}

	/**
	 * Logs an event described by the input parameters to Google Analytics.
	 *
	 * String   category The general event category (e.g. "Videos").
	 * String   actn The action for the event (e.g. "Play").
	 * String   opt_label An optional descriptor for the event.
	 * Int      opt_value An optional value to be aggregated with
	 */
cn.trackEvent = function (category, action, label, value) {
	if (typeof(pageTracker) != 'undefined') {
		try {
			var success = pageTracker._trackEvent(category, action, label, value);
			/*
			if (success)
				console.log("Event Logged: " + category + " - " + action + " / " + label);
			else
				console.log("Event logging FAILED");
			*/
		}
		catch (e) {}
	}
}

/**
 * Tracks a page view to Google Analytics.
 *
 * String   opt_pageURL Optional parameter to indicate what page URL to track metrics under.
 *			When using this option, use a beginning slash (/) to indicate the page URL.
 */
cn.trackPageview = function (pageUrl) {
	if (typeof(pageTracker) != 'undefined') {
		var host = window.location.protocol + "//" + window.location.host;
		if (pageUrl && pageUrl.length > host.length && pageUrl.substr(0, host.length) == host) {
			pageUrl = pageUrl.substr(host.length+1);
		}
		try {
			pageTracker._trackPageview(pageUrl);
			//console.log("Page tracked: " + pageUrl);
		}
		catch (e) { }
	}
}

/**
 * Facebook Share function called by Flash after the Tour is saved.
 */
function fbs_click(tourUrl) {
	var t = 'Commuter Nation Tour';
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(tourUrl)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}


/**
 * Free ride controls
 *
cn.FreeRideSweeps = Class.create({
	initialize: function() {
		this.currentId = null;
		Event.observe(window, 'resize', this.centerModal.bindAsEventListener(this));
		Event.observe(window, 'load', function() { this.preloadImg('/images/bg/sweeps_chooser.png'); }.bind(this));
	},
	
	openSweepsChooser: function() {	
		this.currentId = 'sweepsChooser';
		if($(this.currentId)) return;
		cn.trackPageview('Sweepstakes/Chooser');
		
		var html = '';	
		html += '<div id="' + this.currentId + '">';
		html += '	<div class="mainBg"></div>';
		html += '	<button></button>';
		html += '	<div class="closeBtn" onclick="$(\'' + this.currentId + '\').hide().remove();"></div>';
		html += '</div>';
		$('container').insert({ before:html });
		
		$(this.currentId).select('button').each(function(btn) {
			btn.observe('click', function() {
				$(this.currentId).hide().remove();
				this.openSweeps();
			}.bindAsEventListener(this));
		}.bind(this));
		
		this.centerModal();
		if(typeof(DD_belatedPNG) != "undefined") setTimeout("DD_belatedPNG.fix('#" + this.currentId + "');", 50);
		if(typeof(DD_belatedPNG) != "undefined") setTimeout("DD_belatedPNG.fix('#" + this.currentId + " button');", 50);
		
		this.preloadImg('/images/bg/sweeps_full.png');
	},
	
	openSweeps: function() {
		cn.trackPageview('Sweepstakes/FreeCommuting');
		this.currentId = 'sweepsContainer';
		var iframeId = 'sweepsIframe';
		var url = siteurl + 'sweeps/index';
		
		var html = '';	
		html += '<div id="' + this.currentId + '" style="display:none;">';
		html += '	<iframe src="' + url + '" id="' + iframeId + '" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>';
		html += '	<div class="closeBtn" onclick="$(\'' + this.currentId + '\').hide().remove();" title="Close"></div>';
		html += '</div>';
		$('container').insert({ before:html });
		
		this.centerModal();
		$(this.currentId).show();		
		if(typeof(DD_belatedPNG) != "undefined") setTimeout("DD_belatedPNG.fix('#" + this.currentId + "');", 50);
		
		this.preloadImg('/images/bg/sweeps_full_rightdone.png');
		this.preloadImg('/images/bg/sweeps_full_leftdone.png');
		this.preloadImg('/images/bg/sweeps_full_alldone.png');
	},
	
	closeModal: function() {
		$(this.currentId).hide().remove();
	},
	
	preloadImg: function(src) {
		var image = new Image();
		image.src = src;
	},
	
	centerModal: function() {
		if(!$(this.currentId)) return;
		var dim = $(this.currentId).getDimensions();
		var top = (document.viewport.getDimensions().height/2) - (dim.height/2);
		var left = (document.viewport.getDimensions().width/2) - (dim.width/2);
		$(this.currentId).setStyle({
			top: (top>0 ? top : 0) + 'px',
			left: (left>0 ? left : 0) + 'px'
		});
	}
	
});

var free_ride_sweeps = new cn.FreeRideSweeps();
cn.closeSweeps = function() { free_ride_sweeps.closeModal(); } //handle for iframe to use/call
cn.showSweepsForm = function() { free_ride_sweeps.openSweepsChooser(); } //handle for Flash to use/call

*/