

function hideControls() {
	//  $('body').css('overflow','hidden');
	$('#menu_light,#logo_light').fadeOut('fast');
	$('#menu,#logo').animate({top : '-80px'});
	$('#footer').animate({bottom : '-80px'});
	$('#snap_progress').show().fadeIn();
	$('#menu_floater').hide();
}

function showControls() {
	$('#logo,#menu').animate({top : '0px'});
	$('#footer').animate({bottom : '0px'});
	$('#menu_light,#logo_light').delay(500).fadeIn('slow');	
	$('#snap_progress').fadeOut().hide();
	$('#menu_floater').show();
	//  $('body').css('overflow','scroll');
}

function loadContent(o) {
      targetpanel = $(o.attr('hash')).find('div.container_12');
      var linkToLoad = o.attr('href').split('#')[0]+'?ajax=true';
      if ($(o.attr('hash')).attr('loaded')!=='true') {
      
	targetpanel.load(linkToLoad,
		function(){
				$(o.attr('hash')).attr('loaded','true');
				
				if (targetpanel.parent().height() < targetpanel.height())
				  {targetpanel.parent().height(targetpanel.height()+100)}
				updateSocialLinks();
		});

      }
      
      pageTracker._trackPageview('/'+linkToLoad );

      
}


function scroll(target,delayBefore,onAfter) {
  var currentTop = $('html').scrollTop() ? $('html').scrollTop() : $('body').scrollTop();
  var currentLeft = $('html').scrollLeft() ? $('html').scrollLeft() : $('body').scrollLeft();
  var targetTop = target.offset().top;
  var targetLeft = target.offset().left;

  //var len = Math.sqrt( Math.pow(targetTop-currentTop,2)  + Math.pow(targetLeft-currentLeft,2) )
  
  $('html,body').delay(delayBefore).animate({scrollTop: targetTop}, Math.abs(targetTop-currentTop)*0.4).animate({scrollLeft: targetLeft}, Math.abs(targetLeft-currentLeft)*0.4,onAfter);
  //menuFloater(current);
}

function menuFloater(obj) {

 	if (menuFloaterTimeout) 
		clearTimeout(menuFloaterTimeout);
   
	var new_l = $(obj).offset().left;
	  var new_w = $(obj).width();
	  var mf_l = $('#menu_floater').offset().left;
	  var mf_w = $('#menu_floater').width();
	  if (new_l > mf_l) {
	    $('#menu_floater')
	      .stop(true)
	      .animate({'width' : new_w+new_l-mf_l},100)
	      .animate({'width' : new_w, 'left' : new_l},{duration:400,easing:'easeOutBounce'});
	  } else {
	    $('#menu_floater')
	      .stop(true)
	      .animate({'left' : new_l, 'width' : mf_l+mf_w-new_l},100)
	      .animate({'width' : new_w},{duration:400,easing:'easeOutBounce'});
	  }
}


function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function updateSocialLinks() {
  var socialLinks = [ 
    {'service' : 'twitter', 'url' : 'http://twitthis.com/twit?url=*url*&title=*title*'},
    {'service' : 'facebook','url' : 'http://facebook.com/sharer.php?u=*url*&t=*title*'},
    {'service' : 'delicious','url': 'http://del.icio.us/post?&url=*url*&title=*title*'}
  ]
  
  var title = $('.socialDescription',$(current).attr('hash')).html();
  if (!title) title = document.title;
  for (i=0;i<socialLinks.length;i++) {
    $('.'+socialLinks[i]['service']).attr('href',socialLinks[i]['url'].replace('*title*',urlencode(title)).replace('*url*',urlencode(location.href)));
  }
}

var current = null;
var byMenuLink = false;
var menuFloaterTimeout = null;

$(function(){
	//$('html,body').scrollTop( 0 ).scrollLeft(0);
	$('#index').attr('loaded','true');
	$(".panel").each(function(){
		$(this).append('<div class="snap" parent='+$(this).attr('id')+'></div>');
	});
   
	//$('body').width($(window).width()*2+10);
	$('.panel').width($(window).width()).height($(window).height());
  
	$('#footer li').hover(function(){$(this).fadeTo('normal',1)},function(){$(this).fadeTo('normal',0.5)})
	$('#logo').click(function(){window.location='/'});
	$('#menu a').hover(
		function() {
			menuFloater(this);
		},
		function(){
			menuFloaterTimeout=setTimeout("menuFloater($('#logo'));",500);
		}
	);

	$('a.image_link').live('hover',
		function(){
			$(this).animate({backgroundPosition: '200px 0'},16000,'linear');
		},
		function(){
			$(this).css('background-position', '0 0');
		}
	);
  

	$('#menu a').click(function(e){
		current = $(this);
	
		loadContent(current); 
		e.preventDefault();
		byMenuLink = true;
		hideControls();
	
		scroll($(current.attr('hash')),500,function(){$(window).trigger('scroll')});
		return false;
	})
	

	$(window).bind('scrollstop',function(e){
		if (byMenuLink == false) {
			snapTo = $('.snap:in-viewport:[parent!='+current.attr('hash').substr(1)+']').eq(0);
			
			if (snapTo.length==0) {return false};
		        $('#snap_progress').fadeIn();
			current = $('#'+snapTo.attr('parent')+'_btn');
			//hideControls();
			loadContent(current);
			scroll($(current.attr('hash')),200,function(){$(window).trigger('scroll')});
			byMenuLink = true;
			
			
			
		} else {
		 	showControls();
			location.hash = current.attr('hash');
			byMenuLink = false;
		}
		
		document.title = current.attr('title') + ' |' + document.title.split('|')[1];
		updateSocialLinks();
  	});

	if (current == undefined) {
		current = $('#menu a:first').eq(0);
	}


	if (location.hash)
		$(location.hash+'_btn').trigger('click');
	else
		$('#web_btn').click();
		
})

