$(document).ready(function() {
	// fix the top link for /blog/
	//$('a.top-logo').attr('href', 'http://www.thudrumble.com');

	if (parent.location.hash) {
		var jam = parent.location.hash.substr(1);
		setTimeout(function(){$('html,body').scrollTop($("#a_"+jam).offset().top-185);}, 100);
	}
	
	$.easing.easeIn = function (x, t, b, c, d) { 
	    return c*(t/=d)*t + b;
	}
	$.easing.easeOut = function (x, t, b, c, d) { 
		return -c*(t/=d)*(t-2) + b;
	}

	$('#nav_ul li').click(function(e) {
		var jam = this.id.substr(2);

		if (jam == "downloads") {
			return true;
		}

		if (jam != "blog") {
			if ($("#a_"+jam).length) {
				e.preventDefault();
				$('html,body').animate({ scrollTop: ($("#a_"+jam).offset().top-185) }, 'slow', function() { parent.location.hash = jam });	
			} else window.location = '/#'+jam;
		} else {
			window.location = '/'+jam;
		}
		e.preventDefault();
	});	


	$('.sub_category_valign').each(function(i) { if ($(this).children()[0].childElementCount == 0) $(this).parent().css('display', 'none'); });


	if ($('#lookbook').length) {
			var lookbook;
			var lookhref = 0;
			var looklist = new Array();
			var direction = 0;
			
			var lb_im = $('#lookbook img').length;
			$('#lookbook').width($('#lookbook').width()*lb_im)
			$('#look_right').animate({opacity: 1}, {queue: false, duration: 400, easing:'easeIn'});

			$('.look_img a[href]').each(function(i) { looklist[i] = this.href; })
			$('.look_click').click(function() { window.location = looklist[lookhref]; });

			$('.look_love').click(function(e) {
				if (e.clientX) {
					clearInterval(slidertimer);
					if (this.id == "look_right") direction = 0;
					else direction = 1;
					slidertimer = setInterval(function(){
						if (lookhref+1 == looklist.length) direction = 1;
						else if (lookhref == 0) direction = 0;
						if (direction) $('#look_left').click();
						else $('#look_right').click();
					}, 5000);
				}
			
				var l = $('#lookbook').position().left
				var lb_im = $('#lookbook img').length;
				lookbook = Math.ceil(Math.abs(l/950));

				if ((this.id == "look_right") && lookbook == 0) $('#look_left').stop().animate({opacity: 1}, {queue: false, duration: 400, easing:'easeIn'});
				if ((this.id == "look_right") && lookbook == (lb_im-2)) $(this).stop().animate({opacity: 0}, {queue: false, duration: 400, easing:'easeOut'});
				if (this.id == "look_left" && lookbook == 1) $('#look_left').stop().animate({opacity: 0}, {queue: false, duration: 400, easing:'easeOut'});
				if (this.id == "look_left" && lookbook == (lb_im-1)) $('#look_right').stop().animate({opacity: 1}, {queue: false, duration: 400, easing:'easeIn'});

				if (this.id == "look_right") {
					if ((lookbook+1) == lb_im) return;
					if (l > -($('#lookbook').width()-950))
						$('#lookbook').stop().animate({left: -((lookbook+1)*950)+'px'}, {queue: false, duration: 800, complete: looklink(0)});
				} else {
					if (l >= 0) return;
					if ((lookbook*950-(l*-1)) > 400 && (lookbook*950-(l*-1)) > 0) lookbook <= 1?1:lookbook--
					$('#lookbook').stop().animate({left: -((lookbook-1)*950)+'px'}, { duration: 800, complete: looklink(1)});
				}
			});
			
			function looklink(i) {
				lookhref = i?lookbook-1:lookbook+1;
			}
			
			var slidertimer = setInterval(function(){
				if (lookhref+1 == looklist.length) direction = 1;
				else if (lookhref == 0) direction = 0;
				if (direction) $('#look_left').click();
				else $('#look_right').click();
			}, 4000);
		}
});

