// SLIDING FUNCTIONS
jQuery(function( $ ){
	
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://www.freewebs.com/flesler/jQuery.ScrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	
	var target = $('#slider').get(0);//the scrolled div
	
	/**
	 * restart the scroll position to ( 0, 0 ) (Firefox doesn't reset it)
	 * could use $(target).scrollTo( 0, {axis:'xy'));
	 * but this needs to be quick(synchronous), to reset before $.localScroll.hash() begins
	 */
	if(target) {
		target.scrollLeft = target.scrollTop = 0;
	}

	//scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: target, //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1500
	});
	
	var $last = $([]);//save the last link
	
	/**
	 * NOTE: In the former version of the demo, I called $('#navigation').localScroll()
	 * Now I want to also affect the >> and << links, so I'll use $.localScroll() instead
	 */
	$.localScroll({
		target: target, //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1200,
		hash:false,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			this.blur();//remove the awful outline
			
		}
	});
	
	
	
  //by default, the scroll is only done vertically ('y'), change it to both.
  $.scrollTo.defaults.axis = 'xy'; 			
  //this one is important, many browsers don't reset scroll on refreshes
  $('div#overlay').scrollTo( 0 );//reset all scrollable panes to (0,0)
  $.scrollTo( 0 );//reset the screen to (0,0)
  

  
  //Target examples bindings
  var $paneTarget = $('#overlay');	
  
  $('#content_home').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });
  
  $('#content_home2').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });
  
  $('#content_home3').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });
  
  $('#content_home4').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });
  
  $('#content_home5').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });

  $('#content_home6').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });

  $('#content_home7').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '430px';
  });
  
  $('h1 a').click(function(){
    $paneTarget.stop().scrollTo( '0', 1000, {
      onAfter:function(){
        $("#gallery").hide();
      }
    });
  });

  $('#content_gewurztraminer').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_gewurztraminer'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '710px';
  });
  
  $('#content_gewurztraminer2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_gewurztraminer'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
  });


  $('#content_pinotblank').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_pinotblank'), 1000, {
    });
    document.getElementById('varietals').style.height = '610px';
  });

    
  $('#content_pinotblank2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_pinotblank'), 1000, {
    });
  });
  
  $('#content_cremant').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_cremant'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '560px';
  });
  
  $('#content_cremant2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_cremant'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
  });

  $('#content_other').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_other'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '510px';
  });
  
  $('#content_other2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_other'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
  }); 

  $('#content_late').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_late'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '510px';
  });
  
  $('#content_late2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_late'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
  });

  $('#content_pinotgris').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_pinotgris'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '770px';
  });
  
  $('#content_pinotgris2').click(function(){
    $paneTarget.stop().scrollTo( $('#overlay_pinotgris'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
  });
  
  $('#content_riesling').click(function(){
    $paneTarget.stop().scrollTo($('#overlay_riesling'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '750px';
  });
  
  $('#content_riesling2').click(function(){
    $paneTarget.stop().scrollTo($('#overlay_riesling'), 1000, {
      onAfter:function(){ $("#gallery").hide(); }
    });
    document.getElementById('varietals').style.height = '710px';
  });

  // PNG hack
  if($(document).pngFix) {
    $(document).pngFix(); 
  }
  
  // Controls the full screen mode
  $("a.fullsize").click(function(){
  $("div#overlay").slideToggle("fast");
  $(this).toggleClass("active");
    return false;
  });
  
	/**
	 *  HIDE THE NAV UNTIL THE PAGE HAS FULLY LOADED
	 * 
	 */
	 
	 $("ul.navigation").show();
			

	/**
	 * NAVIGATION STUFF
	 * 
	 */		
			
	$("ul.navigation li a").css({ opacity: 0.4 });
	$("ul.navigation li:first").addClass("active").children().css({ opacity: 1, marginTop: "74px"});

	$("ul.navigation li").click(
		function(){
			$(this).addClass("active");
			$(this).siblings().removeClass("active");
			$(this).siblings().children().animate({ opacity: 0.4, marginTop: "78px"}, 100 );
		}
	);
	$("ul.navigation li").click(
		function(){
			if($(this).hasClass("gallery")){
				$("p.fullsize_link").animate({ height: "0"}, 240 );
			}else{
				$("p.fullsize_link").animate({ height: "24px"}, 240 );
			}
		}
	);

	$("ul.navigation li a").hover(
		function(){
			if($(this).parent().hasClass("active")){
				
			}else{
				$(this).animate({ opacity: 1, marginTop: "74px"}, 100 );
			}
		},
		function(){
			if($(this).parent().hasClass("active")){
			}else{
				$(this).animate({ opacity: 0.4, marginTop: "78px"}, 100 );
			}
		}

	);
	
	
	var url_vars = getUrlVars();
	if(url_vars) {
		switch (url_vars['wine']) {
			case '1': //pino blanc
				$('#overlay').stop().scrollTo( '1320px', 1000, {

				});
				document.getElementById('varietals').style.height = '610px';
				break;
			case '2': //riesling
				$('#overlay').stop().scrollTo( '2120px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '750px';
				break;
			case '3': //pinot gris
				$('#overlay').stop().scrollTo( '2915px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '770px';
				break;
			case '4': //gewurztraminer
				$('#overlay').stop().scrollTo( '3710px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '710px';
				break;
			case '5': //cremant
				$('#overlay').stop().scrollTo( '4505px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '560px';
				break;
			case '6': //other
				$('#overlay').stop().scrollTo( '5305px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '510px';
				break;
			case '7': //late
				$('#overlay').stop().scrollTo( '6100px', 1000, {
					onAfter:function(){
						$("#gallery").hide();
					}
				});
				document.getElementById('varietals').style.height = '510px';
				break;
		}
	}
	
});

function getUrlVars() {
	var map = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		map[key] = value;
	});
	return map;
}

