/**
 * BreadcrumbsController class positions marketplace list
 * under current marketplace name and handles clicks to show/hide list
 * @author tara
 */

//Initialize controllers on page load;
Event.observe(window, "load", function(){
	breadcrumbsController = new adobe.marketplace.BreadcrumbsController();
	searchBoxController = new adobe.marketplace.SearchBoxController();
});

adobe.marketplace.BreadcrumbsController  = Class.create();
adobe.marketplace.BreadcrumbsController.prototype = {
	/*
	 * Sets up variables to indicate list state and whether mouse is over
	 * list and name, sets up name click listener
	 */
	initialize: function() {
		if ($('marketplaceName')) {
			this.listOn = false;
			this.overList = false;
			this.overName = false;
			this.listening;
			var hitDiv = $('marketplaceNameHit');
			var hitImg = $('marketplaceNameHitImg');
			// Position marketplace list under name of current marketplace
			this.hideList();
	 		$('marketplaceList').makeClipping()
						.setStyle({display: "block"})
						.clonePosition($('marketplaceName'), {setWidth: false, setHeight: false, offsetTop: 18})
			// Hit div
			hitDiv.clonePosition($('marketplaceName'));
			var hitDivWidth = $('marketplaceName').getDimensions().width + 15;
			hitDiv.style.width = hitDivWidth + 'px';
			// Hit image
			hitImg.width = hitDivWidth;
			hitImg.height = $('marketplaceName').getDimensions().height;
			hitImg.observe('click', function() {
				this.handleMenuClick();
			}.bind(this));
		}
	},
	/*
	 * Shows or hides marketplace list, depending on state, and
	 * checks if mouse is over name
	 */
	handleMenuClick: function() {
		if (this.listOn == false) {
			this.showList();
		} else {
			this.hideList();
		}
		this.overName = true;
		this.checkForNameMouseOut();
	},
	/*
	 * Shows marketplace list and checks if mouse is over list
	 */
	showList: function () {
		$('marketplaceList').setStyle({visibility: 'visible', 
					      height: "auto"});
		this.listOn = true;
		if(this.listening) return this;
		$('marketplaceList').observe('mouseover', function() {
			this.overList = true;
			var PEList = new PeriodicalExecuter(function() {
				if ((this.overList == false) && (this.overName == false)) {
					this.hideList();
					PEList.stop();
				}
			}.bind(this), 1);
		}.bind(this));
		$('marketplaceList').observe('mouseout', function() {
			this.overList = false;
		}.bind(this));
		$('marketplaceNameHit').observe('mouseover', function() {
			this.overName = true;
			this.checkForNameMouseOut();
		}.bind(this));
		this.listening = true;
	},
	/*
	 * Hides marketplace list
	 */
	hideList: function() {
		$('marketplaceList').setStyle({visibility: 'hidden', 
					      height: "0"});
		this.listOn = false;
		this.overName = false;
		this.overList = false;
	},
	checkForNameMouseOut: function() {
		$('marketplaceNameHit').observe('mouseout', function() {
			this.overName = false;
			var PEName = new PeriodicalExecuter(function() {
				if ((this.overName == false) && (this.overList == false)) {
					this.hideList();
					PEName.stop();
				}
			}.bind(this), 1);
		}.bind(this));
	}
}

/**
 * SearchBoxController creates InputClear object for search_str field and
 * adds click handler for search submit button
 * @author tara
 * @see adobe.marketplace.InputClear
 */
adobe.marketplace.SearchBoxController  = Class.create();
adobe.marketplace.SearchBoxController.prototype = {
	initialize: function() {
		var search = new adobe.marketplace.InputClear;
		search.setupInputClear($('search_str'));
		// prevent default text from being submitted as search value
		Event.observe($('search_icon'), 'click', function() {
			if ($('search_str').value == 'Search Marketplace...') {
				$('search_str').value = '';
			}	
			$('search_marketplace_form').submit();
		});
	}
}