window.addEvent('domready', function() {
	
	var theMooTree = new mooTree({ id:'nav', transition:Fx.Transitions.Circ.easeInOut });
	//var thepopBox = new PopBox();
	
});

var PopBox = new Class({
	options: {
       id:'',
	   linkClassName: '.popBox',
	   width:430,
	   duration:1500,
	   transition:Fx.Transitions.Back.easeInOut,
	   opacity:87,
	   parentEltoColor:null,
	   highlightTextColor:'#99CC33',
	   defaultTextColor:'#716E65'
    },
	mainObj:null,
	
	popLinksArr: [],
	linkId_BoxObj_Arr: [],
	boxId_LinkObj_Arr:[],
	loadedOnceArr:[],
	scroll:null,
	stretch:null,
	slide:null,
	currentPopItem:0,
	
    initialize: function(parentEltoColor){
		if(parentEltoColor){this.options.parentEltoColor = parentEltoColor;}
		//if(options){this.setOptions(options);}
		this.mainObj = $(this.options.id) || document.body;
		
		//disable effects for safari
		if(window.webkit || window.webkit419 || window.webkit420){
			this.options.transition = null;
			this.options.duration = null;
		}
		
		
		this.popLinksArr = $ES(this.options.linkClassName, this.options.id);
		if(this.popLinksArr.length<=0){return;}
		
		var popBoxOW = new Element('div', { 'id': 'popBoxOuterWrapper'	}).setStyles({'opacity':this.options.opacity/100,'width':this.options.width}).inject(document.body,'inside');
		var popBoxIW = new Element('div', { 'id': 'popBoxInnerWrapper'	}).inject($('popBoxOuterWrapper'),'inside');
		var toolsDiv = new Element('div', { 'id': 'tools'	}).inject($('popBoxOuterWrapper'),'inside');
		var nextBtn = new Element('div', { 'id': 'nextBtn'	}).inject($('tools'),'inside');
		var backBtn = new Element('div', { 'id': 'backBtn'	}).inject($('tools'),'inside');
		var closeBtn = new Element('div', { 'id': 'closeBtn'	}).inject($('tools'),'inside');
		
		if(window.ie){
			//popBoxOW.style.filter= "progid:DXImageTransform.Microsoft.blur(pixelradius=2.2, makeshadow='true', ShadowOpacity=30)";
		}
		
				
		
		this.scroll = new Fx.Style('popBoxInnerWrapper', 'margin-left', {
			duration: this.options.duration,
			transition: this.options.transition,
			wait:false
		});
		
		this.stretch = new Fx.Style('popBoxOuterWrapper', 'height', {
			duration: this.options.duration,
			transition: this.options.transition,
			wait:false
		});
		
		this.slide = new Fx.Styles('popBoxOuterWrapper', {
			duration: this.options.duration/2,
			transition: this.options.transition,
			wait:false
		});
		
		closeBtn.addEvents({
			'click' : function(e){
				e = new Event(e).stop();
				$('popBoxOuterWrapper').setStyles({'display':'none','visibility':'hidden'});
				this.colorText();
			}.bind(this)
		});
			
		nextBtn.addEvents({
			'click' : function(e){
				e = new Event(e).stop();
				this.next();
			}.bind(this)
		});
		
		backBtn.addEvents({
			'click' : function(e){
				e = new Event(e).stop();
				this.back();
			}.bind(this)
		});
		
		
		
		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'));
			var popItemIW = new Element('div', { 'class': 'popItemIW'	}).inject(popItemOW);
			
			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();
					
					this.currentPopItem = index;
					var item2= item;
					var index2 = index;
					if(item.getParent().getTag() === "h3" || item.getParent().getTag() === "h5"){
						item2 = this.popLinksArr[index+1];
						index2 = index +1;
						//this.currentPopItem = index+1;
						//this.next();
						//return;
					}
					
					$('popBoxOuterWrapper').setStyles({'display':'block','visibility':'visible'});
					this.positionPopBox(item2);
					
					
					if(!this.loadedOnceArr[popItemIW.id]){	
						this.loadPageContent(item2); 
					}else{
						this.stretchBox(item2);
					}
					
					this.scroll.start(-(this.options.width*index2));
					
					this.colorText(item2);
					
				}.bind(this)
			});
			
		}.bind(this));
		
		
		
	},
	
	colorText:function(link){
		if(this.options.parentEltoColor){
			this.popLinksArr.each(function(item, index){
				if(item.getParent().getPrevious().getTag() === this.options.parentEltoColor){
					
					item.getParent().getPrevious().getFirst().setStyle('color',this.options.defaultTextColor);
				}
				
			}.bind(this));
			
			if (link && link.getParent().getPrevious() && link.getParent().getPrevious().getTag() === this.options.parentEltoColor) {
				link.getParent().getPrevious().getFirst().setStyle('color', this.options.highlightTextColor);
			}
		}else{
			this.popLinksArr.each(function(item, index){
				if(item.getParent().getFirst() && item.getParent().getFirst().getFirst()){
					item.getParent().getFirst().getFirst().setStyle('color',this.options.defaultTextColor);
				}	
				
			
			}.bind(this));
			if (link && link.getParent().getFirst().getFirst()) {
				//console.log(link.getParent().getFirst().getFirst());
				link.getParent().getFirst().getFirst().setStyle('color',this.options.highlightTextColor);
			}
		}
	},
	
	stretchBox:function(link){
		var targetHeight = $(this.linkId_BoxObj_Arr[link.id]).getCoordinates().height + 60;//30 for padding 20 for tools 10 for fudge factor
		this.stretch.start(targetHeight).chain(function(){
			//this.positionPopBox(link);
		}.bind(this));
		
	},
	
	loadPageContent:function(link){
		if(!link.href || !link.href.contains('default.aspx')){return}
		var hrefArr = link.href.split('=');
		var node_id = hrefArr.getLast();
		var popItemIW = this.linkId_BoxObj_Arr[link.id];

		new Ajax('pageContent.aspx?id='+node_id, {
		update: popItemIW,
		method: 'get',
		onComplete: function(result) {
			popItemIW.getParent().setStyle('background',"#fff");
			this.loadedOnceArr[popItemIW.id] = true;
			this.stretchBox(link);
			this.positionPopBox(link);
			
		}.bind(this)
		}).request();
	},
	
	positionPopBox:function(link){
		//$('popBoxOuterWrapper').style.left = link.getCoordinates().right+'px';
		//$('popBoxOuterWrapper').style.left = 710+'px';
		//$('popBoxOuterWrapper').style.top = link.getCoordinates().bottom+'px';
		
		var top=link.getCoordinates().bottom;
		var left=link.getCoordinates().right;
		var height = $(this.linkId_BoxObj_Arr[link.id]).getCoordinates().height + 60;
		
		var difference = 0;

		/*if(top+height >= window.getScrollHeight() ){
			difference = (top+height) - window.getScrollHeight() +50;//50 for padding
		}*/
		
		if(top+height >= $('footer').getCoordinates().top ){
			difference = (top+height) - $('footer').getCoordinates().top ;
		}
		
		if(!this.ranceOnce){
			this.ranceOnce = true;	
			this.slide.set({
				'top': top - difference,
				'left': left
			});
		}
		
		this.slide.start({
			'top': top - difference,
			'left': left
		});
		
		
	},
	
	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;
	},
	
	next:function(){
		this.currentPopItem++;
		if(this.currentPopItem >= this.popLinksArr.length){
			this.currentPopItem = 0;
		}
		var link = this.popLinksArr[this.currentPopItem];
		
		if(link.getParent().getTag() =='h3' || link.getParent().getTag() =='h5'){
			this.next();
			return;
		}
		
		var popItemIW = this.linkId_BoxObj_Arr[link.id];
		if(!this.loadedOnceArr[popItemIW.id]){	
			this.loadPageContent(link); 
		}else{
			this.stretchBox(link);
		}
		
		this.scroll.start(-(this.options.width*this.currentPopItem));
		this.positionPopBox(link);
		this.colorText(link);
	},
	back:function(){
		this.currentPopItem--;
		if(this.currentPopItem < 0){
			this.currentPopItem = this.popLinksArr.length-1;
		}
		var link = this.popLinksArr[this.currentPopItem];
		
		if(link.getParent().getTag() =='h3' || link.getParent().getTag() =='h3'){
			this.back();
			return;
		}
		
		var popItemIW = this.linkId_BoxObj_Arr[link.id];
		if(!this.loadedOnceArr[popItemIW.id]){	
			this.loadPageContent(link); 
		}else{
			this.stretchBox(link);
		}
		
		this.scroll.start(-(this.options.width*this.currentPopItem));
		this.positionPopBox(link);
		this.colorText(link);
	}
	
});