/**
 * @author JJJ
 */
var mooGlobal = {};
mooGlobal.currentTab = null;

window.addEvent('domready', function() { 	
	//inputTextReplace('searchForm','Site Search','#4F7B84','#09651B');
	
	mooGlobal.theMooMenu = new MooMenu({ 
		roundedCorners:false, 
		effect:'slide & fade', 
		duration:250, 
		transition:Fx.Transitions.Cubic.easeInOut, 
		opacity:95, 
		subsequentSubMenuYoffset:-7, 
		initialSubMenuYoffset:-6 
	});	

	//myArray.merge(array);
	var linksArr = $('nav').getElements('a').merge($('header').getElements('a')).merge($('subMenusContainer').getElements('a'));

	//mooGlobal.theSlideNav = new MooSlideNav({ links:linksArr, contentId:'content', width:706 });
	
	$(document.body).getElements('.box-o-tron div').each(function(item,index){
		makeBoxOtron(item);
	});
	

	
});

function makeBoxOtron(box){
	if(!box){return;}
	var hoverEffect = new Fx.Morph(box, {
		link: 'cancel',
		duration: 2000,
		transition: Fx.Transitions.Expo.easeOut
	});
	
	hoverEffect.set({'background-position': '0 0'	});
	box.addEvents({
			'mouseenter': function(e){
				e = new Event(e).stop();
				hoverEffect.start( '.over'	);
			},
			'mouseleave': function(e){
				e = new Event(e).stop();
				hoverEffect.start({
					'background-position': '0 0'
				});
			}
		});
	
}



function inputTextReplace(id, defaultValue, blurColor, focusColor){
	if($(id).getElement('input[type=text]').value.clean() === "" || $(id).getElement('input[type=text]').value.clean() === " " ){
		$(id).getElement('input[type=text]').value = defaultValue;
	}
	$(id).getElement('input[type=text]').addEvents({
		'focus': function(e){
		    var e = new Event(e);
			if(this.value === defaultValue){ this.value = ""; this.setStyle('color',focusColor); }
		},
		'blur': function(e){
			var e = new Event(e);
			if (this.value.clean() === "" || this.value.clean() === " ") {	this.value = defaultValue; this.setStyle('color',blurColor);	}
		}
	})
}
/*
var SlideNav = new Class(
{
	options: {
	   links:[],
       //navId:'',
	   contentId:'',
	   linkClassName:null,
	   width:400,
	   duration:1500,
	   transition:Fx.Transitions.Back.easeInOut,
	  //transition:Fx.Transitions.linear,
	   opacity:90,
	   defaultPage:'default.aspx?id=1'
	  
    },
	//mainObj:null,
	
	popLinksArr: [],
	linkId_BoxObj_Arr: [],
	boxId_LinkObj_Arr:[],
	loadedOnceArr:[],
	scroll:null,
	stretch:null,
	slide:null,
	currentPopItem:0,
	
    initialize: function(options){
		if(Browser.Engine.trident4 ){//disable for IE6
			return ;
		}
		this.setOptions(options);
		//this.mainObj = $(this.options.navId) || document.body;
		//alert(window.location.toString().toLowerCase());
		if(!window.location.toString().toLowerCase().contains('default.aspx') && window.location.toString().charAt(window.location.toString().length-1) != "/" ){return;}

		new Element('iframe', { 'id': 'historyFrame','name':'historyFrame'	}).inject(document.body,'inside');
		new Element('div', { 'id': 'scrubArrow'	}).inject($('subMenusContainer'));
		
		//disable effects for safari
		if(window.webkit || window.webkit419 || window.webkit420){
			this.options.transition = null;
			this.options.duration = null;
		}
		
		//this.popLinksArr = $(this.options.navId).getElements('a');
		this.popLinksArr = this.options.links;
		
		if(this.options.linkClassName){
			this.popLinksArr = this.popLinksArr.filter(function(item, index){ return item.hasClass(this.options.linkClassName); }.bind(this));
		}
		if(this.popLinksArr.length<=0){return;}
		
		this.popLinksArr.each(function(item, index){
			item.href = item.href.toLowerCase().replace('default.aspx','pagecontent.aspx');
		}.bind(this));
		
		$(this.options.contentId).innerHTML = "";
		
		var popBoxOW = new Element('div', { 'id': 'popBoxOuterWrapper'	}).setStyles({'opacity':this.options.opacity/100,'width':this.options.width}).inject($(this.options.contentId),'inside');

		var popBoxIW = new Element('div', { 'id': 'popBoxInnerWrapper'	}).inject(popBoxOW,'inside');
		
		var progressBox = new Element('div', { 'id': 'progressBox'	}).inject(popBoxOW,'after');
		
		this.scroll = new Fx.Morph('popBoxInnerWrapper',{
			duration: this.options.duration,
			transition: this.options.transition,
			link: 'cancel' 
		});
		
		this.stretch =  new Fx.Morph('popBoxInnerWrapper', {
			duration: (this.options.duration/4), 
			transition: this.options.transition, 
			link: 'cancel' 
		});
		
		this.fade =  new Fx.Morph('progressBox', {
			duration: (this.options.duration/3), 
			transition: Fx.Transitions.linear,
			link: 'cancel'
		});
		
		this.scrub =  new Fx.Morph('scrubArrow', {
			duration: (this.options.duration/4), 
			transition: this.options.transition,
			link: 'cancel' 
		});
		
		
			
		this.popLinksArr.each(function(item, index){
			
			var popItemOW = new Element('div', { 'class': 'popItemOW'	}).setStyles({'width':this.options.width-30, 'left':(this.options.width*index)}).inject($('popBoxInnerWrapper'),'inside');
			var popItemIW = new Element('div', { 'class': 'popItemIW'	}).setStyle('diplay','block').inject(popItemOW,'inside');
			
			this.linkId_BoxObj_Arr[this.getOrSetId($(item))] = $(popItemIW);
			this.boxId_LinkObj_Arr[this.getOrSetId($(popItemIW))] = $(item);
			
			item.addEvents({
			
				'click' : function(e){
					e = new Event(e).stop();

					var page = item.href.toString().split('/').getLast();
					
					//if you are already on that link don't go to it again
					if(page.toLowerCase() === this.popLinksArr[this.currentPopItem].href.toString().toLowerCase().split('/').getLast()){
						return
					}
					
					
					page = page.toLowerCase().replace('default.aspx','pagecontent.aspx');
					
					window.location = '#'+page;
					frames['historyFrame'].location.href = page;

					//$('progressBox').setStyle('display','block');
					//this.fade.set({'opacity':0.5});

				}.bind(this)
			});
			
		}.bind(this));
		
		
		
		this.goTo(window.location.toString());
		this.lastLocationAddressBar = [0,window.location.toString()];
		this.lastLocation = [0,frames['historyFrame'].location.href];
		this.checkForChange.periodical(500,this);
		
	},
	
	highlightTab:function(currentURL){
		
		//currentURL = currentURL.toLowerCase().replace('pagecontent.aspx','default.aspx');
		
		
		var highlightedTab = false;
		var highlightedTab2 = false;
		this.popLinksArr.each(function(item,index){
			if(item.getParent('li') && item.getParent('li').getParent().id && item.getParent('li').getParent().id ==='nav'){
				item.getParent('li').removeClass('current');
				
				if(item.href && item.href.toLowerCase().contains(currentURL.toLowerCase())){
					item.getParent('li').addClass('current');
					
					var newLeft = item.getCoordinates().left + (item.getCoordinates().width/2) + 3 ;//- 442;
					this.scrub.start({'left':newLeft});
					highlightedTab = true;
				}
			}
		}.bind(this));
		
		 if(!highlightedTab){
			if(mooGlobal.currentTab){ currentURL = mooGlobal.currentTab.href; }
			this.popLinksArr.each(function(item,index){
			if(item.getParent('li') && item.getParent('li').getParent().id && item.getParent('li').getParent().id ==='nav'){
					item.getParent('li').removeClass('current');
					
					if(item.href && item.href.toLowerCase().contains(currentURL.toLowerCase())){
						item.getParent('li').addClass('current');
						
						var newLeft = item.getCoordinates().left + (item.getCoordinates().width/2) + 3;// - 442;
						this.scrub.start({'left':newLeft});
						highlightedTab2 = true;
					}
				}
			}.bind(this));
			
		}
		
		
		//for when you land on a childmenu page (or refresh while on a childmenu page)
		if(!highlightedTab && !highlightedTab2 && !mooGlobal.currentTab){
			var match = null;
			mooGlobal.theMooMenu.classElHash.each(function(item,index){
				item.childMenu.getElements('a').each(function(item2,index2){
					if(	item2.href.toLowerCase().contains(currentURL.toLowerCase()) || currentURL.toLowerCase().contains(item2.href.toLowerCase()) ){
						match = item;
					}
				}.bind(this));
			}.bind(this));
			if(match && !match.sectionBtn){ match.setSectionBtn();	}
			mooGlobal.currentTab = match.sectionBtn;
			
			if(mooGlobal.currentTab){ currentURL = mooGlobal.currentTab.href; }
			this.popLinksArr.each(function(item,index){
			if(item.getParent('li') && item.getParent('li').getParent().id && item.getParent('li').getParent().id ==='nav'){
					item.getParent('li').removeClass('current');
					
					if(item.href && item.href.toLowerCase().contains(currentURL.toLowerCase())){
						item.getParent('li').addClass('current');
						
						var newLeft = item.getCoordinates().left + (item.getCoordinates().width/2) +3 ;//-3 - 442;
						this.scrub.start({'left':newLeft});
						
						highlightedTab = true;
					}
				}
			}.bind(this));
		}
		//end
		

		
		
	},
	
	checkForChange:function(){
		if(this.lastLocationAddressBar.getLast().toLowerCase() != window.location.toString().toLowerCase()){
			//console.log('A');
			this.goTo(window.location.toString().toLowerCase());
			//console.log('/A');
			
		}else if(this.lastLocation.getLast().toLowerCase() != frames['historyFrame'].location.href.toLowerCase()){
			//console.log('B');
			var page = frames['historyFrame'].location.href.toString().split('/').getLast();
			window.location = '#'+page;
			this.goTo(frames['historyFrame'].location.href.toLowerCase());
			//alert('frame location: '+frames['historyFrame'].location.href + ' | address bar: '+ window.location.toString());
			//console.log('/B');
		}
		
		this.lastLocation.push(frames['historyFrame'].location.href.toLowerCase());
		this.lastLocationAddressBar.push(window.location.toString().toLowerCase());
	},
	
	
	goTo:function(url){
		//console.log(url);
		
		var newURL = url || this.options.defaultPage;

		if(newURL.toString().charAt(newURL.toString().length-1) === "/"){
			newURL = this.options.defaultPage;
		}		

		if(newURL.toString().charAt(newURL.toString().length-1) != "/" ){
			newURL = newURL.toString().split('/').getLast().toLowerCase();	
		}
		if(self.document.location.hash.substring(1)){
			newURL = self.document.location.hash.substring(1).toLowerCase();
		}
				
		//this.highlightTab(newURL);
		
		var targetItem = null;
		
		this.popLinksArr.each(function(item, index){
			var currentHREF = item.href.toString().split('/').getLast().toLowerCase();
			if (newURL === currentHREF) {
				targetItem = item;
				this.currentPopItem = index;
				return;
			}
		}.bind(this));
		
		if(!targetItem){return;}
		
		if(!this.loadedOnceArr[targetItem.id]){	
			this.loadPageContent(targetItem); 
			this.loadedOnceArr[targetItem.id] = true;
			console.log(targetItem.id+' has NOT been loaded');
		}else{
			console.log(targetItem.id+' has already been loaded');
			this.scroll.start({'margin-left':-(this.options.width*this.currentPopItem)}).chain(function(){
						this.stretchBox(targetItem);
						//this.fade.start({'opacity':0}).chain(function(){
							//$('progressBox').setStyle('display','none');
							//this.stretchBox(targetItem);
						//}.bind(this));
					}.bind(this));
		}
			
	},
	
	
	loadPageContent:function(link){
		//if(!link.href || !link.href.toLowerCase().contains('default')){ return }
		if(!link.href || link.href === '#' ){ return }
		
		//var hrefArr = link.href.split('=');
		//var node_id = hrefArr.getLast();
		var popItemIW = this.linkId_BoxObj_Arr[link.id];
		
		//var urlToLoad = 'pageContent.aspx?id='+node_id;
		//if(!link.href.toLowerCase().contains('default.aspx')){urlToLoad = link.href;}
		var urlToLoad = link.href.toLowerCase().replace('default.aspx','pagecontent.aspx');
		console.warn(urlToLoad);
		
		var myHTMLRequest = new Request({
			'url':urlToLoad,
			'onSuccess': function(response){
				
				(function(){ 
					
					var content = response;
					if (response.contains("<!-- begin content -->") && response.contains("<!-- end content -->")) {
						content = response.split("<!-- begin content -->").getLast().split("<!-- end content -->")[0];
					}
					popItemIW.innerHTML = content;
					this.loadedOnceArr[popItemIW.id] = true;
					
					
					
					this.scroll.start({'margin-left':-(this.options.width*this.currentPopItem)}).chain(function(){
						this.stretchBox(link);
						//this.fade.start({'opacity':0}).chain(function(){
						
							//$('progressBox').setStyle('display','none');
							//this.stretchBox(link);
						//}.bind(this));
					}.bind(this));
 
				}.bind(this)).delay(500);
				
			}.bind(this)
		}).send();
	},
	
	
	
	stretchBox:function(link){
		var targetHeight = $(this.linkId_BoxObj_Arr[link.id]).getCoordinates().height + 30;//30 for padding 20 for tools 10 for fudge factor
		this.stretch.start({   'height': targetHeight	 }).chain(function(){
			this.highlightTab(link.href);
		}.bind(this));
	},
	getOrSetId: function(obj){
		if(!obj.id){
			var d = new Date();
			var milli = d.getMilliseconds().toString();
			var randomNumber = Math.floor(Math.random()*1000);
			if(obj.nodeName){
				obj.id = obj.nodeName.toString() + '-' + milli + randomNumber.toString();
			}else{
				obj.id = "element" + milli + randomNumber.toString();	
			}
		}
		return obj.id;
	}
	
});


SlideNav.implement(new Options);
SlideNav.implement(new Events);
*/