jQuery.fn.outerHTML = function(s) {
	return (s)
	? this.before(s).remove()
	: jQuery("<p>").append(this.eq(0).clone()).html();
}

function loadGoogleMap(latitude, longitude) {
    if (GBrowserIsCompatible()) {
        var map = new GMap2($("#map").get(0));
        var mapPrint = new GMap2($("#mapPrint").get(0));

        fillMap(map, latitude, longitude);
        fillMap(mapPrint, latitude, longitude);
    }
}

function fillMap(map, latitude, longitude) {
    map.addControl(new GSmallMapControl());
    //map.addControl(new GMapTypeControl());

    var point = new GLatLng(latitude, longitude);
    map.setCenter(point, 13);

    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.iconSize = new GSize(32, 32);
    blueIcon.image = "/img/house.png";
    blueIcon.iconAnchor = new GPoint(30 >> 1, 30 >> 1);
    blueIcon.shadow = "/img/house-shadow.png";
    blueIcon.shadowSize = new GSize(59, 32);

    var markerOptions = { icon: blueIcon };

    var marker = new GMarker(point, markerOptions);
    /*
    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("Hello");
    });
    */
    map.addOverlay(marker)
}


/*
* SEARCH PAGE
*/
$.fn.fillCities = function(/*string*/province) {
    return this.each(function() {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            //default option
            dropdownList.options.length = 0;
            var option = new Option(global_dropdown_select, '');
            if ($.browser.msie) {
                dropdownList.add(option);
            }
            else {
                dropdownList.add(option, null);
            }

            $.each(locations, function(index, provinceData) {
                if (provinceData.key == province) {
                    var reg = /\s+/;
                    $.each(provinceData.cities, function(index, optionData) {
                        //add cities                                  
                        var cityValue = optionData.replace(reg, '_');
                        var cityText = optionData;
                        var option = new Option(cityText, cityValue);
                        if ($.browser.msie) {
                            dropdownList.add(option);
                        }
                        else {
                            dropdownList.add(option, null);
                        }
                    });
                }
            });
        }
    });
}

//select/unselect all checkboxes from group
$.fn.checkAll = function(checkBoxContainer) {
    if (this.attr('type') == 'checkbox') {
        var checked_status = this.is(':checked');
        $("#" + checkBoxContainer + " input").each(function() {
            this.checked = checked_status;
        });
    }
}

//check if all checkboxes are checked or unchecked
$.fn.validateCheckAll = function(checkBoxContainer, checkAllCheckBox) {
    if (this.attr('id') != checkAllCheckBox) {
        var checkAllStatus = true;
        $("#" + checkBoxContainer + " input").each(function() {
            if (this.id != checkAllCheckBox && !this.checked) {
                checkAllStatus = false;
            }
        });
        $("#" + checkAllCheckBox).attr('checked', checkAllStatus);
    }
}

function encodeText(text)
{
    return text.replace(/\s/, "_");
}

/*View page*/
function imagePreview() 
{
    /* CONFIG */

    xOffset = 10;
    yOffset = 30;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    /* END CONFIG */
    $("a.preview").hover(function(e) {
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img src='" + this.href + "' alt='Image preview' />" + c + "</p>");
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function() {
	    this.title = this.t;
	    $("#preview").remove();
	});
    $("a.preview").mousemove(function(e) {
        $("#preview")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });
};

/*
$.fn.fillSelectWithCitiesRemote = function(data) {
return this.each(function() {
if (this.tagName == 'SELECT') {
var dropdownList = this;

//default option
dropdownList.options.length = 0;
var option = new Option(selectText, '');
if ($.browser.msie) {
dropdownList.add(option);
}
else {
dropdownList.add(option, null);
}

//cities
$.each(data, function(index, optionData) {
//add cities
var option = new Option(optionData.Value, optionData.Value);
if ($.browser.msie) {
dropdownList.add(option);
}
else {
dropdownList.add(option, null);
}
});
}
});
}
*/
