﻿$(function () {
    $.fn.customStyle = function (options) {
        if (!$.browser.msie || ($.browser.msie && $.browser.version > 6)) {
            return this.each(function () {
                var currentSelected = $(this).find(':selected');
                $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">' + currentSelected.text() + '</span></span>').css({ position: 'absolute', opacity: 0 });
                var selectBoxSpan = $(this).next();
                var selectBoxWidth = parseInt($(selectBoxSpan).width()) + parseInt(selectBoxSpan.css('padding-left')) + parseInt(selectBoxSpan.css('padding-right'));
                $(this).css("width", selectBoxWidth);
                var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                selectBoxSpan.css({ display: 'inline-block' });
                selectBoxSpanInner.css({ display: 'block' });
                $(this).change(function () {
                    selectBoxSpanInner.text($(':selected', this).text()).parent().addClass('changed');
                });

            });
        }
        return this;
    };

    $.fn.TexDefaultText = function (defaultText) {
        var that = this;
        var $input = $(this);
        var value = $input.val();
        if (!value) {
            $input.val(defaultText);
        }

        $(that).focus(function () {
            var $input = $(this);
            var value = $input.val();
            if (value == defaultText) {
                $input.val("");
            }
        });
        return that;
    };

    $.validator.addMethod("select-required", function (value, element) {
        var isValid = false;
        var span = $(element).nextAll(".customStyleSelectBox");
        if (!value) {
            $(span).addClass("error");
            $(element).addClass("error");
        }
        else {
            isValid = true;
            $(span).removeClass("error");
            $(element).removeClass("error");
        }
        return isValid;
    }, "");
});

function initialize(address, divId) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(divId));
        GEvent.addListener(map, "load", function () { $("[dir=ltr]", "#" + divId).remove() });
        //map.setZoom(17);
        //map.setUIToDefault();
        map.disableDragging();
        map.disableDoubleClickZoom();
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function (point) {
                if (!point) {
                    alert(address + " not found");
                } else {
                    map.setCenter(point, 16);
                    var marker = new GMarker(point);
                    GEvent.addListener(marker, "click", function () {
                        //alert(address);
                        $("#win-" + divId).show();
                        //marker.openInfoWindowHtml("Marker <b>" + address + "</b>");
                    });
                    map.addOverlay(marker);
                    //marker.openInfoWindowHtml(address);
                }
            }
        );
    }
}
