﻿$(document).ready(function() {
    EggoWaffles.productNavigation();
    EggoWaffles.siteModals();
    EggoWaffles.productKosherInfo();
    EggoWaffles.productNutritionInfo();
    EggoWaffles.relatedProductsScroller();
});

// Creating a namespace for all Eggo Waffles functions
var EggoWaffles = {};

// Enables product navigation accordion
EggoWaffles.productNavigation = function() 
{
	var category = $("#CategoryNameKey").val();
	category = "." + category;
	
	$('#category-nav .cat-sub-nav').css('display', 'block');
	
	$('#category-nav').accordion({
		active: category, // Needs to be dynamic!
		autoHeight: false,
		changestart: function(event, ui)
		{
			$(ui.oldHeader).prev('.view-all').hide();
			$(ui.newHeader).prev('.view-all').show();
		},
		collapsible: true,
		header: 'a.header'
	});
	
	// Add class for extra styling
	$('.cat-sub-nav li:first-child').addClass('first');
	
	// Show view all link on default open tab
	$('.cat-sub-nav:visible').parent().children('.view-all').show();
}

// Container for all modals on the site
EggoWaffles.siteModals = function() {

    /* Video Player */
    $('#global-modal-video').dialog({
        autoOpen: false,
        closeOnEscape: false,
        draggable: false,
        width: 687,
        height: 453,
        modal: true
    }).parents('.ui-dialog:first').find('.ui-dialog-titlebar').hide();

    $('#watch-now-btn').click(function(event) {
        event.preventDefault();
        $('#global-modal-video').dialog('option', 'dialogClass', 'flash-player');
        $('#global-modal-video').dialog('open');
    });

    $('#close-video-modal-btn').click(function(event) {
        event.preventDefault();
        $('#global-modal-video').dialog('close');
    });

    /* Nutritional */
    $('#global-modal-nutrition').dialog({
        autoOpen: false,
        closeOnEscape: false,
        draggable: false,
        modal: true
    }).parents('.ui-dialog:first').find('.ui-dialog-titlebar').hide();

    /* Share with a friend */
    $('#global-modal-share').dialog({
        autoOpen: false,
        closeOnEscape: false,
        draggable: false,
        width: 676,
        modal: true
    }).parents('.ui-dialog:first').find('.ui-dialog-titlebar').hide();

    /* Thank you */
    $('#global-modal-thankyou').dialog({
        autoOpen: false,
        closeOnEscape: false,
        draggable: false,
        width: 400,
        modal: true
    }).parents('.ui-dialog:first').find('.ui-dialog-titlebar').hide();

    $('.share-title').click(function(event) {
        event.preventDefault();
        $('#global-modal-share').dialog('option', 'dialogClass', 'share-modal');
        $('#global-modal-share').dialog('open');

        $('#close-share-modal-btn').bind("click", function(event) {
            event.preventDefault();
            clearFormValues();
            $('#global-modal-share').dialog('close');
        });

        $("#share-cancel").bind("click", function(event) {
            event.preventDefault();
            clearFormValues();
            $('#global-modal-share').dialog('close');
        });
    });
    
    // K Pass Pop Up
    $('.kpass-popup').popup({
        height: 750,
        width: 600,
        scrollbars: false
    });
}

// Rollover for product kosher info
EggoWaffles.productKosherInfo = function ()
{
	$('#btn-allergen').click(function(event)
	{
		event.preventDefault();
	}).hover(function(event){ //Rollover
		$('#allergen-popup').show();
	},
	function(event){ // Rollout
		$('#allergen-popup').hide();
	});
}

// Ajax call for grabbing nutrional info
EggoWaffles.productNutritionInfo = function() {
    $('#btn-nutritional').click(function(event) {
        event.preventDefault();

        // if the ajax call has already been made and the image is already written on to the page,
        //      just show the nutrition info
        if ($('#modal-image').html() != "") {
            $('#global-modal-nutrition').dialog('open');
            return;
        }

        var kicId = $(this).attr('href');

        $.ajax({
            cache: false,
            contentType: "application/json; charset=utf-8",
            data: "{}",
            type: 'GET',
            dataType: "json",
            url: '/Products/GetProductNutritionInfo/' + kicId,
            success: function(data) {

                var nutritionLabel = new Image();

                nutritionLabel.src = data.ResultPacket.NutritionUrl;
                $(nutritionLabel).appendTo('#modal-image');
                $(nutritionLabel).bind("load", function(e) {

                    var labelWidth = nutritionLabel.width + 40;
                    var labelHeight = nutritionLabel.height + 40;

                    if (labelWidth > 40 && labelHeight > 40) {
                        $('#global-modal-nutrition').dialog('option', 'width', labelWidth);
                        $('#global-modal-nutrition').dialog('option', 'height', labelHeight);
                        $('#global-modal-nutrition').dialog('open');
                    }
                });

                $('#close-nutrition-modal-btn').bind("click", function(event) {
                    event.preventDefault();
                    $('#global-modal-nutrition').dialog('close');
                });
            }
        });
    });
}

// Building out related products scroller
EggoWaffles.relatedProductsScroller = function()
{
	// Defining used variables
	var howMany = 0;
	var scrollWidth = 0;
	var currentPosition = 0; // zero-indexed
	var allowedClicks = 0;
	var marginScroll = 0;
	
	// Loop over related products
	$('#product-scroller-container > ul > li').each(function(i)
	{
		scrollWidth += $(this).outerWidth(true); // Add all widths of products
		
		howMany ++; // Add up how many related products
	});
	
	allowedClicks = Math.floor(scrollWidth / 477);
	
	if(howMany > 3)
	{
		$('#scroll-right-btn').show();
	}
	
	// Set width on scroll content
	$('#product-scroller-container > ul').width(scrollWidth);
	
	// Left scroll button
	$('#scroll-left-btn').click(function(event)
	{
		event.preventDefault();
		
		if($('#scroll-right-btn').css('display') != 'block')
		{
			$('#scroll-right-btn').show();
		}
		
		if(currentPosition > 0)
		{
			marginScroll += 477;
			currentPosition --;
			
			if(currentPosition == 0)
			{
				$(this).hide();
			}
			
			$('#product-scroller-container > ul').animate({ 'marginLeft': marginScroll }, 500);
		}
	});
	
	// Right scroll button
	$('#scroll-right-btn').click(function(event)
	{
		event.preventDefault();
		
		if($('#scroll-left-btn').css('display') != 'block')
		{
			$('#scroll-left-btn').show();
		}
		
		if(currentPosition < allowedClicks)
		{
			marginScroll -= 477;
			currentPosition ++;
			
			if(currentPosition == allowedClicks)
			{
				$(this).hide();
			}
			
			$('#product-scroller-container > ul').animate({ 'marginLeft': marginScroll }, 500);
		}
	});
}