﻿/// <reference path="jquery-1.6.2.min.js" />

function ShowMessage(msg) {
    if (msg) {
        alert(msg);
    }
    setTimeout(function () { LoadImageRotator(); }, 0);
}

function LoadImageRotator() {
    $('.image-rotator').each(function () {
        var $this = $(this);
        $this.nivoSlider({
            effect: $this.attr('Effect'),
            animSpeed: $this.attr('animSpeed'), // Slide transition speed
            pauseTime: $this.attr('pauseTime') // How long each slide will show
        });
        $this.show();
    });
}

$(document).ready(function () {
    //alert('1');
    //setTimeout(function () { LoadImageRotator(); }, 300);
    //Apply_Cufon();
    //Cufon.now();
    $(".address-state").autocomplete(USstates, {
        minChars: 0,
        width: 180,
        max: 52,
        matchContains: "word",
        autoFill: false,
        formatItem: function (row, i, max) {
            return row.abbr + " - " + row.name;
        },
        formatMatch: function (row, i, max) {
            return row.abbr;
        },
        formatResult: function (row) {
            return row.abbr;
        }
    });

    $('#show-full').click(function () {
        forceFullOrMobileVersion('full');
        return false;
    });
    $('#show-mobile').click(function () {
        forceFullOrMobileVersion('mobile');
        return false;
    });
});

var USstates = [
	{ abbr: "AL", name: "Alabama" },
	{ abbr: "AK", name: "Alaska" },
	{ abbr: "AZ", name: "Arizona" },
	{ abbr: "AR", name: "Arkansas" },
	{ abbr: "CA", name: "California" },
	{ abbr: "CO", name: "Colorado" },
	{ abbr: "CT", name: "Connecticut" },
	{ abbr: "DE", name: "Delaware" },
	{ abbr: "DC", name: "District Of Columbia" },
	{ abbr: "FL", name: "Florida" },
	{ abbr: "GA", name: "Georgia" },
	{ abbr: "HI", name: "Hawaii" },
	{ abbr: "ID", name: "Idaho" },
	{ abbr: "IA", name: "Iowa" },
	{ abbr: "KS", name: "Kansas" },
	{ abbr: "KY", name: "Kentucky" },
	{ abbr: "LA", name: "Louisiana" },
	{ abbr: "ME", name: "Maine" },
	{ abbr: "MD", name: "Maryland" },
	{ abbr: "MA", name: "Massachusetts" },
	{ abbr: "MI", name: "Michigan" },
	{ abbr: "MN", name: "Minnesota" },
	{ abbr: "MS", name: "Mississippi" },
	{ abbr: "MO", name: "Missouri" },
	{ abbr: "MT", name: "Montana" },
	{ abbr: "NE", name: "Nebraska" },
	{ abbr: "NV", name: "Nevada" },
	{ abbr: "NH", name: "New Hampshire" },
	{ abbr: "NJ", name: "New Jersey" },
	{ abbr: "NM", name: "New Mexico" },
	{ abbr: "NY", name: "New York" },
	{ abbr: "NC", name: "North Carolina" },
	{ abbr: "ND", name: "North Dakota" },
	{ abbr: "OH", name: "Ohio" },
	{ abbr: "OK", name: "Oklahoma" },
	{ abbr: "PA", name: "Pennsylvania" },
	{ abbr: "IL", name: "Illinois" },
	{ abbr: "IN", name: "Indiana" },
	{ abbr: "OR", name: "Oregon" },
	{ abbr: "RI", name: "Rhode Island" },
	{ abbr: "SC", name: "South Carolina" },
	{ abbr: "SD", name: "South Dakota" },
	{ abbr: "TN", name: "Tennessee" },
	{ abbr: "TX", name: "Texas" },
	{ abbr: "UT", name: "Utah" },
	{ abbr: "VT", name: "Vermont" },
	{ abbr: "VA", name: "Virginia" },
	{ abbr: "WA", name: "Washington" },
	{ abbr: "WV", name: "West Virginia" },
	{ abbr: "WI", name: "Wisconsin" },
	{ abbr: "WY", name: "Wyoming" }
];

function forceFullOrMobileVersion(version) {
    if ($.cookie('force-mobile-or-full')) {
        $.cookie('force-mobile-or-full', version, { expires: 0.0001 });
    }
    else {
        $.cookie('force-mobile-or-full', version);
    }
    location.reload();
}

function OpenMap() {
    var zip = $('#saddr').val();
    if (zip) {
        location.href = '/SearchResults?zipOrStateOrCity=' + zip;
    }
}

function QueryString(queryString) {
    if (!queryString) return;
    var that = this;
    $.each(queryString.substr(1).split('&'), function (n, a) {
        var s = a.split('=');
        that[s[0]] = decodeURIComponent(s[1].replace(/\+/g, " "));
    });
}

var oQueryString = new QueryString(location.search);


function endGetPosition(position) {
//map and bounds must be defined globally
    var clientPos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    map.setCenter(clientPos);

    $.post('/Business/GetNearest', { lat: position.coords.latitude, lng: position.coords.longitude }, function (data) {
        $.each(data, function (i, b) {
            var businessPos = new google.maps.LatLng(b.lat, b.lng);
            var marker = new google.maps.Marker({
                map: map,
                position: businessPos,
                title: b.companyName
                //icon: 'http://googlemapsmarkers.com/v1/#/ff8476/'.replace('#', letter)
            });

            var infowindow = new google.maps.InfoWindow();
            var infoContent = "<div class='info'>" + b.companyName + "<br /> <a href='http://maps.google.com/?daddr="
                    + b.lat + ',' + b.lng + "' target='_blank'>Directions</a></div>";
            google.maps.event.addListener(marker, 'click', function () {
                infowindow.setContent(infoContent);
                infowindow.setOptions({ maxWidth: 110 });
                infowindow.open(map, this);
            });
            bounds.extend(businessPos);
        });
        //  Fit bounds to the map
        google.maps.event.addListenerOnce(map, "idle", function () {
            setTimeout(function () {
                map.fitBounds(bounds);
            }, 1000);
        });
    });
}

function disableTouchEvents(map_id) {
    var dragFlag = false;
    var start = 0, end = 0;

    function thisTouchStart(e) {
        dragFlag = true;
        start = e.touches[0].pageY;
    }

    function thisTouchEnd() {
        dragFlag = false;
    }

    function thisTouchMove(e) {
        if (!dragFlag) return;
        end = e.touches[0].pageY;
        window.scrollBy(0, (start - end));
    }

    document.getElementById(map_id).addEventListener("touchstart", thisTouchStart, true);
    document.getElementById(map_id).addEventListener("touchend", thisTouchEnd, true);
    document.getElementById(map_id).addEventListener("touchmove", thisTouchMove, true);

}
