if ( !window.HSN ) { window.HSN = {}; }

HSN.Remote = function (offset) {
	this.offset = offset;

	this.init();
};

HSN.Remote.prototype = {
	init : function() {
		var remote;
		remote = document.getElementById('remote');
		remote.style.visibility = 'visible';

		this.links(remote);
		this.hideLink();
		this.dragRemote(this.offset);
	},

	links : function (remote) {
		var links = remote.getElementsByTagName('li');

		for (var i=0; i<links.length; i++) {
			YAHOO.util.Event.addListener(links[i], 'mouseover', function() { HSN.Remote.prototype.showInfo(remote, this.lastChild); });
			YAHOO.util.Event.addListener(links[i], 'mouseout', function() { HSN.Remote.prototype.hideInfo(this.lastChild); });
		}

		YAHOO.util.Event.addListener(remote, 'mouseover', function() { remote.className = ''; });
	},

	hideLink : function () {
		var page = document.getElementsByTagName('body')[0].id;
		if ((!page) || (page == 'home')) {
			page = 'home';
		} else if (page != 'anywhere') {
			return false;
		}

		var hiddenLink = 'r_' + page;
		hiddenLink = document.getElementById(hiddenLink);
		hiddenLink.style.display = 'none';
	},

	showInfo: function (remote, e) {
		if (e) {
			var position = YAHOO.util.Dom.getXY(remote)[0];

			// Display the info popouts to the left side of the remote unless
			// the remote is positioned to the left of the video window
			if (position > 160) {
				return e.className = 'show';
			} else {
				return e.className = 'showAlt';
			}
		}
	},

	hideInfo: function (e) {
		if (e) {
			return e.className = 'hide';
		}
	},

	dragRemote: function (offset) {
		var Dom = YAHOO.util.Dom,
		Event = YAHOO.util.Event,
		remote;

		YAHOO.example.DDRegion = function(id, sGroup, config) {
			this.cont = config.cont;
			YAHOO.example.DDRegion.superclass.constructor.apply(this, arguments);
		};

		YAHOO.extend(YAHOO.example.DDRegion, YAHOO.util.DD, {
			cont: null,
			init: function() {
				//Call the parent's init method
				YAHOO.example.DDRegion.superclass.init.apply(this, arguments);
				this.initConstraints();
			},

			initConstraints: function() {
				//Get the draggable element we are working on
				var el = this.getEl();

				//Get the xy position of the draggable element
				var xy = Dom.getXY(el);

				//Get the width and height of the viewport
				var viewportWidth = Dom.getViewportWidth();
				var viewportHeight = Dom.getViewportHeight();

				//Get the width and height of the draggable element
				var width = parseInt(Dom.getStyle(el, 'width'), 10);
				//var height = parseInt(Dom.getStyle(el, 'height'), 10);
				var height = 147; // hard coding this for ie6

				//Set left to x minus left
				var left = xy[0];

				//Set right to right minus x minus width plus location on page
				var right = viewportWidth - xy[0] - width + offset;

				//Set top to y minus top
				var top = xy[1];

				//Set bottom to bottom minus y minus height
				var bottom = viewportHeight - xy[1] - height - 70;

				//Set the constraints based on the above calculations
				this.setXConstraint(left, right);
				this.setYConstraint(top, bottom);

				Event.on(window, 'resize', function() {
					this.initConstraints();
				}, this, true);
			}
		});
		Event.onDOMReady(function() {
			remote = new YAHOO.example.DDRegion('remote', '', { cont: 'main'});
		});
	}
};
