jQuery(function( $ ){
	
	// first check for age verification
	var checkCookie = readCookie("isLegal");
//	var checkCookie = 1;	
	
	if (!checkCookie) {
		//remember where the user wanted to go
		section = document.URL;
		var destination = new Array();
		destination = section.split('#');
		//kick to age verification
		window.location="verify/?dest=" + destination[1];
	}
	initPromos();
	
	/**
	 * 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
	 */
	$('body').attr({scrollTop:0,scrollLeft:0});

	var $last = $([]);//save the last link	
	$.localScroll.defaults.axis = 'x';

	//get page location (#hash) from url
	url_section = document.URL;
	var url_destination = new Array();
	url_destination = url_section.split('#');

	$("#about .mask_next_section").show();

	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: 'body', //could be a selector or a jQuery object too.
		axis:'x',//the default is 'y'
		queue:'false',
		duration:600,
		onAfter:function(){
			//highlight initial nav item
			$("#n_" + url_destination[1] + " a").addClass('scrolling');
			var targetPosition = {'left':0};
			
			if(url_destination[1] == "about") {
				$("#about .mask_next_section").hide();
				$("#about .subnav").slideDown();
				$("#cocktails .mask_next_section").show();
				targetPosition = $('#about').position();
			}
			else if(url_destination[1] == "cocktails") {
				$("#about .mask_next_section").hide();
				$("#cocktails .subnav").slideDown();
				$("#events .mask_next_section").show();
				targetPosition = $('#cocktails').position();
			}
			else if(url_destination[1] == "events") {
				$("#about .mask_next_section").hide();
				$("#events .subnav").slideDown();
				$("#ads .mask_next_section").show();
				targetPosition = $('#events').position();
			}
			else if(url_destination[1] == "home") {
				$("#about .mask_next_section").show();
			}
			else if(url_destination[1] == "ads"){
				$("#bot .mask_next_section").show();
				targetPosition = $('#ads').position();
			}
			else if(url_destination[1]=="bot"){
				loadBotSplash();
				targetPosition = $('#bot').position();
			}
//			$('#footer').css({'left':targetPosition.left+"px"});
//			$('#footer').slideDown();
		}
	});
	
	useLocalScrollFrom($('#navigation'));	
	useLocalScrollFrom($('.mask_next_section'));
	useLocalScrollFrom($('#landing_content'));
	gallery_initPagination();

	$('#ad_rotator').jcarousel({
        size: mycarousel_itemList.length,
        scroll: 1,
//		wrap: "both",
		wrap: "circular",
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null,
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
//        itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
    });

//   	$("#ad_rotator a").slimbox();
	// jQuery.slimbox(mycarousel_for_lightbox,0,{loop: true});
	// alert(mycarousel_for_lightbox[0]);
});


/* carousel for The Ads */
var mycarousel_itemList = [
    {url: "images/print_ads/small/01.jpg", title: "ad1"},
    {url: "images/print_ads/small/10.jpg", title: "ad10"},
    {url: "images/print_ads/small/09.jpg", title: "ad9"},
    {url: "images/print_ads/small/07.jpg", title: "ad7"},
    {url: "images/print_ads/small/04.jpg", title: "ad4"},
    {url: "images/print_ads/small/06.jpg", title: "ad6"},
    {url: "images/print_ads/small/08.jpg", title: "ad8"},
    {url: "images/print_ads/small/03.jpg", title: "ad3"},
    {url: "images/print_ads/small/11.jpg", title: "ad11"},
    {url: "images/print_ads/small/05.jpg", title: "ad5"},
    {url: "images/print_ads/small/02.jpg", title: "ad2"},
    {url: "images/print_ads/small/12.jpg", title: "ad12"}
];

var mycarousel_for_lightbox = [
	{url: "images/print_ads/large/01.jpg", title: "ad1"},
    {url: "images/print_ads/large/10.jpg", title: "ad10"},
    {url: "images/print_ads/large/09.jpg", title: "ad9"},
    {url: "images/print_ads/large/07.jpg", title: "ad7"},
    {url: "images/print_ads/large/04.jpg", title: "ad4"},
    {url: "images/print_ads/large/06.jpg", title: "ad6"},
    {url: "images/print_ads/large/08.jpg", title: "ad8"},
    {url: "images/print_ads/large/03.jpg", title: "ad3"},
    {url: "images/print_ads/large/11.jpg", title: "ad11"},
    {url: "images/print_ads/large/05.jpg", title: "ad5"},
    {url: "images/print_ads/large/02.jpg", title: "ad2"},
    {url: "images/print_ads/large/12.jpg", title: "ad12"}
];

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1],mycarousel_for_lightbox[idx - 1],i));
	$("#ad_"+i).slimbox(); //register the newly-added item with slimbox
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

function mycarousel_getItemHTML(item, lightboxitem, index) {
	//generates html for each item in carousel
    return '<a id="ad_'+index+'" href="'+ lightboxitem.url +'" rel="lightbox"><img src="' + item.url + '" width="277" height="377" alt="' + item.url + '" /></a>';
};

function mycarousel_initCallback(carousel) {
	//prev and next controls
    jQuery('#ad_rotator-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#ad_rotator-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function gallery_initPagination()
{	//faux pagination for now
	jQuery('#party_people_2').hide();
	jQuery('#gallery-prev').hide();
	jQuery('#gallery-next').bind('click', function() {
		jQuery('#party_people_1').hide();
		jQuery('#gallery-next').hide();
		jQuery('#gallery-prev').fadeIn(250);
		jQuery('#party_people_2').fadeIn(250);
        return false;
    });

    jQuery('#gallery-prev').bind('click', function() {
        jQuery('#party_people_2').hide();
		jQuery('#gallery-prev').hide();
		jQuery('#gallery-next').fadeIn(250);
		jQuery('#party_people_1').fadeIn(250);
        return false;
    });
}

//PROMO ROTATOR SETUP
var svPromos=new Array();
var svPromoIndex=0;
function initPromos(){
	//put all the links you want to rotate through into svPromos
//	svPromos.push({html:'<a href="#bot"><img src="images/landing/builder_fpo.png" alt="Bot Builder"></a>',duration:6000});
	svPromos.push({html:'<a href="#bot"><img src="images/landing/bb_promo.png" alt="Bot Builder" /></a>',duration:6000});
	//batch add all the promos we have
	/*
	var numAds = 7;
	for(var i=0; i!=numAds; i++){
		svPromos.push({html:'<a href="#ads"><img src="images/landing/ad_promo_'+i+'.png" alt="The Ads"></a>',duration:5000});
	}
	//start looping through the ads
	*/
	svNext();
}
function svNext(){
	$('#promo').hide();
	$('#promo').html(svPromos[svPromoIndex].html);
	$('#promo').fadeIn(500);
	useLocalScrollFrom($('#landing_content'));//need to reset local scroll
	if(svPromos.length>1)setTimeout(svNext,svPromos[svPromoIndex].duration);//cycle if multiple items
	if(++svPromoIndex>=svPromos.length){svPromoIndex=0;}//loop back to 0
}
//END PROMO ROTATOR

function useLocalScrollFrom(object){
	object.localScroll({
//		target: '#content', //could be a selector or a jQuery object too.
		axis:'x', //the default is 'y'
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			//make sure you are leaving the current section and not reloading the landing page
			var destination = new Array();
			var section = this.toString();
			destination = section.split('#');

			if((destination[1] == "about") && ($("#n_about a").hasClass("scrolling"))) {
				$(".about_sub").hide();
				$("#about .subnav a").removeClass("selected");
				$("#about_landing").show();
			}
			else if((destination[1] == "cocktails") && ($("#n_cocktails a").hasClass("scrolling"))) {
				$(".drinks_sub").hide();
				$("#cocktails .subnav a").removeClass("selected");
				$("#drink_list").show();
				$("#drinks_landing").show();
			}
			else if((destination[1] == "events") && ($("#n_events a").hasClass("scrolling"))) {
				$(".events_sub").hide();
				$("#events .subnav a").removeClass("selected");
				$("#events_landing").show();
			}
			else {
				$("#nav_icons a").removeClass('scrolling');
				$("#about .subnav").slideUp();
				$("#cocktails .subnav").slideUp();
				$("#events .subnav").slideUp();
			}
			
			if(loadedBotSplash) onLeaveBotSplash();//stop flash animation if it's loaded
			$last = $(this);
			$nav_item = $("#n_" + destination[1] + " a");
//			$('#footer').slideUp(100);
		},
		onAfter:function( anchor ){
			$nav_item.addClass('scrolling');
			//show/hide subnav
			var justClicked = $last.html();
			var targetPosition = {'left':0};
			
			if(justClicked == "About") {
				$("#about .mask_next_section").hide(); //kill the mask
				$("#about .subnav").slideDown();		  //show subnav
				$("#cocktails .mask_next_section").show();  //add the next section's mask
				targetPosition = $("#about").position();
			}
			else if(justClicked == "Cocktails") {
				$("#cocktails .mask_next_section").hide();
				$("#cocktails  .subnav").slideDown();
				$("#events .mask_next_section").show();
				targetPosition = $("#cocktails").position();
			}
			else if(justClicked == "Events") {
				$("#events .mask_next_section").hide();
				$("#events .subnav").slideDown();
				$("#ads .mask_next_section").show();
				targetPosition = $("#events").position();
			}
			else if(justClicked == "The Ads" || justClicked.match("The Ads") == "The Ads") {
				$("#ads .mask_next_section").hide();
				$("#bot .mask_next_section").show();
				targetPosition = $("#ads").position();
			}
			else if(justClicked == "Svedka") {
				$("#about .mask_next_section").show();
				targetPosition = {'left':0};
			}
			else if(justClicked=="Bot Builder" || justClicked.match("Bot Builder") == "Bot Builder"){
				$("#bot .mask_next_section").hide();
				targetPosition = $('#bot').position();
				loadBotSplash();
			}
			//$('body').attr({scrollTop:0});
			
//			$('#footer').css({'left':targetPosition.left+"px"});
//			$('#footer').slideDown();
		}
	});
}
