$(function(){
	nav.init();
	overlay.init();
	$(window).resize(function() {
	    overlay.windowResize();
    });
});

var nav = {
	top: null,
	navIntro: null,
	nameIntro: null,
	topLeft: null,
	topRight: null,
	topSeal: null,
	
	init: function() {
		this.top = $('#top-nav');
		this.navIntro = $('#nav-intro');
		this.nameIntro = $('#barnett-intro img');
		this.topLeft = $('#nav-goods img#goodsImg');
		this.topRight = $('#nav-facts img#factsImg');
		this.topSeal = $('#nav-seal');
		var goods = $('#nav-goods');
		var facts = $('#nav-facts');
		
		var goodsData = goods.metadata();
		var goodsImg = $('#goodsImg');
		var goodsImgSrc = goodsImg.attr('src');
		var goodsHoverText = $('#nav-goods-hover-text img');
		goods.hoverIntent(function() {
			goodsImg.attr('src', goodsData.h);
			goodsHoverText.slideOutLeft(500);
		}, function() {
			goodsHoverText.slideInFromLeft(500, function() {
				goodsImg.attr('src', goodsImgSrc);
			});
		});
		
		var factsData = facts.metadata();
		var factsImg = $('#factsImg');
		var factsImgSrc = factsImg.attr('src');
		var factsHoverText = $('#nav-facts-hover-text img');
		facts.hoverIntent(function() {
			factsImg.attr('src', factsData.h);
			factsHoverText.slideOutRight(500);
		}, function() {
			factsHoverText.slideInFromRight(500, function() {
				factsImg.attr('src', factsImgSrc);
			});
		});
		
//		if ($('body').attr('id') != 'home') {
//			this.setupInterior();
//		}
	},
	setupHomePage: function() {
		this.top.hide();
		$('#home-content').hide();
		var self = this;
		setTimeout(function() {
			$('#home-content').fadeIn(2000);
			self.navIntro.slideHorizontal('left', 0, 449, 600, function() {
				self.nameIntro.slideInFromRight(1000, function() {
					self.navIntro.hide();
					self.top.show();
					self.topRight.slideHorizontal('left', -110, 0, 1500);
					self.topLeft.slideHorizontal('right', -110, 0, 1500);
				});
			});
		}, 1000);
	},
	setupInterior: function() {
		var self = this;
		this.topLeft.hide();
		this.topRight.hide();
		setTimeout(function() {
			self.topLeft.slideOutLeft(1500);
			self.topRight.slideOutRight(1500);
		}, 300);
		
	}
};

$.fn.slideOutRight = function(duration, callback, additionalPosition) {
	var start = -this.outerWidth();
	var finish = 0;
	if (additionalPosition != undefined && additionalPosition > 0) {
		finish += additionalPosition;
	}
	return this.slideHorizontal('left', start, finish, duration, callback);
};
$.fn.slideInFromRight = function(duration, callback) {
	var start = 0;
	var finish = -this.outerWidth();
	return this.slideHorizontal('left', start, finish, duration, callback);
};
$.fn.slideOutLeft = function(duration, callback) {
	var start = -this.outerWidth();
	var finish = 0;
	return this.slideHorizontal('right', start, finish, duration, callback);
};
$.fn.slideInFromLeft = function(duration, callback) {
	var start = 0;
	var finish = -this.outerWidth();
	return this.slideHorizontal('right', start, finish, duration, callback);
};
$.fn.slideHorizontal = function(position, start, finish, duration, callback, queue) {
	if (queue == undefined) {
		queue = false;
	}
	this.show();
	this.css(position, start);
	var opt = {
		duration: duration,
		easing: 'swing',
		queue: queue,
		complete: callback
	};
	var properties = {};
	if (position == 'left') {
		properties = {left: finish};
	} else {
		properties = {right: finish};
	}
	return this.animate(properties, opt);
};

var overlay = {
	overlay: null,
	init: function() {
		this.overlay = $('#overlay');
		this.overlay.width($(window).width()).height($(window).height()).hide();
	},
	click: function(callback) {
		this.overlay.click(function() {
			callback.apply();
		});
	},
	show: function() {
		this.overlay.show();
	},
	hide: function() {
		this.overlay.hide();
	},
	windowResize: function() {
		if (this.overlay != undefined) {
			this.overlay.width($(window).width()).height($(window).height());
		}
	}
};

// from http://free-latest-blogger-templates.blogspot.com/
// http://bloggerplugnplay.blogspot.com/2009/10/verticle-auto-scroll-news-ticker-using.html
function animator(currentItem) {
    //work out new anim duration
    var distance = currentItem.height();
    duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.01;

    //animate the first child of the ticker
    currentItem.animate({marginTop: -distance}, duration, "linear", function() {
	    //move current item to the bottom
	    currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
	
	    //recurse
	    animator(currentItem.parent().children(":first"));
    }); 
};
  
/* Start scroll gangsta */  
function scrollGangsta(thing,ax,ad){
	if(ax=="deactivate"){
		scrollGangsta[thing]=null
	}else{
		if($("#"+thing).length!=0){
			if(scrollGangsta[thing]){
				return scrollGangsta[thing]
             }else{
            	 if(ax){
            		 scrollGangsta[thing]=new dtScroller($("#"+thing)[0])
        		 }else{
        			 scrollGangsta[thing]=new dtScroller($("#"+thing)[0],ax,ad)
    			 }
        	 }
		}
	}
}
function dtScroller(obj,axis,addSpeed){
	this.isActive=true;
	this.scroller=0;
	this.check=0;
	this.limit=true;
	this.timeout=0;
	this.oldMulti=0;
	this.oldY=0;
	this.obj=obj;
	this.speed=50;
	this.multiplier=10;
	this.addSpeed=addSpeed||0;
	this.axis=axis||'y';
	this.breakpoint=8;
	var parent=this;
	this.plane=function(obj,e){
		obj=$(obj);
		try{
			if(this.axis=='y'){
				var y=e.pageY-obj.offset().top
			}else{
				var y=e.pageX-obj.offset().left;
				this.breakpoint=9
			}
		}catch(err){
			if(this.axis=='y'){
				var y=window.event.y-obj.offset().top
			}else{
				var y=window.event.x-obj.offset().left;
				this.breakpoint=9
			}
		}
		return y
	};
	this.active=function(){
		this.isActive=true;
		tob=$(this.obj);
		tob.bind("mousemove",function(event){
			var owner=parent;
			var y=owner.plane(this,event);
			ths=$(this);
			var offSet=(owner.axis=='x')?ths.width():ths.height();
			if((y-(offSet/2))<0){
				var scrollDirection=-1
			}else{
				var scrollDirection=1
			}
			var h=offSet/2;
			var percent=(y>h)?((y-h)/h):((h-y)/h);
			var scrollMultiplier=Math.round(percent*owner.multiplier);
			var scrollSpeed=Math.round(percent*100);
			scrollSpeed=100-scrollSpeed;
			if(owner.scroller!=0){
				clearInterval(owner.scroller);
				owner.scroller=0;
			}
			if(scrollMultiplier<owner.breakpoint){
				if(owner.oldY>y){
					owner.scrollmonkey(-1,3);
				}else if(owner.oldY<y){
					owner.scrollmonkey(1,3);
				}
				owner.oldY=y
			}else{
				owner.scrollmonkey(scrollDirection,4);
				owner.scroller=setInterval("try{scrollGangsta('"+this.id+"').scrollmonkey("+scrollDirection+",4);}catch(e){if(scrollGangsta) { scrollGangsta["+this.id+"] = null; } clearInterval("+owner.scroller+"); }",owner.speed);
				owner.oldY=y
			}owner.check=1;
			return false
		});
		tob.bind("mouseover",function(){
			owner=parent;
			owner.check=1;
			if(owner.timeout!=0){
				clearTimeout(owner.timeout);
				owner.timeout=0;
			}
		});
		tob.bind("mouseout",function(){
			owner=parent;
			if(owner.check==1){
				owner.check=0;
				owner.timeout=setTimeout("try { scrollGangsta."+this.id+".stopScroll(); } catch(e){}",250);
			}
			return false
		});
	};
	this.stopScroll=function(){
		if(this.check==0){
			clearInterval(this.scroller);
			this.scroller=0;
			this.oldMulti=0;
			this.oldY=0
		}
	};
	this.scrollmonkey=function(direction,multiplier){
		var scrollStart=(this.axis=='x')?"scrollLeft":"scrollTop";
		var scrollSize=(this.axis=='x')?"scrollWidth":"scrollHeight";
		multiplier=multiplier+this.addSpeed;
		if((this.obj[scrollStart]>0&&direction<0)||(this.obj[scrollStart]<this.obj[scrollSize]&&direction>0)){
			this.obj[scrollStart]=this.obj[scrollStart]+(direction*multiplier);
		}else{
			clearInterval(this.scroller);
			this.scroller=0;
		}
	};
	this.sleep=function(){
		tob=$(this.obj);
		tob.unbind("mousemove");
		tob.unbind("mouseover");
		tob.unbind("mouseout");
		this.isActive=false;
		scrollGangsta(this.id,"deactivate")
	};
	this.active();
}
/* End scroll gangsta */
