//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);
};

/**
 * Show the Sweepstakes form
 */
cn.showSweepsForm = function() {
	//wrapped in setTimeout for IE6 loading bug
	setTimeout("cn.openSweeps(siteurl + 'sweeps/index');", 100);
}

/**
 * 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);
}

/**
 * Opens a popup modal to show an email preview page in an iframe
 */
cn.openSweeps = function(url) {
	this.divId = 'sweepsContainer';
	this.iframeId = 'sweepsIframe';
	var winHeight = 775;
	var winWidth = 700;
	
	var html = '';	
	html += '<div id="' + this.divId + '" style="display:none;">';
	html += '	<iframe src="' + url + '" id="' + this.iframeId + '" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"></iframe>';
	html += '	<div style="position:absolute; top: 30px; right: 5px; height:60px; width: 28px; 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: '675px',
		top: (top>0 ? top : 0) + 'px',
		left: (left>0 ? left : 0) + 'px',
		background: 'transparent url(/images/bg/sweepstakes_outer.png) 0 0 no-repeat',
		position: 'absolute',
		zIndex: '99',
		display: 'block',
		lineHeight: '1em',
		padding: '25px 0 0 25px'
	});
	$(this.iframeId).setStyle({
		width: '628px',
		height: '718px'
	});
	if(typeof(DD_belatedPNG)!="undefined") setTimeout("DD_belatedPNG.fix('#sweeps');", 50);
}

cn.closeSweeps = function() {
	$('sweepsContainer').hide().remove();
}

	/**
	 * 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;
}

function sweepbuttonClick() {
	cn.showSweepsForm();
	cn.trackPageview('Sweepstakes');
}

cn.centerSweeps = function() {
	if(!$('sweeps')) return;
	var top = (document.viewport.getDimensions().height/2) - (775/2);
	var left = (document.viewport.getDimensions().width/2) - (700/2);
	$('sweeps').setStyle({
		top: top+"px",
		left: left+"px"
	});
}

Event.observe(window, 'resize', cn.centerSweeps);