/******************************************
 * The Screen Manager class
 ******************************************/

onepica.ScreenManager = Class.create({
    initialize: function() {
		this.cart = null;
		this.initPrototips();
		this.initCallouts();
		this.initCart();
		this.initMessage();
    },

	/**
	 * 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 = 'white';
			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});
			}
		});
	},

	initCallouts: function() {
		var callouts = $$('.main .button-callout img');
		for(i=0; i<callouts.length; i++) {
			callouts[i].observe('mouseover', this.calloutOver.bindAsEventListener(this, callouts[i]));
			callouts[i].observe('mouseout', this.calloutOut.bindAsEventListener(this, callouts[i]));
			var preloadImg = new Image();
			preloadImg.src = callouts[i].getAttribute('hoversrc');
		}
	},

	initMessage: function () {
		this.message = new onepica.Message('message-container');
	},

	/**
	 * Initializes the cart
	 */
	initCart: function () {
		this.cart = new onepica.Cart();
	},

	/**
	 * Returns the cart object.
	 */
	getCart: function () {
		return this.cart;
	},
	
	/**
	 * Returns the message display object.
	 */
	getMessage: function () {
		return this.message;
	},

	calloutOver: function(evt, elm) {
		elm.setAttribute('src', elm.getAttribute('hoversrc'));
	},

	calloutOut: function(evt, elm) {
		elm.setAttribute('src', elm.getAttribute('origsrc'));
	},

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