var GLOBAL_EASING = 'easeInOutQuad';
var opacitySupport = $.support.opacity; //this is so we can do something else besides fades in IE cuz of it's abysmal handling of fading pngs

var siteNav = function(opts){

	var surrogate = this;
	this.defaults = {
		navRef : null,
		dur : 400,
		easingMethod : GLOBAL_EASING
	};
	this.nav = $('#header').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 = $('#header').find('.nav');

	this.sfWidth = 140;

	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){//Why IE?? WHY YOU SO BAD AT ALPHA TRANSPARENCY??
			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 + 25;
		// 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()});
		
		if(t.hasClass('userInfo')){
			t.css({left: -t.parent().position().left, top:t.parent().position().top + t.parent().height()});
		}
	});
	

}

siteNav.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');
	}
	
	// TODO:fix bg arrow for IE
	if(opts.mode !="ie"){
		dd.siblings('a').animate({backgroundPosition : '50% 55px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}

siteNav.prototype.hideDropDown = function(dd, opts){
	var surrogate = this;
	var defaults = { mode : 'slide' };
	var opts = $.extend(defaults, opts);
	
	// TODO:fix bg for IE
	if(opacitySupport){
		dd.siblings('a').stop().delay(surrogate.options.dur/2).animate({backgroundPosition : '50% 37px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}
siteNav.prototype.showDrawer = function(ht, indicatorX){
	var surrogate = this;
	if(!this.isDrawerDown){this.indicator.css({top:-15, 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; }});
}
siteNav.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(){
	$("#topNav li:has(ul)").toggleClass("ddLink");
	$('.children').wrap('<div class="dd" />');
	$("#topNav ul li:last-child").addClass("last");
	$("#topNav li > a").attr('title', '');


	var nav = new siteNav();
	
	jQuery('#home-slider .inner').cycle({ 
    			fx:     'fade', 
    			speed:  500, 
    			timeout: 4000, 
    			pause: 1,
    			pager:  '.pager',
    			next:   '.next', 
    			prev:   '.prev'  
				});
				
						
        
        $(".next").hover(function() {
            $(this).stop().animate({right: -53}, 250);
        }, function() {
            $(this).stop().animate({right: -63}, 250);
        });

    

         $(".prev").hover(function() {
            $(this).stop().animate({left: -53}, 250);
        }, function() {
            $(this).stop().animate({left: -63}, 250);
        });
});
