﻿var timeout = 200;
var closetimer = 0;
var ddmenuitem = 0;
var animating = 0;
var likes = 0;
var showResultsInc = 5;
var totalShowing = 1;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').show();
    //console.log(ddmenuitem);
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.hide(); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

$(document).ready(function () {
    $('#menuList > li').bind('mouseover', jsddm_open);
    $('#menuList > li').bind('mouseout', jsddm_timer);
    //cans
    $('#rolloverCrispWhite, #rolloverRipeRouge, #rolloverFreshTropical, #rolloverBrightCitrus, #rolloverBerryCassis, #rolloverBottleCrispWhite, #rolloverBottleRipeRouge, #rolloverBottleLightRed').bind('mouseover', function () {
        $(this).children().animate({ opacity: 'show', height: 'show' }, 'fast');
    });
    $('#rolloverCrispWhite, #rolloverRipeRouge, #rolloverFreshTropical, #rolloverBrightCitrus, #rolloverBerryCassis, #rolloverBottleCrispWhite, #rolloverBottleRipeRouge, #rolloverBottleLightRed').bind('mouseout', function () {
        $(this).children().hide();
    });

    //Nutrition facts lightbox
    if ($('#nutritionLightbox').length > 0) {
        $('#nutritionFacts').bind('click', function () { $('#nutritionLightbox').fadeIn('slow'); });
        $('#nutritionLightbox').bind('click', function () { $('#nutritionLightbox').fadeOut('slow'); });

        var leftOffset = (($(window).width() - $('#content').width()) / 2) * -1;
        if (leftOffset > 0)
            leftOffset = 0;
        var lightboxWidth = $(window).width();
        if (lightboxWidth < $('#content').width())
            lightboxWidth = $('#content').width();
        $('#nutritionLightbox').css('height', $(document).height() + 'px').css('width', lightboxWidth + 'px').css('marginLeft', leftOffset + 'px');

        $(window).resize(function () {
            var leftOffset = (($(window).width() - $('#content').width()) / 2) * -1;
            if (leftOffset > 0)
                leftOffset = 0;
            var lightboxWidth = $(window).width();
            if (lightboxWidth < $('#content').width())
                lightboxWidth = $('#content').width();
            $('#nutritionLightbox').css('width', lightboxWidth + 'px').css('marginLeft', leftOffset + 'px');
        });
    }

    if ($('#connect').length > 0) {
        var $container = $('#connect');
        $container.isotope({
            masonry: {
                columnWidth: 10
            },
            getSortData: {
                sortid: function ($elem) {
                    return parseInt($elem.find('.sortid').text(), 10);
                }
            },
            sortBy: 'sortid'
        });
    }

    //FIND US
    if ($('#findUsHeadline').length > 0) {
        $('#findUsHeadline').hide().fadeIn(500);
        $('#findUsFormWrapper').hide().fadeIn(500);
        $('#state').chosen();
        $('#radius').chosen();
        loadGoogleMap();
        $('#findUsMap').hide();
    }
    $('#findusSubmit').click(function () {
        searchLocations();
        $('#formError').hide();
        $('#findUsLoader').show();
    });
    $('#searchAgain').click(function () {
        $('#findUsMap').fadeOut(500, function () {
            $('#findUsFormWrapper').fadeIn(500);
        });
    });
    $('#mapViewMore').click(function () {
        var start = totalShowing + 1;
        var end = totalShowing + showResultsInc;
        var totalMarkers = $('.resultListing').length;
        if (end >= totalMarkers) {
            end = totalMarkers;
            $('#mapViewMore').slideUp();
        }
        for (var resCount = start; resCount <= end; resCount++) {
            $('#mapresult-' + resCount).slideDown();
            totalShowing++;
        }
        $('#resultsShowing').html("(Showing 1-" + totalShowing + " of " + totalMarkers + " stores)");
    });

});

document.onclick = jsddm_close;

$(window).load(function () {
    var ie8 = false;
    if ($.browser.msie != undefined) {
        ie8 = ($.browser.msie && ((parseInt($.browser.version) == 8) || (parseInt($.browser.version) == 7)));
    }
    if ($('#homeProduct').length) {
        if (!ie8) {
            $('#homeProduct').delay(400).fadeIn('slow', function () { $('#homeLike').fadeIn('fast', function () { $('#homeDiscover').fadeIn('slow') }) });
        }
        else {
            $('#homeProduct').delay(400).fadeIn('slow', function () { $('#homeLike').show(); $('#homeDiscover').show(); });
        }
    };
    if ($('#homeProductFindUs').length) {
        if (!ie8) {
            $('#homeProductFindUs').delay(400).fadeIn('slow', function () { $('#homeLike').fadeIn('fast', function () { $('#homeDiscover').fadeIn('slow') }) });
        }
        else {
            $('#homeProductFindUs').delay(400).fadeIn('slow', function () { $('#homeLike').show(); $('#homeDiscover').show(); });
        }
    };
    if ($('#cansProduct').length) {
        if (!ie8) {
            $('#cansProduct').delay(400).fadeIn('slow', function () { $('#cansCTA').fadeIn('slow'); });
        }
        else {
            $('#cansProduct').show();
            $('#cansCTA').show();
        }
    };
    if ($('#cansProductCrispWhite').length) {
        if (!ie8) {
            $('#cansProductCrispWhite').delay(500).fadeIn('slow', function () { $('#canLike').fadeIn('fast'); });
        }
        else {
            $('#cansProductCrispWhite').show();
            $('#canLike').show();
        }
        $('#pairsWith1CrispWhite').bind('mouseover', function () { $('#pairsWith1CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith1CrispWhite').bind('mouseout', function () { $('#pairsWith1CrispWhiteOver').hide(); });
        $('#pairsWith2CrispWhite').bind('mouseover', function () { $('#pairsWith2CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith2CrispWhite').bind('mouseout', function () { $('#pairsWith2CrispWhiteOver').hide(); });
        $('#pairsWith3CrispWhite').bind('mouseover', function () { $('#pairsWith3CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith3CrispWhite').bind('mouseout', function () { $('#pairsWith3CrispWhiteOver').hide(); });
    };
    if ($('#cansProductRipeRouge').length) {
        if (!ie8) {
            $('#cansProductRipeRouge').delay(500).fadeIn('slow', function () { $('#canLike').fadeIn('fast'); });
        }
        else {
            $('#cansProductRipeRouge').show();
            $('#canLike').show();
        }
        $('#pairsWith1RipeRouge').bind('mouseover', function () { $('#pairsWith1RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith1RipeRouge').bind('mouseout', function () { $('#pairsWith1RipeRougeOver').hide(); });
        $('#pairsWith2RipeRouge').bind('mouseover', function () { $('#pairsWith2RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith2RipeRouge').bind('mouseout', function () { $('#pairsWith2RipeRougeOver').hide(); });
        $('#pairsWith3RipeRouge').bind('mouseover', function () { $('#pairsWith3RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith3RipeRouge').bind('mouseout', function () { $('#pairsWith3RipeRougeOver').hide(); });
    };
    if ($('#cansProductFreshTropical').length) {
        if (!ie8) {
            $('#cansProductFreshTropical').delay(500).fadeIn('slow', function () { $('#canLike').fadeIn('fast'); });
        }
        else {
            $('#cansProductFreshTropical').show();
            $('#canLike').show();
        }
        $('#pairsWith1FreshTropical').bind('mouseover', function () { $('#pairsWith1FreshTropicalOver').fadeIn('fast'); });
        $('#pairsWith1FreshTropical').bind('mouseout', function () { $('#pairsWith1FreshTropicalOver').hide(); });
        $('#pairsWith2FreshTropical').bind('mouseover', function () { $('#pairsWith2FreshTropicalOver').fadeIn('fast'); });
        $('#pairsWith2FreshTropical').bind('mouseout', function () { $('#pairsWith2FreshTropicalOver').hide(); });
        $('#pairsWith3FreshTropical').bind('mouseover', function () { $('#pairsWith3FreshTropicalOver').fadeIn('fast'); });
        $('#pairsWith3FreshTropical').bind('mouseout', function () { $('#pairsWith3FreshTropicalOver').hide(); });
    };
    if ($('#cansProductBrightCitrus').length) {
        if (!ie8) {
            $('#cansProductBrightCitrus').delay(500).fadeIn('slow', function () { $('#canLike').fadeIn('fast'); });
        }
        else {
            $('#cansProductBrightCitrus').show();
            $('#canLike').show();
        }
        $('#pairsWith1BrightCitrus').bind('mouseover', function () { $('#pairsWith1BrightCitrusOver').fadeIn('fast'); });
        $('#pairsWith1BrightCitrus').bind('mouseout', function () { $('#pairsWith1BrightCitrusOver').hide(); });
        $('#pairsWith2BrightCitrus').bind('mouseover', function () { $('#pairsWith2BrightCitrusOver').fadeIn('fast'); });
        $('#pairsWith2BrightCitrus').bind('mouseout', function () { $('#pairsWith2BrightCitrusOver').hide(); });
        $('#pairsWith3BrightCitrus').bind('mouseover', function () { $('#pairsWith3BrightCitrusOver').fadeIn('fast'); });
        $('#pairsWith3BrightCitrus').bind('mouseout', function () { $('#pairsWith3BrightCitrusOver').hide(); });
    };
    if ($('#cansProductBerryCassis').length) {
        if (!ie8) {
            $('#cansProductBerryCassis').delay(500).fadeIn('slow', function () { $('#canLike').fadeIn('fast'); });
        }
        else {
            $('#cansProductBerryCassis').show();
            $('#canLike').show();
        }
        $('#pairsWith1BerryCassis').bind('mouseover', function () { $('#pairsWith1BerryCassisOver').fadeIn('fast'); });
        $('#pairsWith1BerryCassis').bind('mouseout', function () { $('#pairsWith1BerryCassisOver').hide(); });
        $('#pairsWith2BerryCassis').bind('mouseover', function () { $('#pairsWith2BerryCassisOver').fadeIn('fast'); });
        $('#pairsWith2BerryCassis').bind('mouseout', function () { $('#pairsWith2BerryCassisOver').hide(); });
        $('#pairsWith3BerryCassis').bind('mouseover', function () { $('#pairsWith3BerryCassisOver').fadeIn('fast'); });
        $('#pairsWith3BerryCassis').bind('mouseout', function () { $('#pairsWith3BerryCassisOver').hide(); });
    };
    if ($('#bottlesProductCrispWhite').length) {
        if (!ie8) {
            $('#bottlesProductCrispWhite').delay(500).fadeIn('slow', function () { $('#bottleLike').fadeIn('fast'); });
        }
        else {
            $('#bottlesProductCrispWhite').show();
            $('#bottleLike').show();
        }
        $('#pairsWith1CrispWhite').bind('mouseover', function () { $('#pairsWith1CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith1CrispWhite').bind('mouseout', function () { $('#pairsWith1CrispWhiteOver').hide(); });
        $('#pairsWith2CrispWhite').bind('mouseover', function () { $('#pairsWith2CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith2CrispWhite').bind('mouseout', function () { $('#pairsWith2CrispWhiteOver').hide(); });
        $('#pairsWith3CrispWhite').bind('mouseover', function () { $('#pairsWith3CrispWhiteOver').fadeIn('fast'); });
        $('#pairsWith3CrispWhite').bind('mouseout', function () { $('#pairsWith3CrispWhiteOver').hide(); });
    };
    if ($('#bottlesProductRipeRouge').length) {
        if (!ie8) {
            $('#bottlesProductRipeRouge').delay(500).fadeIn('slow', function () { $('#bottleLike').fadeIn('fast'); });
        }
        else {
            $('#bottlesProductRipeRouge').show();
            $('#bottleLike').show();
        }
        $('#pairsWith1RipeRouge').bind('mouseover', function () { $('#pairsWith1RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith1RipeRouge').bind('mouseout', function () { $('#pairsWith1RipeRougeOver').hide(); });
        $('#pairsWith2RipeRouge').bind('mouseover', function () { $('#pairsWith2RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith2RipeRouge').bind('mouseout', function () { $('#pairsWith2RipeRougeOver').hide(); });
        $('#pairsWith3RipeRouge').bind('mouseover', function () { $('#pairsWith3RipeRougeOver').fadeIn('fast'); });
        $('#pairsWith3RipeRouge').bind('mouseout', function () { $('#pairsWith3RipeRougeOver').hide(); });
    };
    if ($('#bottlesProductLightRed').length) {
        if (!ie8) {
            $('#bottlesProductLightRed').delay(500).fadeIn('slow', function () { $('#bottleLike').fadeIn('fast'); });
        }
        else {
            $('#bottlesProductLightRed').show();
            $('#bottleLike').show();
        }
        $('#pairsWith1LightRed').bind('mouseover', function () { $('#pairsWith1LightRedOver').fadeIn('fast'); });
        $('#pairsWith1LightRed').bind('mouseout', function () { $('#pairsWith1LightRedOver').hide(); });
        $('#pairsWith2LightRed').bind('mouseover', function () { $('#pairsWith2LightRedOver').fadeIn('fast'); });
        $('#pairsWith2LightRed').bind('mouseout', function () { $('#pairsWith2LightRedOver').hide(); });
    };
    if ($('#bottlesProduct').length) {
        if (!ie8) {
            $('#bottlesProduct').delay(400).fadeIn('slow', function () { $('#bottlesCTA').fadeIn('slow'); });
        }
        else {
            $('#bottlesProduct').show();
            $('#bottlesCTA').show();
        }
    };
});

//////////////////////////////////////
/* STORE LOCATOR / GOOGLE MAP BEGIN */
//////////////////////////////////////
var map;
var geocoder;

function loadGoogleMap() {
    if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('mapResults'));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(40, -100), 4);
    }
}

function searchLocations() {
    var address = "";
    if ($('#zip').val() != "")
        address = $('#zip').val();
    else
        address = $('#city').val() + ", " + $('#state').val();

    totalShowing = 0;
    $('.resultListing').remove();

    geocoder.getLatLng(address, function (latlng) {
        if (!latlng) {
            $('#formError').fadeIn();
            $('#findUsLoader').hide();
        } else {
            searchLocationsNear(latlng, address);
        }
    });
}

function searchLocationsNear(center, searchTerm) {
    var radius = $('#radius').val();
    var searchUrl = '/Information/GetLocations?lat=' + center.lat() + '&lng=' + center.lng() + '&rad=' + radius;
    GDownloadUrl(searchUrl, function (data) {

        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName('marker');
        //map.clearOverlays();

        var sidebar = document.getElementById('resultsWrapper');
        sidebar.innerHTML = '';
        $('#findUsLoader').hide();
        if (markers.length == 0) {
            $('#formError').fadeIn();
            return;
        } else {
            $('#formError').hide();
            $('#findUsFormWrapper').fadeOut(500, function () {
                $('#findUsMap').fadeIn(500);
            });
            $('#resultsFor').html("Results for '" + searchTerm + "':");
            $('#resultsShowing').html("(Showing 1-" + showResultsInc + " of " + markers.length + " stores)");
        }

        var bounds = new GLatLngBounds();
        for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute('name');
            var address = markers[i].getAttribute('address');
            var distance = parseFloat(markers[i].getAttribute('distance'));
            var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));

            var marker = createMarker(point, name, address);
            map.addOverlay(marker);
            var sidebarEntry = createSidebarEntry(marker, name, address, distance, i);
            sidebar.appendChild(sidebarEntry);
            bounds.extend(point);
        }
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

        //DISPLAY INITAL NUMBER OF LOCATIONS
        for (var cnt = 1; cnt <= showResultsInc; cnt++) {
            $('#mapresult-' + cnt).slideDown();
            totalShowing++;
        }
        if (totalShowing < $('.resultListing').length)
            $('#mapViewMore').show();
        else
            $('#mapViewMore').hide();
    });
}

function createMarker(point, name, address) {
    var marker = new GMarker(point);
    var html = '<h3>' + name + '</h3>' + address;
    GEvent.addListener(marker, 'click', function () {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}

function createSidebarEntry(marker, name, address, distance, count) {
    var divClass;
    if((count + 1) % 2 == 1)
        divClass = "odd";
    else
        divClass = "even";

    var div = document.createElement('div');
    div.className = "resultListing " + divClass;
    div.setAttribute('id', 'mapresult-' + (count + 1));
    var dirLink = "http://maps.google.com/maps?q=" + address.replace(new RegExp(' ', 'g'), '+') + "&hl=en&vpsrc=0&z=17";
    var html = '<span class="name">' + name + '</span><span class="address">' + address + '</span><span class="distance">' + distance.toFixed(1) + ' miles away</span><a class="directions" href="' + dirLink + '" target="_blank">DIRECTIONS</a>';
    
    div.innerHTML = html;
    GEvent.addDomListener(div, 'click', function () {
        GEvent.trigger(marker, 'click');
    });
    return div;
}

