﻿
// autoheight resets background image to cover full height of page
function autoHeight() {
	var height = ($('.generalContent').height()) + ($('.content fall_winter').height()) + ($('.mainContent').height());
	$('.main').height(height + 100);
	var bkg = $('#bkgImage').height();
	$('#bkgImage img').height(height + 100);
};
   

/* RECIPE DETAIL LOADED
------------------------------------------ */
function recipeLoad() {
	// run these functions when a recipe loads
	bgSrc();
	printBtn();
};

/* RECIPE BACKGROUND 
------------------------------------------ */
function bgSrc() {
	$('#bkgImage img').attr('src', '/images/bkg_fall_winter.jpg');
};

/* PRINT FUNCTIONS
------------------------------------------ */
var printPgID = '';
//  take url parameters
var url = document.URL.split('?')[1];
if (url == undefined) {
	url = '';
};
// If the parameter exists create the message and insert into our paragraph
if (url != '') {
	var param = url;
};
// show only printable recipe
function printPg() {
	$('#bkgImage').fadeOut('medium');
	$('html, body').css('background-color', '#FFFFFF');
	$('.print').fadeIn('medium');
	window.print();
};

// action for print btn
function printBtn() {
	// check param and init print
	if (param == 'print=true') {
		$('#allIn').css('display', 'none');
		printPg();
	} else if (printPgID != '' && printPgID != null) {
		// set print button from printPgID var
		$('.printRecipe').css('display', 'block').click(function () {
			window.open('/recipes/' + printPgID + '.aspx?print=true', '');
			return false;
		});
	} else {
		// set print button from page URL
		$('.printRecipe').css('display', 'block').click(function () {
			//ALLOW DEFAULT LINK ACTION SINCE IT SHOULD BE POINTING TO THE CORRECT ADDRESS.
			//var targetPage = $(this).attr('href');
			//var targetPgFile = targetPage.substring(targetPage.lastIndexOf('/') + 1);
			//var targetPgID = targetPgFile.match(/\w+(?=.)/);
			//window.open('/recipes/' + targetPgFile + '?print=true', '');
			//return false;
		});
		
	};
};

/* EMAIL BUTTON 
------------------------------------------ */
// Hidden and not to be used within this round of the project.

/* RECIPE SLIDER 
------------------------------------------ */
var position = 0;
var elementsPerPage = 5;

function slideToPage(pageNumber, duration, forceLimit, andThen) {
    $('.paginationItems ul li').removeClass('active');
    var pageStart = pageNumber * elementsPerPage;
	var topStart = $('.recipe').length - elementsPerPage;
	if (pageStart > topStart && forceLimit != true) pageStart = topStart;
	var elementWidth = $('.recipe').outerWidth() + 8;
	$('.recipeList').animate({ left: (-pageStart * elementWidth) + 'px' },
        duration,
        function () {
            if (andThen != undefined) {
                andThen();
            }
        });
    position = pageNumber;
    if (forceLimit != true) $('.paginationItems ul li').eq(pageNumber).addClass('active');
}

function slide(dir) {
	var slideCount = $('.recipe').length;
	var slideSets = (Math.ceil(slideCount / 5));
	// slide to the right revealing content on left
	if (dir == 'left') {
	    if (position == 0) {
	        $('.recipeList').fadeOut(400, function () {
	            slideToPage(slideSets - 1, 0);
	            $('.recipeList').fadeIn(400);
	        });
		} else {
    		slideToPage(position - 1, 1000);
		};
	};
	// slide to the left revealing content on right
	if (dir == 'right') {
	    if (position + 1 >= slideSets) {
	        $('.recipeList').fadeOut(400, function () {
	            slideToPage(0, 0);
	            $('.recipeList').fadeIn(400);
	        });
	    } else {
			slideToPage(position + 1, 1000);
		};
	};
};

function recipeListPaging() {
	// based on number of slide sets create bullets
	var slideCount = $('.recipe').length;
	var slideSets = (Math.ceil(slideCount / 5));
	var i = 1;
	var $ul = $('.paginationItems ul');
	$ul.empty();
	for (i = 0; i < slideSets; i++) {
		$ul.append('<li></li>');
    }

	// $('.paginationItems ul li').first().addClass('active');
	$('.paginationItems').css('width', (slideSets * 12) + 'px');
	// clicking li jumps to slide set
	$('.paginationItems ul li').click(function () {
	    var posJump = $(this).index();
	    slideToPage(posJump, 1000);
	});
    slideToPage(0, 0);
};

function recipeDetail() {
	$('.recipe').click(function () {
		var targetPage = $(this).attr('href');
		var targetPgFile = targetPage.substring(targetPage.lastIndexOf('/') + 1);
		var targetPgID = targetPgFile.match(/\w+(?=.)/);
		$('.recipesColummn').load(targetPage + ' .recipesColummn', function () {
			$('html, body').scrollTop(1);
			autoHeight();
			printPgID = targetPgID;
			recipeLoad();
			//alert(targetPage + ' | ' + targetPgFile + ' | ' + targetPgID);
		});
		//console.log('recipeChange?');
		$('.recipe, .recipeName').removeClass('selected');
		$(this).addClass('selected');
		$(this).find('.recipeName').addClass('selected');

		return false;
	});

};

/* INITIATE ON READY
------------------------------------------  */
$(document).ready(function () {
    printBtn();
    recipeDetail();
    // slider nav
    $('.recipesLeftArrow').click(function () { slide('left'); });
    $('.recipesRightArrow').click(function () { slide('right'); });

    var setCurrentFilter = function () {
        //$('.recipeList').hide();
        $('.recipeList a').removeClass('recipe').addClass('recipeOff');
        $('.recipeList a.' + $('.recipesSelect select').val()).removeClass('recipeOff').addClass('recipe');

        var slideCount = $('.recipe').length;
        $('.recipeList').css('width', (slideCount * 154 + 2) + 'px');
        //$('.recipeList').fadeIn();
        recipeListPaging();
    };

    setCurrentFilter();
    $('.recipesSelect select').change(setCurrentFilter);
});

