jQuery.fn.idle = function(time){
	var i = $(this);
	i.queue(function(){
		setTimeout(function(){ i.dequeue(); }, time);
	});
};

// second lvl nav
var secondLvlNav = function(){
	var subNavItem = $('#Taste').html();
	$('li.Taste').append(subNavItem);
};

// contact hover
var contactHover = function(){
	$('#contact').hover(function () {
		$('#contactInformation').queue(function(){
			$(this).slideDown('slow').idle(1000);
			$(this).dequeue();
		});
	}, function () {
		$('#contactInformation').queue(function(){
			$(this).slideUp('slow').idle(1000);
			$(this).dequeue();
			$(this).clearQueue();
		});
		return false;
	});
};

// accordion
var ingredients = function(){
	$("#accordion").accordion({collapsible:true, autoHeight:false}).accordion("activate" , false);
};

// facebook pos
var posFacebook = function(){
	var socialBox = {
		positionBox : function(boxOpen, idBox, idButton) {
			var totalWidth = $(window).width();
		
			if (boxOpen == true) {
				$(idBox).css('left', totalWidth - $(idBox).width() + 4);
			} else {
				if (firstTime == true) {$('#socialMedia').css('display','block'); firstTime = false;}
				$(idBox).css('left', totalWidth - $(idButton).width() + 4);
			}
		},
		getPosition : function(boxOpen, idBox, idButton) {
			var totalWidth = $(window).width();
		
			if (boxOpen == true) {
				return totalWidth - $(idBox).width() + 4;
			} else {
				return totalWidth - $(idButton).width() + 4;
			}
		},
		events : function(idBox, idButton) {
			$(idButton).toggle(function () {
				$(idBox).animate({
					left: socialBox.getPosition(true, idBox, idButton)
				}, 1000, function() {
					$(idButton).addClass('close');
				});
				return false;
			}, function () {
				$(idBox).animate({
					left: socialBox.getPosition(false, idBox, idButton)
				}, 1000, function() {
					$(idButton).removeClass('close');
				});
				return false;
			}); 
			
			$(window).resize(function() {
				if ($(idButton).hasClass('close')) {
					$(idBox).css('left', socialBox.positionBox (true, idBox, idButton));
				} else {
					$(idBox).css('left', socialBox.positionBox (false, idBox, idButton));
				}
			});
		}
	}
	var firstTime = true;
	
	socialBox.positionBox(false, '#facebookBox', '#facebookButton');
	socialBox.positionBox(false, '#twitterBox', '#twitterButton');
	
	socialBox.events('#facebookBox', '#facebookButton');
	socialBox.events('#twitterBox', '#twitterButton');	
	
	function positionFacebook (boxOpen) {
		var totalWidth = $(window).width();
		if (boxOpen == true) {return totalWidth - $('#facebookBox').width();} else {return totalWidth - $('#facebookButton').width();}
	}
};

var startListening = function (obj,evnt,func) {
	if (obj.addEventListener) {
		 obj.addEventListener(evnt,func,false);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + evnt,func);
	}
};

var trackExternalLinks = function (evnt) {
	var e = (evnt.srcElement) ? evnt.srcElement : this;
	while (e.tagName != "A") {
		 e = e.parentNode;
	}
	var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
	if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
	var ext = lnk.substring(lnk.lastIndexOf('.') + 1);
	var file = lnk.substring(lnk.lastIndexOf('/')+1);
	
	if (e.hostname == location.host) {
		_gaq.push(['_trackPageview', location.pathname + '/downloads' + e.pathname]);_gaq.push(['_trackEvent','downloads', ext,file]);
	} 
	else if (e.hostname != location.host) {
		_gaq.push(['_trackPageview', '/outbound/' + e.hostname]);_gaq.push(['_trackEvent','outbound links','clicks',e.hostname]);
	}

};

var trackLikeButton = function(){
	var path = location.pathname;
	var lang = "";
	if (path.indexOf("/1/") > -1){
		lang = "NL";
	} else if (path.indexOf("/2/") > -1){
		lang = "FR";
	} else {
		lang = "EN";
	}
	// LIKE
	FB.Event.subscribe('edge.create', function () {
		_gaq.push(['_trackPageview', '/'+lang+'/facebook/like']);
		_gaq.push(['_trackEvent', 'facebook','like', document.location]);
		_gaq.push(['_setCustomVar', 4, 'facebooklikers','true', 1]);
    });
	// DISLIKE
    FB.Event.subscribe('edge.remove', function () {
		_gaq.push(['_trackPageview', '/'+lang+'/facebook/dislike']);
		_gaq.push(['_trackEvent', 'facebook','dislike', document.location]);
	});
};
