//==================================================
//
// 		Project: Mealeo
// 		Author: Rareview Interactive
// 		Main Javascript
//
//==================================================

// Clear input text and darken color on Sign Up form

function clearAndColor(el) {
	if (el.defaultValue==el.value) el.value = ""
	if (el.style) el.style.color = "#505050"
}


// Begin jQuery

$(document).ready(function() {

    // Start Your Order - sliding forms

    // show forms

    $('a.toggleStartOrderForm').click(function() {
        // hide currently open forms
        $('a.cancelStartOrder:visible').animate({ top: '283' }, 100, function() {
            $(this).hide();
            $(this).prev('form').slideUp(100);
        });
        // show selected form
        $(this).next('form').slideDown(200, function() {
            $(this).next('a.cancelStartOrder').show().animate({ top: '205' }, 200);
        });
        return false;
    });

    // hide forms

    $('a.cancelStartOrder').click(function() {
        $(this).animate({ top: '283' }, 100, function() {
            $(this).hide();
            $(this).prev('form').slideUp(100);
        });
        return false;
    });

    // catering radio options

    var cateringPickup = $("#startCateringDelivery");
    if (cateringPickup.is(":checked")) {
        $("#cateringPickupSteet:hidden").show();
    }
    $('input[name=cateringDeliveryPickup]').click(function() {
        if (cateringPickup.is(":checked")) {
            $("#cateringPickupSteet:hidden").slideDown(150);
        } else {
            $("#cateringPickupSteet:visible").slideUp(150);
        }
    });

    // Not Logged In Layover Panel

    // show panel

    $('.toggleLoginOverlay').click(function() {
        $('#notLoggedInOverlay').show().animate({ top: '227' }, 350);
		// get id of LI and add to inputs
        var restID = $(this).parent().attr('id');
        $('#lgSelectedRest, #selectedRest').val(restID);
        return false;
    });

    // hide panel

    $('#notLoggedInOverlay .copy').bind('mouseleave', function() {
        $('#notLoggedInOverlay').animate({ top: '-479' }, 350, function() {
            $(this).hide();
        });
    });

    // Top login and add menu buttons

    // append an extra span to make the hover effect work

    $('#header_buttons li a, #header_buttons_user li a, #header_buttons_rest li a').append('<span class="hover"></span>');

    // make the hover animate in and out

    $('#header_buttons li a, #header_buttons_user li a, #header_buttons_rest li a').hover(function() {
        $('.hover', this).stop().animate({ 'opacity': 1 }, 250);
    }, function() {
        $('.hover', this).stop().animate({ 'opacity': 0 }, 450);
    })

    // Home page - slide down delivery fields

    $("#home_find [name='m']").click(function() {
        if ($("#start_order_delivery").attr("checked")) {
            $("#home_find_address:hidden").slideDown(350);
        } else {
            $("#home_find_address:visible").slideUp(350);
        }
    });

    if ($("#start_order_delivery").attr("checked")) {
        $("#home_find_address").show();
    }

    // Slide up catering bubble and slide down address change bubble if switching from pickup to delivery method on interior pages

    $("#change_to_delivery").click(function() {
        if ($("#catering_bubble").length > 0) {
            $("#catering_bubble").animate({ top: "-300" }, 200, function() {
                $("#delivery_address_bubble").show().animate({ top: "-96" }, 200);
            });
        } else {
            $("#delivery_address_bubble").show().animate({ top: "-96" }, 200);
        }
        return false;
    });

    $("#cancel_address_change").click(function() {
        $("#delivery_address_bubble").animate({ top: "-316" }, 200, function() {
            $("#delivery_address_bubble").hide();
            $("#catering_bubble").animate({ top: "-50" }, 200);
        });
        return false;
    });

    // Tooltips - general tooltips used on many pages

    $(".tooltips_window strong").hover(function() {
        $(this).prev("span").animate({ opacity: "show", bottom: "18" }, 200);
    }, function() {
        $(this).prev("span").animate({ opacity: "hide", bottom: "-15" }, 200);
    });

    // Tooltips - specific tooltips for the favorites links on Menu Item pages

    $(".tooltips_window a").hover(function() {
        $(this).prev("span").animate({ opacity: "show", bottom: "18" }, 200);
    }, function() {
        $(this).prev("span").animate({ opacity: "hide", bottom: "-15" }, 200);
    });

    // Toggle "add to favorites" in menu items

    $(".menu_items ul li span.tooltips_window a").click(function() {
        $(this).toggleClass("favorited");
        return false;
    });

    // Toggle "add to favorites" everywhere else

    $("a.add_to_favorites").click(function() {
        $(this).toggleClass("favorited");
        return false;
    });

    // Accordion

    $(".accordion h4").eq(100).addClass("active");
    $(".accordion p").eq(100).show();
    $(".accordion h4").click(function() {
        $(this).next("p").slideToggle("normal")
		.siblings("p:visible").slideUp("normal");
        $(this).toggleClass("active");
        $(this).siblings("h4").removeClass("active");
    });

    // Account info drop form

    $(".package_prices .package_1").click(function() {
        $("#account_info_drop").slideDown("slow");
        $(this).addClass("active");
        $(".active").not(this).removeClass("active");
        document.frmAddmenu.package.value = "1";
    });
    $(".package_prices .package_2").click(function() {
        $("#account_info_drop").slideDown("slow");
        $(this).addClass("active");
        $(".active").not(this).removeClass("active");
        document.frmAddmenu.package.value = "2";
    });
    $(".package_prices .package_3").click(function() {
        $("#account_info_drop").slideDown("slow");
        $(this).addClass("active");
        $(".active").not(this).removeClass("active");
        document.frmAddmenu.package.value = "3";
    });

    // Choose plan buttons - toggles Account Info form

    $("#monthly_plan a").click(function() {
        $("#account_info_drop").slideDown("slow");
        $(this).addClass("active");
        $(".active").not(this).removeClass("active");
        document.frmAddmenu.package.value = "2";
    });
    $("#yearly_plan a").click(function() {
        $("#account_info_drop").slideDown("slow");
        $(this).addClass("active");
        $(".active").not(this).removeClass("active");
        document.frmAddmenu.package.value = "1";
    });

    // Menu add item - remove visible form when clicking to another meal item
    $(".menu_items span.item").click(function() {
        $("form.menu_add_item:visible").remove();
        return false;
    });
    $(".menu_items ul li span.left").click(function() {
        $(this).next("span").css("display", "inline");
    });
    $(".menu_items ul li span.right").click(function() {
        $(this).next("span").css("display", "inline");
    });

    // Menu Filter 

    // slide down categories on page load
    $('#menuFilter .content').slideDown(650);

    // toggle category visibility
    $('#toggleCategories').click(function() {
        $('#menuFilter .content').slideToggle(350, function() {
            $('#toggleCategories').toggleClass('active');
        });
        return false;
    });

    // Menu page highlighted search results

    $(window).load(function() {
        $(".search_result").animate({ backgroundColor: "#fff890" }, 1500);
    });

    // Show/Hide "Select a Restaurant" Filter inputs

    $('.showhide a').toggle(function() {
        $(this).parent().prev("ul").slideDown('normal').removeClass('hide').preventDefault;
        $(this).html('Less...');
        $(this).toggleClass("active");
    }, function() {
        $(this).parent().prev("ul").slideUp('normal').addClass('hide').preventDefault;
        $(this).html('More...');
        $(this).removeClass("active");
    });

    // Show/Hide Four Steps bar - If bar is shown...

    $('.show_steps a.showhide').toggle(function() {
        $(this).parents("#four_steps_container").animate({ "bottom": "-=79px" }, "slow").preventDefault;
        $(this).html('Show');
        $(this).toggleClass("active");
    }, function() {
        $(this).parents("#four_steps_container").animate({ "bottom": "+=79px" }, "slow").preventDefault;
        $(this).html('Hide');
        $(this).removeClass("active");
    });

    // Show/Hide Four Steps bar - If bar was hidden by user...

    $('.hide_steps a.showhide').toggle(function() {
        $(this).parents("#four_steps_container").animate({ "bottom": "+=79px" }, "slow").preventDefault;
        $(this).html('Hide');
        $(this).toggleClass("active");
    }, function() {
        $(this).parents("#four_steps_container").animate({ "bottom": "-=79px" }, "slow").preventDefault;
        $(this).html('Show');
        $(this).removeClass("active");
    });

    // Show/Hide "Profile" banner code

    $('.code_snippet a').toggle(function() {
        $(this).next('code').fadeIn('normal').removeClass('hide').preventDefault;
        $(this).html('Close code...');
        $(this).toggleClass("active");
    }, function() {
        $(this).next('code').fadeOut('normal').addClass('hide').preventDefault;
        $(this).html('View code...');
        $(this).removeClass("active");
    });

    // Show/Hide "Profile" Chef Comments

    $('#profile_chef_comments li.view_comments a').toggle(function() {
        $('li.chef_comments').slideDown('normal').removeClass('hide').preventDefault;
        $(this).html('Close Chef Comments...');
        $(this).toggleClass("active");
    }, function() {
        $('li.chef_comments').slideUp('normal').addClass('hide').preventDefault;
        $(this).html('View Chef Comments...');
        $(this).removeClass("active");
    });

    // Show/Hide "Order History" Review form

    $('#order_history a.toggle').click(function() {
        $(this).parents("ul").find('li.review').slideDown('normal');
        return false;
    });

    // "Order History" Review form close link

    $('#order_history a.cancel').click(function() {
        $(this).parents("ul").find('li.review').slideUp('normal');
        return false;
    });

    // Disable specific inputs if "Pick-up Only" radio is checked on "Profile" page

    $("[name='delivery']").click(function() {
        if ($("#pickup_only").attr("checked")) {
            $(".delivery_radius").animate({ color: '#b2b2b2' }, 400);
            $(".delivery_radius input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 400);
            $(".delivery_fee").animate({ color: '#b2b2b2' }, 400);
            $(".delivery_fee input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 400);
            $(".delivery_time").animate({ color: '#b2b2b2' }, 400);
            $(".delivery_time select").attr("disabled", "disabled").animate({ color: '#b2b2b2' }, 400);
            $(".delivery_min").animate({ color: '#b2b2b2' }, 400);
            $(".delivery_min input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 400);
        } else {
            $(".delivery_radius").animate({ color: '#181818' }, 400);
            $(".delivery_radius input").attr("disabled", "").animate({ borderTopColor: '#b2b2b2', borderRightColor: '#b2b2b2', borderBottomColor: '#b2b2b2', borderLeftColor: '#b2b2b2', color: '#575757' }, 400);
            $(".delivery_fee").animate({ color: '#181818' }, 400);
            $(".delivery_fee input").attr("disabled", "").animate({ borderTopColor: '#b2b2b2', borderRightColor: '#b2b2b2', borderBottomColor: '#b2b2b2', borderLeftColor: '#b2b2b2', color: '#575757' }, 400);
            $(".delivery_time").animate({ color: '#181818' }, 400);
            $(".delivery_time select").attr("disabled", "").animate({ color: '#111' }, 400);
            $(".delivery_min").animate({ color: '#181818' }, 400);
            $(".delivery_min input").attr("disabled", "").animate({ borderTopColor: '#b2b2b2', borderRightColor: '#b2b2b2', borderBottomColor: '#b2b2b2', borderLeftColor: '#b2b2b2', color: '#575757' }, 400);
        }
    });
    if ($("#pickup_only").attr("checked")) {
        $(".delivery_radius").animate({ color: '#b2b2b2' }, 1);
        $(".delivery_radius input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 1);
        $(".delivery_fee").animate({ color: '#b2b2b2' }, 1);
        $(".delivery_fee input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 1);
        $(".delivery_time").animate({ color: '#b2b2b2' }, 1);
        $(".delivery_time select").attr("disabled", "disabled").animate({ color: '#b2b2b2' }, 1);
        $(".delivery_min").animate({ color: '#b2b2b2' }, 1);
        $(".delivery_min input").attr("disabled", "disabled").animate({ borderTopColor: '#cbcbcb', borderRightColor: '#cbcbcb', borderBottomColor: '#cbcbcb', borderLeftColor: '#cbcbcb', color: '#b2b2b2' }, 1);
    }

    // Edit Menu - Add size options

    $(".add_size_option a").click(function() {
        $(this).parent().prev("li").after('<li class="size new_size_option"><label>Size 4:</label> <input class="text" maxlength="40" name="size" /></li><li class="new_size_price"><label>Price:</label> <input class="text" maxlength="7" name="sizep" /></li>');
        $("li.new_size_option label").each(function(i) {
            $(this).text("Size " + (i + 4)).append(":");
            $("#addsize").attr('value', +(i + 1));
        });
        //$("li.new_size_option input").each(function(i) {
        //	$(this).attr('name', 'size' + (i + 4));
        //});
        //$("li.new_size_price input").each(function(i) {
        //	$(this).attr('name', 'size' + (i + 4) + 'p');
        //});
    });

    // Edit Menu - Add included options

    $(".add_included_option a").livequery('click', function() {
        $(this).parent().prev("li").after('<li class="option new_included_option"><label>Option 4:</label> <input class="text" maxlength="40" name="opt" /></li><li class="new_included_cost"><label>Additional Cost:</label> <input class="text" maxlength="7" name="optp" /></li>');
        $(this).parents("fieldset").find("li.new_included_option label").each(function(i) {
            $(this).text("Option " + (i + 4)).append(":");
            $("#addincluded").attr('value', +(i + 1));
        });
        // find which name attribute is being used in this part of the form and apply to new Option input
        $("#new_included_options li.new_included_option input[name=opt]").each(function() {
            var currentInput = this;
            var siblingName = $(currentInput).parents("fieldset").find("li.option input").attr("name");
            $(this).attr('name', siblingName);
        });
        // find which name attribute is being used in this part of the form and apply to new Price input
        $("#new_included_options li.new_included_cost input[name=optp]").each(function() {
            var currentInput = this;
            var siblingName = $(currentInput).parents("fieldset").find("li.cost input").attr("name");
            $(this).attr('name', siblingName);
        });
    });

    // Edit Menu - Add included options SET

    $("#add_included_option_set a").click(function() {
        $("#new_included_options").append('<fieldset id="new_options_set" class="light_divide how_options_work"></fieldset>');
        $("#new_options_set").load("includes/menuOptions.asp", function() {
            // get the number of extra included option sets
            var numberOfSets = $("#new_included_options fieldset").size();
            // increase the name attribute of the name inputs
            var nameAttr = $("#new_included_options input[name=incname]").not(".nameChanged").attr("name");
            $("#new_included_options input[name=incname]").not(".nameChanged").each(function(i) {
                $(this).attr('name', nameAttr + numberOfSets).addClass("nameChanged");
            });
            // increase the name attribute of the option inputs
            var optionAttr = $("#new_included_options input[name=opt]").not(".nameChanged").attr("name");
            $("#new_included_options input[name=opt]").not(".nameChanged").each(function(i) {
                $(this).attr('name', optionAttr + numberOfSets).addClass("nameChanged");
            });
            // increase the name attribute of the price inputs
            var priceAttr = $("#new_included_options input[name=optp]").not(".nameChanged").attr("name");
            $("#new_included_options input[name=optp]").not(".nameChanged").each(function(i) {
                $(this).attr('name', priceAttr + numberOfSets).addClass("nameChanged");
            });
            // increase the name attribute of the mode select
            var modeAttr = $("#new_included_options select[name=mode]").not(".nameChanged").attr("name");
            $("#new_included_options select[name=mode]").not(".nameChanged").each(function(i) {
                $(this).attr('name', modeAttr + numberOfSets).addClass("nameChanged");
            });
            // change id of previous option sets
            $("#new_options_set").each(function(i) {
                $(this).attr('id', 'new_options_set_prev');
            });
        });
    });

    // Edit Menu - Add additional options

    $(".add_additional_option a").click(function() {
        $(this).parent().prev("li").after('<li class="option new_additional_option"><label>Option 4:</label> <input class="text" maxlength="40" name="addopt" /></li><li class="new_additional_cost"><label>Additional Cost:</label> <input class="text" maxlength="7" name="addoptp" /></li>');
        $("li.new_additional_option label").each(function(i) {
            $(this).text("Option " + (i + 4)).append(":");
            $("#addoptions").attr('value', +(i + 1));
        });
        //$("li.new_additional_option input").each(function(i) {
        //	$(this).attr('name', 'addopt' + (i + 4));
        //});
        //$("li.new_additional_cost input").each(function(i) {
        //	$(this).attr('name', 'addopt' + (i + 4) + 'p');
        //});
    });

    // Confirmation box before deletion

    $(".confirmation_box a.open_confirmation").click(function() {
        $(this).parents(".confirmation_box").find(".confirmation_msg").animate(
		{ opacity: "show", bottom: "18" }, 300);
        return false;
    });
    $(".confirmation_box .close_confirmation").click(function() {
        $(this).parents(".confirmation_box").find(".confirmation_msg").animate(
	 	{ opacity: "hide", bottom: "-15" }, 125);
        return false;
    });

    // Account Settings - toggle "change" password field

    $(".toggle-password a").click(function() {
        $(this).parent().next(".password").fadeIn("slow");
        $(this).parent().remove();
        return false;
    });

    // Reveal credit card fields	

    $(".edit-credit a").click(function() {
        $(this).parents().next(".new-credit").fadeIn("slow");
        $(this).parents(".current-credit").hide();
        document.frmBilling.cngPay.value = "1";
        return false;
    });
    $(".save-credit a").click(function() {
        $(this).parents().prev(".current-credit").fadeIn("slow");
        $(this).parents(".new-credit").hide();
        document.frmBilling.cngPay.value = "0";
        return false;
    });

    // Confirm and Pay - toggle between not a member and login forms	

    $(".confirm_and_pay_is_member a.toggle").click(function() {
        $(".confirm_and_pay_not_member").fadeIn("slow");
        $(this).parents(".confirm_and_pay_is_member").hide();
        return false;
    });
    $(".confirm_and_pay_not_member a.toggle").click(function() {
        $(".confirm_and_pay_is_member").fadeIn("slow");
        $(this).parents(".confirm_and_pay_not_member").hide();
        return false;
    });

    // Profile menu upload toggle

    $('#upload_menu a.toggle').toggle(function() {
        $(this).parents().find('#upload_menu > form').slideDown('normal');
        $(this).html('Cancel and close form?');
    }, function() {
        $(this).parents().find('#upload_menu > form').slideUp('normal');
        $(this).html("Let our design team do it for you! ($147 one time fee)");
    });

    // Limit number of selectable checkboxes

    // Empty append, required to trick livequery into thinking the DOM has been updated via jQuery

    $(".menu_items").mouseover(function() {
        $(this).append("");
    });

    // Limit 1

    var limit1 = function() {
        if ($(".limit_1 input:checked").length >= 1) {
            $(".limit_1 input:checkbox").not(":checked").attr("disabled", "disabled");
        } else {
            $(".limit_1 input:checkbox").not(":checked").attr("disabled", "");
        }
    };
    $(".limit_1 input:checkbox").livequery('click', limit1);

    // Limit 2

    var limit2 = function() {
        if ($(".limit_2 input:checked").length >= 2) {
            $(".limit_2 input:checkbox").not(":checked").attr("disabled", "disabled");
        } else {
            $(".limit_2 input:checkbox").not(":checked").attr("disabled", "");
        }
    };
    $(".limit_2 input:checkbox").livequery('click', limit2);

    // Limit 3

    var limit3 = function() {
        if ($(".limit_3 input:checked").length >= 3) {
            $(".limit_3 input:checkbox").not(":checked").attr("disabled", "disabled");
        } else {
            $(".limit_3 input:checkbox").not(":checked").attr("disabled", "");
        }
    };
    $(".limit_3 input:checkbox").livequery('click', limit3);

    // Limit 4

    var limit4 = function() {
        if ($(".limit_4 input:checked").length >= 4) {
            $(".limit_4 input:checkbox").not(":checked").attr("disabled", "disabled");
        } else {
            $(".limit_4 input:checkbox").not(":checked").attr("disabled", "");
        }
    };
    $(".limit_4 input:checkbox").livequery('click', limit4);

    // Limit 5

    var limit5 = function() {
        if ($(".limit_5 input:checked").length >= 5) {
            $(".limit_5 input:checkbox").not(":checked").attr("disabled", "disabled");
        } else {
            $(".limit_5 input:checkbox").not(":checked").attr("disabled", "");
        }
    };
    $(".limit_5 input:checkbox").livequery('click', limit5);


    // Disable/Enable the dropdowns for the "later" date option when confirming order

    var orderNow = $('#orderTimeNow');
    var orderLater = $('#orderTimeLater');

    if (orderNow.is(":checked")) {
        $('.laterOrderSelect').attr("disabled", "true");
    }

    orderLater.click(function() {
        $('.laterOrderSelect').removeAttr("disabled");
    });

    orderNow.click(function() {
        $('.laterOrderSelect').attr("disabled", "true");
    });



    // Slide down sign in/not a member panel
    $('#frmAcctLogin').submit(function() {

        var loginEmail = $('#signInEmail').attr('value');
        var loginPW = $('#signInPassword').attr('value');
        var loginCPg = $('#lgCurPage').attr('value');
        var loginSelRest = $('#lgSelectedRest').attr('value');
        $.post("/signin.asp?sid=" + Math.random(), { 'email': loginEmail, 'pw': loginPW, 'curPg': loginCPg, 'selectedRest': loginSelRest }, function(data) {
            //alert(data);
            if (data == 'valid') {
                window.location = "/includes/created.asp";
            } else {
                $('#signInError').html(data);
            }
        });

        return false;
    });


    $('#frmAcctSignup').submit(function() {

        var suEmail = $('#signUpEmail').attr('value');
        var suZip = $('#signUpZip').attr('value');
        var suPW = $('#signUpPw').attr('value');
        var suPWR = $('#signUpPwRepeat').attr('value');
        var suCPg = $('#curPage').attr('value');
        var suSelRest = $('#selectedRest').attr('value');
        //alert(suEmail + ',' + suZip + ',' + suPW + ',' + suPWR);
        $.post("/signup.asp?sid=" + Math.random(), { 'email': suEmail, 'zip': suZip, 'pw': suPW, 'pwr': suPWR, 'curPg': suCPg, 'selectedRest': suSelRest }, function(data) {
            //alert(data);
            if (data == 'valid') {
                window.location = "/includes/created.asp";
            } else {
                $('#signUpError').html(data);
            }
        });

        return false;
    });

    // end jQuery

});
