/**
 * FAME Assets web site JS functions
 */

function trim(strText) 
{ 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

function CheckRequired(R)
{
	if (!R) return true;
	
	if (trim(R.value) == "") {
		alert(R.name + " is a required field");
		R.focus();
		return false;
	}
 	return true;
}

function ValidateEmail(str)
{
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    return true;
  }
  return false;
}

function CheckEmail(field)
{
  var str = field.value; // email string
  if (ValidateEmail(str) == true) return true; 
  alert("\"" + str + "\" is an invalid e-mail! Please enter a valid Email address."); // this is also optional
  field.focus();
  field.select();
  return false;
}


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();
		
		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;
			
			if (!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);				
			}
		});
		
		el.on('mouseout', function(e){
			if(tid && !e.within(el, true)){
				clearTimeout(tid);
				tid = 0;				
			}
		});
	}
	
	createMenu('products');
	createMenu('services');
	createMenu('support');
	createMenu('partners_resellers');
	createMenu('company');
	
	// 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'});
		
	InitMessages();
	InitAds();
	
});

function InitMessages()
{
	// messages
	if (typeof msgs == "undefined") return;
/*	var msgs = [
		{text: 'FAME Version 6.0 Released &raquo;', url:'news.php'},
		{text: 'New - Energy &amp; Sustainability Module &raquo;', url:'fame_sus.php'},
		{text: 'Online or onsite training available &raquo;', url:'fame_training.php'}  
	]; */
	
	var msgIndex = 0;
	var msg = Ext.get('msg'),
		msgInner = Ext.get('msg-inner'), 
		active = null;
		
	msgInner.addClassOnOver('msg-over');
	
	msg.on('click', function(){
		window.location = active.url;
	});
	
	function doUpdate(){
		msgInner.update(active.text);
		msg.slideIn('b');
	}
	
	function showMsg(index){
		if(!msgInner.hasClass('msg-over')) {
			active = msgs[index];
			if(msg.isVisible()){
				msg.slideOut('b', {callback: doUpdate});
			}else{
				doUpdate();	
			}			
		}
	}
	setInterval(function(){
		msgIndex = msgs[msgIndex+1] ? msgIndex+1 : 0;
		showMsg(msgIndex);		
	}, 5000);
	
	showMsg(0);
}

function InitAds()
{
	var ad = Ext.get('ad');
	if (!ad) return;
	
	// messages
	if (typeof(Ads) == "undefined") return;
		
	var adIndex = 0;
	var adInner = Ext.get('ad-inner'), 
		adActive = null;
	var timerID = null;
		
	adInner.addClassOnOver('ad-over');
	
	ad.on('click', function(){
		if (adActive.url == '#') {
			nextAd();
		} else window.location = adActive.url;
	});
	
	function doUpdate(){
		var t = adActive.text;
		if (adActive.url == '#')
			t += '<img class="nxtarrow" title="Advanced to next image" src="images/arrow-right.gif" style="float:right;position:relative;top:-4px;right:0px;">';
		adInner.update(t);
		switch(adActive.effectIn || null) {
			case 'slide-r':
				ad.slideIn('r');
				break;
			case 'slide-t':
				ad.slideIn('r');
				break;
			default:
				ad.fadeIn({duration: .7,useDisplay:true});
				break;
		}
	}
	
	function showAd(index){
	//	if(!adInner.hasClass('ad-over')) {
			adActive = Ads[index];
			if(ad.isVisible()) {
				switch(adActive.effectOut || null) {
					case 'slide-b':
						ad.slideOut('b', {callback: doUpdate});
						break;
					case 'slide-l':
						ad.slideOut('l', {callback: doUpdate});
						break;
					default:
						ad.fadeOut({duration: .5,callback:doUpdate,useDisplay:true});
						break;
				}
			} else {
				doUpdate();	
			}			
	//	}
	}
	
	function nextAd() {
		clearInterval(timerID);
		adIndex = Ads[adIndex+1] ? adIndex+1 : 0;
		timerID = setInterval(nextAd, 7000);
		showAd(adIndex);
	}
	
	timerID = setInterval(nextAd, 7000);
	
	showAd(0);
}