Ext.onReady(function(){
	
	if(!Ext.getDom('hd')){
		return;
	}
	
	var activeMenu;
	
	function createMenu(name){
		var el = Ext.get(name+'-link');		
		var tid = 0, menu, doc = Ext.getDoc();
		
		//console.log(el);
		//console.log(tid);
		
		var handleOver = function(e, t){
			if(t != el.dom && t != menu.dom && !e.within(el) && !e.within(menu)){
				hideMenu();
			}	
		};
				
		var hideMenu = function(){
			if(menu){
				menu.hide();
				el.setStyle('text-decoration', '');
				doc.un('mouseover', handleOver);
				doc.un('mousedown', handleDown);
			}
		}
		
		var handleDown = function(e){
			if(!e.within(menu)){
				hideMenu();
			}
		}
		
		var showMenu = function(){
			clearTimeout(tid);
			tid = 0;
			//console.log("tid despues: " + tid);
			
			if (!menu) {
				//console.log(name+"-menu");
				menu = new Ext.Layer({shadow:'sides',hideMode: 'display'}, name+'-menu');
			}
			menu.hideMenu = hideMenu;
				
			menu.el = el;
			if(activeMenu && menu != activeMenu){
				activeMenu.hideMenu();
			}
			activeMenu = menu;
			
			if (!menu.isVisible()) {
				menu.show();
				menu.alignTo(el, 'tl-bl?');
				menu.sync();
				el.setStyle('text-decoration', 'underline');
				
				doc.on('mouseover', handleOver, null, {buffer:150});
				doc.on('mousedown', handleDown);
			}
		}
		
		el.on('mouseover', function(e){
			if(!tid){
				tid = showMenu.defer(150);
				//console.log("tid antes: " + tid);
			}
		});
		
		el.on('mouseout', function(e){
			if(tid && !e.within(el, true)){
				clearTimeout(tid);
				tid = 0;				
			}
		});
	}
	
	createMenu('ziving');
	createMenu('ortodoncia');
	//createMenu('store');
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// expanders
	Ext.getBody().on('click', function(e, t){
		t = Ext.get(t);
		e.stopEvent();
		
		var bd = t.next('div.expandable-body');
		bd.enableDisplayMode();
		var bdi = bd.first();
		var expanded = bd.isVisible();
		
		if(expanded){
			bd.hide();
		}else{
			bdi.hide();
			bd.show();
			bdi.slideIn('l', {duration:.2, stopFx: true, easing:'easeOut'});	
		}
		
		t.update(!expanded ? 'Hide details' : 'Show details');
			
	}, null, {delegate:'a.expander'});
	
	var gs = Ext.get('gsearch-box');
	if(gs){
		gs.on('focus', function(){
			if(gs.getValue() == 'Search with google'){
				gs.dom.value = '';
				gs.up('div').addClass('gs-active');
			}
		});
		
		gs.on('blur', function(){
			if(gs.getValue() == ''){
				gs.dom.value = 'Search with google';
				gs.up('div').removeClass('gs-active');
			}
		});
		
		if(gs.getValue() == ''){
			gs.dom.value = 'Search with google';
		}
	}
	
    
	
	// messages
	var msgs = [
		// {text: 'Ext Enterprise Training Now Available &raquo;', url:'http://extjs.com/support/training/'},
		// {text: 'Ext GWT 2.1.1 Release Now Available &raquo;', url:'http://extjs.com/products/gxt/download.php'},
		// {text: 'Ext 3.1.1 Release Now Available &raquo;', url:'http://extjs.com/products/extjs/download.php'},
		{text: 'Ext JS is now Sencha &raquo;', url:'http://www.sencha.com/blog/?p=3356'},
		{text: 'Introducing Sencha Touch &raquo;', url:'http://www.sencha.com/blog/2010/06/17/introducing-sencha-touch-html5-framework-for-mobile/'},
		{text: 'Watch Ext Designer Screencast &raquo;', url:'http://www.sencha.com/blog/2010/04/22/ext-designer-released-watch-the-screencast/'},
		{text: 'Ext JS 3.2.1 Now Available &raquo;', url:'http://www.sencha.com/products/js/download.php'},
		{text: 'New Ext JS Themes Example &raquo;', url:'http://www.sencha.com/products/js/themes.php'}
	];
	
	var msgIndex = 0;
	var msg = Ext.get('msg'),
		msgInner = Ext.get('msg-inner'), 
		active = null;
		
        if (msgInner) {
	    msgInner.addClassOnOver('msg-over');
        }
	
        if (msg) {
	    msg.on('click', function(){
	       window.location = active.url;
	    });
        }
	
	function doUpdate(){
            if (msgInner) {
	        msgInner.update(active.text);
            }
            if (msg) {
	        msg.slideIn('b');
            }
	}
	
	function showMsg(index){
            if(msgInner && !msgInner.hasClass('msg-over')) {
                active = msgs[index];
		if(msg && msg.isVisible()){
		    msg.slideOut('b', {callback: doUpdate});
		}else{
    		doUpdate();	
		}			
            }
	}
	
	setInterval(function(){
        msgIndex = msgs[msgIndex+1] ? msgIndex+1 : 0;
        showMsg(msgIndex);      
    }, 5000);
    showMsg(0);
	
    
    var hd = Ext.get('hd');
    if(hd){
        hd.createChild({tag:'a', cls:'extcon', href: 'http://extjs.com/conference/'});
    }
    
});

