// User interface library
cUI = function(o_main){
	
	this._parent = o_main;
	
	// Banner node
	this.banners = {
		top: null
	}
	
	// Tooltip options
	this.optionsets = {
		qtipstyle	: { 
			padding		: 5,
			background	: '#2E2E2E',
			color		: '#ccc',
			textAlign	: 'center',
			fontSize	: '10px',
			border		: {
				width	: 3,
				radius	: 3,
				color	: '#99CC00'
			},
			tip			: { 
				corner: 'topLeft', 
				color: '#99CC00',
				size: {
					x: 8, y: 8
				}
			},
			name		: 'dark' // Inherit the rest of the attributes from the preset dark style
		},
		qtipposition : {
			corner: {
				target: 'bottomRight',
				tooltip: 'topLeft'
			},
			adjust: { 
				screen: true
			}
		},
		accordion	: {
			collapsible: true, 
		    autoHeight: false, 
		    active: false
		}
	}
	
	// Page Initializer
	this.initPage = function(){
		
		// Load any flash
		this.convertAllFlash();
		
		// Enhance contactforms
		$('form.contactform :input').not(':button').each(function(){
			$(this).hover(
				function(e){$(this).addClass('active');},
				function(e){$(this).removeClass('active');}
			);
		});

		// Enhance linkpanels
		$('div#linkpanels a[title]').each(function(){
			$(this).removeAttr('title');
		});
		
		// Enhance site search
		$('div#sitesearch input')
			.hover(
				function(e){
					var $this = $(this);
					$this
						.addClass('active')
						.val(($this.val() == 'Search the site..')? '' : $this.val());
				},
				function(e){
					var $this = $(this);
					$this
						.removeClass('active')
						.val(($this.val() == '')? 'Search the site..' : $this.val());
				}
			)
			.val('Search the site..');
	
		// Convert all tooltips
		this.convertAllTooltips();
		
		// Convert popup links to open in new window/tab
		this.activateExternalLinks();
		
		// Convert accordions
		this.convertAccordions();
		
		return this;
	}
	
	/*
	 	Convert any accorion divs to jQuery accorion controls
	*/
	this.convertAccordions = function(){
	
		$('.accordion').accordion(this.optionsets.accordion);
		
		return this;
	} // this.convertAccordion()
	
	/*
		Size an array of elements to the max height
	*/
	this.sizeToTallest = function(elements){
		
		// For each element in array, store the largest height
		var maxHeight = 0;
		$(elements).each(function(){
			maxHeight = Math.max(maxHeight, $(this).outerHeight(true));
		});

		// Size to tallest
		$(elements).each(function(){
			$this = $(this);
			$this.height(maxHeight - ($this.outerHeight(true) - $this.height()));
		});
	
		return this;
	} // this.sizeToTallest()
	
	/*

	*/
	this.showTopBanner = function(v_banner){
		
		// Create banner if necessary
		if (!this.banners.top){
			this.banners.top = $('<div/>').attr({'id': 'uc-banner'}).html(v_banner).hide();
			$(document.body).append(this.banners.top);
		}
	
		// Fire the event timer
		window.setTimeout(function(){			
			_tfo.ui.positionTopBanner();
			_tfo.ui.banners.top.slideDown().fadeIn();			
			$(window).resize(_tfo.ui.positionTopBanner);
			$(window).scroll(_tfo.ui.positionTopBanner);
		}, 2000);
		
		return this;
	} // this.showTopBanner()
	
	/*
		Position under construction banner
		(Align top of viewport and make right width)
	*/
	this.positionTopBanner = function(){
		_tfo.ui.banners.top.width(($(window).width()) + 'px')
			.css({'top': $(window).scrollTop() + 'px'});
	} // this.positionTopBanner()
	
	/*
		Create a DHTML tooltip from [title] attribute
	*/
	this.makeTooltipFromTitle = function(o_node, v_delay){
		if ($(o_node)){
			$(o_node).qtip({
				"delay": ((v_delay) ? v_delay : 1000), 
				solo: true, 
				hide: { 
					delay: 0, when: {event: 'mouseleave'} 
				},
				position: this.optionsets.qtipposition, 
				style: this.optionsets.qtipstyle				   
			});
		}
	} // this.makeTooltipFromTitle()
	
	/*
		Remove a tooltip from an element
	*/
	this.removeTooltip = function(o_node){
		$(o_node).qtip('destroy').attr('title', '');
		return this;
	}
	
	/*
		Make all links with rel*="popup" popup links
	*/
	this.activateExternalLinks = function(){
		
		// Convert all links to use link text as title if none specified
		$('a[rel*="popup"], a[class*="popup"]').each(function(){
			$(this).attr('target', '_blank');					   
		});
		
		return this;
	} // this.activateExternalLinks()
	
	/*
		Convert all relevant elements' title attributes to DHTML tooltips
	*/
	this.convertAllTooltips = function(o_parent){
	
		// Get parent
		if (!o_parent){o_parent = $(document)} else {o_parent = $(o_parent);}
			
		// Convert all links to use link text as title if none specified
		/*$('a[title=""]').each(function(){
			$this = $(this);
			$this.attr("title", $this.text());					   
		});*/
		
		// Store the title attr as data for links
		$('a[title!=""]').each(function(){
			$(this).data('title', $(this).attr('title'));
		});
		
		// If an element has a rel="notooltip" attribute specified,
		// then we remove the title attr
		// We remove the title attribute so that the native tooltip doesnt interfere with ours
		o_parent.find('*[title!=""][rel*="notooltip"]').each(function(){
			$(this).removeAttr("title");
		});
		
		// Convert all elements with title attributes set, to DHTML tooltips
		o_parent.find('*[title!=""]').qtip({
			solo: true, 
			content: {
				prerender: true
			},
			delay: 1000, 
			hide: { 
				delay: 0, when: {event: 'mouseleave'} 
			},
			position: this.optionsets.qtipposition, 
			style: this.optionsets.qtipstyle					   
		});
	
		return this;
		
	} // this.convertAllTooltips()
	
	/*
		Convert all flash divs to flash players
	*/
	this.convertAllFlash = function(){
		
		$('div.flashembed').each(function(){
			_tfo.ui.convertFlash(this);
		});
	
		return this;
		
	} // convertAllFlash()
	
	/*
		Convert given flash divs to flash player
	*/
	this.convertFlash = function(o_obj){
		var $this = $(o_obj);
		$this.append($('<a/>', {'href': $this.attr('src')}).addClass('target'));
		$this.find('a.target').eq(0).media({
		    'autoplay'	: false,
		    'width'		: 500,
		    'height'	: 395,
		    'bgColor'	: 'transparent'
		});
	
		return this;
		
	} // convertFlash()
		
} // cUI()
