function ScrollDownMenuController() {
	// boolean flag to determine whether or not to process next command
	this.enabled = 0;

	// attributes
	this.menu_array = new Array;
	this.selected_item = undefined;
	this.speed = 300;

	// attach methods
	this.toggle = menu_toggle;
	this.addMenu = menu_addToArray;
	this.isInArray = menu_isInArray;
	this.selectItem = menu_selectItem;
	this.clearSelection = menu_clearSelection;
	this.display = menu_display;
}

// implementation of controller methods

function menu_toggle() {
	if ( this.enabled == 1 ) {
		this.enabled = 0;
	} else {
		this.enabled = 1;
	}
}

function menu_addToArray(id) {
	var obj = this;
	obj.menu_array.push(id);
	$("#"+id).hover(
	function() {
		obj.selectItem(id);
		obj.display();
	}
	,
	function() {
		obj.clearSelection();
		setTimeout("menu_controller.display();", obj.speed+20);
	}
	);
}

function menu_isInArray(id) {
	for(i in this.menu_array) {
		if(this.menu_array[i]==id) {
			return true;
		}
	}
	return false;
}
	
function menu_selectItem(id) {
	if(this.isInArray(id)) {
		this.selected_item = id;
	}
}

function menu_clearSelection() {
	this.selected_item = undefined;
}

function menu_display() {
	if ( this.enabled == 1 ) {
		for(i in this.menu_array) {
			$("#"+this.menu_array[i]+"_Content").hide();
		}
		if(typeof this.selected_item != typeof never_ever_defined_var) {
			this.toggle();
			$("#"+this.selected_item+"_Content").css("position","absolute");
			$("#"+this.selected_item+"_Content").slideDown(this.speed);
			setTimeout('menu_controller.toggle()', 500);
		}
	}
}


function planViewer() {
	this.option_selected = 0;
	this.option = new Array();
	this.option_title = new Array();
	this.option_content = new Array();
	
	this.register = planViewerRegister;
	this.toggle = planViewerToggle;
}

function planViewerRegister( id, target, title ) {
	this.option[this.option.length] = id;
	if ( id == 'linkTable' ) {
		this.option_title[this.option_title.length] = $('#headerComparison').html();
		this.option_content[this.option_content.length] = $('#vps-hosting-plans').html();
	} else {
		this.option_title[this.option_title.length] = title;
		this.option_content[this.option_content.length] = '<iframe id="planFrame" src="' + target + 'ajax/" frameborder="0"><p style="text-align:center;font-weight:bold;">iFrame support required for display.</p></iframe>';
	}
}

function planViewerToggle( id ) {
	if ( !id ) {
		id = 'linkTable';
	}
	
	$('#optionComparison li').removeClass('lit');
	$('#optionComparison a').css('display', 'block');
	$('#optionComparison span').css('display', 'none');
	
	for ( i = 0; i < this.option.length; i++ ) {
		if ( id == this.option[i] ) {
			$('#optionComparison #' + id + ' a').css('display', 'none');
			$('#optionComparison #' + id + ' span').css('display', 'block');
			$('#optionComparison #' + id).addClass('lit');
			$('#vps-hosting-plans').html( this.option_content[i] );
		}
	}
	return false;
}

var menu_controller = new ScrollDownMenuController();

$(document).ready( function() {
	
	// Scroll-down menu functions
	menu_controller.addMenu('vpshostingContainer');
	menu_controller.addMenu('supportContainer');
	menu_controller.addMenu('aboutContainer');
	menu_controller.addMenu('compareContainer');
	menu_controller.toggle();
	menu_controller.display();
	$('.nav').css('z-index', 999);
	
	// Scroll-down menu submenu functions
	if ( $('ul.nav li a').length ) {
		var iterID = 1;
		$('ul.nav ul li').each(function() {
			var thisID = iterID;
			$(this).attr('id', "sMenuItem" + thisID);
			if ( $('#' + "sMenuItem" + thisID + ' a').length ) {
				$('#' + "sMenuItem" + thisID).hover(
					function() {
						$('#' + "sMenuItem" + thisID + ' a').css('text-decoration', 'underline');
						$('#' + "sMenuItem" + thisID).css('cursor', 'pointer');
					},
					function() {
						$('#' + "sMenuItem" + thisID + ' a').css('text-decoration', 'none');
						$('#' + "sMenuItem" + thisID).css('cursor', 'auto');
					}
				);
				$('#' + "sMenuItem" + thisID).click(function() {
					window.location = $('#' + "sMenuItem" + thisID + ' a').attr('href');
				});
			}
			iterID++;
		});
	}
	
	// Plan table functions
	if ( $('#optionComparison li').length ) {
		var plan_views = new planViewer();
		$('#optionComparison li').each(function() {
			var viewID = $(this).attr('id');
			plan_views.register(
				viewID,
				$('#' + viewID + ' a').attr('href'),
				$('#' + viewID + ' a').attr('title')
			);
			$('#' + viewID).append('<span class="bold">' + $('#' + viewID + ' a').text() + '</span>');
			$('#optionComparison li#' + viewID + ' a').click(function() {
				plan_views.toggle(viewID);
				return false;
			});
		});
		
		plan_views.toggle();
		
	}
	
});
