var map = null;
var infowindows = new Array();
var markersArray = new Array();
var browser = function() {
    var ua = navigator.userAgent, gecko = /Gecko\//.test(ua) ? ua.match(/; rv:1\.(\d+?)\.(\d)/) : 0,
    webkit = /AppleWebKit\//.test(ua), safari = webkit && /Safari\//.test(ua),
    ie = 0 /*@cc_on + @_jscript_version * 10 % 10 @*/;
    return {
        ie: ie >= 5 ? ie : 0,
        gecko: gecko ? '1.' + gecko.slice(1).join('.') : 0,
        firefox: gecko ? (gecko[1] == 9 ? 3 : gecko[1] == 8 && gecko[2] > 0 ? 2 : 0) : 0,
        opera: window.opera && opera.version ? opera.version()[0] : 0,
//        webkit: webkit ? ua.match(/AppleWebKit\/(\d+?\.\d+?.\d+?\s)/)[1] : 0,
		webkit: webkit,
        safari: safari && /Version\//.test(ua) ? ua.match(/Version\/(\d{1})/)[1] : 0,
        chrome: safari && /Chrome\//.test(ua) ? ua.match(/Chrome\/(\d+?\.\d)/)[1] : 0
    };
}();


jQuery(function () {
    function initialize() {
        var myLatlng = new google.maps.LatLng(currentLocation.latitude, currentLocation.longtitude);
        var myOptions = {
            zoom: 14,
            center: myLatlng,
            mapTypeControl: false,
            mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
            navigationControl: true,
            navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'You are here!'
        });

        var ifb_hide = $("#ifb_hide");
        ifb_hide.find(".ibf_top").addClass("green_top");
        ifb_hide.find(".ibf_body").addClass("green_body");
		ifb_hide.find(".ibf_end").addClass("green_end");
		ifb_hide.find(".ibf_arrow").addClass("green_arrow");
        ifb_hide.find(".ibf_title").html("You are here!");
        ifb_hide.find(".ibf_text").html('<p>' + currentLocation.address + '</p>');

		var yDelta = -172;
		if(browser.ie) yDelta = -169;
		if(browser.webkit) yDelta = -169;
		if(browser.opera) yDelta = -167;
		
        var infoBox = new InfoBox({
            latlng: marker.getPosition(),
            map: map,
            content: ifb_hide.html(),
            closeBoxURL: "../images/infobox/ico_close.png",
            closeBoxMargin: 0,
            pixelOffset: new google.maps.Size(-165, yDelta)
        });
        infowindows.push(infoBox);

        google.maps.event.addListener(marker, "click", function () {
            $.each(infowindows, function (indexInArray, valueOfElement) {
                valueOfElement.close();
            })
            infoBox.open(map, marker);
        });

    }

    initialize();

    $('.coordinates').each(function () {
        var id = $(this).find('#id').text();
        var naan_type = $(this).find('#naan_type').text();
        var name = $(this).find('#name').text();
        var type = "";
        var addr = $(this).find('#addr').html();
        var phone = $(this).find('#phone').text();
        var logo = $(this).find('#logo').text();
        var verified = $(this).find('#verified').text();
        var detaillink = $(this).find('#detaillink').text();
        var longtitude = $(this).find('#long').text();
        var latitude = $(this).find('#lat').text();
        var dist = $(this).find('#distance').text();
        var myLatlng = new google.maps.LatLng(latitude, longtitude);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map
        });



        if (dist != '') {
            dist = '<br /><h3>' + dist + ' miles</h3>&nbsp;';
        }

        var ifb_hide = $("#ifb_hide");
        ifb_hide.find(".ibf_title").html(name);
        ifb_hide.find(".ibf_text").html('<p>' + '<small>' + addr + '<br />' + phone + dist + '</small>' + '</p>');
        if (naan_type == 1) {
            ifb_hide.find(".ibf_top").removeClass('red_top');
            ifb_hide.find(".ibf_top").addClass('green_top');
            ifb_hide.find(".ibf_body").removeClass('red_body');
            ifb_hide.find(".ibf_body").addClass('green_body');
			ifb_hide.find(".ibf_end").removeClass('red_end');
			ifb_hide.find(".ibf_end").addClass('green_end');
			ifb_hide.find(".ibf_arrow").removeClass('red_arrow');			
			ifb_hide.find(".ibf_arrow").addClass('green_arrow');			
        } else {
            ifb_hide.find(".ibf_top").removeClass('green_top');
            ifb_hide.find(".ibf_top").addClass('red_top');
            ifb_hide.find(".ibf_body").removeClass('green_body');
            ifb_hide.find(".ibf_body").addClass('red_body');
			ifb_hide.find(".ibf_end").removeClass('green_end');
			ifb_hide.find(".ibf_end").addClass('red_end');
			ifb_hide.find(".ibf_arrow").removeClass('green_arrow');
			ifb_hide.find(".ibf_arrow").addClass('red_arrow');
        }
		

		if($("#resultsContainer").length){
			var yDelta = -172;
			if(browser.ie) yDelta = -169;
			if(browser.webkit) yDelta = -169;
			if(browser.opera) yDelta = -167;
        } else {
			var yDelta = -172;
			if(browser.ie) yDelta = -169;
			if(browser.webkit) yDelta = -169;
			if(browser.opera) yDelta = -167;
		}
        var infoBox = new InfoBox({
            latlng: marker.getPosition(),
            map: map,
            content: ifb_hide.html(),
            closeBoxURL: "../images/infobox/ico_close.png",
            closeBoxMargin: 0,
            pixelOffset: new google.maps.Size(-165, yDelta),
            infoBoxClearanceX: new google.maps.Size(10, 240),
			infoBoxClearanceY: new google.maps.Size(10, 70)
        });

        infowindows.push(infoBox);

        var itemBox = $(this).parent('.hotContentBodyItem');
        $mapLink = $(this).parent('.hotContentBodyItem').find('#element' + id);
        $mapLink.click(function () {
            $('.hotContentBodyItem').removeClass('hotContentBodyItemSelected');
            itemBox.addClass('hotContentBodyItemSelected');

            $.each(infowindows, function (indexInArray, valueOfElement) {
                valueOfElement.close();
            })
            infoBox.open(map, marker);

            return false;
        })

        $mapLink2 = $(this).parent('.hotContentBodyItem').find('#el' + id);
        $mapLink2.click(function () {
            $('.hotContentBodyItem').removeClass('hotContentBodyItemSelected');
            itemBox.addClass('hotContentBodyItemSelected');

            $.each(infowindows, function (indexInArray, valueOfElement) {
                valueOfElement.close();
            })
            infoBox.open(map, marker);

            return false;
        })

        google.maps.event.addListener(marker, "click", function () {
            $('.hotContentBodyItem').removeClass('hotContentBodyItemSelected');
            itemBox.addClass('hotContentBodyItemSelected');
            if ($.scrollTo) {
                $('#resultsScroll').scrollTo($('#element' + id), 800);
            }

            $.each(infowindows, function (indexInArray, valueOfElement) {
                valueOfElement.close();
            })
            infoBox.open(map, marker);
        });

        markersArray[id] = marker;

    });
});

