//set main ajax paths and methods
sb.ajax.defaultURL = '../data/index.php';
sb.ajax.defaultMethod = 'post';

//instantiate the pages object
var pages = {
	
	//all the list items in drop downs
	listItems : [],
	
	chainId : '',
	
	loadDropDownFromServer : function(e){
		var self=this;
		
		new sb.ajax({
			debug : 0,
			format :'json',
			data : 'c=get_children&pid='+this.pid,
			handler : function(r){
				self.info.childList = r;
				pages.childListToHTML.call(self, e);
			}
		}).fetch();
	
	},
	
	loadPage : function(){
		
		new sb.ajax({
			debug : 0,
			data : 'c=get_page&pid='+current_page,
			format : 'js',
			handler : function(r){
				try{
					if(pages.checkForRedirect() !=1){
						
						pages.createPage();
						
					}
					
				}catch(e){
					
					if($_GET['debug']){
						$('body').innerHTML = sb.objects.dump(page, 1);
						$('body').innerHTML +=sb.objects.dump(e, 1);
					}
				}
			}
		}).fetch();
	},
	
	preLoadFromServer : function(li){
		
		new sb.ajax({
			debug : 0,
			format :'json',
			data : 'c=get_children&pid='+li.pid,
			handler : function(r){
				li.info.childList = r;
				
			}
		}).fetch();
	
	},
	
	//update page naviagtion
	updateNav : function(){
		new sb.ajax({
			data : 'c=get_nav&pid='+page.pid,
			debug : 0,
			format : 'js',
			async: 0,
			handler: function(r){
				pages.removeAllDropDowns();
				pages.breadCrumbsToHTML();
				pages.navMainToHTML();
			}
		}).fetch();
		
	},

	hideAllDropDowns : function(){
		$('#nav_main_box ul').forEach(function(v,k,a){
			
			if(typeof v.chainId !='undefined'){
					v.hide();
			}
		});
	},
	
	removeAllDropDowns : function(){
		$('nav_main_box ul').forEach(function(v,k,a){
			
			if(typeof v.chainId !='undefined'){
				
					v.remove();
			}
		});
	},
	
	listItemAddStyles : function(){
		
		pages.listItems.push(this);
		
		pages.hasChildren.call(this);
		
		if(this.info.redirect !=undefined){
			this.title = 'OUTSIDE LINK TO: '+this.info.redirect;
			this.firstChild.data +='^';	
		}
		
		this.styles({
			cursor:'pointer',
			display : 'block'
		});
	},
	
	//checks for children lists and preloads them
	hasChildren : function(){
		
		if(this.info.children >0){
			
			if(!this.firstChild.nodeValue.match(pages.symbols.expandable)){
				this.innerHTML+=' '+pages.symbols.expandable;
			}
			
			this.childList = null;
			pages.preLoadFromServer(this);
		}
	},
	
	//returns order lists
	childListToHTML : function(e){
		
		if(typeof this.info.childList =='undefined'){
			if(typeof this.loading =='undefined'){
				this.loading =1;
			}
			
			return;
		}
		
		if($('#dropd'+this.pid)){
			$('#dropd'+this.pid).show();
			return;
		}
		
		var t=this;
		
		var capsule = new sb.element({
			nodeName :'capsule',
			className : 'capsule'
		});
		
		var dropDown = new sb.element({
			nodeName :'ul',
			className : 'dropDown',
			chainId : pages.chainId,
			pid :this.pid,
			id : 'dropd'+this.pid
		});
		
		dropDown.appendTo(pages.navMainBox);
		
		this.info.childList.forEach(function(v){
			
			//info includes all props of v
			var it=new sb.element({
				nodeName: 'li', 
				innerHTML : v.title,
				info : v,
				chainId : pages.chainId,
				parentPid : t.pid,
				kind : 'drop',
				pid : v.pid,
				location : '../app/index.php?pid='+v.pid
			});
			
			pages.preLoadFromServer(it);
			pages.listItemAddStyles.call(it);
	
			it.appendTo(dropDown);
			
		});
		
		
		
		if(typeof this.getX =='function'){
			
			var x = this.getX()+this.offsetWidth;
			
			if(sb.browser.ie6 ===1 && this.parentNode == pages.navMain){
				x += s$('#parent').css('border').toNumber();
			}
			
			var y = this.getY();
			y -= this.css('margin').toNumber();
			dropDown.mv(x, y,  999);
			
		}
		
			dropDown.style.display='block';
	},

	removeLis : function(){
		$(this, 'li').forEach(function(v){
			sb.dom.remove(v);
		});
	},
	
	navMainToHTML : function(){
		
		//if there is main nav data create a new main nav
		if(page.navMain != null){
			
			//remove the main nav or create it
			if(typeof this.navMain !='undefined'){
				this.removeLis.call(this.navMain);
			} else {
				this.navMainBox.innerHTML ='';
				
				this.navMain = new sb.element({
					nodeName:'ul',
					id : 'nav_main',
					pid : 0
				});
			}
			
			pages.navigation = this.navMain.appendToTop(this.navMainBox);
			
			//create all the links
			page.navMain.forEach(function(v,k,a){
				
				//info includes all props of v
				var li=new sb.element({
					nodeName: 'li',
					innerHTML : v.title.replace(/\|/, "<br />"),
					info : v,
					kind : 'nav_main',
					pid : v.pid,
					chainId : v.pid,
					id : 'pid'+v.pid,
					location : '../app/index.php?pid='+v.pid,
					className : (v.pid == page.pid) ? 'selected' : ''
				});
				
				li.event('mouseover', function(){
					pages.chainId = v.pid;
				});
			
				li.appendTo(pages.navMain);
				
				pages.listItemAddStyles.call(li);
				
			});
			
			if(typeof onnavmainload =='function'){
				onnavmainload(this.navMainBox);
			}
			//<img src="http://www.rideforroswell.org/media/54/Register.png" />
			if(typeof page.breadCrumbs!='undefined'){
				this.breadCrumbsToHTML();
			}
			
		}
	},

	
	breadCrumbsToHTML : function(){

		if(typeof this.breadCrumbs !='undefined'){
			
			this.removeLis.call(this.breadCrumbs);
		}
	
		page.breadCrumbs.forEach(function(v,k){
			
			//set the page root for hilighting in main nav
			if(k ==0){
				page.root = {
					pid : v.pid,
					title : v.title
				}
				
				if($('#pid'+v.pid)){
					$('#pid'+v.pid).pid = v.pid;
					$('#pid'+v.pid).className = 'selected';
				}
			}
			
			//create breadcrumbs
			var li = new sb.element({
				nodeName :'li'
			});
			
			pages.breadCrumbs.appendChild(li);	
		
			var lnk = new sb.element({
				nodeName :'a',
				id : 'crumb'+v.pid,
				pid : v.pid,
				href : '../app/index.php?pid='+v.pid
			});
			
			li.appendChild(lnk);
			
			if(v.pid == page.pid){
				lnk.className += ' selectedCrumb';
			}
			
			lnk.innerHTML +=pages.symbols.breadCrumbSeparator;
			
			lnk.appendChild(txt(v.title.replace(/\|/, "")));
			
			lnk = li = null;
			
			
		});

		if(typeof pages.author !='undefined'){
			pages.author.addNewChildInput();
		}
	
		
	},

	checkForRedirect : function(){
	
		if(typeof pages.author == 'undefined'){
			
			if(page.redirect != 'null' && page.redirect !=null){
			 
				window.location = page.redirect;
				return 1;
			 }
		}
		return 0;
	},
	
	fixNavMainHeight: function(){
		var cw = $('#content_wrapper');
		if(cw.offsetHeight < 500){
			cw.style.height='500px';
		}
		$('#nav_main_box').style.height = cw.offsetHeight+'px';

	},
	
	queryServer : function(query){
		var aj = new sb.ajax({
			data : 'c=search&q='+query,
			debug : 0,
			handler : function(results){
				
				pages.liveSearch.lastChild.innerHTML=results;
				pages.liveSearch.mv(pages.search.getX(), pages.search.getY()+25,999);
				pages.liveSearch.appendTo('body');
				pages.liveSearch.show();
			}
		});
		aj.fetch();
	},
	
	liveSearch : new sb.element({
		tag : 'div',
		id : 'search_results',
		
		children : [
		{ tag : 'button', 
			innerHTML : 'close',
			className : 'close',
			events : {
				click : function(){
					this.parentNode.hide();
				}
			}
		},
		{ tag : 'ol'}
		]
	}),

	//initialize the page
	createPage : function(){
		//this.pageTitle.show();
		
		this.breadCrumbsToHTML();
		
		if(typeof pages.author !='undefined'){
			
			pages.author.init();
		}
		
		
		return;
	},
	
	addEvents : function(){
		
		this.navMainBox.events({
			
			mousedown : function(e){
				var target = sb.events.target(e);
				
				if(target.nodeName == 'LI'){
					
					
					if(target.info.redirect != 'null' && target.info.redirect !=null && typeof pages.author == 'undefined'){
						if(pages.config.redirectTarget =='_blank' && target.info.redirect){
							window.open(target.info.redirect);
						} else {
							window.location = target.info.redirect;
					 	}
					 
					} else {
						window.location = target.location;
					}
				}
			},
			
			mouseover : function(e){
				var target = sb.events.target(e);
				
				if(target.nodeName =='LI' && target.info.childList !==null){
				
					target.addClassName('li_rollover');
		
					//clean any left over dropdowns
					if(target.kind == 'nav_main'){
						pages.hideAllDropDowns();
					}
					
					for(var x=0;x<target.parentNode.childNodes.length;x++){
						if(typeof target.parentNode.childNodes[x].pid !='undefined'){
							
							if($('#dropd'+target.parentNode.childNodes[x].pid)){
								$('#dropd'+target.parentNode.childNodes[x].pid).hide();
							}
						}
					}
					
					if(target.info.childList !==null){
						pages.childListToHTML.call(target);
					}
			
				}
			},
		
			mouseout : function(e){
				var target = sb.events.target(e);
				if(target.nodeName =='LI' && target.info.childList !==null){
				
					target.removeClassName('li_rollover');
					//target.className = '';
				}
				
				if(!sb.events.relatedTarget(e).isDescendantOf(pages.navMainBox)){
					pages.hideAllDropDowns();
				}
			}
		});
		
		this.parent.events({
			mousedown : function(e){
				var target = sb.events.target(e);
				//banner is clicked
				if(target.id == 'banner'){
					window.location = bannerLocation || '../app/index.php?pid=1';
				}
			}
		});
		this.searchQuery.events({
			mouseup : function(){
				this.focus();
				this.select();
			},
			keyup : function(e){
				
				if(e.keyCode == 27){
					this.value ='';
					pages.liveSearch.hide();
				} else if(e.keyCode == 13){
					sb.events.preventDefault(e);
					pages.queryServer(this.value);
				} 
				
				if(this.value !=''){
					this.count = 0;
					var self = this;
					if(typeof this.counter !='undfined'){
						window.clearInterval(this.counter);
					}
					
					this.counter = window.setInterval(function(){
						self.count++;
						//document.title = self.count;
						if(self.count > 10){
							//document.title = 'send'+self.value+new Date();
							window.clearInterval(self.counter);
							pages.queryServer(self.value);
						}
					}, 100);	
				}
				
				
			}
		});
		
		this.search.events({
			submit : function(e){
				sb.events.preventDefault(e);
				if(this.q.value === ''){return false;}
				pages.queryServer(this.q.value);
				
				
			}
		})
	},
	
	
	config : function(){
		
		//the navigation box where the main nav is
		this.navMainBox = s$('#nav_main_box');
		
		//the whole page - the control panel
		this.parent = s$('#parent');
		
		//the main content area's parent
		this.main = s$('#main');
		
		
		//the content area
		this.content = s$('#content');
		
		//the breadcrumbs
		this.breadCrumbs = $('#breadcrumbs');
		
		//the search box
		this.search = s$('#search');
		
		//the search box
		this.searchQuery = s$('#q');
		this.searchQuery.setAttribute('autocomplete', 'Off');
		this.redirectTarget = (typeof redirectTarget !='undefined') ? redirectTarget : '_blank',
		
		this.symbols = {
			expandable : (typeof expandableSymbol != 'undefined') ? expandableSymbol : "&raquo;",
			breadCrumbSeparator : (typeof breadCrumbSeparator != 'undefined') ? breadCrumbSeparator : "&raquo;"
			
		};
		
	},
	addShadow : function(){
		
		var shadow =$('#shadow');
		if(shadow){
			if(sb.browser.ie6===1){
				shadow.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='../media/shadow.png')";
			} else {
				shadow.style.backgroundImage="url('../media/shadow.png')";
			}
		}	
	},
	
	init : function(){
		this.addShadow();
		this.config();
		this.addEvents();
		
		this.navMainToHTML();
		
		this.loadPage();
	}
};

//REMOVED
//sb.ondomload.push(pages.init);
