/**
 * Copyright (c) 2008 eSolutions Group Ltd.
 *
 * Author: Timothy Grant Vogelsang <tvogelsang@esolutionsgroup.ca>
 */

Share = Class.create();

Share.prototype = {

	/********************************************************************************
	 *
	 * INITIALIZATION ROUTINES
	 *
	 *******************************************************************************/
	
	// This public function handles the initialization.
	initialize: function(variables) {
		this.variables = Object.extend( {
			elementId:	'DropDown',
			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 = '';
		
		this.variables.items.each( function(item) {
				var t = new Template(item.URL);
				
				s +=	'<a href="' + t.evaluate( { URL: document.location } ) + '" target="_blank">'+
								'<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() {
		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)
			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 ShareControl = {

	/********************************************************************************
	 *
	 * 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)");
		}
		$A(document.getElementsByTagName("script")).findAll( function(s) {
				return (s.src && s.src.match(/share\.js\?target=/))
			}
		).each( function(s) {
				var path = s.src.replace(/share\.js(\?.*)?$/,'');
				var a = s.src.split('?')[1].toQueryParams();
				
				ShareControl.require(path + 'Scriptaculous/effects.js');
				
				new Share( { target: a.target, items: eval(a.items) } );
			}
		);
	}
	
}

ShareControl.load();