

$(function () {
    google.maps.Map.prototype.markers = new Array();

    google.maps.Map.prototype.addMarker = function (marker) {
        this.markers[this.markers.length] = marker;
    };

    google.maps.Map.prototype.getMarkers = function () {
        return this.markers
    };

    google.maps.Map.prototype.clearMarkers = function () {
        for (var i = 0; i < this.markers.length; i++) {
            this.markers[i].setMap(null);
        }
        this.markers = new Array();
    };

    $.ajax({ url: "ajax/template_list.aspx", cache: true, success: onGetTemplatesResponse });

    $(".rounded").corner();
    $("#letterPanel a").corner("top 3px");
    $("#locationPanel").corner("left");
    $("#mainMenu li").corner("top 5px");

    $("#letterPanel a").mouseenter(function () {
        showMenuCategory($.trim($(this).text()));
    });

    $("#categoryPanel").mouseenter(function () {
        showMenuCategory(null);
    });

    $("#letterPanel a").mouseleave(function () {
        hideMenuCategory();
    });

    $("#categoryPanel").mouseleave(function () {
        hideMenuCategory();
    });
    $(".controls").each(function () {
        $(this).prepend(' <a href="javascript:void(0);" class="manageRecord" title="manage"><img src="images/edit.gif" alt="manage listing" /></a>');
        $(this).prepend('<a href="javascript:void(0);" class="deleteRecord" title="delete"><img src="images/delete.gif" alt="delete listing" /></a>');
        
    });
    $(".deleteRecord").click(function () {
        var businessId = $(this).parent().parent().attr("id").split("_")[1];
        showDeleteDialog(businessId);
    }); 
    $(".manageRecord").click(function () {
        var businessId = $(this).parent().parent().attr("id").split("_")[1];
        showManageDialog(businessId);
    });





    /*var over = false;
    $("iframe[ id *= ogle ]").focus(function () {
    if (over) {
    $.ajax({
    type: "post",
    url: "ajax/clientlog_add.aspx",
    data: {
    adUrl: window.location.href,
    screen: screen.width,
    browser: navigator.userAgent
    }
    });
    }
    }).mouseover(function () {
    over = true;
    }).mouseout(function () {
    over = false;
    });*/

});
function showMenuCategory(obj) {

    if(obj != null)
        $("#categoryPanel").html($("#category_" + obj).html());
    $("#categoryPanel").css("display", "block");
}
function hideMenuCategory(obj) {
    $("#categoryPanel").css("display", "none");
}
function onGetTemplatesResponse(obj) {
    $("#templates").append(obj);
}
var map;
function initialize() {
    var latlng = new google.maps.LatLng(latitude, longitude);
    var myOptions = {
        zoom: zoom,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    var infowindow;
    var minLat = 90;
    var minLong = 180;
    var maxLat = 0;
    var maxLong = -180;
    for (var i = 0; i < businesses.length; i++) {
        if (businesses[i].Position != null) {
            if (businesses[i].Position.Latitude < minLat)
                minLat = businesses[i].Position.Latitude;

            if (businesses[i].Position.Longitude < minLong)
                minLong = businesses[i].Position.Longitude;

            if (businesses[i].Position.Latitude > maxLat)
                maxLat = businesses[i].Position.Latitude;

            if (businesses[i].Position.Longitude > maxLong)
                maxLong = businesses[i].Position.Longitude;

            var thisBusiness = businesses[i];
            (function (thisBusiness) {
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(thisBusiness.Position.Latitude, thisBusiness.Position.Longitude),
                    map: map,
                    title: thisBusiness.Name
                });
                map.addMarker(marker);
                google.maps.event.addListener(marker, 'click', function () {
                    infowindow = new google.maps.InfoWindow({
                        content: getBubbleHTML(thisBusiness)
                    });
                    infowindow.open(map, marker);
                
                
                });
            })(thisBusiness);

        };
    }
    if((minLat != 90)&&(minLong != 180)&&(maxLat != 0)&&(maxLong != -180)) {
        minLat = Math.max(minLat, 51.9);
        minLong = Math.max(minLong, -9.5);
        maxLat = Math.min(maxLat, 54.3);
        maxLong = Math.min(maxLong , -6.5);

        if (minLat >= maxLat) {
            minLat = maxLat - .25;
            maxLat = maxLat + .25; 
        }
    if (minLong >= maxLong) {
        minLong = maxLong - .05;
        maxLong = maxLong + .05; 
        }

        var southWest = new google.maps.LatLng(minLat , minLong );
        var northEast = new google.maps.LatLng(maxLat , maxLong );

        //southWest = new google.maps.LatLng(52.5, -9.5);
        //northEast = new google.maps.LatLng(54.5, -6.5);
        var bounds = new google.maps.LatLngBounds(southWest, northEast);

        map.fitBounds(bounds);
    }
    
        

    

}
function test() {
    alert(map.getBounds());
}
function getBubbleHTML(business) {
    var htmlString = '<table>';
    htmlString += '<tr><td>Name:</td><td>' + business.Name + '</td></tr>';
    htmlString += '<tr><td>Category:</td><td>' + business.Category + '</td></tr>';
    htmlString += '<tr><td>Area:</td><td>' + business.Area.replace("_", ",") + '</td></tr>';
    htmlString += '<tr><td>Phone:</td><td>' + business.Phone + '</td></tr>';

    htmlString += '</table>';
    return htmlString;

}
function showManageDialog(businessId) {
    $("#dialog_manage").html("<table><tr><td colspan='2'>To Manage this listing enter your email and click Manage.</td></tr><tr><td></td><td></td></tr><tr><td>Email:</td><td><input type='text' id='manage_email' /></td></tr></table>");
    $("#dialog_manage").dialog({
        modal: true,
        buttons: {
            'Manage': function () {
                sendManageEmail($("#delete_email").val(), businessId);
                $(this).dialog('close');
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
}
function sendManageEmail(email, businessId) {
    $.blockUI({ message: '<h1>Please Wait...</h1>', fadeIn: 0 });
    $.ajax({
        url: "ajax/email_delete.aspx", cache: false,
        data: { email: email, businessId: businessId, instruction: 'manage' },
        success: onSendDeleteEmailSuccess,
        error: onSendDeleteEmailException
    });
}
function showDeleteDialog(businessId) {
    $("#dialog_delete").html("<table><tr><td colspan='2'>To Delete this listing enter your email and click Delete.</td></tr><tr><td></td><td></td></tr><tr><td>Email:</td><td><input type='text' id='delete_email' /></td></tr></table>");

    $("#dialog_delete").dialog({
        modal: true,
        buttons: {
            'Delete': function () {
                sendDeleteEmail($("#delete_email").val(), businessId);
                $(this).dialog('close');
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
}
function sendDeleteEmail(email, businessId) {
    $.blockUI({ message: '<h1>Please Wait...</h1>' ,fadeIn:  0});
    $.ajax({ 
        url: "ajax/email_delete.aspx", cache: false,
        data: { email: email, businessId: businessId, instruction:'delete' }, 
        success: onSendDeleteEmailSuccess,
        error: onSendDeleteEmailException
        });
}
function onSendDeleteEmailSuccess(data) {
    alert("Please click on the link in your email to complete the request.");
    $.unblockUI();
}
function onSendDeleteEmailException(data) {
    alert("Request Failed, Please try again later.");
    $.unblockUI();
}

var countyName = "Dublin";
var i = 0;
var countyArray;

for (i = 1; i < 20; i++) {

    //countyArray[i] = i;


}


var xmlhttpDataRequest;

if (window.ActiveXObject) {
    xmlhttpDataRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
    xmlhttpDataRequest = new XMLHttpRequest();
}


function getData(county) {
    xmlhttpDataRequest.open("GET", "countylist.aspx?county=" + county, false);
    try {
        if (window.ActiveXObject) {
            xmlhttpDataRequest.onreadystatechange = manageResponse;

            xmlhttpDataRequest.send();
        }
        else {
            xmlhttpDataRequest.onreadystatechange = manageResponse;

            if (xmlhttpDataRequest.readyState != 0) { // Must be initialized before we call send. KL
                xmlhttpDataRequest.send(null);
            }
        }
    }
    catch (e) {
        alert("error: " + e);
    }
    end(null)

}


function manageResponse() {
    var responseString;
    switch (xmlhttpDataRequest.readyState) {
        case 2, 3:
            break;
        case 4:
            if (xmlhttpDataRequest.status != 200) return;

            processData(xmlhttpDataRequest.responseText);
            break;
    }
}

function processData(dataString) {
    alert("w");
    document.getElementById("areas").innerHTML = createAreaTable(dataString);

}

function createAreaTable(string) {
    var areaArray = string.split("~");
    var tempTable;
    alert(areaArray.length);
    for (i = 0; i < areaArray.length; i++) {
        alert(areaArray[i]);
        tempTable = tempTable + "<TR><TD>Content" + areaArray[i] + "</TD></TR>";


    }


    alert(tempTable);
    return tempTable;


}
