var remax = {

	fadeImage: function(arrayPoint) {
		var previousArrayPoint = arrayPoint - 1;
		if(arrayPoint == 0) {
			previousArrayPoint = 3;
		}

		$("#fading_image" + previousArrayPoint).fadeOut("fast");
		$("#fading_image" + arrayPoint).fadeIn("slow");
        setTimeout(function() { remax.fadeImage((arrayPoint == 3 ? 0 : arrayPoint + 1)); }, 5000);
	},

	textHolder: ""
}

// Popovers Defined Below

var popover = {
    create: function(areaName, width, height) {
        var shadow = document.getElementById("popover_shadow_background"),
            popover_layer = document.getElementById("popover_box"), 
            docHeight = Math.max(((document.documentElement.scrollTop || document.body.scrollTop)+(Math.round((window.innerHeight 
                || document.documentElement.clientHeight)/2))-Math.round(height/2)-40),10);

        shadow.style.display = "block";
        popover_layer.style.display = "block";

        $(popover_layer).removeClass().addClass(areaName);

        if (typeof window.innerWidth != 'undefined') {
            this.viewportwidth = window.innerWidth,
            this.viewportheight = window.innerHeight;
        } else {
            this.viewportwidth = document.documentElement.clientWidth,
            this.viewportheight = document.documentElement.clientHeight;
        }

        var medianwidth = this.viewportwidth - width,
            medianheight = this.viewportheight - height,
            default_20 = (height > this.viewportheight ? true : false);

        shadow.style.height = popover.getTotalDocSize() - 22 + "px"; 
        return false;
    },

    destroy: function() {
        var popover_shadow = document.getElementById("popover_shadow_background"),
            popover_box = document.getElementById("popover_box");

        popover_box.style.display = "none";
        popover_shadow.style.display = "none"; 
    },

    getTotalDocSize: function() { return Math.max((document.documentElement.scrollHeight || document.body.scrollHeight), 
        (document.documentElement.offsetHeight || document.body.offsetHeight)); 
    }
}

$(document).ready(function() {
	remax.fadeImage(0);

	$(".search_prices").focus(function() {
		if($(this).val() == "1000" || $(this).val() == "1000000") {
			remax.textHolder = $(this).val();
			$(this).val("");
		}
	}).blur(function() {
		if($(this).val() == "") $(this).val(remax.textHolder);
	});

	$("#mls_id_input").focus(function() {
		$(this).val("");
	}).blur(function() {
		if($(this).val() == "") $(this).val("e.g, st6976682");
	});

    if($("#ihfDetail10_MainContainer")[0]) {
        $("#ihfDetail10_MainContainer").prepend('<a id="makeAnOfferButton" href="http://www.realping.com/accts/connect2.cfm?id=19810&tz=0&src=web" title="Talk to an Agent now!" target="_blank">Talk to an Agent now</a>');
        return false;
    }

    if($("#fredericksburg_area_communities")[0]) $("#fredericksburg_area_communities").click(function() { popover.create("fredericksburg", "900", "375"); return false; });
    if($("#caroline_county_communities")[0]) $("#caroline_county_communities").click(function() { popover.create("caroline", "900", "375"); return false; });
    if($("#orange_county_communities")[0]) $("#orange_county_communities").click(function() { popover.create("orange", "900", "375"); return false; });
    if($("#king_george_communities")[0]) $("#king_george_communities").click(function() { popover.create("king_george", "900", "375"); return false; });
    if($("#close_popover_button")[0]) $("#close_popover_button").click(function() { popover.destroy(); return false; });
});
