﻿/// <reference path="JQuery/jquery-1.3.1.js" />

var map = null;
var geocoder = null;
var isInitialized = false;

//initialize googlemap
function googleInitialize() {
    if (GBrowserIsCompatible() && isInitialized === false) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(59.327405, 18.073250), 13);
        map.addControl(new GLargeMapControl);
        map.enableScrollWheelZoom();
        map.enableContinuousZoom();
        
        geocoder = new GClientGeocoder();

        isInitialized = true;
    }
}

//created the marker and the storeinfo
function googleCreateMarker(latlng, address, zipcode, city, storename, phoneNumbers, pageid, iconName) {
    var icon = new GIcon();
    icon.image = "http://mora.iteam.se/Styles/Default/Images/RetailerCategories/" + iconName + ".png";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(28, 28);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(10, 10);
    icon.infoWindowAnchor = new GPoint(5, 1);

    var marker = new GMarker(latlng, icon);
    var myHtml = '<div style="float:left;padding-right:5px;padding-top:2px;"><img src="http://mora.iteam.se/Styles/Default/Images/RetailerCategories/' + iconName + '.png"></div><div style="float:left;"><b>' + ' ' + storename + '</b><br>' + address + '<br>' + zipcode + ' ' + city + phoneNumbers + '</div>';
    map.openInfoWindowHtml(latlng, myHtml);

    //var marker = new GMarker(latlng);
    marker.value = latlng;
    GEvent.addListener(marker, "click", function() {
        var myHtml = "<b>" + " " + storename + "</b><br>" + address + "<br>" + zipcode + " " + city + phoneNumbers;
        map.openInfoWindowHtml(latlng, myHtml);

        setRetailerActive(pageid);
    });
    return marker;
}

//sets the retailer active in the retailerlist
function setRetailerActive(pageid) {
    //clear all retailers from being active - jquery
    if (document.getElementById) {
        var currentelement;
        $(".searchResult").css("border", "0px");
        $("#" + pageid).css("border", "1px solid #cccccc");
    }
}

//adds an address in the map    
function googleAddAddress(address, zipcode, city, country, storename, phoneNumbers, pageid, forcecenter, iconName) {
    if (!isInitialized)
        googleInitialize();

    //'Hornsgatan 12,111 11 Stockholm, Sweden'
    fixedaddress = address + "," + zipcode + " " + city + "," + country;

    geocoder.getLatLng(fixedaddress, function(point) {
        if (point) {
            map.addOverlay(googleCreateMarker(point, address, zipcode, city, storename, phoneNumbers, pageid, iconName));
            if (forcecenter)
                googleSetAddressCenter(point, forcecenter);
        }
    });
}

//center the map after parameter point
function googleSetAddressCenter(point, setcenter) {
    if (setcenter === true)
        map.setCenter(point, 13);
}

//remove all markers
function removeMarkers()
{
    if (map)
       map.clearOverlays();
}

var timeout;

//Render the content part (between the first and last <!--Content--> comment)of a page into a div 
function renderPageIntoContainerCallback(response,a,b, options) {
    var responseText = response;
    var cleanedResponseText = responseText.substring(responseText.indexOf('<!--Content-->'), responseText.lastIndexOf('<!--Content-->'));

    if (options.indicator) options.indicator.hide();
    options.targetContainer.innerHTML = cleanedResponseText;

    if (options.callAfterSuccess)
        options.callAfterSuccess();
}

function renderPageIntoContainer(pageUrl, container, callback, delay, indicatorElement) {
    //Ext.Ajax.on("abort", renderPageFailure, null, { indicator: indicatorElement });
    if (delay) {
        window.clearTimeout(timeout);
        timeout = window.setTimeout(function() {
         $(container).load(pageUrl +"#content", null, callback);
        }, delay);
    } else {
        $(container).load(pageUrl + "#content", null, callback);
    }
}

function renderControl(control, container, parameters, callback, delay, indicator) {
   if (indicator) indicator.show();

   renderPageIntoContainer(baseUrl + "AJAX/RenderControl?control=" + control + "&" + parameters + "&originator=" + document.location, container, callback, delay, indicator);
}

//End of file...
