/*
Script: hp-conversa.js
Automate all functions for the HP Conversa web pages

*/

/* load sIFR scripts */
if($defined(sIFR)){
	var luzsans_med = { src: 'flash/luzsans-med.swf' };
	
	sIFR.activate(luzsans_med);
	sIFR.replace(luzsans_med, {
		selector: 'div#welcome-screen h2 span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 18px; color: #4f6c7b }'
	    ], 
	    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: 'div#quick-action h3 span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 13px; color: #5c5c53; }'
	    ], 
	    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: '#user-name-full span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 18px; color: #4f6c7b }'
	    ], 
	    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: 'div.box-content h2 span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 18px; color: #ffffff }'
    ], 
    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: 'h1#txt-create-group span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 24px; color: #3F5A70 }'
	    ], 
	    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: 'div#dashboard h1 span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 24px; color: #39738f }'
	    ], 
	    wmode: 'transparent'
	});
	sIFR.replace(luzsans_med, {
		selector: '#conversation-info-container div.feature h2 span.sifr'
	    ,css: [
	      '.sIFR-root { font-size: 16px; color: #617b88 }'
	    ], 
	    wmode: 'transparent'
	});
}

/* resize bars class -- CURRENTLY NOT IN USE
var resizeBars = new Class({
	resizer: '.resize-column', 
	maxWidth: 0, 
	initialize: function(resizeBars) {
		this.resizeBars = resizeBars.clean();
		this.resizeBars.each(function(b) {
			this.maxWidth = b.getStyle('width').toInt() + this.maxWidth;
		}.bind(this));
		this.resizeBars.each(this.resize.bind(this));
	}, 
	resize: function(bar) {
		if ($defined(bar.getElement(this.resizer))) {
			bar.makeResizable({
				snap: 1, 
				handle: bar.getElement(this.resizer).setStyle('cursor','col-resize'), 
				modifiers: { 'x':'width', 'y':false }, 
				limit: { 'x': [ bar.getSize().x+1, bar.getSize().x*1.3 ] }, 
				onDrag: function(b) {
					b.getNext().setStyle('width', (this.maxWidth - b.getSize().x - 10) + 'px');
				}.bind(this)
			});
		}
		
	}
}); */

/* set up modal dialog box -- CURRENTLY NOT IN USE
function shellFunctionOne() {
	var modalBox = new MUI.ModalElementBox($("mbModalBox"), {
		duration: 'normal', 
		position:{x:0, y:0}
	});
	modalBox.show();
	$('mbCloseLink').addEvent('click', function(ev) {
		ev.stop();
		modalBox.hide();
	});
} */



/* set tooltips */
var Tooltips = new Class({
	toolTip: null, 
	fade: null, 
	
	toolTipElement: new Element('div', { 'class':'tooltip'}).adopt(
		new Element('div', { 'class':'tip top-left'}), 
		new Element('div', { 'class':'tip top-right'}),
		new Element('div', { 'class':'tip'}).grab(
			new Element('div', { 'class':'tip title'})
		),
		new Element('div', { 'class':'tip bot-left'}),
		new Element('div', { 'class':'tip bot-right'}), 
		new Element('div', { 'class':'tip arrow bottom'})
	), 
	
	toolTipDefaultElement: function(el){
		if(!$defined(el.get('title'))) { return; }
		var tip = this.toolTipElement.clone(true,true).inject(document.body);
		tip.getElement('div.tip.title').appendText(el.get('title'));
		el.store('title',el.getProperty('title'));
		el.removeProperty('title');
		tip.store('division',3);
		this.toolTipPosition(el,tip);
		return tip;
	}, 
	
	toolTipHzElement: function(){
		var tip = this.toolTipElement.clone(true,true).addClass('hz').inject(document.body);
		return tip;
	}, 
	
	toolTipPopupElement: function(el,data){
		var tip = this.toolTipElement.clone(true,true).addClass('popup').inject(document.body);
		new Element('div', {
			'class':'tip'
		}).grab(
			new Element('div')
		).replaces(tip.getElement('div.tip.title'));
		tip.getElement('div.tip div.tip div').set('html', data.get('html'));
		tip.store('division',4);
		this.toolTipPosition(el,tip);
		return tip;
	}, 
	
	toolTipIconElement: function(el,data){
		var tip = this.toolTipElement.clone(true,true).addClass('popup icon').inject(document.body);
		new Element('div', {
			'class':'tip'
		}).grab(
			new Element('div')
		).replaces(tip.getElement('div.tip.title'));
		tip.getElement('div.tip div.tip div').set('html', data.get('html'));
		tip.store('division',2);
		this.toolTipPosition(el,tip);
		return tip;
	}, 
	
	toolTipPosition: function(el,tip){
		tip.setStyles({
			'top': (el.getPosition().y - tip.getSize().y - 5), 
			'left': (el.getPosition().x - (tip.getSize().x/tip.retrieve('division')) + 4)
		});
	},
	
	initialize: function(elements,type){
		elements.each(function(el){
			if(!$defined(el.retrieve('Tooltip'))){
				switch(type){
				case "popup":
					var data = el.getElement('span.data');
					if(!$defined(data)) { return; }
					el.store('tooltip',this.toolTipPopupElement(el,data));
					break;
				case 'status':
					el.store('tooltip',this.toolTipDefaultElement(el));
					break;
				case 'icon':
					var data = el.getElement('span.data');
					if(!$defined(data)) { return; }
					el.store('tooltip',this.toolTipIconElement(el,data));
					break;
				}
				el.store('Tooltip',this);
			}
			el.addEvent('mouseenter', function(ev){
				ev.stop();
				this.hoverOn(el);
			}.bind(this));
			el.addEvent('mouseleave', function(ev){
				ev.stop();
				this.hoverOff(el);
			}.bind(this));
		}.bind(this));
	}, 
	
	hoverOn: function(el){
		el.removeEvents('mouseenter');
		this.toolTipPosition(el,el.retrieve('tooltip'));
		el.retrieve('tooltip').fade(0.8);
	}, 
	
	hoverOff: function(el){
		el.retrieve('tooltip').fade('out');
		el.addEvent('mouseenter', function(ev){
			ev.stop();
			this.hoverOn(el);
		}.bind(this));
	}
});

window.addEvent('domready', function() {
	
	/* sort dropdown menu */
	$$('.hover-baby').each(function(el) {
		el.addEvent('mouseover', function() { this.addClass('hover'); });
		el.addEvent('mouseout', function() { this.removeClass('hover'); });
	});
	
	/* set input field functionality */
	$$('body input[type=text]').each(function(inp){
		inp.store('value',($defined(inp.defaultValue))?inp.defaultValue:'');
		inp.set('value',inp.defaultValue);
		function flip(ev){
			ev.stop();
			ev.target.toggleClass('focused');
			if(ev.target.hasClass('ftext')){ return; }
			if(ev.target.get('value') == ""){ ev.target.set('value', ev.target.retrieve('value')); }
			else if(ev.target.get('value') == ev.target.retrieve('value')){ ev.target.set('value', ''); }
		}
		inp.addEvent('focus', flip).addEvent('blur', flip);
	});
		
	/* define vertical scrollbars */
	$$('#my-stuff .scrollbar','.scrolling-list .scrollbar','body#videos div.scrolling-list .scrollbar','div#dashboard .col .scrollbar').each(function(scroll) {
		new scrollers(scroll,scroll.getPrevious('ul'));
	});
	
});
	
/* Tooltips need to run after window onload, not DOMload, for positioning purposes */
window.addEvent('load', function(){
	new Tooltips($$('a.status'),'status');
	new Tooltips($$('#viewer-panel a.item'),'icon');
	new Tooltips($$('#sm-video-scroller li.video-thumb'),'popup');
});