﻿
//methods associated with home page transitions
var mainLinks = ["flexibles", "retail", "security"];
var nextBackgroundIndex = 1;
var backgroundTimerId;
var triggeredByClick = false;

//begin call to change background transitions
$(document).ready(function() {
    beginTimedBackgroundTransitions();
    $("body, form").css("background-color", "#000000");
});

//start the interval and store the timer id
function beginTimedBackgroundTransitions() {

    if (triggeredByClick == false) 
    {
        backgroundTimerId = setInterval("timerBackgroundChange()", 15000);
    }
    triggeredByClick = false;
}

//creates manual call to change background
function timerBackgroundChange() {   
    manuallyChangeBackground(mainLinks[nextBackgroundIndex]);
    if (nextBackgroundIndex == 2) {
        nextBackgroundIndex = 0;
    }
    else {
        nextBackgroundIndex++;
    }
}

function categoryOut(name) {
    beginTimedBackgroundTransitions();
}

//similiar to categoryOver() would reuse and refactor method, but no time... 
function manuallyChangeBackground(name) {
    var middleWrapper = $(".middleWrapper");
    var defaultCurrentClass = "middleWrapper home";
    var newClassToAppend = " " + name + "BackgroundHome";
    var newBackgroundClass = defaultCurrentClass + newClassToAppend;
    middleWrapper.attr("class", newBackgroundClass);
    resetMainLinksCss();
    setActiveLink(name);
}

function categoryClick(name) {
    triggeredByClick = true;
    categoryOver(name);
}

function categoryClosed() {
    beginTimedBackgroundTransitions();
}



function categoryOver(name) {
    clearInterval(backgroundTimerId);
    var middleWrapper = $(".middleWrapper");
    var defaultCurrentClass = "middleWrapper home";
    var newClassToAppend = " " + name + "BackgroundHome";
    var newBackgroundClass = defaultCurrentClass + newClassToAppend;
    middleWrapper.attr("class", newBackgroundClass);
    resetMainLinksCss();
    setActiveLink(name);
}

function setActiveLink(name) {
    var selectorString = ".mainLinks a#" + name;
    $(selectorString).attr("class", name + "Active");
}

function resetMainLinksCss() {
    $(".mainLinks a").each(function() {
        var currentName = $(this).attr("id");
        var newClass = currentName + "Inactive"
        $(this).attr("class", newClass);

    });
}



function styleDropDowns(selector) {
   
    if (selector == null)
        selector = "select";

    $(selector).msDropDown();

  
}


var $billingDropDown, $shippingDropDown;

function styleCheckoutDropDowns() {

    $billingDropDown = $(".billingForm .Type").msDropDown().data("dd");
    $shippingDropDown = $(".shippingForm .Type").msDropDown().data("dd");   
    $("select").not(".billingForm .Type,.shippingForm .Type").msDropDown();
}


$(document).ready(function() {  loadBackgroundHomeImages(); });
function loadBackgroundHomeImages() {
//    var baseUrl = "/app_themes/lfi_ampac/images/layout/";
//    var securityPic = "backgroundHome_security.jpg";
//    var hiddenDiv = $("<div style='display: none;'>");

//    var img = $("<img src='" + baseUrl + securityPic + "' />").appendTo(".middleWrapper");

//    $.get(baseUrl + "backgroundHome_security.jpg", function() { });
}


$(document).ready(function() {
    var $ = jQuery;

    //Toggle form field values based on their title
    var inputElements = $("form").find("input[type='text'],textarea");
    inputElements.live("focus", function(event) {
        var titleValue = $(this).attr("title");
        if (titleValue.length > 0) {
            var currentValue = $(this).val();
            if (titleValue == currentValue) {
                event.preventDefault();
                var _this = this;
                setTimeout(function() { _this.focus(); _this.select(); }, 100);

            } else if (currentValue.length == 0) {
                $(this).val(titleValue);
            }
        }
    });

    inputElements.live("blur", function(event) {
        var titleValue = $(this).attr("title");
        if (titleValue.length > 0) {
            var currentValue = $(this).val();
            if (currentValue.length == 0) {
                $(this).val(titleValue);
            }
        }
    });

    inputElements.live("mouseup", function(event) {
        var titleValue = $(this).attr("title");
        if (titleValue.length > 0) {
            var currentValue = $(this).val();
            if (titleValue == currentValue) {
                return false;
            }
        }
    });




    $(".billingForm input[title='Company']").live("blur", function() {
        try {
            if ($(this).val().length > 0 && $(this).val() != 'Company') {
                $billingDropDown.ddProp.value = '1';
                $billingDropDown.selectedIndex(1);
                $(".billingForm .Type").val('1');
            } else {
                $billingDropDown.ddProp.value = '0';
                $billingDropDown.selectedIndex(0);
                $(".billingForm .Type").val('0');
            }
        } catch (err) { }
    });

    $(".shippingForm input[title='Company']").live("blur", function() {
        try {
            if ($(this).val().length > 0 && $(this).val() != 'Company') {
                $shippingDropDown.ddProp.value = '1';
                $shippingDropDown.selectedIndex(1);
                $(".shippingForm .Type").val('1');
            } else {
                $shippingDropDown.ddProp.value = '0';
                $shippingDropDown.selectedIndex(0);
                $(".shippingForm .Type").val('0');
            }
        } catch (err) { }
    });

    //Sprite rollover code, set to live to handle controls that are displayed on ajax post
    $(".btnSprite").live("mouseenter", function() {
        $(this).css("background-position", "0px -27px");
    });
    $(".btnSprite").live("mouseleave", function() {
        $(this).css("background-position", "0px 0px");
    });

    $(".innovations_POP").live("click", (function() {
        window.location.href = "/innovations.aspx";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'Innovations', 'N/A']);
    }));


    img1 = new Image();
    img1.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_innovations_roll.jpg';

    img2 = new Image();
    img2.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_sustainability_roll.jpg';

    img3 = new Image();
    img3.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_featured_roll.jpg';

    img4 = new Image();
    img4.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_contact_roll.jpg';

    img5 = new Image();
    img5.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_shoponline_roll.jpg';

    img6 = new Image();
    img6.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_news_roll.jpg';

    img7 = new Image();
    img7.src = '/App_Themes/lfi_Ampac/images/POPs/amp_POP_FAQ_roll.jpg';

    $(".innovations_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_innovations_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_innovations.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".sustainability_POP").live("click", (function() {
        window.location.href = "/sustainability/";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'Sustainability', 'N/A']);
    }));

    $(".sustainability_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_sustainability_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_sustainability.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".featured_product_POP").live("click", (function() {
        window.location.href = "/Wine-Bag--P170.aspx";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'Featured Product', 'N/A']);
    }));

    $(".featured_product_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_featured_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_featured.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".contact_POP").live("click", (function() {
        window.location.href = "/contact/";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'Contact', 'N/A']);
    }));

    $(".contact_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_contact_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_contact.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".shop_online_POP").live("click", (function() {
        window.location.href = "/shop-ampac/";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'Shop Online', 'N/A']);
    }));

    $(".shop_online_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_shoponline_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_shoponline.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".ampac_news_POP").live("click", (function() {
        window.location.href = "/news/";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'News', 'N/A']);
    }));

    $(".ampac_news_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_news_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_news.jpg)');
            $(this).css('cursor', 'default');
        });

    $(".ampac_FAQs_POP").live("click", (function() {
        window.location.href = "/ampac-faqs/";
        //google analytics call
        _gaq.push(['_trackEvent', 'POP', 'Click', 'FAQs', 'N/A']);
    }));

    $(".ampac_FAQs_POP").hover(function() {
        $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_FAQ_roll.jpg)');
        $(this).css('cursor', 'pointer');
    },
        function() {
            $(this).css('background-image', 'url(/App_Themes/lfi_Ampac/images/POPs/amp_POP_FAQ.jpg)');
            $(this).css('cursor', 'default');
        });
    
    //document.ready ends here
});

    var iviewer = {};
    function initializeImageZoom() {
        $(".mainImage img").bind("click", ImageModal);

        $(".closeDialog").live("click", function() {
            $(".imageModal").dialog("close");
            $(".imageModal").css('visibility', 'hidden');

        });

        $(".zoomedImage").iviewer(
                  {
                      src: $("img.mainImage").attr("src"),
                      zoom: 50,
                      zoom_min: 50,
                      zoom_max: 400,
                      update_on_resize: false,
                      initCallback: function() {
                          iviewer = this;
                          $("#in").click(function() { iviewer.zoom_by(1); });
                          $("#out").click(function() { iviewer.zoom_by(-1); });
                          $("#fit").click(function() { iviewer.fit(); });
                          $("#orig").click(function() { iviewer.set_zoom(100); });
                          $("#update").click(function() { iviewer.update_container_info(); });
                      },
                      onMouseMove: function(object, coords) { },
                      onStartDrag: function(object, coords) { },
                      onDrag: function(object, coords) { }
                  });
                  
        $(".productDetailsBoxImages a img").bind("click", ImageModal);



                
   }


   var ImageModal = function() {
       //if it's in the small images section, we the get parent link location
       var imageUrl = $(this).parent().attr("href");

       //if its the main image, it won't be inside a link
       //NOTE: We should consider placing this inside a link for non JS users..

       if (imageUrl == undefined) {
           imageUrl = $("img.mainImage").attr("src");
       }
       var obj = $(".zoomedImage").data('viewer');
       obj.loadImage(imageUrl);

       var newContent = ".imageModal"

       $(newContent).dialog({ dialogClass: 'fullScreenImage', modal: true, show: 'explode', hide: 'slide' });
       $(newContent).css('visibility','visible');
       return false;
   }
    
    

    function ToggleOrderSummary()
    {
        var allowContinue = false;
        if (Page_ClientValidate()) {
            if ($(".orderSummaryPayment").is(":visible") || $(".payMyOrderPage").length > 0) {
                allowContinue = true;
            }
            else {
                $(".orderSummaryPayment").show("slow");
                $(".couponDialog").hide();
                $(".paymentInputFields").hide();
                $(".shippingMethodsWrapper").hide();
                $(".registrationPaymentAddresses").hide();
                $(".paymentMethodWrapper").hide();
                $(".paymentMethod h2").hide();
                $(".createAccountWrapper").hide();
                $(".sprtContinueChkOut").addClass("sprtComplete").removeClass("sprtContinueChkOut");
                $('.checkOutBCPlaceHolder').html("&nbsp;&gt;&nbsp;&nbsp;<a href='/checkout/'>Order Summary</a>");
                scroll(0, 0);
               
            }
        }
        return allowContinue;
    }

    function RevertToPaymentForm() {
        $(".orderSummaryPayment").hide("fast");
        $(".couponDialog").show();
        $(".paymentInputFields").show();
        $(".shippingMethodsWrapper").show();
        $(".registrationPaymentAddresses").show();
        $(".paymentMethodWrapper").show();
        $(".paymentMethod h2").show();
        $(".createAccountWrapper").show();
        $(".sprtComplete").addClass("sprtContinueChkOut").removeClass("sprtComplete");
        scroll(0, 0);    
    }

    //home page video setup.
    var player = null;
    var miniPlayer = null;
    $(document).ready(function() {

        //$("#homeModalVideoLinkMini").append("<img src='/images/ui/Icons/ampacVideo.gif' alt='Ampac Video' />");
        $("#homeModalVideoLinkMini").hover(function() {
            if (miniPlayer == null) {

                miniPlayer = flowplayer("homeModalVideoLinkMini", "/flowplayer/flowplayer.leapfrog-3.1.5.swf",
                           {
                               key: 'e7be95efb0b31a8cabb',
                               clip:
                                {
                                    url: "/video/AMPAC_clip_320x180_1_NoAudio.flv",
                                    autoPlay: true,
                                    autoBuffering: true
                                },
                               plugins:
                               {
                                   controls: null
                               },
                               onBeforePause: function() { return false; },
                               onBeforeFinish: function() { return false; }
                           });
                miniPlayer.load();
            }
        });

        $("#homeModalVideoLink").click(function() {

            $(".homeVideoModal").dialog({ dialogClass: 'vidViewer', modal: true, show: 'explode', hide: 'slide' });

            //player = flowplayer("homePlayer", "/flowplayer/flowplayer.leapfrog-3.1.5.swf");

            player = flowplayer("homePlayer", "/flowplayer/flowplayer.leapfrog-3.1.5.swf",
                            {
                                key: 'e7be95efb0b31a8cabb',
                                clip:
                                {
                                    url: "/video/AMPAC_0917_FINAL_H264_640x360.mov",
                                    autoPlay: true,
                                    autoBuffering: true
                                }
                            }
                      );
            player.load();
            return false;

        });


        $(".closeDialog").click(function() {
            if (player != null) {
                player.unload();
            }
            $(this).parent().dialog("close");
        });

    });
