$(document).ready(function() {	

	var ieVer = msieversion();
	
	$('#mainNav a').hover(function(){ $('#container').addClass('hover'); }, function(){ $('#container').removeClass('hover'); })
	
	
	// Tab to show or hide sub nav menu
	$('#subNav ul').stop().animate({ width:160 }, 800);
	$('#subTab').toggle(function() {
		$('#subNav ul').stop().animate({
			width:0
		}, 800, function() {
			$('#subTab').css('background-position','left');
		});
	}, function() {
		$('#subNav ul').stop().animate({
			width:160
		}, 800, function() {
			$('#subTab').css('background-position','right');
		});
	});
	
	
	// Change content using the sub nav menu
	$('#subNav li a[href^="#"]').click(function(){
		var subTar = $(this).attr('href');
		if (!($(subTar).hasClass('active'))) {
			$('#content .active').addClass('prevActive').css('z-index',4).removeClass('active');
			$(subTar).addClass('active');
			$('#content .prevActive').stop().animate({ left:-360 }, 800, function() {
				$('#content .prevActive').animate({ left:0 }, 300);
				$(this).removeClass('prevActive').removeAttr('style');
			});
		}
		
		return false;
	});
	
	
	// Slideshow
	if ($('ul#gallery').length ) {
		// Slideshow with thumbnails
		if ($('ul#gallery').hasClass('thumbs')) {
			$('#content h1').after('<ul id="galleryThumbs"></ul>');
			$('ul#gallery').cycle({
				fx: 'fade',
				pager: '#galleryThumbs',
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#"><img src="' + $('img', slide).attr('src') + '" /></a></li>'; 
				}
			});
			$('#galleryThumbs img').click(function() {
				$('ul#gallery').cycle('pause');
			});
			$('#gallery').click(function() {
				$('ul#gallery').cycle('resume');
			});
		}
		else {
			$('ul#gallery').cycle({
				fx: 'fade'
			});
		}
	}
	
	
	/* The event slider */
	if($('#events ul').children().size()>1) {
		$('#events ul').cycle({
			fx: 'scrollVert',
			speed: 400, 
			timeout: 5500,
			pause: 1,
			next: '#eNext', 
			prev: '#ePrev'
		});
		// Stop Autoplay after prev or next buttons are pressed
		$('#eNext, #ePrev').click(function() { 
			$('#events>ul').cycle('pause'); 
		});
	}
	else if($('#events ul').children().size()==1) {
		$('#eNext, #ePrev').css('display','none');
	}
	else
		$('#events').css('display','none');
	
	
	// Reservations Panel
	$('#reserve>a').click(function() {
		if ($('#reserve div').is(':visible')) {
			$('#reserve').animate({ height: 16+'px' }, 'fast');
			$('#reserve>a').animate({ lineHeight: 16+'px', marginRight: -10+'px' }, 'fast');
		} else {
			$('#reserve').animate({ height: 26+'px' }, 'fast');
			$('#reserve>a').animate({ lineHeight: 26+'px', marginRight: 10+'px' }, 'fast');
		}
		$('#reserve div').toggle('fast');
		return false;
	});
	$("a#reserveNow").click(function() { 
		window.open("http://www.opentable.com/nisen-sushi-reservations-commack?rid=38143&restref=38143&p="+$("#resPeople").val()+"&d="+$("#resDate").val()+"+"+$("#resHour").val()+":"+$("#resMin").val()+"+"+$("#resAMPM").val()+"");
		
		// Rehide the panel
		$('#reserve').animate({ height: 16+'px' }, 'fast');
		$('#reserve>a').animate({ lineHeight: 16+'px', marginRight: -10+'px' }, 'fast');
		$('#reserve div').toggle('fast');
		
		return false;
	});
	
	
	// Floats the right half of the nav menu in  LTE IE8
	//if (!jQuery.support.opacity)
	//	$("#mainNav li:nth-child(n+4)").css('float','right');
	
	
	// Date Picker
	var todayDate = new Date();
	$('#resDate').val((todayDate.getMonth()+1) + '/' + todayDate.getDate() + '/' + todayDate.getFullYear());
	$('#resDate').DatePicker({
		format:'m/d/Y',
		date: $('#resDate').val(),
		current: $('#resDate').val(),
		position: 'b',
		onBeforeShow: function(){
			$('#resDate').DatePickerSetDate($('#resDate').val(), true);
		},
		onChange: function(formated, dates){
			$('#resDate').val(formated);
			$('#resDate').DatePickerHide();
		},
		onRender: function(date) {
			return {
				disabled: (date.valueOf() < todayDate.valueOf())
			}
		}
	});
	
	
	// Color Box
	$("a.cb").colorbox();
	$("a.cbIF").colorbox({width:"90%", height:"90%", iframe:true});


	// Get IE version
	function msieversion() {
		var ua = window.navigator.userAgent;
		var msie = ua.indexOf ( "MSIE " );
		if ( msie > 0 ) {
			return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
		}
		else
			return 0;
	}
	// Check if version is <= 8
	function ieLTE8() {
		if (msieversion() <= 8)
			return true;
	}
});
