﻿function initIndex() {

    $("#thumb ul").tabs("#panes > div", { effect: 'fade', fadeOutSpeed: 400 });

    $("#emailDialog").dialog({
        autoOpen: false,
        width: 500,
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });

    $("#emailSignupForm").validate({
        submitHandler: function (form) {

            $("#emailDialog").html("<center><img src='../img/submitImage.gif' style='vertical-align:middle;'/>&nbsp;&nbsp;<strong>Submitting Aaxeon Insight Subscription</strong></center>");
            $("#emailDialog").dialog("open");

            $.ajax({
                type: "POST",
                url: "insight",
                data: {
                    "email": $("#emailInput").val()
                },
                success: function (data) {
                    if (data.success == "True") {
                        $("#emailDialog").html("<center>You have successfully subscribed to Aaxeon Insight!</center>");
                    } else {
                        $("#emailDialog").html("<center>An Unexpected Error Occured<br/> Please email <b>info@aaxeon.com</b></center>");
                    }
                },
                error: function () {
                    $("#emailDialog").html("<center>An Unexpected Error Occured<br/> Please email <b>info@aaxeon.com</b></center>");
                },
                dataType: "json"
            });
            return false;
        },
        errorPlacement: function (error, element) {
            return;
        }
    });
}

function initMaps() {
    var chicago = new google.maps.LatLng(41.924832, -87.697456),
          pointToMoveTo,
          first = true,
          curMarker = new google.maps.Marker({}),
          $el;

    var myOptions = {
        zoom: 10,
        center: chicago,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map($("#map_canvas")[0], myOptions);

    $("#locations li").mouseenter(function () {

        $el = $(this);

        if (!$el.hasClass("hover")) {

            $("#locations li").removeClass("hover");
            $el.addClass("hover");

            if (!first) {

                // Clear current marker
                curMarker.setMap();

                // Set zoom back to Chicago level
                // map.setZoom(10);
            }

            // Move (pan) map to new location
            pointToMoveTo = new google.maps.LatLng($el.attr("data-geo-lat"), $el.attr("data-geo-long"));
            map.panTo(pointToMoveTo);

            // Add new marker
            curMarker = new google.maps.Marker({
                position: pointToMoveTo,
                map: map,
                icon: "img/marker.png"
            });

            // On click, zoom map
            google.maps.event.addListener(curMarker, 'click', function () {
                map.setZoom(14);
            });



            // No longer the first time through (re: marker clearing)        
            first = false;
        }

    });

    $("#locations li:first").trigger("mouseenter");
}

function initApp() {

    var sectionName = $.trim($("#breadcrumb > li:nth-child(2)").text());
    if (sectionName == "") {
        $("#nav > li.current").removeClass("current");
        $("#nav > li:nth-child(1)").addClass("current");
    } else {
        $("#nav > li.current").removeClass("current");

        switch (sectionName) {
            case "Products":
                $("#nav > li:nth-child(2)").addClass("current");
                break;
            case "Solutions":
                $("#nav > li:nth-child(3)").addClass("current");
                break;
            case "News":
                $("#nav > li:nth-child(4)").addClass("current");
                break;
            case "Support":
                $("#nav > li:nth-child(5)").addClass("current");
                break;
            case "About Us":
                $("#nav > li:nth-child(6)").addClass("current");
                break;
            case "Contact Us":
                $("#nav > li:nth-child(7)").addClass("current");
                break;
        }
    }

    jQuery("#searchBox").autocomplete({
        source: "/products/itemsearch",
        delay: 50,
        select: function (event, ui) {
            var path = "http://" + window.location.host + ui.item.id;
            window.location = path;
        }
    });

    $("#itemSearch").bind("click", function (e) {
        e.preventDefault();
    });

    $("#searchButton").bind("click", function (e) {
        var path = "/search?productsearch=" + $("#searchBox").val();

        window.location = path;
    });
}

