// setup the namespace
if (typeof onepica == "undefined" || !onepica) {
    var onepica = {};
}

onepica.ScreenManager = Class.create({
	initialize: function() {
		this.topNav = null;
		this.miniCart = null;
		this.checkboxReplacer = null;
		this.radioReplacer = null;
		this.layeredNav = null;
		
		this.initTopnav();
		this.initNavSearch();
		this.initImageFormElements();
		this.initMiniCart();
		this.initNavTier3();
		this.initFlashPopups();
		this.setMyAccountHeight();		
	},

	initTopnav: function () {
		this.topNav = new onepica.TopNav();
	},

	/**
	 * Dropdown, sliding search in the main nav.
	 */
	initNavSearch: function () {
		this.navSearch = new onepica.navSearch();
	},

	/**
	 * Replace form elements with images
	 */
	initImageFormElements: function () {
		this.checkboxReplacer = new onepica.imageCheckboxes();
		this.radioReplacer = new onepica.imageRadios();
	},

	/**
	 * Click-able My-Cart link.
	 */
	initMiniCart: function () {
		this.miniCart = $('mini-cart');
		if (this.miniCart) { 
			var as = this.miniCart.adjacent('a');
			var closeCallback = this.toggleMiniCart.bindAsEventListener(this);
			as[0].observe('click', closeCallback);
			this.miniCart.down('a.cart-closer').observe('click', closeCallback);
		}
	},

	/**
	 * 3rd-tier Nav closer link.
	 */	
	initNavTier3: function () {
		this.layeredNav = $('TopLayeredNav');
		if (this.layeredNav) {
			this.layeredNav.down('a.cart-closer').observe('click', this.toggleNavTier3.bindAsEventListener(this));
			this.layeredNavDecrement = 74;
			this.layeredNavStart = parseInt(this.layeredNav.getStyle('top'));
			this.layeredNavEnd = this.layeredNavStart - this.layeredNavDecrement;
			this.layeredNavContainer = $(this.layeredNav.parentNode.parentNode);
			this.layeredNavContainerStart = parseInt(this.layeredNavContainer.getStyle('height'));
			this.layeredNavContainerEnd = this.layeredNavContainerStart - this.layeredNavDecrement;
		 }
	},

	toggleNavTier3: function (event) {
		var duration = 0.5;

		if (this.layeredNav.hasClassName('closed')) { 
			this.layeredNav.removeClassName('closed');
			new Effect.Parallel([
				new Effect.Morph(this.layeredNav, {style: {top: this.layeredNavStart.toString()+'px'}}),
				new Effect.Morph(this.layeredNavContainer, {style: {height: this.layeredNavContainerStart.toString()+'px'}})
			], { duration: duration });
		}
		else {
			new Effect.Parallel([
				new Effect.Morph(this.layeredNav, {style: {top: this.layeredNavEnd.toString()+'px'}}),
				new Effect.Morph(this.layeredNavContainer, {style: {height: this.layeredNavContainerEnd.toString()+'px'}})
			], { duration: duration });
			var closeHandle = function () {$('TopLayeredNav').addClassName('closed');}
			setTimeout(closeHandle, duration * 1000);
		}
	},

	/**
	 * Auto adjust paper size in my account
	 */
	setMyAccountHeight: function () {
		var element = $$('#my-account .my-account-mid')[0];
		if(element) {
			var lineHeight = 24;
			var height = element.getHeight() - lineHeight;
			var newHeight = height + (lineHeight - (height%24)) - 3;
			element.setStyle({'height':newHeight+"px"});
		}
	},

	/**
	 * Setup click listeners for each of the links that causes a flash movie to fill the screen
	 */
	initFlashPopups: function() {
		this.flashManager = new onepica.FlashManager();
		if (this.flashManager)
			this.flashManager.replace();
	},

	getNavSearch: function () {
		return this.navSearch;
	},

	getTopNav: function () {
		return this.topNav;
	},

	getMiniCart: function () {
		return this.miniCart;
	},

	getFlashManager: function() {
		return this.flashManager;
	},

	getCheckboxReplacer: function () {
		return this.checkboxReplacer;
	},

	getRadioReplacer: function () {
		return this.radioReplacer;
	},

	toggleMiniCart: function (event) {
		if (this.miniCart.visible()) {
			this.miniCart.hide();
		}
		else {
			this.miniCart.show();
		}
	},

	/**
	 * 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 (!Object.isUndefined(pageTracker)) {
			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 (!Object.isUndefined(pageTracker)) {
			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) {}
		}
	}
});
