// --- Sliding Drawer Nav

var GLOBAL_EASING = 'easeInOutQuad';
var opacitySupport = $.support.opacity; 


var slideNav = function(opts){
	var surrogate = this;
	this.defaults = {
		navRef : null,
		dur : 300,
		easingMethod : GLOBAL_EASING
	};
	this.nav = $('#pageHeader').find('.nav');
	this.dds = this.nav.find('.dd');
	this.ddLs = this.dds.parent();
	this.drawer = $('<div id="ddDrawer"></div>');
	this.indicatorTrack = $('<div class="indicatorTrack"></div>');
	this.indicator = $('<div class="indicator"></div>');
	this.isDrawerDown = false;
	this.iMoving = false;
	this.isOverLink = false;
	this.options = $.extend(this.defaults, opts);
	this.nav = $('#pageHeader').find('.nav');
	this.sfWidth = 140;
	
	this.searchForm = this.nav.find('li.navSearch');
	this.searchField = this.searchForm.find('input.text');
	
	this.optTrigger = this.searchForm.find('.optionTrigger');
	this.searchDD = this.searchForm.find('.searchOptions');
	this.optsList = this.searchDD.find('ul');
	this.isDrawerDown = false;
	this.isOverLink = false;
	
	// MAIN SETUP
	this.indicatorTrack.append(this.indicator);
	this.drawer.append(this.indicatorTrack).insertAfter(this.nav);
	
	this.drawer.bind('mouseleave', function(){
		if(!this.isOverLink){
			surrogate.hideDrawer();
		}
	});
	
	//highlight the proper nav element
	var loc = document.location.pathname.split('/')[1];
	if(loc == 'news' || loc =='library'){
		loc = 'about'
	}else if(loc == 'service'){
		loc = 'retail'
	}
	$('#topNav > li > a').not('[href="/' + loc +'/"]').parent().removeClass('active').end().end().filter('[href="/' + loc +'/"]').parent().addClass('active');
	
	// DD SETUPS
	var hoverConfig = {
		interval:this.options.dur/2,
		timeout:this.options.dur,
		over:function(){ddOver(this)},
		out:function(){ddOff(this)}
	}
	var ddOver = function(elem){
		var elem = $(elem);
		var theDD = $(elem).children('.dd');
		var theA = elem.children('a:first-child');
		var drawerMode = surrogate.isDrawerDown == true ? 'fade' : 'slide';
		if(!opacitySupport){ // IE fix
			drawerMode = 'ie';
		}
				
		surrogate.showDropDown(theDD, { mode: drawerMode });
		var theHeight =  theDD.children().outerHeight();
		//if(theDD.hasClass('collection')){
			//theHeight = 330;
		// }
		var targX = theA.outerWidth()/2 + elem.position().left + 4;
		// console.log(theA.offset().left);
		surrogate.showDrawer(theHeight, targX);
	}
	var ddOff = function(elem){
		surrogate.hideDropDown($(elem).children('.dd'));
		if(!surrogate.isOverLink){
			surrogate.hideDrawer();
		}
	}
	this.ddLs.each(function(){
		$(this).hoverIntent(hoverConfig);
	}).addClass('ddLink').bind("mouseenter", function(){
		surrogate.isOverLink = true;
	}).bind("mouseleave", function(){
		surrogate.isOverLink = false;
	});
	
	this.dds.each(function(){
		var t = $(this);
		if(t.hasClass('collection')){
			t.children().wrapAll('<div class="inner"/>');
		}
		
		t.css({visibility:'visible'}).children().css({marginTop: -t.height()});
		
		
	});
	
	
	this.optsList.show().css({marginTop: -(this.optsList.height() + 22) }).delegate('a', 'click', function(e){
		surrogate.searchOptionsClick(e);
		return false;
	}).hide();
	this.optTrigger.css({right:5}).bind('click', function(){
		$(this).addClass('active');
		surrogate.optsList.show().animate({marginTop:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod});
		return false;
	});
}
slideNav.prototype.showDropDown = function(dd, opts){
	var surrogate = this;
	var defaults = { mode : 'slide' };
	var opts = $.extend(defaults, opts);
	var theCont = dd.children();
	
	if(opts.mode == 'fade'){
		this.dds.children().not(theCont).each(function(){
			var t = $(this);
			t.fadeTo(surrogate.options.dur/3, 0, function(){t.hide()});
		});
		theCont.hide().delay(surrogate.options.dur).css({marginTop:0}).fadeTo(surrogate.options.dur/2, 1).addClass('active');
	}else if(opts.mode =="ie"){//sigh...
		this.dds.children().not(theCont).each(function(){
			var t = $(this);
			t.animate({marginTop: -t.outerHeight()}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete:function(){t.hide()}});
		});
		theCont.show().css({marginTop:-theCont.outerHeight()}).animate({marginTop: 0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).addClass('active');
	}else{
		theCont.show().fadeTo(0,1).css({marginTop:-theCont.outerHeight()}).animate({marginTop: 0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).addClass('active');
	}
	
	if(opts.mode !="ie"){
		// remove bkg animation
		//dd.siblings('a').animate({backgroundPosition : '50% 50px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}

slideNav.prototype.hideDropDown = function(dd, opts){
	var surrogate = this;
	var defaults = { mode : 'slide' };
	var opts = $.extend(defaults, opts);
	
	// TODO:fix bg for IE
	if(opacitySupport){
		// remove animation of bkg image
		//dd.siblings('a').stop().delay(surrogate.options.dur/2).animate({backgroundPosition : '50% 36px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}
slideNav.prototype.showDrawer = function(ht, indicatorX){
	var surrogate = this;
	if(!this.isDrawerDown){this.indicator.css({top:-10, left:indicatorX});}
	this.indicator.animate({top:0, left: indicatorX}, {duration:surrogate.options.dur/1.5, easing:surrogate.options.easingMethod});
	this.drawer.stop().animate({height:ht}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete: function(){ surrogate.isDrawerDown = true; }});
}
slideNav.prototype.hideDrawer = function(){
	var surrogate = this;
	this.indicator.stop().animate({top:-10}, {duration:surrogate.options.dur/1.5, easing:surrogate.options.easingMethod});
	this.drawer.stop().animate({height:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete: function(){ surrogate.isDrawerDown = false; }});
	this.dds.children().each(function(){
		$(this).animate({marginTop:-$(this).outerHeight()}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, queue:false});
	});
}



$(document).ready(function() {
	var nav = new slideNav();
	
});


