// Prime JavaScript

$(document).ready(function() {
	
	// Check if mobile
	var notMobile;
	//if( $('#swooshImg').is(':visible') )
	if (screen.width >= 480)
		notMobile = true;
	else
		notMobile = false;
	
	// Only apply if a pane element is present
	if (($("#pane").length > 0) && notMobile){
	
	var pKids = $('ul#mPanel').children().size()-1;
	var pPos = 0;
	var mpl = 0; // Left position for #mPanel
	
	$('#paRight').addClass('disabled');
	$('#paLeft').addClass('disabled');
	$('#paStart').addClass('disabled');
	
	// Move the first panel out of the frame and slide it in
	$('ul#mPanel').css({ 'left': "640px" });
	$('ul#mPanel').show();
	$('ul#mPanel').delay(400).animate({ 'left': 580-$('ul#mPanel>li:first-child').width() }, 'slow', function() { 
		mpl = $('ul#mPanel').position().left;
		$('#paRight').removeClass('disabled');
		$('#paLeft').removeClass('disabled');
		$('#paStart').removeClass('disabled');
	});
	
	$('#stLinks>ul>li>a').click(function() {
		var target = $(this).attr("href");
		if (target.split("#")[1].length >= 1) {
			var cPos = pPos+1;
			pPos = $('#mPanel>li').index($(target));
			var offs = 34;
			var foo = $('ul#mPanel').position().left;
			for (i=cPos; i<=pPos; i++) {
				var subTar = $('#pane').find('#mPanel>li:eq('+i+')');
				foo += (subTar.width()+offs)*-1;		
			}
			mpl = foo;
			$('ul#mPanel').stop().animate({ 'left': foo+"px" }, 'slow');
			return false;
		}
	});
	
	//$('#paRight').click(function() {
	$('#paRight:not(.disabled)').live("click", function() {
		if (pPos < pKids) {
			//$('#pane').find('#mPanel>li:eq('+pPos+')').fadeTo("slow",.5);
			pPos += 1;
			var target = $('#pane').find('#mPanel>li:eq('+pPos+')');
			var offs = 34;
			//var foo = ((target.width()+offs)*-1)+$('ul#mPanel').position().left;
			var foo = ((target.width()+offs)*-1)+mpl;
			mpl = foo;
			$('ul#mPanel').stop().animate({ 'left': foo+"px" }, 'slow');
			//target.fadeTo("slow", 1);
		} else {
			$('ul#mPanel').stop().animate({ 'left': mpl+-20+"px" }, 'slow').animate({ 'left': mpl+"px" }, 'fast');
		}
		return false;
	});
	//$('#paLeft').click(function() {
	$('#paLeft:not(.disabled)').live("click", function() {
		if (pPos > 0) {
			//$('#pane').find('#mPanel>li:eq('+pPos+')').fadeTo("slow",.5);
			var offs = 34;
			var target = $('#pane').find('#mPanel>li:eq('+pPos+')');
			var foo = (target.width()+offs)+mpl;
			mpl = foo;
			$('ul#mPanel').stop().animate({ 'left': foo+"px" }, 'slow');
			//target.fadeTo("slow", 1);
			pPos -= 1;
		} else if (pPos == 0) {
			pPos -= 1;
			mpl = 614;
			$('ul#mPanel').animate({ 'left': mpl }, 'slow');
		} else {
			$('ul#mPanel').stop().animate({ 'left': mpl+20+"px" }, 'slow').animate({ 'left': mpl+"px" }, 'fast');
		}
		return false;
	});
	//$('#paStart').click(function() {
	$('#paStart:not(.disabled)').live("click", function() {
		if (pPos > 0) {
			pPos = 0;
			$('ul#mPanel').animate({ 'left': 580-$('ul#mPanel>li:first-child').width() }, 'slow', function() { mpl = $('ul#mPanel').position().left; });
			//target.fadeTo("slow", 1);
		} else if (pPos == 0) {
			pPos -= 1;
			mpl = 614;
			$('ul#mPanel').animate({ 'left': mpl }, 'slow');
		} else {
			$('ul#mPanel').stop().animate({ 'left': mpl+20+"px" }, 'slow').animate({ 'left': mpl+"px" }, 'fast');
		}
		return false;
	});
	
	} // End if pane
	
	if (notMobile){
		
	/* The content in the bottom right block */
	$('#rightContentSlider').cycle({
		fx: 'scrollVert',
		speed: 400, 
		timeout: 5500,
		pause: 1,
		next: '#brDown', 
		prev: '#brUp',
		cleartypeNoBg:true
	});
	// Stop Autoplay after prev or next buttons are pressed
	$('#brDown, #brUp').click(function() { 
    	$('#rightContentSlider').cycle('pause'); 
	});
	
	if ($("#leftGal").length > 0){
	// The left photo gallery
	$('#leftGal').cycle({
		fx: 'fade',
		speed: 900, 
		timeout: 5000,
		cleartypeNoBg:true
	});
	}
	if ($("#rightGal").length > 0){
	// The right photo gallery
	$('#rightGal').cycle({
		fx: 'fade',
		speed: 900, 
		timeout: 5000,
		cleartypeNoBg:true
	});
	}
	
	$("#hideText").hover(
		function () {
			$("#leftContent").stop();
			$("#leftContentR").stop();
			
			$("#leftContent").animate({ left: "-"+$('#leftContent').width() }, 'slow');
			$("#leftContentR").animate({ right: "-"+$('#leftContentR').width() }, 'slow');
		}, 
		function () {
			$("#leftContent").stop();
			$("#leftContentR").stop();
			
			$("#leftContent").animate({ left: "0px" }, 'slow');
			$("#leftContentR").animate({ right: "0px" }, 'slow');
		}
	);
	
	} //notMobile
	
	// Reservations Panel
	var resHeight = $('#reserveContent').height(); // Store the height of the panel
	var resText = $('#reserveTab').html(); // Store the original label of the button
	$('#reserveContent').css({ height: "0px" }); // Set the panel height to 0 to hide
	$('#reserveContent').show(); // The panel is originally set to be hidden so that it doesn't appear before the height can be set to 0
	$('#reserveTab').on('click', function(e) {	
		if ($('#reserveContent').height() == 0) {
			console.log('1');
			$('#reserveContent').animate({ height: resHeight+20+'px' }).animate({ height: resHeight+"px" }, 'fast' );
			$('#reserveTab').html("Close");
		} else {
			console.log('2');
			$('#reserveContent').animate({ height: resHeight+20+"px" }).animate({ height: "0px" }, 'fast' );
			$('#reserveTab').html(resText);
		}
		console.log('3');
		e.preventDefault();
	});
	// Datepicker
	$('#resDate').datepicker({
		minDate: 0,
		showAnim: "slideDown",
		gotoCurrent: true
		//altField: '#alternate',
		//altFormat: 'DD, d MM, yy'
	});
	$("#resDate").datepicker('setDate', new Date());
	//$('#resDate').datePicker().val(new Date().asString()).trigger('change');
	// Hour Slider
	$("#resHourSlider").slider({
		value:19,
		min: 0,
		max: 23,
		step: 1,
		slide: function(event, ui) {
			if (ui.value == 0)
				$("#resHour").val(12);
			else if (ui.value > 12) {
				$("#resHour").val(ui.value-12);
				$("#resAMPM").val("PM");
			} else if (ui.value == 12){
				$("#resHour").val(ui.value);
				$("#resAMPM").val("PM");
			} else {
				$("#resHour").val(ui.value);
				$("#resAMPM").val("AM");
			}
		}
	});
	$("#resHour").val("7");
	$("#resAMPM").val("PM");
	// Minute Slider
	$("#resMinSlider").slider({
		value:00,
		min: 00,
		max: 45,
		step: 15,
		slide: function(event, ui) {
			if (ui.value == 0)
				$("#resMin").val("0"+ui.value);
			else
				$("#resMin").val(ui.value);
		}
	});
	$("#resMin").val("00");
	// People Slider
	$("#resPeopleSlider").slider({
		value:2,
		min: 1,
		max: 20,
		step: 1,
		slide: function(event, ui) {
			$("#resPeople").val(ui.value);
		}
	});
	$("#resPeople").val($("#resPeopleSlider").slider("value"));
	// Button
	$("a#reserveNow").button({
		icons: { primary: 'ui-icon-arrowreturnthick-1-e' }
	});
	$("a#reserveNow").click(function() { 
		window.open("http://www.opentable.com/single.aspx?rid=7356&restref=7356&p="+$("#resPeople").val()+"&d="+$("#resDate").val()+"+"+$("#resHour").val()+":"+$("#resMin").val()+"+"+$("#resAMPM").val()+"");
		
		// Rehide the panel
		$('#reservations>#reserveContent').animate({ height: resHeight+20+"px" }).animate({ height: "0px" }, 'fast' );
		$('#reserveTab').html(resText);
			return false;
	});
	
	
	
	/* Weather */
	function getWeather() {
		$.getJSON('weather.asp', 
			function(data) {
				//$('#weatherTemp').html(data.temp + "&deg;");
				//$('#weatherCond').html(data.cond);
				setWeatherToday(data.cTemp, data.cCond);
				setWeatherForecast("1", data.fTemp1, data.fCond1, data.fDay1);
				setWeatherForecast("2", data.fTemp2, data.fCond2, data.fDay2);
				setWeatherForecast("3", data.fTemp3, data.fCond3, data.fDay3);
				/*for(i=1; i<=3; i++) {
					alert(i+" "+[data.fTemp]+" "+[data.fCond+i]+" "+[data.fDay+i]);
					//setWeatherForecast(i, [data.fTemp+i], [data.fCond+i], [data.fDay+i]);
				}*/
			}
		);
	}
	//getWeather();
	
	//The months between which the calendar should be displayed (inclusive)
	function showWeather(sMonth,eMonth){
		var d = new Date();
		sMonth -= 1;
		eMonth -= 1;
		cMonth = d.getMonth();
		if (cMonth >= sMonth && cMonth <= eMonth)
			displayCalendar();
	}
	showWeather(5,9);
	
	function displayCalendar() {
		$('#weatherHolder').slideDown(300);
		getWeather();
	}
	
	function setWeatherToday(cTemp, cCond) {
		$('#weatherTemp').html(cTemp + "&deg;");
		$('#weatherIconLarge').html("<span class=\"condtionTT\">"+cCond+"</span>");
		$('#weatherIconLarge').addClass(conditionMatch(cCond));
	}
	function setWeatherForecast(fi, fTemp, fCond, fDay) {
		$('#fTemp'+fi).html(fTemp + "&deg;");
		$('#fDay'+fi).html(dayMatch(fDay));
		$('#fCond'+fi).html("<span class=\"condtionTT\">"+fCond+"</span>");
		$('#fCond'+fi).addClass(conditionMatch(fCond));
	}
	
	function conditionMatch(cond) {
		var condition = cond.toLowerCase();
		var sunny  = new Array("sunny", "clear", "mostly sunny");
		var partly = new Array("partly sunny", "partly cloudy", "mostly cloudy");
		var cloudy = new Array("overcast", "cloudy", "mist", "dust", "fog", "smoke", "haze");
		var rainy  = new Array("scattered thunderstorms", "showers", "scattered showers", "rain and snow", "light snow", "freezing drizzle", "chance of rain", "chance of storm", "chance of showers", "rain", "chance of snow", "storm", "thunderstorm", "chance of tstorm", "sleet", "snow", "icy", "flurries", "light rain", "isolated thunderstorms");
		
		var weather = new Array(sunny, partly, cloudy, rainy);
		var weatherStrings = new Array("sunny","partly","cloudy","rainy");
		
		for(i=0; i<weather.length; i++) {
			for(j=0; j<weather[i].length; j++) {
				if(condition == weather[i][j])
					return weatherStrings[i];
			}
		}
	}
	
	function dayMatch(theDay) {
		var dayAbr = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
		var dayFull = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
		
		for(i=0; i<dayAbr.length; i++) {
			if(theDay == dayAbr[i])
				return dayFull[i];
		}
	}
	
	$('#weatherArrow').click(function() {
		openWidth = 314;
		closedWidth = 150;
		if(notMobile == false)
			closedWidth = 44;
		if ($('#weatherHolder').width() == closedWidth) {
			$('#weatherHolder').animate({ width: openWidth+"px" }, 'slow');
			$('#weatherArrow').css({ 'backgroundPosition': "left" });
		} else {
			$('#weatherHolder').animate({ width: closedWidth+"px" }, 'slow');
			$('#weatherArrow').css({ 'backgroundPosition': "right" });
		}
		return false;
	});	
});
