﻿/*
Author:			Dennis Milandt
Description:	User Interface scripts
*/



/*************************/
/* Variables             */
/*************************/




/*************************/
/* User Interface        */
/*************************/
var UI = {
	// User Interface setup function.
	setup: function()
	{
		Nav.setup();
		ProdImgs.setup();
		Forms.setup();
		
		$(function()
		{
			$('#tabs').tabs();
			$('#adminContent').tabs();
		});
	}
}




/*************************/
/* Navigation            */
/*************************/
var Nav = {
    // Navigation setup function.
    setup: function()
    {

        $('#nav li').hover(
        function()
        {
            $(this).find('ul').fadeIn(100);
        },
        function()
        {
            $(this).find('ul').fadeOut(100);
        });

        $('#subnav li img').click(
		function()
		{
		    var list = $(this).parent().children('ul');
		    Nav.toggleList(list, $(this));
		});

        $('#expandAll').click(function() { Nav.openAll(); });

        $('#collapseAll').click(function() { Nav.closeAll(); });

        $('#nav ul li.home a, #logo img').mouseover(function()
        {
            $('#nav ul li.home a img').attr('src', $('#nav ul li.home a img').attr('src').replace('home', 'home_hover'));
        });
        $('#nav ul li.home a, #logo img').mouseout(function()
        {
            $('#nav ul li.home a img').attr('src', $('#nav ul li.home a img').attr('src').replace('home_hover', 'home'));
        });
    },
    // Toggle the navigation expand/collapse
    toggleList: function(list, img, resetTo)
    {

        var len = (list.find('ul.open > li').add(list.children('li'))).length;
        var speed = len * 75;
        if (speed < 200)
            speed = 200;

        if (resetTo == undefined)
        {
            list.slideToggle(speed,
			function()
			{
			    list.toggleClass('open');
			    Nav.setImage(list, img);
			});
        }
        else if (resetTo == 'open')
        {
            list.slideDown(speed,
			function()
			{
			    list.addClass('open');
			    Nav.setImage(list, img);
			});
        }
        else if (resetTo == 'close')
        {
            list.slideUp(speed,
			function()
			{
			    list.removeClass('open');
			    Nav.setImage(list, img);
			});
        }
    },
    openAll: function()
    {
        $('#subnav ul ul').each(
		function()
		{
		    Nav.toggleList($(this), $(this).parent().children('img'), 'open');
		});
    },
    closeAll: function()
    {
        $('#subnav ul ul').each(
		function()
		{
		    Nav.toggleList($(this), $(this).parent().children('img'), 'close');
		});
    },
    setImage: function(list, img)
    {
        if (img.attr('src') != undefined)
        {
            if (list.hasClass('open'))
                img.attr('src', img.attr('src').replace('expand', 'collapse')).attr('alt', '[-]');
            else
                img.attr('src', img.attr('src').replace('collapse', 'expand')).attr('alt', '[+]');
        }
    }

}



/*************************/
/* Product Images        */
/*************************/
var ProdImgs = {
	// Product Images setup function.
	setup: function()
	{
		return;
	}
}




/*************************/
/* Forms                 */
/*************************/
var Forms = {
	setup: function()
	{
		$('div.optionsList div.field').click(function()
		{
			$(this).closest('div.optionsList').find('div.field').removeClass('selected');
			$(this).closest('div.field').addClass('selected');

			// Set radio
			$(this).find('input.radio, span.radio input').attr('checked', 'checked');

			// Toggle checkbox
			$(this).find('input.chk, span.chk input').each(function()
			{
				if ($(this).attr('checked') == true)
				{
					$(this).removeAttr('checked')
					$(this).closest('div.field').removeClass('selected');
				}
				else
				{
					$(this).attr('checked', 'checked');
					$(this).closest('div.field').addClass('selected');
				}
			})
		});

		var checkedInputs = $('div.optionsList input.radio:checked, div.optionsList span.radio input:checked, div.optionsList input.chk:checked, div.optionsList span.chk input:checked');
		checkedInputs.each(function()
		{
			$(this).closest('div.optionsList').find('div.field').removeClass('selected');
			$(this).closest('div.field').addClass('selected');
		});
	}
}




/*************************/
/* Ingredients           */
/*************************/
var Ingredients = {
	display: function()
	{
		$('#displayIngredients').hide();
		$('#ingredients').show();
	}
}




/*************************/
/* Cart                  */
/*************************/
var Cart = {
	transfer: function(from)
	{
		setTimeout(function()
		{
			var options = { to: '#nav li.cart' };
			$(from).show('transfer', options, (1000 + $(window).scrollTop()));
		}, 500);
	}
}




/*************************/
/* Product Images        */
/*************************/
var Popup = {
	popupBlockerEnabled: function()
	{
		var mine = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
		if (mine)
		{
			mine.close();
			return false;
		}
		else
			return true;

	}
}




