var itemMap = new Array();

//ON DOM LOADED
$(document).ready(function() {

    $(".pca-slide").bind("mouseover", pca_slide_mouseover);
    $(".pca-slide").bind("mouseout", pca_slide_mouseout);
    $(".pcb-slide").bind("mouseover", pcb_slide_mouseover);
    $(".pcb-slide").bind("mouseout", pcb_slide_mouseout);
    var cheeseSelected = false;
    var baseSelected = false;
    var ingredients = 0;

    // -------------------------------------------------------------------------------------------------------------------------------//
    // ---------------------------------------------------- COMPRESSOR -------------------------------------------------------------- //
    // -------------------------------------------------------------------------------------------------------------------------------//

    //HELPFUL NOTE: 'open' and 'close' refer to the function of the compressor link, not the state of the silo.


    /* Customize pizza */


    $('div.customise-container a.compressor').click(function(e) {
        if ($(this).hasClass("open")) {
            //close all divs
            articleholder = $(this).parent("div").parent("div").parent("div");
            $(articleholder).find('.content').addClass("hidden");
            $(articleholder).find('.content').removeClass("show");
            $(articleholder).find('a.compressor img').attr("src", "../images/order/controls_expand.gif");
            $(articleholder).find('a.compressor').addClass("open");
            $(articleholder).find('a.compressor').removeClass("close");


            //open current div
            article = $(this).parent("div").parent("div");
            openPizzaItem(e, article);
        }
        else {
            article = $(this).parent("div").parent("div");
            closePizzaItem(e, article);
        }
    });


    function closeItem(e, article) {
        e.preventDefault();

        //CLOSE THE CURRENTLY OPEN SILO
        $(article).find('.content').addClass("hidden");
        $(article).find('.content').removeClass("show");

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "../images/order/controls_expand.gif");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("open");
        $(article).find('a.compressor').removeClass("close");


    }

    function openItem(e, article) {

        e.preventDefault();

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "../images/order/controls_contract.gif");

        //OPEN THE DIV THAT WAS CLICKED
        $(article).find('.content').addClass("show");
        $(article).find('.content').removeClass("hidden");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("close");
        $(article).find('a.compressor').removeClass("open");

    }

    /* Half & Half pizza */


    $('div.pizza-container a.compressor').live("click", function(e) {
        if ($(this).hasClass("open")) {
            //close all divs
            articleholder = $(this).parent("div").parent("div").parent("div");
            $(articleholder).find('.content').addClass("hidden");
            $(articleholder).find('.content').removeClass("show");
            $(articleholder).find('a.compressor img').attr("src", "../images/order/controls_expand.gif");
            $(articleholder).find('a.compressor').addClass("open");
            $(articleholder).find('a.compressor').removeClass("close");


            //open current div
            article = $(this).parent("div").parent("div");
            openPizzaItem(e, article);
        }
        else {
            article = $(this).parent("div").parent("div");
            closePizzaItem(e, article);
        }
    });


    function closePizzaItem(e, article) {
        e.preventDefault();

        //CLOSE THE CURRENTLY OPEN SILO
        $(article).find('.content').addClass("hidden");
        $(article).find('.content').removeClass("show");

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "../images/order/controls_expand.gif");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("open");
        $(article).find('a.compressor').removeClass("close");


    }

    function openPizzaItem(e, article) {

        e.preventDefault();

        //CHANGE IMAGE ICONS
        $(article).find('a.compressor img').attr("src", "../images/order/controls_contract.gif");

        //OPEN THE DIV THAT WAS CLICKED
        $(article).find('.content').addClass("show");
        $(article).find('.content').removeClass("hidden");

        //CHANGE THE open/close STATUS OF THE COMPRESSOR LINK
        $(article).find('a.compressor').addClass("close");
        $(article).find('a.compressor').removeClass("open");

    }




    ///////////////////////////////////
    // Glen - Half and Half Advanced //
    ///////////////////////////////////


    function pca_slide_mouseover() {
        var values = $(this).find("input").attr("value").split(';');

        showPizza(values, "a");

        // CSS
        $(".pia, .psia, .pta, .pda").removeClass("hide");
        $(".pma").addClass("hide");
    }

    function pcb_slide_mouseover() {
        var values = $(this).find("input").attr("value").split(';');

        showPizza(values, "b");

        // CSS
        $(".pib, .psib, .ptb, .pdb").removeClass("hide");
        $(".pmb").addClass("hide");
    }

    function pca_slide_mouseout() {
        // CSS
        $(".pia, .psia, .pta, .pda").addClass("hide");
        $(".pma").removeClass("hide");
    }

    function pcb_slide_mouseout() {
        // CSS
        $(".pib, .psib, .ptb, .pdb").addClass("hide");
        $(".pmb").removeClass("hide");
    };

    $(".pca-slide").click(function() {
        var values = $(this).find("input").attr("value").split(';');

        // BINDING
        $(".pca-slide").unbind("mouseover").unbind("mouseout");

        // CSS
        $(".pma, .pda").addClass("hide");
        $(".pia, .psia, .pta, .changepa").removeClass("hide");

        showPizza(values, "a");

        // ANIMATION
        $(".pca").slideUp("slow");
        $(".ica, .header.ingredients").animate({ opacity: 1.0 }, 1000).fadeIn();
    });

    $(".pcb-slide").click(function() {
        var values = $(this).find("input").attr("value").split(';');

        // BINDING
        $(".pcb-slide").unbind("mouseover").unbind("mouseout");

        // CSS
        $(".pmb, .pdb").addClass("hide");
        $(".pib, .psib, .ptb, .changepb").removeClass("hide");

        showPizza(values, "b");

        // ANIMATION
        $(".pcb").slideUp("slow");
        $(".icb, .header.ingredients").animate({ opacity: 1.0 }, 1000).fadeIn();
    });

    $(".pcb-slide-build").click(function() {
        // ANIMATION
        $(".header").animate({ opacity: 1.0 }, 1000).fadeIn();
    });

    function showPizza(values, side) {
        var pizza = values[0];
        var img = values[1];

        if (side == "a") {
            // IMAGES
            $(".pia").attr("src", img);
            $(".psia").attr("src", "../images/sauces/tomato.jpg");

            // TEXT
            $(".pta").text(pizza);
        }
        else if (side == "b") {
            // IMAGES
            $(".pib").attr("src", img);
            $(".psib").attr("src", "../images/sauces/tomato.jpg");

            // TEXT
            $(".ptb").text(pizza);
        }
    }

    $(".ica-rem").live("click", function() {
        var valueStr = $(this).parent().parent().find("input").attr("value");
        var values = valueStr.split(";");
        var ingredientID = values[0];

        $("#" + itemMap[$(this).closest("li").attr("id")]).append(
                "<li id='liIngredient_" + values[0] + "'>" +
                     "<input type='hidden' value='" + valueStr + "' id='hidIngredient_" + values[0] + "' name='hidIngredient_" + values[0] + "'/>" +
                     "<a class='ica-slide ica-item' href='#'>" +
                     "    " + values[1] + "</a>" +
                "</li>");

        // ANIMATION
        $(this).closest("li").remove();

        removeIngredient(ingredientID, "a");
    });

    $(".ica-rem-customize").live("click", function() {
        var valueStr = $(this).parent().find("input").attr("value");
        var values = valueStr.split(";");
        var ID = values[1];
        var type = values[0];

        $("#" + itemMap[$(this).closest("li").attr("id")]).append(
                "<li id='li" + values[0] + "_" + values[1] + "'>" +
                     "<input type='hidden' value='" + valueStr + "' id='hid" + values[0] + "_" + values[1] + "' name='hid" + values[0] + "_" + values[1] + "'/>" +
                     "<a class='pcb-slide-build ica-item-customize' href='#'><span class='ing-label'>" +
                     "    " + values[2] + "</span> <span class='price'>$" + values[3] + "</span></a>" +
                "</li>");

        // ANIMATION
        $(this).closest("li").remove();

        if (type == "Base") {
            baseSelected = false;
            removeBase();
        }
        else if (type == "Cheese") {
            cheeseSelected = false;
            removeIngredient_Cheese();
        }
        else
            removeIngredient(ID);

        ingredients -= 1;

        if (ingredients > 0)
            $(".current-ingredients").show();
        else
            $(".current-ingredients").hide();
    });

    $(".icb-rem").live("click", function() {
        var valueStr = $(this).parent().parent().find("input").attr("value");
        var values = valueStr.split(";");
        var ingredientID = values[0];

        $("#" + itemMap[$(this).closest("li").attr("id")]).append(
                "<li id='liIngredient_" + values[0] + "'>" +
                     "<input type='hidden' value='" + valueStr + "' id='hidIngredient_" + values[0] + "' name='hidIngredient_" + values[0] + "'/>" +
                     "<a class='icb-slide icb-item' href='#'>" +
                     "    " + values[1] + "</a>" +
                "</li>");

        // ANIMATION
        $(this).closest("li").remove();
- +
        removeIngredient(ingredientID, "b");
    });

    //update ingredient quantity
    $(".ica-select").live("change", function() {
        var ingredientID = $(this).parent().parent().find("input").attr("value").split(";")[0];
        var qty = $(this).val();

        removeIngredient(ingredientID, "a");
        addIngredient(ingredientID, qty, "a");
    });

    $(".icb-select").live("change", function() {
        var ingredientID = $(this).parent().parent().find("input").attr("value").split(";")[0];
        var qty = $(this).val();

        removeIngredient(ingredientID, "b");
        addIngredient(ingredientID, qty, "b");
    });

    $(".ica-select-customize").live("change", function() {
        var ID;
        var qty = $(this).val();
        var type = "";

        if ($(this).parent().parent().find("input").length > 0) {
            type = $(this).parent().find("input").attr("value").split(";")[0];
            ID = $(this).parent().find("input").attr("value").split(";")[1];
        }

        if (type == "Base")
            setBaseID(ID);
        else if (type == "Cheese")
            setIngredient_Cheese(ID, qty);
        else {
            removeIngredient(ingredientID);
            addIngredient(ingredientID);
        }
    });

    //REMOVE HREF '#' RELOAD ACTION
    $(".hh-pizzas a").live("click", (function(event) { event.preventDefault(); }));
    $(".hh-pizzas b").live("click", (function(event) { event.preventDefault(); }));
    $(".ica-rem a").live("click", (function(event) { event.preventDefault(); }));
    $(".ica-rem b").live("click", (function(event) { event.preventDefault(); }));

    $(".ica-item").live("click", function() {
        var ingredientID = $(this).parent().find("input").attr("value").split(";")[0];

        itemMap[$(this).closest("li").attr("id")] = $(this).parent().parent().attr("id");

        // MOVE TO INCLUDED INGREDIENTS AND FORMAT
        $(this).closest("li").appendTo($('.ica-ul'));
        $(this).closest("li").wrapInner('<div class="ica-slide left"></div>');
        //$(this).closest("li").wrapInner("<p></p>");
        //$(this).closest("p").addClass("ica-slide").addClass("ica-added");
        //$(this).closest("p").prepend('<select name="select" id="select" class="ica-select"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> ');
        //$(this).closest("p").append('<a href="#" class="ica-rem">Remove</a>');
        $(this).closest("div").prepend('<select name="select" id="select" class="icb-select"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>');
        $(this).closest("div").append('</div>');
        $(this).closest("li").append('<div class="ica-slide right"><a href="#" class="ica-rem">Remove</a></div>');

        // REMOVE LINK
        var ingselect = $(this).closest("a");
        var ingtext = ingselect.text();
        ingselect.replaceWith(ingtext);

        addIngredient(ingredientID, 1, "a");
    });

    $(".icb-item").live("click", function() {
        var ingredientID = $(this).parent().find("input").attr("value").split(";")[0];

        itemMap[$(this).closest("li").attr("id")] = $(this).parent().parent().attr("id");

        // MOVE TO INCLUDED INGREDIENTS AND FORMAT
        $(this).closest("li").appendTo($('.icb-ul'));
        $(this).closest("li").wrapInner('<div class="icb-slide left"></div>');
        //$(this).closest("p").addClass("icb-slide").addClass("icb-added");
        //$(this).closest("p").prepend('<select name="select" id="select" class="icb-select"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> ');
        //$(this).closest("p").append('<a href="#" class="icb-rem">Remove</a>');
        $(this).closest("div").prepend('<select name="select" id="select" class="icb-select"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>');
        $(this).closest("div").append('</div>');
        $(this).closest("li").append('<div class="icb-slide right"><a href="#" class="icb-rem">Remove</a>');

        // REMOVE LINK
        var ingselect = $(this).closest("a");
        var ingtext = ingselect.text();
        ingselect.replaceWith(ingtext);

        addIngredient(ingredientID, 1, "b");
    });

    //VARIATION FOR THE CUSTOMIZE PIZZA PAGE
    $(".ica-item-customize").live("click", function() {
        var ID;
        var type = "";

        if ($(this).parent().find("input").length > 0) {
            type = $(this).parent().find("input").attr("value").split(";")[0];
            ID = $(this).parent().find("input").attr("value").split(";")[1];
        }

        if (type == "Base" && baseSelected == true)
            return;
        else if (type == "Cheese" && cheeseSelected == true)
            return;

        itemMap[$(this).closest("li").attr("id")] = $(this).parent().parent().attr("id");

        // MOVE TO INCLUDED INGREDIENTS AND FORMAT
        $(this).closest("li").appendTo($('.ica-ul'));
        $(this).closest("li").wrapInner("<p></p>");
        $(this).closest("p").addClass("ica-slide").addClass("ica-added");

        if (type == "Cheese")
            $(this).closest("p").prepend('<select name="select" id="select" class="ica-select-customize"><option value="1">1</option><option value="2">2</option></select> ');

        $(this).closest("p").append('<a href="#" class="ica-rem-customize">Remove</a>');

        // REMOVE LINK
        var ingselect = $(this).closest("a");
        var ingtext = ingselect.html();
        ingselect.replaceWith(ingtext);

        if (!isNaN(parseInt(ID))) {
            if (type == "Base") {
                baseSelected = true;
                setBaseID(ID);
            }
            else if (type == "Cheese") {
                cheeseSelected = true;
                setIngredient_Cheese(ID, 1);
            }
            else
                addIngredient(ID);

            ingredients += 1;
        }

        if (ingredients > 0)
            $(".current-ingredients").show();
        else
            $(".current-ingredients").hide();
    });

    $(".icb-item-customize").live("click", function() {

        // MOVE TO INCLUDED INGREDIENTS AND FORMAT
        $(this).closest("li").appendTo($('.icb-ul'));
        $(this).closest("li").wrapInner("<p></p>");
        $(this).closest("p").addClass("icb-slide").addClass("icb-added");
        $(this).closest("p").prepend('<select name="select" id="select" class="icb-select-customize"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> ');
        $(this).closest("p").append('<a href="#" class="icb-rem">Remove</a>');


        // REMOVE LINK
        var ingselect = $(this).closest("b");
        var ingtext = ingselect.html();
        ingselect.replaceWith(ingtext);
    });

    /*     
      	
    // Left Side (A) Functions
      	
    $(".margherita-a").mouseover(function () { // Roll On (hover state)
    $(".pia").removeClass("hide").attr("src", "images/pizzas/meat_crustsupreme.gif");  // Unhide & Change the Pizza Image
    $(".psia").removeClass("hide").attr("src", "images/sauces/tomato.jpg");  // Unhide & Change the Pizza Sauce Image
    $(".pta").removeClass("hide").text("Roast Potato & Pumpkin"); // Unhide & Change the Pizza Title
    //$(".pda").removeClass("hide").text("Roasted Sweet Potato, Roasted Capsicum, Spanish Onions, Toasted Pinenuts, Cherry Tomatoes, Gorgonzola, topped with Fresh Herbs, Cracked Pepper & Sea Salt");  // Unhide & Change the Pizza Description
    $(".pma").addClass("hide");
    });
	    
	     
    $(".margherita-a").mouseout(function () { // Roll Off (hide content & show empty pizza icon)
    $(".pia, .psia, .pta, .pda").addClass("hide");
    $(".pma").removeClass("hide");
    });
	    
      	
    $(".margherita-a").click(function () { // Click (lock it in Eddy)
    $(".margherita-a").unbind("mouseover").unbind("mouseout");
    $(".pma").addClass("hide");
    $(".pia").removeClass("hide").attr("src", "images/pizzas/meat_crustsupreme.gif");  // Unhide & Change the Pizza Image
    $(".psia").removeClass("hide").attr("src", "images/sauces/tomato.jpg");  // Unhide & Change the Pizza Sauce Image
    $(".pta").removeClass("hide").text("Roast Potato & Pumpkin"); // Unhide & Change the Pizza Title
    //$(".pda").removeClass("hide").text("Roasted Sweet Potato, Roasted Capsicum, Spanish Onions, Toasted Pinenuts, Cherry Tomatoes, Gorgonzola, topped with Fresh Herbs, Cracked Pepper & Sea Salt");  // Unhide & Change the Pizza Description
    $(".pda").addClass("hide"); // Close the list of Ingredients
    $(".pca").slideUp("slow"); // Close the list of Options
    $(".changepa").removeClass("hide");
    });
    */

    $(".changepa").click(function() {
        $(".ica, .header.hidden.ingredients.a").hide();
        $(".pca").slideDown("slow");
        $(".pia, .psia, .pta, .pda").addClass("hide");
        $(".pma").removeClass("hide");
        $(".changepa").addClass("hide");

        $(".pca-slide").bind("mouseover", pca_slide_mouseover);
        $(".pca-slide").bind("mouseout", pca_slide_mouseout);

        resetHalf("a");

        return false;
    });

    $(".changepb").click(function() {
        $(".icb, .header.hidden.ingredients.b").hide();
        $(".pcb").slideDown("slow");
        $(".pib, .psib, .ptb, .pdb").addClass("hide");
        $(".pmb").removeClass("hide");
        $(".changepb").addClass("hide");

        $(".pcb-slide").bind("mouseover", pcb_slide_mouseover);
        $(".pcb-slide").bind("mouseout", pcb_slide_mouseout);

        resetHalf("b");

        return false;
    });





    // Right Side (B) Functions
    // Comments as per above


    // HOVER - PIZZA
    //    $(".margherita-b").mouseover(function() {
    //        $(".pib").removeClass("hide").attr("src", "../images/pizzas/meat_crustsupreme.gif");
    //        $(".psib").removeClass("hide").attr("src", "../images/sauces/tomato.jpg");
    //        $(".ptb").removeClass("hide").text("Margherita");
    //        //$(".pdb").removeClass("hide").text("Mozarella & Oregano.");
    //        $(".pmb").addClass("hide");
    //    });


    //    // HOVER OFF - PIZZA
    //    $(".margherita-b").mouseout(function() {
    //        $(".pib, .psib, .ptb, .pdb").addClass("hide");
    //        $(".pmb").removeClass("hide");
    //    });


    //    // CLICK - SELECT PIZZA
    //    $(".margherita-b").click(function() {
    //        $(".margherita-b").unbind("mouseover").unbind("mouseout");
    //        $(".pmb").addClass("hide");
    //        $(".pib").removeClass("hide").attr("src", "../images/pizzas/meat_crustsupreme.gif");
    //        $(".psib").removeClass("hide").attr("src", "../images/sauces/tomato.jpg");
    //        $(".ptb").removeClass("hide").text("Margherita");
    //        //$(".pdb").removeClass("hide").text("Mozarella & Oregano.");
    //        $(".pcb").slideUp("slow");
    //        $(".changepb").removeClass("hide");
    //    });

    //CLICK - CHANGE PIZZA






    /*


BIND BIND BIND BIND
	
	
    function pizzaHoverB() {
    $(".pib").removeClass("hide").attr("src", "images/pizzas/meat_crustsupreme.gif");
    $(".psib").removeClass("hide").attr("src", "images/sauces/tomato.jpg");
    $(".ptb").removeClass("hide").text("Margherita");
    //$(".pdb").removeClass("hide").text("Mozarella & Oregano.");
    $(".pmb").addClass("hide");
    }
	
	$(".margherita-b").mouseover(function () {
    $(".margherita-b").bind('mouseover', pizzaHoverB)
    });
	
	
    function aClick() {
    $("div").show().fadeOut("slow");
    }
    
    $("#bind").click(function () {
    // could use .bind('click', aClick) instead but for variety...
    $("#theone").click(aClick)
    .text("Can Click!");
    });
    
    $("#unbind").click(function () {
    $("#theone").unbind('click', aClick)
    .text("Does nothing...");
    });


*/





    // -------------------------------------------------------------------------------------------------------------------------------//
    // ---------------------------------------------------- LIGHTBOX -------------------------------------------------------------- //
    // -------------------------------------------------------------------------------------------------------------------------------//


    $(window).scroll(function() {

        var ScrollTop = document.body.scrollTop;

        if (ScrollTop == 0) {
            if (window.pageYOffset)
                ScrollTop = window.pageYOffset;
            else
                ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        }

        $("#popup").css("top", ScrollTop);
        $("#fade").css("top", ScrollTop);
    });


    $("#popup").css("display", "none");
    $("#fade").css("display", "none");


    // Toggle Popups
    $('a.checkPizza').click(function() {
        $("#popup").css("visibility", "visible");
        $("#popup").fadeIn("slow");
        //			$("#fade").fadeTo("slow", 0.5);
        $("#fade").css("display", "block");
        //				$("#fade").animate( { opacity:"0"}, 1 );
        //				$("#fade").animate( { opacity:"0.6"}, 1000 );

        return false;
    }
	);

    $('a.lightBoxClose').click(function() {
        $("#popup").fadeOut("slow");
        $("#fade").css("display", "none");

        return false;
    }
	);

    $(".qty").live("change", function() {
        var itemPrice = parseFloat($(this).parent().find("input").attr("value"));
        var qty = parseInt($(this).val());

        if (!isNaN(qty) && !isNaN(itemPrice))
            $(".subtotal").text("$" + (itemPrice * qty).toFixed(2));
        else
            $(".subtotal").text("");
    });

});
