var getItemPosition = function(thisMMDD) {
	var i = null;
	for (i = 0; holidayPicker.length > i; i += 1) {
		if (holidayPicker[i].mmdd >= thisMMDD) {
			return i;
		}
	}
	return i;
};

var getItemPositionByTitle = function(thisTitle) {
	var i = null;
	for (i = 0; holidayPicker.length > i; i += 1) {if (holidayPicker[i].title == thisTitle) {return i;}}
	return i;
};



jQuery(document).ready(function($) {	


	parseURLParams();//for testing only. safe to comment out
	//loop through holidayPicker, and stop at thisMMDD
	itemPosition= getItemPosition(thisMMDD);
	

	//ONLY DO THIS ON HOLIDAY EVENTS PAGE
	if(top.location.pathname.search(/holiday/)==1){

		//jquery ajax to pull in appropriate middle content
		thisGutsURL = "middle/" + holidayPicker[itemPosition].mmdd  + ".shtml"
		$('#middleGuts').load(thisGutsURL);
	
		//get ads
		theseAds = holidayPicker[itemPosition].ads;
		//try removing id1 from holidayPicker before rendering the carousel
		for (i = 0; theseAds.length > i; i += 1) {
			thisPosition = getItemPositionByTitle(theseAds[i]);
			holidayPicker.splice(thisPosition,1);
		}
	}
	




	jQuery('#storyCarousel').jcarousel({
      	scroll: 4,
      	start:(itemPosition+1),
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: storyCarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: storyCarousel_itemVisibleOutCallback}
    });

	$('#billboardContent').attr({scrollTop:0,scrollLeft:0});   
	
	
		
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#billboardContent', //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: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#billboardContent', //could be a selector or a jQuery object too.
		axis:'xy', //the default is 'y'
		queue:true,
		duration:1000,
		hash:false,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			$last.removeClass('scrolling');
			$last = $(this).addClass('scrolling');
			if( this.blur )
				this.blur();//remove the awful outline
		},
		onAfter:function( anchor ){
			$last.removeClass('scrolling');
		}
	});    


	
	
	
});
	
	
var holidayPicker = [
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/mlk.jpg', title: 'MLK Lunch',  mmdd:'0117', ads:['MLK Lunch','valentine&rsquo;s day'], href:"https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=MLK%20Lunch"},
	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/uniqueparties.jpg', title: 'unique parties', href:'/fun/?picker=holidayPicker'},
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/groundhog.gif', title: 'groundhog day',  mmdd:'0202', ads:['groundhog day','valentine&rsquo;s day'], href:"https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Groundhog's"},
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/chinese-newyear.jpg', title: 'chinese new year',  href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Chinese%20New%20Year\'s%20Dinnertini',holiday:true},
 	{url: 'http://www2.omansion.com/holiday_events/images/picker/valentines-day.jpg', title: 'valentine&rsquo;s day',  mmdd:'0214', ads:['st patrick&rsquo;s day','valentine&rsquo;s day'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Valentine\'s%20Buffet%20Dinner',holiday:true},
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/sadie.jpg', title: 'sadie hawkins', href:"https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Valentine's%20Party"},
   	{url: 'http://www2.omansion.com/holiday_events/images/picker/mardi-gras.jpg', title: 'mardi gras',  mmdd:'0308', ads:['st patrick&rsquo;s day','april fool&rsquo;s day'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=BOTH:%20Mardi%20Gras%20Martini%20Party',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/st-patricks-day.jpg', title: 'st patrick&rsquo;s day',  mmdd:'0317', ads:['st patrick&rsquo;s day','april fool&rsquo;s day'],  href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=BOTH:%20St.%20Patrick\'s%20Martini%20Party',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/april-fool.jpg', title: 'april fool&rsquo;s day',  mmdd:'0401',ads:['easter','april fool&rsquo;s day'],  href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=J\'O\'kester%20Martini',holiday:true},
 	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/dandilion.jpg', title: 'earth day',  mmdd:'0422',ads:['easter','cinco de mayo'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Earth%20Day'},
 	{url: 'http://www2.omansion.com/holiday_events/images/picker/easter.jpg', title: 'easter', mmdd:'0424',ads:['easter','cinco de mayo'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Easter%20Brunch',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/chilis.jpg', title: 'cinco de mayo',  mmdd:'0505',ads:['cinco de mayo','mother&rsquo;s day'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Cinco%20de%20Mayo',holiday:true},  
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/mothers-day.jpg', title: 'mother&rsquo;s day', mmdd:'0508',ads:['father&rsquo;s day','mother&rsquo;s day'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Mother\'s%20Day%20Brunch',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/fathers-day.jpg', title: 'father&rsquo;s day', mmdd:'0619',ads:['father&rsquo;s day','4th of july'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=FD%20Brunch',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/4th-of-july.jpg', title: '4th of july',  mmdd:'0704', ads:['4th of july'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Fourth',holiday:true},
	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/razzle-dazzle.jpg', title: 'mansion sale',  mmdd:'0905',ads:['halloween','thanksgiving'], href:'/yard_sale/?picker=holidayPicker'},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/halloween.jpg', title: 'halloween', mmdd:'1031',ads:['halloween','thanksgiving'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Halloween%20Party',holiday:true},
    {url: 'http://www2.omansion.com/holiday_events/images/picker/thanksgiving.gif', title: 'thanksgiving', mmdd:'1127',ads:['halloween','december events'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Turkey%20Dinner',holiday:true},
	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/xmas-tours.jpg', title: 'tours', mmdd:'1225',ads:['new year&rsquo;s eve','december events'], href:'/museum/visitor_information/tours/'},
   	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/gift-cert.jpg', title: 'gift certificates', href:'https://www.omansion.com/events/gift-certificates.shtml?picker=holidayPicker'},
 	{url: 'http://www2.omansion.com/holiday_events/images/picker/6-days-xmas.jpg', title: 'open house',  href:'/christmas_sale/?picker=holidayPicker'},
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/snowmen.jpg', title: 'december events',  href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Dec%20Martini',bubble:"bwXmasEvents"},
 	{url: 'http://www2.omansion.com/holiday_events/images/picker/christmas-in-summer.jpg', title: 'xmas dining',  href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=Xmas%20Eve%20Dinner',bubble:"bwXmas"},
  	{url: 'http://www2.omansion.com/holiday_events/images/picker/other/new-year-picker.jpg', title: 'new year&rsquo;s eve',  mmdd:'1231',ads:['new year&rsquo;s eve','MLK Lunch'], href:'https://www.omansion.com/events/reservation.shtml?picker=holidayPicker&Event=BOTH:%20New%20Year\'s%20Eve%20Dinner%20Party',bubble:"bwNewYear"} 
];

function storyCarousel_itemVisibleInCallback(carousel, item, i, state, evt){
    var idx = carousel.index(i, holidayPicker.length);
    carousel.add(i, storyCarousel_getItemHTML(holidayPicker[idx - 1]));
};

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


function storyCarousel_getItemHTML(item){
	var isPopup=item.popup, thisHTML="", thisTitle = item.title;
	//thisID = thisTitle.replace(/&rsquo;/g,"");
	//thisID = "HP_"+thisID.replace(/ /g,"");
	//LOG(thisID);
	if (isPopup=='yes'){//unused???
	    return  '<a href="#" onclick="displayWindow(\''+ item.href+'\',320,800,1)"><img src="' + item.url + '" width="94" height="88" alt="' + thisTitle + '" /><p>' +thisTitle+'</p></a>';
		//if you want to do this, look at the old code for the itsallhere js file
		//    return  '<a href="'+item.href+'?width=320&height=500" class="thickbox"><img src="' + item.url + '" width="94" height="88" alt="' + item.title + '" /><p>' +item.title+'</p></a>';
		//tb_init(DOMitem);
	}else{

	    thisHTML=  '<a href="'+item.href+'"><img src="' + item.url + '" width="94" height="88" alt="' + thisTitle + '" /><p>' +thisTitle+'</p></a>';
	}
	return thisHTML
};


function parseURLParams(){d = unescape($.getURLParam("d"));if(d !="null"){thisMMDD = d; LOG("d = " + thisMMDD)}}
