/**
 * Copyright (c) 2008 eSolutions Group Ltd.
 *
 * Author: Timothy Grant Vogelsang <tvogelsang@esolutionsgroup.ca>
 * 		   Hang (Anthony) Yuan <hyuan@esolutionsgroup.ca>
 */

DropDown = Class.create();

DropDown.prototype = {

	/********************************************************************************
	 *
	 * INITIALIZATION ROUTINES
	 *
	 *******************************************************************************/
	
	// This public function handles the initialization.
	initialize: function(variables) {
		this.variables = Object.extend( {
			elementId:	'DropDown',
			newWindow:	true,
			items:		null,
			target:		null
		}, variables || { } );
	
		Event.observe(window, 'load', this.bind.bindAsEventListener(this));
	},
	
	/********************************************************************************
	 *
	 * RENDER ROUTINES
	 *
	 *******************************************************************************/
	
	// This public function handles the render dropdown event.
	render: function() {
		var s =	'<div id="' + this.variables.elementId + '" style="display: none" class="DropDown">' +
					this.renderItems() +
				'</div>';
		
		new Insertion.After(this.variables.target, s);
		
		this.position(this.variables.elementId, this.variables.target);
		
		return $(this.variables.elementId);
	},
	
	// This public function handles render dropdown items event.
	renderItems: function() {
		var s = '';
		var sTarget;
		if (this.variables.newWindow === true) {
			sTarget = ' target="_blank"';
		} else {
			sTarget = '';
		}

		this.variables.items.each( function(item) {
				var t = new Template(item.URL);
				
				s +=	'<a href="' + t.evaluate( { URL: document.location } ) + '" ' + sTarget + '>'+
								'<div class="DropDownOption" onMouseOver="this.className=\'DropDownOptionHover\'" onMouseOut="this.className=\'DropDownOption\'">';
				
				if (item.Image)
					s +=			'<img align=\'absmiddle\' src=\'' + item.Image + '\' title=\'' + item.Title + '\'>';
				
				s +=				item.Title +
								'</div>' +
							'</a>';
			}
		);
		
		return s;
	},
	
	/********************************************************************************
	 *
	 * EVENT ROUTINES
	 *
	 *******************************************************************************/

	// This public function handles the click event.
	onClick: function(e) {
		this.toggle(this.variables.elementId);
		
		Event.stop(e);  // propogation stop
	},
	
	// This public function handles the select event.
	onCancel: function(e, element) {
		if (element.visible())
			this.hide(element);
	},
	
	/********************************************************************************
	 *
	 * GENERAL ROUTINES
	 *
	 *******************************************************************************/
	
	// This public function handles the binding the click event to the target.
	bind: function() {
		if ($(this.variables.target) == null) { return; }
		Event.observe(this.variables.target, 'click', this.onClick.bindAsEventListener(this));
		Event.observe(window, 'resize', function() {
				var element = $(this.variables.elementId);
				if (element && element.visible())
					this.position(this.variables.elementId, this.variables.target);
			}.bindAsEventListener(this)
		);
	},
	
	// This public function handles binding of events to the element.
	bindElement: function(element) {
		Event.observe(document, 'click', this.onCancel.bindAsEventListener(this, element));
	},
	
	// This public function handles positioning the dropdown.
	position: function(element, target) {
		if (typeof element == 'string')
			element = $(element);
		if (typeof target == 'string')
			target = $(target);
		var offset = Position.page(target);		

		// element.setStyle( { top: (target.getHeight() + offset[1]) + 'px', left: offset[0] + 'px' } );
	},
	
	// This public function handles toggling the display of the dropdown.
	toggle: function(element) {
		if (typeof element == 'string')
			element = $(element);
		
		// Only if the element does not exist
		if (!element) {
			element = this.render();
		}
		
		// Toggle the visibility of the element
		if (element.visible())
			this.hide(element);
		else
			this.show(element);
	},
	
	// This public function handles showing the dropdown.
	show: function(element) {
	
		if (typeof element == 'string')
			element = $(element);
		
		this.position(this.variables.elementId, this.variables.target)

		if (document.all && typeof(Effect) != 'undefined')
			new Effect.BlindDown(element, { duration: 0.2 } );
		else
			element.show();
		
		this.bindElement(element);
	},
	
	// This public function handles hiding the dropdown.
	hide: function(element) {
		if (typeof element == 'string')
			element = $(element);
		
		/*
		if (document.all)
			new Effect.BlindUp(element, { duration: 0.2 } );
		else
		*/
			element.hide();
			
	}

}

var DropDownControl = {

	/********************************************************************************
	 *
	 * GLOBAL VARIABLES
	 *
	 *******************************************************************************/
	
	Version: '1.0',
	
	
	/********************************************************************************
	 *
	 * LOAD ROUTINES
	 *
	 *******************************************************************************/
	
	// This public function handles loading the required script.
	require: function(libraryName) { 
		if (document.all) {
			var h = $A(document.getElementsByTagName('head')).first();
			var s = document.createElement('script');
			
			s.type = 'text/javascript';
			s.src = libraryName;
			
			h.appendChild(s);
		} else {
			document.write('<script type=""text/javascript"" src=""' + libraryName + '""></script>');
		}
	}, 
	
	// This public function handles loading the script.
	load: function() {
		if (typeof(Prototype) == 'undefined' || typeof(Element) == 'undefined') {
			alert("Prototype JavaScript Framework (Required 1.5.0)");
			throw("Prototype JavaScript Framework (Required 1.5.0)");This 
		}
		if(typeof(Effect) == 'undefined') {
			DropDownControl.require('../../scriptaculous/effects.js');
		}
		
		new DropDown( { target: '_Share', items: _ShareItems, elementId: '_ShareItems' } );
		new DropDown( { target: '_Translate', items: _TranslateItems, elementId: '_TranslateItems', newWindow: false } );
	}
	
}

DropDownControl.load();