/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2009 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var UpSell = {
	COOKIE_NAME: 'up_sell_window_displayed',
	ADDED_TO_CART_COOKIE_NAME: 'up_sell_added_to_cart',
	ALREADY_ADDED_TO_CART_COOKIE_NAME: 'up_sell_already_added_to_cart',
	
	load: function() {
		try {
			//Already added to cart
			if(doesCookieExist(UpSell.ALREADY_ADDED_TO_CART_COOKIE_NAME)) {
				try {
					_gaq.push(['t1._trackEvent', 'UpSell', 'Skipped - Already Added to Cart']);
					_gaq.push(['t2._trackEvent', 'UpSell', 'Skipped - Already Added to Cart']);
					_gaq.push(['t3._trackEvent', 'UpSell', 'Skipped - Already Added to Cart']);
				} catch(e1) {
					/* no-op */
				}
				
				//do not display again since already added to cart
				return;
			}
			//If here, did not already add to cart, so continue
			
			//Just added to cart, so track
			if(doesCookieExist(UpSell.ADDED_TO_CART_COOKIE_NAME)) {
				//Get the product code that was just added
				var productCode = unescape(getCookieValue(UpSell.ADDED_TO_CART_COOKIE_NAME));
				//Remove the product code cookie to prevent tracking twice
				deleteCookie(UpSell.ADDED_TO_CART_COOKIE_NAME);
				//Set already added to cart cookie
				addCookie(UpSell.ALREADY_ADDED_TO_CART_COOKIE_NAME, 'true');
				
				try {
					_gaq.push(['t1._trackEvent', 'UpSell', 'Added to Cart', productCode]);
					_gaq.push(['t2._trackEvent', 'UpSell', 'Added to Cart', productCode]);
					_gaq.push(['t3._trackEvent', 'UpSell', 'Added to Cart', productCode]);
				} catch(e1) {
					/* no-op */
				}
				
				//do not display again since just added to cart
				return;
			}
			//If here, did not just add to cart, so continue
			
			//Cookie already exists, just stop
			if(doesCookieExist(UpSell.COOKIE_NAME)) {
				try {
					_gaq.push(['t1._trackEvent', 'UpSell', 'Skipped - Already Closed']);
					_gaq.push(['t2._trackEvent', 'UpSell', 'Skipped - Already Closed']);
					_gaq.push(['t3._trackEvent', 'UpSell', 'Skipped - Already Closed']);
				} catch(e1) {
					/* no-op */
				}
				
				//do not display since already shown
				return;
			}
			//If here, have not shown yet, so continue
			
			//Add needed files
			UpSell.addPrerequisites();
			
			//Start up once window loads
			Event.observe(window, 'load', UpSell.windowLoad);
			
			//Add cookie, so not repeated
			addCookie(UpSell.COOKIE_NAME, 'true');
		}
		catch(e) {/*Ignore*/}
	},
	
	addPrerequisites: function() {
		document.write('<link href="/v/up_sell/css/up_sell.css" rel="stylesheet" type="text/css"/>');
	},
	
	windowLoad: function() {
		//Create window
		new UpSellWindow();
	}
};

var UpSellWindow = Class.create({
	MOVE_DURATION: 3.0 /*seconds*/,
	MOVE_FPS_PERCENT: 50 /*percent of 66 FPS*/,
	OVERLAY_OPACITY: 0.45,
	
	initialize: function() {
		var midPoint = null;
		var elements = null;
		
		//Clear instance variables
		this.overlay = null;
		this.body = null;		
		
		//Get the body
		this.body = $$('body').first();

		//Create the overlay
		this.overlay = new Element('div');
					
		this.overlay.setStyle({
			display: 'none',
			position: 'absolute',
			left: '0px',
			top: '0px',
			height: this.getViewportHeight() + 'px',
			width: this.getViewportWidth() + 'px'
		});
		this.overlay.addClassName('up_sell_overlay');
		this.overlay.setOpacity(this.OVERLAY_OPACITY);
		
		//Create the window
		this.window = new Element('div');
		this.window.setStyle({
			display: 'none',
			position: 'absolute',
			top: '0px'		
		});
		this.window.addClassName('up_sell_window');
		//Setup window content
		elements = this.createWindow();
		elements.each(function(element){
			this.window.insert(element);
		}, this);
		
		//Add to document
		this.body.insert(this.overlay);
		this.body.insert(this.window);
		
		//Calculate mid-point
		midPoint = {
			left: Math.floor((this.getViewportWidth() / 2.0) - (this.window.getWidth() / 2.0)), 
			top: Math.floor((this.getViewportHeight() / 2.0) - (this.window.getHeight() / 2.0))
		};
		
		//Set initial position
		this.window.setStyle({
			left: midPoint.left + 'px'
		});

		//Listen to resize events
		Event.observe(window, 'resize', this.scroll.bind(this));
		Event.observe(window, 'scroll', this.scroll.bind(this));

		//Display overlay and window
		this.overlay.show();
		this.window.show();
		
		//Move window down to center
		new Effect.Move(this.window, {
			x: midPoint.left, 
			y: midPoint.top, 
			mode: 'absolute', 
			duration: this.MOVE_DURATION,
			fps: this.MOVE_FPS_PERCENT
		});
		
		try {
			_gaq.push(['t1._trackEvent', 'UpSell', 'Displayed']);
			_gaq.push(['t2._trackEvent', 'UpSell', 'Displayed']);
			_gaq.push(['t3._trackEvent', 'UpSell', 'Displayed']);
		} catch(e1) {
			/* no-op */
		}
	},
	
	getViewportWidth: function() {
		return Try.these(
			function(){return document.viewport.getWidth();},
			function(){return document.body.clientWidth;}
		);		
	},
	
	getViewportHeight: function() {
		return Try.these(
			function(){return document.viewport.getHeight();},
			function(){return document.body.clientHeight;}
		);		
	},
	
	createWindow: function() {
		var closeButton = null;
		var content = null;
		var header = null;
		var middle = null;
		
		//Create close button
		header = new Element('div');
		closeButton = new Element('input', {type: 'image', src: '//assets3.assets.redhatsocietystore.com/v/up_sell/images/top.jpg', alt: 'Close'});
		header.insert(closeButton);
		
		//Middle content
		middle = new Element('div');
		content = new Element('iframe', {src: '/v/up_sell/content/', frameborder: 0, scrolling: 'no'});
		middle.insert(content);
		
		//Setup close button event
		Event.observe(closeButton, 'click', this.close.bind(this));
		
		//Setup overlay click to close
		Event.observe(this.overlay, 'click', this.close.bind(this));
		
		return [header, middle];
	},
	
	scroll: function() {
		var offset = this.body.viewportOffset();
		if(offset == null) {
			offset = {left: 0, top: 0};
		}
		
		this.overlay.setStyle({
			left: Math.abs(offset.left) + 'px',
			top: Math.abs(offset.top) + 'px',
			height: this.getViewportHeight() + 'px',
			width: this.getViewportWidth() + 'px'			
		});		
	},
	
	close: function() {
		this.overlay.hide();
		this.window.hide();	
		
		try {
			_gaq.push(['t1._trackEvent', 'UpSell', 'Closed']);
			_gaq.push(['t2._trackEvent', 'UpSell', 'Closed']);
			_gaq.push(['t3._trackEvent', 'UpSell', 'Closed']);
		} catch(e1) {
			/* no-op */
		}
	}
});

/*--------------------------------------------------------------------------*
 * Startup immediately
 *--------------------------------------------------------------------------*/
UpSell.load();
/*--------------------------------------------------------------------------*/
