$(document).ready(function () {
	
	initScrollNav();
	 auxOverlays();
	
});





$(function(){
/* reset to origin when refresh */
	$("#bg_window").scrollTo( 0 );
	$.scrollTo( 0 );
});

var current_pos = 0; /* starting current position tracking */

/* -- FOR THE NAV --- */
function initScrollNav () {
	
	
	
	/* scroll nav */
	$("a.scrollNav").click(function(){
		
		var scroll_target = $(this).attr("rel");
		var content_target = $(this).attr("href");
		
		/* make link selected */
		$("a.scrollNav.selected").removeClass("selected");
		$(this).addClass("selected");
		
		
		/* check if already at destination */
		if	( scroll_target == current_pos ){ return false;}
		else {
				
				$(".jScrollPaneContainer").fadeOut(500); /* for IE */
				$("#content_container").fadeOut(
					500,
					function(){
						$("#content_container").empty();
						$("#bg_window").stop().scrollTo(
									scroll_target,
									2000,
									{
										axis:"x",
										onAfter:function(){
											$("#content_container")
											.show()
											.css({"height":"0px", "overflow":"hidden"}) /* create a collapsed hidden placeholder so scrollpane can initialize */
											.load(content_target, function(){
													$(".scroll-pane").jScrollPane(); /* initialize scrollpane */
													
													
													/* after loaded content into cache, empty, and reload, so scroll-pane can reinitialize with proper height */
													$("#content_container").empty().load(content_target, function(){	
															auxOverlays(); /* init aux contact + shop overlays */
															tb_auto_init(); /* init thickbox */
															viewProfile(); /* init view profile for chef bios */
															initSideNav();	/* init side nav */
															$(".scroll-pane").jScrollPane(); /* initialize scrollpane */	
															$(".jScrollPaneContainer").hide().fadeIn(500); /* for IE */
															$("#content_container").hide()	/* after initialization is done, hide, expand, fade in scrollpane */
																.css({"height": "auto"})
																.fadeIn(500);	
															});
													});
											current_pos = scroll_target;
										}
									}
						);
				  		
					}	
					
					
				);
				
		
		}
		return false;
	});
	

		 
};

function loadTwitterFeed () {
	new TWTR.Widget({
	  version: 2,
	  type: 'profile',
	  rpp: 5,
	  interval: 30000,
	  width: 695,
	  height: 330,
	  id: 'twitterFeed',
	  theme: {
	    shell: {
		 background: 'transparent',
		 color: '#ffffff'
	    },
	    tweets: {
		 background: 'transparent',
		 color: '#666666',
		 links: '#900000'
	    }
	  },
	  features: {
	    scrollbar: true,
	    loop: false,
	    live: true,
	    hashtags: true,
	    timestamp: true,
	    avatars: true,
	    behavior: 'all'
	  }
	}).render().setUser('ChefsChallenge').start();
}
 
/* --- SIDENAV TABS -- */
function initSideNav() {
	$("a.tab").bind("click", (function(){
		
		var content_target = $(this).attr("href");
		var is_twitter_feed = $(this).hasClass('twitter-feed');
		
		$("#content_loading").show();						  
		$("#content_ajax").empty().css({"height":"0px", "overflow":"hidden"}).show().load(content_target,function(){
				$(".scroll-pane").jScrollPane(); /* initialize scrollpane */	
				
				/* load content into cache, empty, then reload so scroll-pane can reinitialize with proper height */
				$("#content_ajax").empty().load(content_target, function(){
						$("#content_loading").hide();
						if ( !!is_twitter_feed ) loadTwitterFeed();		
						$("#content_ajax").hide().css({"height":"auto"}).show();
						auxOverlays(); /* init aux contact + shop overlays */
						tb_auto_init(); /* init thickbox */
						$(".scroll-pane").jScrollPane(); /* initialize scrollpane */	
				});
		});
		
		$(".tab.selected").removeClass("selected");
		$(this).addClass("selected");	
		
		return false;
	}));

};






/* profile overlay */
function viewProfile(){

	$("a.view_profile").click(function(){
		var target_content= $(this).attr("href");
		var margin_top = $(document).scrollTop();
		
		$("div.overlay").css({"opacity":"0.75","filter":"alpha(opacity=75)" }).show();
		
		$(".overlay_container").css({"height":"0px"}).show().load(target_content, function(){
			$(".profile_scroll").jScrollPane(); /* initialize scrollpane */	
			$(".overlay_container").hide().css({"height": "auto"}).show();	
			$(".profile_content").css( {"margin-top": margin_top + 30});
			
			/* --- close overlay if clicked off of content --- */
			$(document).click(function(){
				$(".overlay_container").hide().empty();
				$("div.overlay").hide();
				
			});
				
			/* ---- keep overlay on if clicked within content --- */
			$(".profile_content").click(function(e){
			    e.stopPropagation();
			  });
			
			/* ------ close overlay button ----- */
			$("a.close_profile").click(function(){
				$(".overlay_container").hide().empty();
				$("div.overlay").hide();
			
			});
			
		});
		
		return false;
	});
	
	
};



/* CONTACT AND SHOP OVERLAYS */
function auxOverlays(){
	
	
	$("a.overlay").click(function(){	
		var target_content=$(this).attr("href");
		var margin_top = $(document).scrollTop();
		$("div.overlay").css({"opacity":"0.75", "filter": "alpha(opacity=75)"}).show();
		
		
		$(".overlay_container").css({"height":"auto"}).load(target_content, function(){
			$(".overlay_container").show();	
			$(".overlay_container > div").css({"margin-top":margin_top});
		
			/* --- close overlay if clicked off of content --- */
			$(document).click(function(){
				$(".overlay_container").hide().empty();
				$("div.overlay").hide();
				
			});
				
			/* ---- keep overlay on if clicked within content --- */
			$(".overlay_container div").click(function(e){
			    e.stopPropagation();
			  });
			
			
			 $('a.close_overlay').click(function(){
				$(".overlay_container").hide().empty();
				$("div.overlay").hide();
				return false;
		
			});
		
		
		});
	
		
		return false;
		
	});
	
	
	
	
	
};




