// JavaScript Document

var Lilly = {
	
	InputFocus: new Class({
		text: null,
		input: null,
		initialize: function (elm) {
			this.input = $(elm);
			if (this.input == null || this.input == undefined) return false;
			this.text = this.input.value;
			this.input.addEvent('focus', this.handler_focus.bindWithEvent(this));
			this.input.addEvent('blur', this.handler_blur.bindWithEvent(this));
		},
		handler_focus: function (e) {
			if (this.input.value == this.text) this.input.value = "";
		},
		handler_blur: function (e) {
			if (this.input.value == "") this.input.value = this.text;
		}
	}),
	
	Accordion: new Class({
		initialize: function() {
			var accordion = new Accordion($$('.toggler'),$$('.element'), {  
			opacity: 0, 
			alwaysHide: true,
			show: -1,
			duration: 'short',
			onActive: function(toggler) { toggler.addClass('toggleactive'); },  
			onBackground: function(toggler) { toggler.removeClass('toggleactive'); } 
			});  	
		}
	}),

	ExternalLinks: new Class({

		initialize: function () {
			$$('[rel=external]').each( function (item) {
				if (item.getProperty('rel') == "external") {
					item.setProperty('target', '_blank');
					item.addClass('external');
					if (
						(/^.+\.pdf$/i).test(item.getProperty('href')) == false
						&& item.hasClass('noToolTip') == false
					) {
						this.addToolTip(item);
					}
				}
			}.bind(this));
			
			this.initToolTips();
		},
		
		addToolTip: function (elm) {
			elm.addClass('tooltip');
			elm.store('tip:title', '');
			elm.store('tip:text', 'Advertencia: Al hacer click en este link va a abandonar el sitio www.lilly-argentina.com.ar');
		},
		
		initToolTips: function () {
			var myTips = new Tips($$('.tooltip'), {
				timeOut		: 700,
				maxTitleChars	: 50,
				hideDelay		: 200,
				className		: 'external_tips',
				initialize: function () {
					this.fx = new Fx.Tween(this.tip, {duration: 200, link: 'cancel'});					
					this.fx.set('opacity', 0);
				},
				onShow: function(tip){					
					this.fx.start('opacity', .8);
				},
				onHide: function(tip){
					this.fx.start('opacity', 0);
				}
			});				
		}
	}),

	Subnav: new Class({
		MARK_CLASS: 'selected',
		HIDDEN_CLASS: 'hidden',
		initialize: function () {
			var location, subnav;
						
			location = this.getUrlParts(document.URL);
			
			this.hideChildNav();
			
			$$('#subnav a').each( function (item) {
				var path = this.getUrlParts(item.getProperty('href'));
				if (path == location) this.mark(item);
			}.bind(this));
			
			$('subnav').setStyle('visibility', 'visible');
		},
		
		hideChildNav: function () {
			$$('#subnav ul ul').each( function (item) {
				item.toggleClass(this.HIDDEN_CLASS);
			}.bind(this));
		},
		
		mark: function (item) {
			var liParents, ulParents;
			
			liParents = item.getParents('li');
			ulParents = item.getParents('ul');
			ulChildren = item.getParent().getFirst('ul');
			
			item.addClass(this.MARK_CLASS);
			
			liParents.each( function (li) {
				li.addClass(this.MARK_CLASS);				
			}.bind(this));
			ulParents.each( function (ul) {
				ul.removeClass(this.HIDDEN_CLASS);
			}.bind(this));
			
			if (ulChildren) ulChildren.removeClass(this.HIDDEN_CLASS);
		},
		
		getUrlParts: function (url) {
			url = url.replace(/^\/*/, '').replace(/\/*$/, '');
			url = url.split('/');
						
			if (url[0] == "http:") {
				url = url.slice(3);
			}						
			
			return url.join('/');
		}
	}),

	SelectForm: new Class({
		select: null,
		initialize: function (form) {
			var submit;
			
			form = $(form);
			if (!form) return;
			
			submit = form.getElement('input');
			
			if (!submit) return;
			
			submit.setProperty('rel', 'external');
			
			this.select = form.getElement('select');
			
			if (!this.select) return;
			
			form.addEvent("submit", this.onSubmit.bind(this))
		},
		onSubmit: function (e) {
			var href;
			
			if (this.select.selectedIndex == 0) {
				alert('Please select a product before pressing "Go"')
			} else {
				href = this.select.options[this.select.selectedIndex].value;
				window.open(href)
				this.select.selectedIndex = 0;
			}
			
			e.stop();
			return false;
		}
	}),

	XMLMenu: new Class({
		div: '',
		target: '',
		intervalId: '',
		url: '',
		initialize: function (target, url, id) {
			this.target = $(target);
			this.url = url;
			this.div = new Element('div', {
				'id': id,
				'styles': {
					'display': 'none'
				}
			});
			this.parent = $('parentId');			
			this.getXML();			
			this.div.inject(document.body);
			this.addEvents();
		},
		getXML: function () {
			var req = new Request.HTML({
				method: 'get',
				update: this.div, 
				url: this.url, 
				onComplete: this.xmlComplete.bind(this)
			}).send();
		},
		addEvents: function () {
			this.target.addEvent('click', function (e) {e.stop()});
			this.target.addEvent('mouseover', this.onMouseOver.bind(this));
			this.target.addEvent('mouseleave', this.onMouseOut.bind(this));
			this.div.addEvent('mouseleave', this.onMouseOut.bind(this));
			this.div.addEvent('mouseenter', this.onMouseEnter.bind(this));
		},
		xmlComplete: function (obj) {
			var pos, coord;
			
			pos = this.target.getPosition();
			coord = this.target.getCoordinates();
			
			this.div.setStyle("top", coord.top + coord.height + 3);		
			this.div.setStyle("left", coord.left);
		},
		show: function () {
			this.div.setStyle('display', 'block')
		},
		hide: function () {
			this.div.setStyle('display', 'none')
		},
		onMouseOver: function (e) {
			this.show();
			e.stop();
		},
		onMouseEnter: function (e) {
			clearInterval(this.intervalId);
		},
		onMouseOut: function (e) {
			e.stop();			
			this.intervalId = this.hide.delay(300, this);			
		}
	}),

	Pages: new Class({
				 
		initialize: function () {
						
			$(document.body).getProperty('class').split().each( function (item) {
				if (this[item]) this[item]();
			}.bind(this));
			
			this.global();
		},

		global: function () {
			if ($('subnav')) { new Lilly.Subnav(); }
			new Lilly.ExternalLinks();		
			new Lilly.Accordion();
			new Lilly.InputFocus('s');
			new Lilly.XMLMenu(
				'worldwide', 
				'/Library/locales.htm', 
				'locales'
			);
		},
		
		home: function () {
			new Lilly.SelectForm('productSelectForm');			
		},
		
		products: function () {
			new Lilly.SelectForm('productSelectForm');
		}
	})
}

window.addEvent('domready', function(){
	new Lilly.Pages();
});
