(function(a){a.fn.nInputDefault=function(b){var c={focusClass:"focus"};var b=a.extend(c,b);return this.each(function(){var e=a(this);if(e.attr("type")=="text"){var d=e.val();e.unbind("focus");e.bind("focus",function(){if(e.val()==d||e.val()==""){e.val("");e.addClass(b.focusClass)}});e.bind("blur",function(){if(e.val()==d||e.val()==""){e.removeClass(b.focusClass);e.val(d)}})}})}})(jQuery);
var tb = tb || {}, lastHash;

var tb = {
    vars : {
       objWidth : [0,0,0],
       oldPos : 0,
       oStep : 0,
       currHash : location.hash,
       scrolling : false
    },
	init : function(){
		if($('body').hasClass('frontpage')){ // init for all frontpage specific stuff
		    tb.calcWidths();
		    setInterval(tb.checkHash,500);
		}else if($('body').hasClass('article')){
		    tb.vars.oStep = jQuery.inArray($('#YearContainer').attr('class'),tb.vars.hashes);
		    $('#close a').bind('click',function(e){
		        e.preventDefault();
		        window.location = 'http://'+location.host+'/#'+$('#YearContainer').attr('class');
		    });
		}
		tb.handleSlider();
		tb.setActiveSlider();
	},
	
	checkHash : function(){
	    if(location.hash != tb.vars.currHash && tb.vars.scrolling != true){
	        var hashValue = location.hash;
	        hashValue = (hashValue!="") ? jQuery.inArray(hashValue.substring(1),tb.vars.hashes) : 0; // if no hash assume first hash in stored hashes (weee!)

		    $('#tl').slider('value',hashValue);
		    tb.scrollBoxes($('#tlWrapper ul li a:eq('+hashValue+')'));
	        tb.vars.currHash = location.hash;
	    }else{
	        tb.vars.currHash = location.hash;
	    }
	},
	
	calcWidths : function(){
	    // Calculate the real width of all scrollers
		$('#l > div').each(function(){
		    tb.vars.objWidth[0] +=$(this).outerWidth();
		});
		$('#m > div').each(function(){
		    tb.vars.objWidth[1] +=$(this).outerWidth();
		});
		$('#s > div').each(function(){
		    tb.vars.objWidth[2] +=$(this).outerWidth();
		});
	},
	
	handleSlider : function(){
		if ($('body').hasClass('frontpage')) { // If on startpage
			$('#tlWrapper ul li a').each(function(i){
				$(this).bind('click', {index:i}, function(e){
					e.preventDefault();
			        location.hash = '#'+tb.vars.hashes[e.data.index];
			        tb.vars.currHash = location.hash;
					$('#tl').slider('value',e.data.index);
					tb.scrollBoxes($(this));
				});
			});
		}else{ // If on article page
		    $('#tlWrapper ul li a').each(function(i){
				$(this).bind('click', {index:i}, function(e){
					e.preventDefault();
					$('#tl').slider('value',e.data.index);
					window.location = 'http://'+location.host+'/#'+tb.vars.hashes[e.data.index];
				});
			});
		}
		
		$('#tl').slider({
			min : 0,
			max : (tb.vars.hashes.length-1),
			value : tb.vars.oStep,
			animate : true,
			snap : true,
			slide : function(e,ui){
				if ($('body').hasClass('frontpage')) {
				    tb.scrollBoxes($('#tlWrapper ul li a:eq('+ui.value+')'));
				}
			},
			change : function(e, ui){
			    if ($('body').hasClass('frontpage')) {
			        location.hash = '#'+tb.vars.hashes[ui.value];
			        tb.vars.currHash = location.hash;
			    }else{
			        window.location = 'http://'+location.host+'/#'+tb.vars.hashes[ui.value];
			    }
			    tb.setActiveSlider();
			}
		});
		
		if($('body').hasClass('frontpage') && location.hash != ""){
		    var hashValue = location.hash;
		    hashValue = jQuery.inArray(hashValue.substring(1),tb.vars.hashes);
		    $('#tl').slider('option','animate',false); // slider can't be chained... :/
		    $('#tl').slider('value',hashValue);
		    $('#tl').slider('option','animate',true);
		    tb.moveBoxes($('#tlWrapper ul li a:eq('+hashValue+')'));
		}
	},
	
	scrollBoxes : function(obj){
	    obj = obj.attr('id');
	    target = '.wrp' + (obj.replace(/btn/,""));
	    var intPos = 0, aSpeed = 800, intWidth;
	    for(i=0;i<=2;i++){
		    if ($('#scroller>div:eq(' + i + ') ' + target + ' div').length > 0) {
		        intPos = $('#scroller>div:eq(' + i + ') ' + target).position().left;
		        if( (tb.vars.objWidth[i] - intPos) < 960 ){
		            if(i==0){
		                intWidth = 960;
		            }else if(i==1){
		                intWidth = 968;
		            }else if(i==2){
		                intWidth = 966;
		            }
		            intPos = parseInt(tb.vars.objWidth[i]-intWidth);
		        }
		        
		        aSpeed = tb.calcSpeed(intPos);
		        
		        $('#scroller>div:eq(' + i + ')').clearQueue().animate({
			        marginLeft: -(intPos)
		        },aSpeed,'easeInOutCirc');
		    }
	    }
	    tb.vars.oldPos = $('#tl').slider('value'); // Save new position as old position
	},
	
	moveBoxes : function(obj){
	    obj = obj.attr('id');
	    target = '.wrp' + (obj.replace(/btn/,""));
	    var intPos = 0, intWidth;
	    for(i=0;i<=2;i++){
		    if ($('#scroller>div:eq(' + i + ') ' + target).length > 0) {
		        intPos = $('#scroller>div:eq(' + i + ') ' + target).position().left;
		        if( (tb.vars.objWidth[i] - intPos) < 960 ){
		            if(i==0){
		                intWidth = 960;
		            }else if(i==1){
		                intWidth = 968;
		            }else if(i==2){
		                intWidth = 966;
		            }
		            intPos = parseInt(tb.vars.objWidth[i]-intWidth);
		        }
		        $('#scroller>div:eq(' + i + ')').css({
			        marginLeft: -(intPos)
		        });
		    }
	    }
	},
	
	setActiveSlider : function(){
	    $('#tlWrapper ul li a').removeClass('active');
	    $('#tlWrapper ul li a:eq('+$('#tl').slider('value')+')').addClass('active');
	},
	
	calcSpeed : function(newPos){
	    var amount = tb.vars.oldPos - $('#tl').slider('value');
	    if(amount<0) amount = -amount;
	    if(amount < 6 ){
	        amount += 3;
	    }else{
	        amount -=3;
	    }
	    amount = amount * 100;
	    return amount;
	},
	
	goUrl : function(obj){
	    obj = obj.attr('href');
	}
}

$(function(){
	tb.init();
	$('.sInput').nInputDefault();
});
