﻿GoodNites.Storybook = function(serverVars) {
	this.ServerVars = serverVars;
	this.Initialize();
};
GoodNites.Storybook.prototype = {
	Initialize: function() {
		// ajax variables
		var path = '/Services/Storybook.asmx/';
		// If we are in the production or QA environment, 
		// we need to prepend the /na directory to root
		if (document.location.href.indexOf("goodnites.com") != -1) {
			path = "/na" + path;
		}

		var storybookService = new this.ServiceProxy(path);
		var storyBookServiceParams = { storyId: null, categoryId: null };
		var fadeTime = 750;
		var isFirstLoad = true;
		var selectedBook = this.ServerVars.SelectedBook;

		// pagination variables
		var nextStory;

		$('.storyTitle').click(function() {
			ShowStory($(this));
		});

		$('#nextStory').click(function() {
			if (nextStory !== undefined) {
				ShowStory($(nextStory));
			} else {
				// display the end of story view
				switch (selectedBook.toLowerCase()) {
					case 'sweet dreams':
						DisplayEndOfStory('#SweetDreamsTheEnd');
						break;
					case 'story time':
						DisplayEndOfStory('#StoryTimeTheEnd');
						break;
					case 'night tunes':
						DisplayEndOfStory('#NightTunesTheEnd');
						break;
					case 'just you and me':
						DisplayEndOfStory('#JustYouAndMeTheEnd');
						break;
					case 'perfect end to the day':
						DisplayEndOfStory('#PerfectEndTheEnd');
						break;
					default:
						break;
				}
			}
		});

		var ShowStory = function(element) {
		    
		    // title line text is hidden on pageload; show it (behind the hat) now.
		    $('#authorH').css('text-indent', '0');
			
			$('.storyTitle').css('font-weight', 'normal');

			element.css('font-weight', 'bold');
			var storyKey = element.attr('id').split('_');
			var selectedStoryId = storyKey[0];
			var selectedStoryCategoryId = storyKey[1];

			// get the next storiy in the table of contents.
			nextStory = element.parent().next().children()[0];

			if (isFirstLoad) {
				QueryStory(selectedStoryId, selectedStoryCategoryId, DisplayStory);
			} else {
				$('#storyHat').fadeIn(fadeTime, function() {
					QueryStory(selectedStoryId, selectedStoryCategoryId, DisplayStory);
				});
			}

			isFirstLoad = false;
		};

		var QueryStory = function(storyId, categoryId, funct) {
			storyBookServiceParams.storyId = storyId;
			storyBookServiceParams.categoryId = categoryId;

			storybookService.invoke('GetStory', storyBookServiceParams, function(result) {
				$('#title').text(result.Title);
				$('#author').text(result.Author);
				$('#city').text(result.City);
				$('#state').text(result.State);
				$('#body').text(result.Story);

				funct();
			}, storyDisplayError);
		};

		var storyDisplayError = function() {
			$('#story').hide();
			$('.storyTitle').css('font-weight', 'normal');

			if ($('#storyHat').is('visible')) {
				$('#ajaxErrorPanel').show(function() {
					$('#storyHat').fadeOut(fadeTime);
				});
			} else {
				$('#storyHat').fadeIn(fadeTime, function() {
					$('#ajaxErrorPanel').show(function() {
						$('#storyHat').fadeOut(fadeTime);
					});
				});
			}
		};

		var DisplayEndOfStory = function(selector) {
			$('#storyHat').fadeIn(fadeTime, function() {
				$('#story').hide(function() {
					$(selector).show();
					$('#storyHat').fadeOut(fadeTime);
				});
			});
		};

		var DisplayStory = function() {
			// hide any of the "the end" "pages"
			$('.theEnd').hide();

			// take off any of the css classes.
			$('#storyContainer').removeClass();
			$('#storyContainer').addClass('storyPage'); // this is the base css for the container.

			// move the story off the screen
			$('#story').css({ 'position': 'absolute', 'left': '-9000px' });
			// get the size of the story to determine which class to apply
			var height = $('#titleH').height() + $('#authorH').height() + $('#body').height();
			var c = '';

			// figure out which class to add to the container to size it properly
			if (height < 270) {
				c = 'shortStoryPage';
			} else if (height >= 270 && height < 320) {
			    c = 'regularStoryPage';
			} else if (height >= 320 && height < 345) {
				c = 'medStoryPage';
			} else if (height >= 345 && height < 370) {
				c = 'medLongStoryPage';
			} else if (height >= 370 && height < 420) {
				c = 'longStoryPage';
			} else if (height > 420) {
				c = 'superLongStoryPage';
			}

			$('#storyContainer').addClass(c);

			// move the story back to the browser frame
			$('#story').css({ 'position': '', 'left': '' });
			
			$('#ajaxErrorPanel').hide();
			$('#story').show(function() {
				$('#storyHat').fadeOut(fadeTime);
			});
		};

		// the first time this loads we need to get the first story in the table of contents and display it.
		var firstTitle = $($('#storyTitles').children()[0]).children()[0];
		ShowStory($(firstTitle));
	}
};
GoodNites.Extend(GoodNites.Storybook, GoodNites.Core);