/******************************************
 * The Screen Manager class
 ******************************************/
//var isIe = Prototype.Browser.IE && /MSIE (5\.5|6\.|7\.)/.test(navigator.userAgent);

onepica.ScreenManager = Class.create({
    initialize: function() {
		this.initNav();
		this.initPrototips();
		this.originalPageTitle = document.title;
    },

	/**
	 * Initializes the navigation, positioning the subnav ULs.
	 */
	initNav: function () {
		this.nav = $('nav');
		this.nav.childElements().each(function (navelm) {
			Event.mouseEnter(navelm, function() {
				//if it's the current nav, don't do anything
				if(navelm.hasClassName('active')) return;
				//turn on shim
				$('nav-shim').setStyle({display: "block"});
			});
			Event.mouseLeave(navelm, function() {
				//if it's the current nav, don't do anything
				if(navelm.hasClassName('active')) return;
				//turn off shim
				$('nav-shim').setStyle({display: "none"});
			});
		});
		this.nav.select('ul.level0').each(function (subnav) {
			subnav.observe('mouseout', function () {
				setTimeout("Cufon.refresh('#nav li a')", 3);  // cufon bug, trigger refresh to unhighlight
			});
			var parent = $(subnav.parentNode);
			var lis = subnav.select('li.level1');
			var ulWidth = 0;
			for (i = 0;  i < lis.length; i++) {
				ulWidth += lis[i].getWidth();
			}
			//ulWidth += parseInt(subnav.getStyle('paddingLeft')) + parseInt(subnav.getStyle('paddingRight'))

			var ulOffset = (ulWidth / 2) - (parent.getWidth() / 2);
			var liOffset = parent.offsetLeft - parent.parentNode.offsetLeft;
			if (ulOffset > liOffset) {  // don't let the subnav go past the beginning of the main nav
				ulOffset = liOffset - 12;  // couple px of padding.
			}
//			alert(
//				'ul width: ' + ulWidth + "\n" +
//				'li offset: ' + liOffset + "\n" +
//				'ul offset: ' + ulOffset + "\n"
//			);
			subnav.setStyle({width: ulWidth + 'px', left: (ulOffset * -1) + 'px'});
		});
		this.nav.addClassName('nav-loaded');  // hides the subnav uls
		
	},

	appendToPageTitle: function (append) {
		document.title += " - " + append;
	},

	restorePageTitle: function () {
		document.title = this.originalPageTitle;
	},

	/**
	 * Initializes the Prototip styles and open links around the site.
	 */
	initPrototips: function () {
		// add prototips to links with class 'open-tip'
		$$('a.open-tip').each(function (el) {
			var content = null;
			var tipStyle = 'onepica';
			if (el.getAttribute('tipstyle')) {
				tipStyle = el.getAttribute('tipstyle');
			}
			if (el.rel) {
				content = el.rel;
			}
			else if (el.getAttribute('element')) {
				content = $(el.getAttribute('element'));
			}
			if (content) {
				new Tip(el, content, { style: tipStyle });
			}
		});
	},

	/**
	 * 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
	 */
	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.
	 */
	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) {}
		}
	}
});